From 95790d55d587f07cd16cef9855f984e281f598cb Mon Sep 17 00:00:00 2001 From: Aparajita Fishman Date: Fri, 25 Jan 2013 19:19:01 +0800 Subject: [PATCH] Object description formatting - Made CPDescriptionOfObject smarter about detecting CG objects - CPObject, CPArray, CPDictionary do a better (as in perfect) job of indenting nested objects - CPDictionary is displayed using Cocoa-style key = value; notation --- AppKit/CPColor.j | 4 +-- AppKit/CoreGraphics/CGGradient.j | 1 + Foundation/CPArray/_CPArray.j | 12 ++++---- Foundation/CPDictionary.j | 7 +++-- Foundation/CPObject.j | 47 +++++++++++++++++++++++++++---- Tools/dump_theme/dump_theme | 48 +++----------------------------- 6 files changed, 60 insertions(+), 59 deletions(-) diff --git a/AppKit/CPColor.j b/AppKit/CPColor.j index 36e852291..31ecd93cd 100644 --- a/AppKit/CPColor.j +++ b/AppKit/CPColor.j @@ -740,7 +740,7 @@ url("data:image/png;base64,BASE64ENCODEDDATA") // if there is a pattern image for (var i = 0; i < slices.length; ++i) { - var imgDescription = [slices[i] description]; + var imgDescription = [slices[i] description] || "nil"; description += imgDescription.replace(/^/mg, " ") + ",\n"; } @@ -748,7 +748,7 @@ url("data:image/png;base64,BASE64ENCODEDDATA") // if there is a pattern image description = description.substr(0, description.length - 2) + "\n ]\n}"; } else - description += [patternImage description].replace(/^/mg, " ") + "\n}"; + description += ([patternImage description] || "nil").replace(/^/mg, " ") + "\n}"; return description; } diff --git a/AppKit/CoreGraphics/CGGradient.j b/AppKit/CoreGraphics/CGGradient.j index 2635f0514..3681c086e 100644 --- a/AppKit/CoreGraphics/CGGradient.j +++ b/AppKit/CoreGraphics/CGGradient.j @@ -33,6 +33,7 @@ function CGGradientCreateWithColorComponents(aColorSpace, components, locations, { var num_of_colors = components.length / 4, locations = []; + for (var idx = 0; idx < num_of_colors; idx++) locations.push( idx / (num_of_colors - 1) ); } diff --git a/Foundation/CPArray/_CPArray.j b/Foundation/CPArray/_CPArray.j index 7b61092eb..2832bba8d 100755 --- a/Foundation/CPArray/_CPArray.j +++ b/Foundation/CPArray/_CPArray.j @@ -874,18 +874,20 @@ var concat = Array.prototype.concat, { var index = 0, count = [self count], - description = "@["; + description = "["; for (; index < count; ++index) { if (index === 0) - description += "\n\t"; + description += "\n"; var object = [self objectAtIndex:index]; - description += CPDescriptionOfObject(object); - if (index !== count - 1) - description += ",\n\t"; + // NOTE: replace(/^/mg, " ") inserts 4 spaces at the beginning of every line + description += CPDescriptionOfObject(object).replace(/^/mg, " "); + + if (index < count - 1) + description += ",\n"; else description += "\n"; } diff --git a/Foundation/CPDictionary.j b/Foundation/CPDictionary.j index 2e600d463..c2199e683 100755 --- a/Foundation/CPDictionary.j +++ b/Foundation/CPDictionary.j @@ -638,17 +638,20 @@ */ - (CPString)description { - var string = "@{\n", + var string = "@{", keys = self._keys, index = 0, count = self._count; for (; index < count; ++index) { + if (index === 0) + string += "\n"; + var key = keys[index], value = self.valueForKey(key); - string += "\t" + key + ": " + CPDescriptionOfObject(value).split('\n').join("\n\t") + ",\n"; + string += " " + key + " = " + CPDescriptionOfObject(value).split("\n").join("\n ") + ";\n"; } return string + "}"; diff --git a/Foundation/CPObject.j b/Foundation/CPObject.j index 34bc2f4af..bdb11df0b 100644 --- a/Foundation/CPObject.j +++ b/Foundation/CPObject.j @@ -538,24 +538,59 @@ CPLog(@"Got some class: %@", inst); function CPDescriptionOfObject(anObject) { + if (anObject === nil) + return "nil"; + + if (anObject === undefined) + return "undefined"; + if (anObject.isa) { if ([anObject isKindOfClass:CPString]) return '@"' + [anObject description] + '"'; + return [anObject description]; } if (typeof(anObject) !== "object") return String(anObject); - var desc = "JSObject\n{\n"; + var properties = [], + desc; for (var property in anObject) - { if (anObject.hasOwnProperty(property)) - desc += " " + property + ": " + CPDescriptionOfObject(anObject[property]) + "\n"; - } - desc += "}"; + properties.push(property); - return desc.split('\n').join("\n\t"); + properties.sort(); + + if (properties.length === 2 && anObject.hasOwnProperty("width") && anObject.hasOwnProperty("height")) + desc = [CPString stringWithFormat:@"CGSize: { width:%f, height:%f }", anObject.width, anObject.height]; + else if (properties.length === 2 && anObject.hasOwnProperty("x") && anObject.hasOwnProperty("y")) + desc = [CPString stringWithFormat:@"CGPoint: { x:%f, y:%f }", anObject.x, anObject.y]; + else if (properties.length === 2 && anObject.hasOwnProperty("origin") && anObject.hasOwnProperty("size")) + desc = [CPString stringWithFormat:@"CGRect: { x:%f, y:%f }, { width:%f, height:%f }", anObject.origin.x, anObject.origin.y, anObject.size.width, anObject.size.height]; + else if (properties.length === 4 && anObject.hasOwnProperty("top") && anObject.hasOwnProperty("right") && anObject.hasOwnProperty("bottom") && anObject.hasOwnProperty("left")) + desc = [CPString stringWithFormat:@"CGInset: { top:%f, right:%f, bottom:%f, left:%f }", anObject.top, anObject.right, anObject.bottom, anObject.left]; + else + { + desc = "{"; + + for (var i = 0; i < properties.length; ++i) + { + if (i === 0) + desc += "\n"; + + desc += " " + properties[i] + ": " + CPDescriptionOfObject(anObject[properties[i]]).split("\n").join("\n "); + + if (i < properties.length - 1) + desc += ",\n"; + else + desc += "\n"; + } + + desc += "}"; + } + + return desc; } diff --git a/Tools/dump_theme/dump_theme b/Tools/dump_theme/dump_theme index b6b6ddef0..379b5c6fa 100755 --- a/Tools/dump_theme/dump_theme +++ b/Tools/dump_theme/dump_theme @@ -1,7 +1,8 @@ #!/usr/bin/env objj -@import -@import +@import +@import +@import @import var FILE = require("file"), @@ -133,7 +134,7 @@ function main(args) { var state = states[stateIndex], value = [theme valueForAttributeWithName:attributeName inState:state forClass:className], - description = [self descriptionForValue:value].replace(/^/mg, " "); + description = CPDescriptionOfObject(value).replace(/^/mg, " "); colorPrint("cyan", " " + CPThemeStateName(state)); stream.print(description + "\n"); @@ -144,47 +145,6 @@ function main(args) } } -- (CPString)descriptionForValue:(id)aValue -{ - var description = @""; - - if (!aValue) - return @""; - - if (!aValue.isa) - { - if (typeof(aValue) === "object") - { - if (aValue.hasOwnProperty("width") && aValue.hasOwnProperty("height")) - description = [CPString stringWithFormat:@"CGSize: { width:%f, height:%f }", aValue.width, aValue.height]; - else if (aValue.hasOwnProperty("x") && aValue.hasOwnProperty("y")) - description = [CPString stringWithFormat:@"CGPoint: { x:%f, y:%f }", aValue.x, aValue.y]; - else if (aValue.hasOwnProperty("origin") && aValue.hasOwnProperty("size")) - description = [CPString stringWithFormat:@"CGRect: { x:%f, y:%f }, { width:%f, height:%f }", aValue.origin.x, aValue.origin.y, aValue.size.width, aValue.size.height]; - else if (aValue.hasOwnProperty("top") && aValue.hasOwnProperty("right") && aValue.hasOwnProperty("bottom") && aValue.hasOwnProperty("left")) - description = [CPString stringWithFormat:@"CGInset: { top:%f, right:%f, bottom:%f, left:%f }", aValue.top, aValue.right, aValue.bottom, aValue.left]; - else - { - description = "Object\n{\n"; - - for (var property in aValue) - { - if (aValue.hasOwnProperty(property)) - description += " " + property + ":" + aValue[property] + "\n"; - } - - description += "}"; - } - } - else - description = "Unknown object"; - } - else - description = [aValue description]; - - return description; -} - // Returns undefined if $CAPP_BUILD is not defined, false if path cannot be found in $CAPP_BUILD - (id)findResourceInCappBuild:(CPString)path isDirectory:(BOOL)isDirectory callback:(SEL)callback {