From 08ff0472b2db47fc35c7fb973177e4ae9163443e Mon Sep 17 00:00:00 2001 From: Robert Payne Date: Sat, 11 Apr 2015 20:33:13 +1200 Subject: [PATCH] Added better debugging support Support for snp_label and better descriptions when printing LayoutConstraint's --- Snap.xcodeproj/project.pbxproj | 9 +- Source/Debugging.swift | 175 +++++++++++++++++++++++++++++++++ Source/LayoutConstraint.swift | 3 +- Source/View+Snap.swift | 3 + 4 files changed, 187 insertions(+), 3 deletions(-) create mode 100644 Source/Debugging.swift diff --git a/Snap.xcodeproj/project.pbxproj b/Snap.xcodeproj/project.pbxproj index 88ce245..9c28aba 100644 --- a/Snap.xcodeproj/project.pbxproj +++ b/Snap.xcodeproj/project.pbxproj @@ -31,6 +31,7 @@ EECDB39B1AC0CBFF006BBC11 /* LayoutConstraint.swift in Sources */ = {isa = PBXBuildFile; fileRef = EECDB3651AC0C95C006BBC11 /* LayoutConstraint.swift */; }; EECDB39C1AC0CBFF006BBC11 /* View+Snap.swift in Sources */ = {isa = PBXBuildFile; fileRef = EECDB3671AC0C95C006BBC11 /* View+Snap.swift */; }; EECDB39D1AC0CC03006BBC11 /* Snap.h in Headers */ = {isa = PBXBuildFile; fileRef = EECDB3661AC0C95C006BBC11 /* Snap.h */; settings = {ATTRIBUTES = (Public, ); }; }; + EEFCF32C1AD910B900A425FA /* Debugging.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEFCF32B1AD910B900A425FA /* Debugging.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -62,6 +63,7 @@ EECDB36A1AC0C95C006BBC11 /* Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Tests.swift; sourceTree = ""; }; EECDB37A1AC0C9D4006BBC11 /* Snap.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Snap.framework; sourceTree = BUILT_PRODUCTS_DIR; }; EECDB3841AC0C9D4006BBC11 /* Snap OSX Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "Snap OSX Tests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; }; + EEFCF32B1AD910B900A425FA /* Debugging.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Debugging.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -104,7 +106,6 @@ isa = PBXGroup; children = ( EECDB35D1AC0C95C006BBC11 /* Source */, - EECDB3681AC0C95C006BBC11 /* Tests */, EE94F60C1AC0F113008767FF /* Frameworks */, DDC9FD961981B4DD009612C7 /* Products */, ); @@ -142,7 +143,9 @@ EECDB3631AC0C95C006BBC11 /* EdgeInsets.swift */, EECDB3651AC0C95C006BBC11 /* LayoutConstraint.swift */, EECDB3671AC0C95C006BBC11 /* View+Snap.swift */, + EEFCF32B1AD910B900A425FA /* Debugging.swift */, EECDB36B1AC0C967006BBC11 /* Supporting Files */, + EECDB3681AC0C95C006BBC11 /* Tests */, ); path = Source; sourceTree = ""; @@ -153,7 +156,8 @@ EECDB3691AC0C95C006BBC11 /* Info.plist */, EECDB36A1AC0C95C006BBC11 /* Tests.swift */, ); - path = Tests; + name = Tests; + path = ../Tests; sourceTree = ""; }; EECDB36B1AC0C967006BBC11 /* Supporting Files */ = { @@ -338,6 +342,7 @@ buildActionMask = 2147483647; files = ( EECDB36C1AC0C9A6006BBC11 /* Constraint.swift in Sources */, + EEFCF32C1AD910B900A425FA /* Debugging.swift in Sources */, EECDB3701AC0C9A6006BBC11 /* ConstraintRelation.swift in Sources */, EECDB3731AC0C9A6006BBC11 /* View+Snap.swift in Sources */, EECDB3711AC0C9A6006BBC11 /* EdgeInsets.swift in Sources */, diff --git a/Source/Debugging.swift b/Source/Debugging.swift new file mode 100644 index 0000000..8e6a5f7 --- /dev/null +++ b/Source/Debugging.swift @@ -0,0 +1,175 @@ +// +// Snap +// +// Copyright (c) 2011-2014 Masonry Team - https://github.com/Masonry +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#if os(iOS) +import UIKit +#else +import AppKit +#endif + +/** + * View extension that exposes snp_label debugging api + */ +public extension View { + + public var snp_label: String? { + get { + return objc_getAssociatedObject(self, &labelKey) as? String + } + set { + objc_setAssociatedObject(self, &labelKey, newValue, objc_AssociationPolicy(OBJC_ASSOCIATION_COPY_NONATOMIC)) + } + } + +} + +/** + * LayoutConstraint extension that exposes snp_label debugging api + */ +public extension LayoutConstraint { + + public var snp_label: String? { + get { + return objc_getAssociatedObject(self, &labelKey) as? String + } + set { + objc_setAssociatedObject(self, &labelKey, newValue, objc_AssociationPolicy(OBJC_ASSOCIATION_COPY_NONATOMIC)) + } + } + + override public var description: String { + var description = "<" + + description += descriptionForObject(self) + + description += " \(descriptionForObject(self.firstItem))" + if self.firstAttribute != .NotAnAttribute { + description += ".\(self.firstAttribute.snp_description)" + } + + description += " \(self.relation.snp_description)" + + if let secondItem: AnyObject = self.secondItem { + description += " \(descriptionForObject(secondItem))" + } + + if self.secondAttribute != .NotAnAttribute { + description += ".\(self.secondAttribute.snp_description)" + } + + if self.multiplier != 1.0 { + description += " * \(self.multiplier)" + } + + if self.secondAttribute == .NotAnAttribute { + description += " \(self.constant)" + } else { + if self.constant > 0.0 { + description += " + \(self.constant)" + } else if self.constant < 0.0 { + description += " - \(CGFloat.abs(self.constant))" + } + } + + if self.priority != 1000.0 { + description += " ^\(self.priority)" + } + + description += ">" + + return description + } + +} + +private var labelKey = "" + +private func descriptionForObject(object: AnyObject) -> String { + let pointerDescription = NSString(format: "%p", [object]) + if let object = object as? UIView { + return "<\(object.dynamicType):\(object.snp_label ?? pointerDescription)>" + } else if let object = object as? LayoutConstraint { + return "<\(object.dynamicType):\(object.snp_label ?? pointerDescription)>" + } + return "<\(object.dynamicType):\(pointerDescription)>" +} + +private extension NSLayoutRelation { + + private var snp_description: String { + switch self { + case .Equal: return "==" + case .GreaterThanOrEqual: return ">=" + case .LessThanOrEqual: return "<=" + } + } + +} + +private extension NSLayoutAttribute { + + private var snp_description: String { + #if os(iOS) + switch self { + case .NotAnAttribute: return "notAnAttribute" + case .Top: return "top" + case .Left: return "left" + case .Bottom: return "bottom" + case .Right: return "right" + case .Leading: return "leading" + case .Trailing: return "trailing" + case .Width: return "width" + case .Height: return "height" + case .CenterX: return "centerX" + case .CenterY: return "centerY" + case .Baseline: return "baseline" + case .FirstBaseline: return "firstBaseline" + case .TopMargin: return "topMargin" + case .LeftMargin: return "leftMargin" + case .BottomMargin: return "bottomMargin" + case .RightMargin: return "rightMargin" + case .LeadingMargin: return "leadingMargin" + case .TrailingMargin: return "trailingMargin" + case .CenterXWithinMargins: return "centerXWithinMargins" + case .CenterYWithinMargins: return "centerYWithinMargins" + } + #else + switch self { + case .NotAnAttribute: return "notAnAttribute" + case .Top: return "top" + case .Left: return "left" + case .Bottom: return "bottom" + case .Right: return "right" + case .Leading: return "leading" + case .Trailing: return "trailing" + case .Width: return "width" + case .Height: return "height" + case .CenterX: return "centerX" + case .CenterY: return "centerY" + case .Baseline: return "baseline" + } + #endif + + } + +} diff --git a/Source/LayoutConstraint.swift b/Source/LayoutConstraint.swift index 1d92389..6f78022 100644 --- a/Source/LayoutConstraint.swift +++ b/Source/LayoutConstraint.swift @@ -57,4 +57,5 @@ public func ==(left: LayoutConstraint, right: LayoutConstraint) -> Bool { return false } return true -} \ No newline at end of file +} + diff --git a/Source/View+Snap.swift b/Source/View+Snap.swift index 749799c..5d7c1f8 100644 --- a/Source/View+Snap.swift +++ b/Source/View+Snap.swift @@ -29,6 +29,9 @@ import AppKit public typealias View = NSView #endif +/** + * View extension that exposes primary api + */ public extension View { // normal