Skip to content

[SR-88] Fixing and reapplying stdlib mirror API changes #1058

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 20 additions & 75 deletions stdlib/public/SDK/AppKit/AppKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,94 +13,39 @@
import Foundation
@_exported import AppKit

struct _NSCursorMirror : _MirrorType {
var _value: NSCursor

init(_ v: NSCursor) { _value = v }

var value: Any { return _value }

var valueType: Any.Type { return (_value as Any).dynamicType }

var objectIdentifier: ObjectIdentifier? { return .None }

var count: Int { return 0 }

subscript(_: Int) -> (String, _MirrorType) {
_preconditionFailure("_MirrorType access out of bounds")
extension NSCursor : CustomPlaygroundQuickLookable {
public func customPlaygroundQuickLook() -> PlaygroundQuickLook {
return .Image(image)
}

var summary: String { return "" }

var quickLookObject: PlaygroundQuickLook? {
return .Some(.Image(_value.image))
}

var disposition : _MirrorDisposition { return .Aggregate }
}

extension NSCursor : _Reflectable {
public func _getMirror() -> _MirrorType {
return _NSCursorMirror(self)
}
internal struct _NSViewQuickLookState {
static var views = Set<NSView>()
}

struct _NSViewMirror : _MirrorType {
static var _views = Set<NSView>()

var _v : NSView

init(_ v : NSView) { _v = v }

var value: Any { return _v }

var valueType: Any.Type { return (_v as Any).dynamicType }

var objectIdentifier: ObjectIdentifier? { return .None }

var count: Int { return 0 }

subscript(_: Int) -> (String, _MirrorType) {
_preconditionFailure("_MirrorType access out of bounds")
}

var summary: String { return "" }

var quickLookObject: PlaygroundQuickLook? {
// adapted from the Xcode QuickLooks implementation

var result: PlaygroundQuickLook? = nil

extension NSView : CustomPlaygroundQuickLookable {
public func customPlaygroundQuickLook() -> PlaygroundQuickLook {
// if you set NSView.needsDisplay, you can get yourself in a recursive scenario where the same view
// could need to draw itself in order to get a QLObject for itself, which in turn if your code was
// instrumented to log on-draw, would cause yourself to get back here and so on and so forth
// until you run out of stack and crash
// This code checks that we aren't trying to log the same view recursively - and if so just returns
// nil, which is probably a safer option than crashing
// an empty view, which is probably a safer option than crashing
// FIXME: is there a way to say "cacheDisplayInRect butDoNotRedrawEvenIfISaidSo"?
if !_NSViewMirror._views.contains(_v) {
_NSViewMirror._views.insert(_v)

let bounds = _v.bounds
if let b = _v.bitmapImageRepForCachingDisplayInRect(bounds) {
_v.cacheDisplayInRect(bounds, toBitmapImageRep: b)
result = .Some(.View(b))
if _NSViewQuickLookState.views.contains(self) {
return .View(NSImage())
} else {
_NSViewQuickLookState.views.insert(self)
let result: PlaygroundQuickLook
if let b = bitmapImageRepForCachingDisplayInRect(bounds) {
cacheDisplayInRect(bounds, toBitmapImageRep: b)
result = .View(b)
} else {
result = .View(NSImage())
}

_NSViewMirror._views.remove(_v)
_NSViewQuickLookState.views.remove(self)
return result
}

return result

}

var disposition : _MirrorDisposition { return .Aggregate }
}

extension NSView : _Reflectable {
/// Returns a mirror that reflects `self`.
public func _getMirror() -> _MirrorType {
return _NSViewMirror(self)
}
}

Expand Down
6 changes: 3 additions & 3 deletions stdlib/public/SDK/CoreGraphics/CGFloat.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,10 @@ public var CGFLOAT_MAX: CGFloat {
fatalError("can't retrieve unavailable property")
}

extension CGFloat : _Reflectable {
extension CGFloat : CustomReflectable {
/// Returns a mirror that reflects `self`.
public func _getMirror() -> _MirrorType {
return _reflect(native)
public func customMirror() -> Mirror {
return Mirror(reflecting: native)
}
}

Expand Down
1 change: 0 additions & 1 deletion stdlib/public/SDK/CoreGraphics/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
add_swift_library(swiftCoreGraphics IS_SDK_OVERLAY
CoreGraphics.swift
CGFloat.swift.gyb
CoreGraphicsMirrors.swift.gyb
# rdar://problem/20891746
# SWIFT_COMPILE_FLAGS -Xfrontend -sil-serialize-all
SWIFT_MODULE_DEPENDS ObjectiveC Dispatch Darwin
Expand Down
48 changes: 48 additions & 0 deletions stdlib/public/SDK/CoreGraphics/CoreGraphics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ public extension CGPoint {
}
}

extension CGPoint : CustomReflectable, CustomPlaygroundQuickLookable {
public func customMirror() -> Mirror {
return Mirror(self, children: ["x": x, "y": y], displayStyle: .Struct)
}

public func customPlaygroundQuickLook() -> PlaygroundQuickLook {
return .Point(Double(x), Double(y))
}
}

extension CGPoint : CustomDebugStringConvertible {
public var debugDescription: String {
return "(\(x), \(y))"
}
}

extension CGPoint : Equatable {}
@_transparent // @fragile
@warn_unused_result
Expand Down Expand Up @@ -68,6 +84,22 @@ public extension CGSize {
}
}

extension CGSize : CustomReflectable, CustomPlaygroundQuickLookable {
public func customMirror() -> Mirror {
return Mirror(self, children: ["width": width, "height": height], displayStyle: .Struct)
}

public func customPlaygroundQuickLook() -> PlaygroundQuickLook {
return .Size(Double(width), Double(height))
}
}

extension CGSize : CustomDebugStringConvertible {
public var debugDescription : String {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra space before the colon.

return "(\(width), \(height))"
}
}

extension CGSize : Equatable {}
@_transparent // @fragile
@warn_unused_result
Expand Down Expand Up @@ -357,6 +389,22 @@ public extension CGRect {
}
}

extension CGRect : CustomReflectable, CustomPlaygroundQuickLookable {
public func customMirror() -> Mirror {
return Mirror(self, children: ["origin": origin, "size": size], displayStyle: .Struct)
}

public func customPlaygroundQuickLook() -> PlaygroundQuickLook {
return .Rectangle(Double(origin.x), Double(origin.y), Double(size.width), Double(size.height))
}
}

extension CGRect : CustomDebugStringConvertible {
public var debugDescription : String {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra space before the colon.

return "(\(origin.x), \(origin.y), \(size.width), \(size.height))"
}
}

extension CGRect : Equatable {}
@_transparent // @fragile
@warn_unused_result
Expand Down
59 changes: 0 additions & 59 deletions stdlib/public/SDK/CoreGraphics/CoreGraphicsMirrors.swift.gyb

This file was deleted.

6 changes: 3 additions & 3 deletions stdlib/public/SDK/Darwin/Darwin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ public struct DarwinBoolean : BooleanType, BooleanLiteralConvertible {
}
}

extension DarwinBoolean : _Reflectable {
extension DarwinBoolean : CustomReflectable {
/// Returns a mirror that reflects `self`.
public func _getMirror() -> _MirrorType {
return _reflect(boolValue)
public func customMirror() -> Mirror {
return Mirror(reflecting: boolValue)
}
}

Expand Down
1 change: 0 additions & 1 deletion stdlib/public/SDK/Foundation/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
add_swift_library(swiftFoundation IS_SDK_OVERLAY
Foundation.swift
FoundationMirrors.swift.gyb
NSError.swift
NSStringAPI.swift
NSValue.swift
Expand Down
63 changes: 63 additions & 0 deletions stdlib/public/SDK/Foundation/Foundation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1376,6 +1376,10 @@ extension NSKeyedUnarchiver {
}
}

//===----------------------------------------------------------------------===//
// NSURL
//===----------------------------------------------------------------------===//

extension NSURL : _FileReferenceLiteralConvertible {
private convenience init(failableFileReferenceLiteral path: String) {
let fullPath = NSBundle.mainBundle().pathForResource(path, ofType: nil)!
Expand All @@ -1388,3 +1392,62 @@ extension NSURL : _FileReferenceLiteralConvertible {
}

public typealias _FileReferenceLiteralType = NSURL

//===----------------------------------------------------------------------===//
// Mirror/Quick Look Conformance
//===----------------------------------------------------------------------===//

extension NSURL : CustomPlaygroundQuickLookable {
public func customPlaygroundQuickLook() -> PlaygroundQuickLook {
return .URL(absoluteString)
}
}

extension NSRange : CustomReflectable {
public func customMirror() -> Mirror {
return Mirror(self, children: ["location": location, "length": length])
}
}

extension NSRange : CustomPlaygroundQuickLookable {
public func customPlaygroundQuickLook() -> PlaygroundQuickLook {
return .Range(Int64(location), Int64(length))
}
}

extension NSDate : CustomPlaygroundQuickLookable {
var summary: String {
let df = NSDateFormatter()
df.dateStyle = .MediumStyle
df.timeStyle = .ShortStyle
return df.stringFromDate(self)
}

public func customPlaygroundQuickLook() -> PlaygroundQuickLook {
return .Text(summary)
}
}

extension NSSet : CustomReflectable {
public func customMirror() -> Mirror {
return Mirror(reflecting: self as Set<NSObject>)
}
}

extension NSString : CustomPlaygroundQuickLookable {
public func customPlaygroundQuickLook() -> PlaygroundQuickLook {
return .Text(self as String)
}
}

extension NSArray : CustomReflectable {
public func customMirror() -> Mirror {
return Mirror(reflecting: self as [AnyObject])
}
}

extension NSDictionary : CustomReflectable {
public func customMirror() -> Mirror {
return Mirror(reflecting: self as [NSObject : AnyObject])
}
}
Loading