Skip to content

Commit d72d808

Browse files
author
Dave Abrahams
committed
Merge pull request #838 from austinzheng/az-port-mirror
Migrate mirrors to use CustomReflectable API, rewrite dump() ...and there was much rejoicing.
2 parents fc6c313 + 9798dfd commit d72d808

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+882
-1511
lines changed

stdlib/public/SDK/AppKit/AppKit.swift

Lines changed: 20 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -13,94 +13,39 @@
1313
import Foundation
1414
@_exported import AppKit
1515

16-
struct _NSCursorMirror : _MirrorType {
17-
var _value: NSCursor
18-
19-
init(_ v: NSCursor) { _value = v }
20-
21-
var value: Any { return _value }
22-
23-
var valueType: Any.Type { return (_value as Any).dynamicType }
24-
25-
var objectIdentifier: ObjectIdentifier? { return .None }
26-
27-
var count: Int { return 0 }
28-
29-
subscript(_: Int) -> (String, _MirrorType) {
30-
_preconditionFailure("_MirrorType access out of bounds")
16+
extension NSCursor : CustomPlaygroundQuickLookable {
17+
public func customPlaygroundQuickLook() -> PlaygroundQuickLook {
18+
return .Image(image)
3119
}
32-
33-
var summary: String { return "" }
34-
35-
var quickLookObject: PlaygroundQuickLook? {
36-
return .Some(.Image(_value.image))
37-
}
38-
39-
var disposition : _MirrorDisposition { return .Aggregate }
4020
}
4121

42-
extension NSCursor : _Reflectable {
43-
public func _getMirror() -> _MirrorType {
44-
return _NSCursorMirror(self)
45-
}
22+
internal struct _NSViewQuickLookState {
23+
static var views = Set<NSView>()
4624
}
4725

48-
struct _NSViewMirror : _MirrorType {
49-
static var _views = Set<NSView>()
50-
51-
var _v : NSView
52-
53-
init(_ v : NSView) { _v = v }
54-
55-
var value: Any { return _v }
56-
57-
var valueType: Any.Type { return (_v as Any).dynamicType }
58-
59-
var objectIdentifier: ObjectIdentifier? { return .None }
60-
61-
var count: Int { return 0 }
62-
63-
subscript(_: Int) -> (String, _MirrorType) {
64-
_preconditionFailure("_MirrorType access out of bounds")
65-
}
66-
67-
var summary: String { return "" }
68-
69-
var quickLookObject: PlaygroundQuickLook? {
70-
// adapted from the Xcode QuickLooks implementation
71-
72-
var result: PlaygroundQuickLook? = nil
73-
26+
extension NSView : CustomPlaygroundQuickLookable {
27+
public func customPlaygroundQuickLook() -> PlaygroundQuickLook {
7428
// if you set NSView.needsDisplay, you can get yourself in a recursive scenario where the same view
7529
// could need to draw itself in order to get a QLObject for itself, which in turn if your code was
7630
// instrumented to log on-draw, would cause yourself to get back here and so on and so forth
7731
// until you run out of stack and crash
7832
// This code checks that we aren't trying to log the same view recursively - and if so just returns
79-
// nil, which is probably a safer option than crashing
33+
// an empty view, which is probably a safer option than crashing
8034
// FIXME: is there a way to say "cacheDisplayInRect butDoNotRedrawEvenIfISaidSo"?
81-
if !_NSViewMirror._views.contains(_v) {
82-
_NSViewMirror._views.insert(_v)
83-
84-
let bounds = _v.bounds
85-
if let b = _v.bitmapImageRepForCachingDisplayInRect(bounds) {
86-
_v.cacheDisplayInRect(bounds, toBitmapImageRep: b)
87-
result = .Some(.View(b))
35+
if _NSViewQuickLookState.views.contains(self) {
36+
return .View(NSImage())
37+
} else {
38+
_NSViewQuickLookState.views.insert(self)
39+
let result : PlaygroundQuickLook
40+
if let b = bitmapImageRepForCachingDisplayInRect(bounds) {
41+
cacheDisplayInRect(bounds, toBitmapImageRep: b)
42+
result = .View(b)
43+
} else {
44+
result = .View(NSImage())
8845
}
89-
90-
_NSViewMirror._views.remove(_v)
46+
_NSViewQuickLookState.views.remove(self)
47+
return result
9148
}
92-
93-
return result
94-
95-
}
96-
97-
var disposition : _MirrorDisposition { return .Aggregate }
98-
}
99-
100-
extension NSView : _Reflectable {
101-
/// Returns a mirror that reflects `self`.
102-
public func _getMirror() -> _MirrorType {
103-
return _NSViewMirror(self)
10449
}
10550
}
10651

stdlib/public/SDK/CoreGraphics/CGFloat.swift.gyb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,10 @@ public var CGFLOAT_MAX: CGFloat {
145145
fatalError("can't retrieve unavailable property")
146146
}
147147

148-
extension CGFloat : _Reflectable {
148+
extension CGFloat : CustomReflectable {
149149
/// Returns a mirror that reflects `self`.
150-
public func _getMirror() -> _MirrorType {
151-
return _reflect(native)
150+
public func customMirror() -> Mirror {
151+
return Mirror(reflecting: native)
152152
}
153153
}
154154

stdlib/public/SDK/CoreGraphics/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
add_swift_library(swiftCoreGraphics IS_SDK_OVERLAY
22
CoreGraphics.swift
33
CGFloat.swift.gyb
4-
CoreGraphicsMirrors.swift.gyb
54
# rdar://problem/20891746
65
# SWIFT_COMPILE_FLAGS -Xfrontend -sil-serialize-all
76
SWIFT_MODULE_DEPENDS ObjectiveC Dispatch Darwin

stdlib/public/SDK/CoreGraphics/CoreGraphics.swift

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,22 @@ public extension CGPoint {
3939
}
4040
}
4141

42+
extension CGPoint : CustomReflectable, CustomPlaygroundQuickLookable {
43+
public func customMirror() -> Mirror {
44+
return Mirror(self, children: ["x": x, "y": y], displayStyle: .Struct)
45+
}
46+
47+
public func customPlaygroundQuickLook() -> PlaygroundQuickLook {
48+
return .Point(Double(x), Double(y))
49+
}
50+
}
51+
52+
extension CGPoint : CustomDebugStringConvertible {
53+
public var debugDescription : String {
54+
return "(\(x), \(y))"
55+
}
56+
}
57+
4258
extension CGPoint : Equatable {}
4359
@_transparent // @fragile
4460
@warn_unused_result
@@ -68,6 +84,22 @@ public extension CGSize {
6884
}
6985
}
7086

87+
extension CGSize : CustomReflectable, CustomPlaygroundQuickLookable {
88+
public func customMirror() -> Mirror {
89+
return Mirror(self, children: ["width": width, "height": height], displayStyle: .Struct)
90+
}
91+
92+
public func customPlaygroundQuickLook() -> PlaygroundQuickLook {
93+
return .Size(Double(width), Double(height))
94+
}
95+
}
96+
97+
extension CGSize : CustomDebugStringConvertible {
98+
public var debugDescription : String {
99+
return "(\(width), \(height))"
100+
}
101+
}
102+
71103
extension CGSize : Equatable {}
72104
@_transparent // @fragile
73105
@warn_unused_result
@@ -357,6 +389,22 @@ public extension CGRect {
357389
}
358390
}
359391

392+
extension CGRect : CustomReflectable, CustomPlaygroundQuickLookable {
393+
public func customMirror() -> Mirror {
394+
return Mirror(self, children: ["origin": origin, "size": size], displayStyle: .Struct)
395+
}
396+
397+
public func customPlaygroundQuickLook() -> PlaygroundQuickLook {
398+
return .Rectangle(Double(origin.x), Double(origin.y), Double(size.width), Double(size.height))
399+
}
400+
}
401+
402+
extension CGRect : CustomDebugStringConvertible {
403+
public var debugDescription : String {
404+
return "(\(origin.x), \(origin.y), \(size.width), \(size.height))"
405+
}
406+
}
407+
360408
extension CGRect : Equatable {}
361409
@_transparent // @fragile
362410
@warn_unused_result

stdlib/public/SDK/CoreGraphics/CoreGraphicsMirrors.swift.gyb

Lines changed: 0 additions & 59 deletions
This file was deleted.

stdlib/public/SDK/Darwin/Darwin.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ public struct DarwinBoolean : BooleanType, BooleanLiteralConvertible {
4242
}
4343
}
4444

45-
extension DarwinBoolean : _Reflectable {
45+
extension DarwinBoolean : CustomReflectable {
4646
/// Returns a mirror that reflects `self`.
47-
public func _getMirror() -> _MirrorType {
48-
return _reflect(boolValue)
47+
public func customMirror() -> Mirror {
48+
return Mirror(reflecting: boolValue)
4949
}
5050
}
5151

stdlib/public/SDK/Foundation/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
add_swift_library(swiftFoundation IS_SDK_OVERLAY
22
Foundation.swift
3-
FoundationMirrors.swift.gyb
43
NSError.swift
54
NSStringAPI.swift
65
NSValue.swift

stdlib/public/SDK/Foundation/Foundation.swift

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,6 +1376,10 @@ extension NSKeyedUnarchiver {
13761376
}
13771377
}
13781378

1379+
//===----------------------------------------------------------------------===//
1380+
// NSURL
1381+
//===----------------------------------------------------------------------===//
1382+
13791383
extension NSURL : _FileReferenceLiteralConvertible {
13801384
private convenience init(failableFileReferenceLiteral path: String) {
13811385
let fullPath = NSBundle.mainBundle().pathForResource(path, ofType: nil)!
@@ -1388,3 +1392,62 @@ extension NSURL : _FileReferenceLiteralConvertible {
13881392
}
13891393

13901394
public typealias _FileReferenceLiteralType = NSURL
1395+
1396+
//===----------------------------------------------------------------------===//
1397+
// Mirror/Quick Look Conformance
1398+
//===----------------------------------------------------------------------===//
1399+
1400+
extension NSURL : CustomPlaygroundQuickLookable {
1401+
public func customPlaygroundQuickLook() -> PlaygroundQuickLook {
1402+
return .URL(absoluteString)
1403+
}
1404+
}
1405+
1406+
extension NSRange : CustomReflectable {
1407+
public func customMirror() -> Mirror {
1408+
return Mirror(self, children: ["location": location, "length": length])
1409+
}
1410+
}
1411+
1412+
extension NSRange : CustomPlaygroundQuickLookable {
1413+
public func customPlaygroundQuickLook() -> PlaygroundQuickLook {
1414+
return .Range(Int64(location), Int64(length))
1415+
}
1416+
}
1417+
1418+
extension NSDate : CustomPlaygroundQuickLookable {
1419+
var summary: String {
1420+
let df = NSDateFormatter()
1421+
df.dateStyle = .MediumStyle
1422+
df.timeStyle = .ShortStyle
1423+
return df.stringFromDate(self)
1424+
}
1425+
1426+
public func customPlaygroundQuickLook() -> PlaygroundQuickLook {
1427+
return .Text(summary)
1428+
}
1429+
}
1430+
1431+
extension NSSet : CustomReflectable {
1432+
public func customMirror() -> Mirror {
1433+
return Mirror(reflecting: _convertNSSetToSet(self) as Set<NSObject>)
1434+
}
1435+
}
1436+
1437+
extension NSString : CustomPlaygroundQuickLookable {
1438+
public func customPlaygroundQuickLook() -> PlaygroundQuickLook {
1439+
return .Text(self as String)
1440+
}
1441+
}
1442+
1443+
extension NSArray : CustomReflectable {
1444+
public func customMirror() -> Mirror {
1445+
return Mirror(reflecting: self as [AnyObject])
1446+
}
1447+
}
1448+
1449+
extension NSDictionary : CustomReflectable {
1450+
public func customMirror() -> Mirror {
1451+
return Mirror(reflecting: _convertNSDictionaryToDictionary(self) as [NSObject : AnyObject])
1452+
}
1453+
}

0 commit comments

Comments
 (0)