Skip to content
Merged
Changes from 2 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
26 changes: 26 additions & 0 deletions commands/FBPrintCommands.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def lldbcommands():
FBPrintOnscreenTableViewCells(),
FBPrintInternals(),
FBPrintInstanceVariable(),
FBPrintKeyPath(),
]

class FBPrintViewHierarchyCommand(fb.FBCommand):
Expand Down Expand Up @@ -272,3 +273,28 @@ def run(self, arguments, options):

printCommand = 'po' if ('@' in ivarTypeEncodingFirstChar) else 'p'
lldb.debugger.HandleCommand('{} (({} *)({}))->{}'.format(printCommand, objectClass, object, ivarName))

class FBPrintKeyPath(fb.FBCommand):
def name(self):
return 'pkp'

def description(self):
return "Print out the value of [self valueForKeyPath:<>]."

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The use of self here is misleading. To offer a possible wording:

Print out the value of the key path expression using -valueForKeyPath:.


def args(self):
return [
fb.FBCommandArgument(arg='keypath', type='NSString *', help='The keypath to print'),
]

def run(self, arguments, options):
keys = arguments[0].split('.')

if keys.count == 1:
print '"' + arguments[0] + '" is not a keypath =('

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What do you think about treating this case as equal to po? That way, we can all type pkp except when evaluating something fancy.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

It's an interesting idea, to add durable-ness, but I think it'd need a lot more checks than the presence of a '.' to be worth implementing. IE, [UIApplication sharedApplication].keyWindow would still blow up. I'd rather blow up in all slightly-wrong scenarios just keep clarity of intent.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

See my other comment. Personally, while debugging, I'd rather make things as convenient as possible :)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Yea, looking at it again, I totally agree.

return

object = keys[0]
keys = keys[1:len(keys)]
keypath = ".".join(keys)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I liked the previous way, using split('.', 1), makes the above 3 lines a one liner, and still allows for handling the case where there is no key path.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

My python is terrible. Cleaned it up a tad. I also see the possibility of stepping through the key path in python and asking the object if it responds, and the stepping, or printing a good error message if it doesn't respond.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

My python is terrible.

Don't worry, Chisel was written by objc developers :)

I also see the possibility of stepping through the key path in python and asking the object if it responds, and the stepping, or printing a good error message if it doesn't respond.

💵

Ooh! It would even allow typos correction if we queried inspected the selectors on an object and found the nearest match.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

So I added some basic stepping through the key path, but an object can return a value from -valueForKey: and return false from -respondsToSelector:. So I'm not sure this will work without some more thought. There's no -respondsToKey: functionality.

I'm not sure if you use FuzzyAutocomplete, but that does perform a good deal of completion/typo fixes when using po (when it works). pkp does not get the auto-completion help like po does though. Any idea how that could be fixed?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for looking into it! It's probably diminishing returns to implement walking the key path.

printCommand = 'po [(NSObject *){} valueForKeyPath:@"{}"]'.format(object, keypath)
lldb.debugger.HandleCommand(printCommand)