Browse Source

Added better debugging support

Support for snp_label and better descriptions when printing LayoutConstraint's
Robert Payne 10 years ago
parent
commit
08ff0472b2
4 changed files with 187 additions and 3 deletions
  1. 7 2
      Snap.xcodeproj/project.pbxproj
  2. 175 0
      Source/Debugging.swift
  3. 2 1
      Source/LayoutConstraint.swift
  4. 3 0
      Source/View+Snap.swift

+ 7 - 2
Snap.xcodeproj/project.pbxproj

@@ -31,6 +31,7 @@
 		EECDB39B1AC0CBFF006BBC11 /* LayoutConstraint.swift in Sources */ = {isa = PBXBuildFile; fileRef = EECDB3651AC0C95C006BBC11 /* LayoutConstraint.swift */; };
 		EECDB39B1AC0CBFF006BBC11 /* LayoutConstraint.swift in Sources */ = {isa = PBXBuildFile; fileRef = EECDB3651AC0C95C006BBC11 /* LayoutConstraint.swift */; };
 		EECDB39C1AC0CBFF006BBC11 /* View+Snap.swift in Sources */ = {isa = PBXBuildFile; fileRef = EECDB3671AC0C95C006BBC11 /* View+Snap.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, ); }; };
 		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 */
 /* End PBXBuildFile section */
 
 
 /* Begin PBXContainerItemProxy section */
 /* Begin PBXContainerItemProxy section */
@@ -62,6 +63,7 @@
 		EECDB36A1AC0C95C006BBC11 /* Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Tests.swift; sourceTree = "<group>"; };
 		EECDB36A1AC0C95C006BBC11 /* Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Tests.swift; sourceTree = "<group>"; };
 		EECDB37A1AC0C9D4006BBC11 /* Snap.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Snap.framework; sourceTree = BUILT_PRODUCTS_DIR; };
 		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; };
 		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 = "<group>"; };
 /* End PBXFileReference section */
 /* End PBXFileReference section */
 
 
 /* Begin PBXFrameworksBuildPhase section */
 /* Begin PBXFrameworksBuildPhase section */
@@ -104,7 +106,6 @@
 			isa = PBXGroup;
 			isa = PBXGroup;
 			children = (
 			children = (
 				EECDB35D1AC0C95C006BBC11 /* Source */,
 				EECDB35D1AC0C95C006BBC11 /* Source */,
-				EECDB3681AC0C95C006BBC11 /* Tests */,
 				EE94F60C1AC0F113008767FF /* Frameworks */,
 				EE94F60C1AC0F113008767FF /* Frameworks */,
 				DDC9FD961981B4DD009612C7 /* Products */,
 				DDC9FD961981B4DD009612C7 /* Products */,
 			);
 			);
@@ -142,7 +143,9 @@
 				EECDB3631AC0C95C006BBC11 /* EdgeInsets.swift */,
 				EECDB3631AC0C95C006BBC11 /* EdgeInsets.swift */,
 				EECDB3651AC0C95C006BBC11 /* LayoutConstraint.swift */,
 				EECDB3651AC0C95C006BBC11 /* LayoutConstraint.swift */,
 				EECDB3671AC0C95C006BBC11 /* View+Snap.swift */,
 				EECDB3671AC0C95C006BBC11 /* View+Snap.swift */,
+				EEFCF32B1AD910B900A425FA /* Debugging.swift */,
 				EECDB36B1AC0C967006BBC11 /* Supporting Files */,
 				EECDB36B1AC0C967006BBC11 /* Supporting Files */,
+				EECDB3681AC0C95C006BBC11 /* Tests */,
 			);
 			);
 			path = Source;
 			path = Source;
 			sourceTree = "<group>";
 			sourceTree = "<group>";
@@ -153,7 +156,8 @@
 				EECDB3691AC0C95C006BBC11 /* Info.plist */,
 				EECDB3691AC0C95C006BBC11 /* Info.plist */,
 				EECDB36A1AC0C95C006BBC11 /* Tests.swift */,
 				EECDB36A1AC0C95C006BBC11 /* Tests.swift */,
 			);
 			);
-			path = Tests;
+			name = Tests;
+			path = ../Tests;
 			sourceTree = "<group>";
 			sourceTree = "<group>";
 		};
 		};
 		EECDB36B1AC0C967006BBC11 /* Supporting Files */ = {
 		EECDB36B1AC0C967006BBC11 /* Supporting Files */ = {
@@ -338,6 +342,7 @@
 			buildActionMask = 2147483647;
 			buildActionMask = 2147483647;
 			files = (
 			files = (
 				EECDB36C1AC0C9A6006BBC11 /* Constraint.swift in Sources */,
 				EECDB36C1AC0C9A6006BBC11 /* Constraint.swift in Sources */,
+				EEFCF32C1AD910B900A425FA /* Debugging.swift in Sources */,
 				EECDB3701AC0C9A6006BBC11 /* ConstraintRelation.swift in Sources */,
 				EECDB3701AC0C9A6006BBC11 /* ConstraintRelation.swift in Sources */,
 				EECDB3731AC0C9A6006BBC11 /* View+Snap.swift in Sources */,
 				EECDB3731AC0C9A6006BBC11 /* View+Snap.swift in Sources */,
 				EECDB3711AC0C9A6006BBC11 /* EdgeInsets.swift in Sources */,
 				EECDB3711AC0C9A6006BBC11 /* EdgeInsets.swift in Sources */,

+ 175 - 0
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
+        
+    }
+    
+}

+ 2 - 1
Source/LayoutConstraint.swift

@@ -57,4 +57,5 @@ public func ==(left: LayoutConstraint, right: LayoutConstraint) -> Bool {
         return false
         return false
     }
     }
     return true
     return true
-}
+}
+

+ 3 - 0
Source/View+Snap.swift

@@ -29,6 +29,9 @@ import AppKit
 public typealias View = NSView
 public typealias View = NSView
 #endif
 #endif
 
 
+/**
+ * View extension that exposes primary api
+ */
 public extension View {
 public extension View {
     
     
     // normal
     // normal