App idea: extract method name from selected text

This is an idea for a Service (as in the Services menu) to help Objective-C programmers.

Often I'm looking at a method declaration or method invocation and I'd like to be able to copy and paste just the method name and not any of the arguments.

For example, say I'm looking at a method declaration like this:

- (void)putBook:(NSString *)theBookName
      inSection:(NSString *)theSectionName
      ofLibrary:(NSString *)theLibraryName
         inCity:(NSString *)theCityName;

…or a method invocation like this:

[masterLibrarian putBook:@"The Dark Knight"
               inSection:@"Graphic Novels"
               ofLibrary:@"Mid-Manhattan Branch"
                  inCity:@"New York City"];

I'd like to be able to select either of these code snippets and invoke a Service that puts "putBook:inSection:ofLibrary:inCity:" into my pasteboard. Ideally, my selection could be sloppy at the beginning and end, so I could select:

             ian putBook:@"The Dark Knight"
               inSection:@"Graphic Novels"
               ofLibrary:@"Mid-Manhattan Branch"

…and the Service would know what I mean.

Lately I've been running into situations like the above pretty frequently. One case is when I'm writing a method comment:

 * @method      putBook:inSection:ofLibrary:inCity:
 * @discussion  Blah blah blah...

Another case is when I'm studying someone else's code and I'm taking notes about how a group of classes interact:

Upon receiving the XXBookDidGetClassified notification, we send a putBook:inSection:ofLibrary:inCity: message to the Librarian object and commit the change to the database.

I can imagine other uses as well — for example, selecting a fragment of code in a cocoa-dev email and pasting the method name into either Xcode's or AppKiDo's search field.

It would be nice if code completion could help here, but code completion in comments only completes the first word in a method name, without the colon — i.e., "putBook" in the example above. (I just filed rdar://7197881 about this.) Also, code completion only works in Xcode, and I use TextWrangler to edit notes that are not in the code.

Another way to do this would be to click anywhere in the method name and bring up the Refactoring window, which has the method name already selected and ready to copy to the pasteboard. But this adds two keystrokes — one to open the Refactoring window and one to close it — plus a moment's delay before the window appears. And this only works if I'm looking at the code in Xcode.

Come to think of it, if Xcode can populate that field in the Refactoring window, it can just as easily put the method name into my pasteboard. It could provide this functionality in a Service as well, so I'd be able to use it when I select code in a cocoa-dev message, for example, or on a web page. I just filed rdar://7198039 asking for this.

2 thoughts on “App idea: extract method name from selected text

  1. I wrote a command line tool for that a long time ago. I've just put it online on my Bitbucket:

    But that's a command-line tool, not a service. No problem: ThisService makes it easy to turn that command-line tool into a service, and then to make it distributable. Here's the service:

    The tool I wrote is not exactly what you wanted (unsurprisingly, considering it pre-dates this post by nearly four and three-quarter years). For one thing, it only works on declarations, not message expressions nor fragments thereof. But triple-click-and-drag makes selecting the whole declaration easy enough. The other problem is that it acts as a filter, replacing the selection with its output instead of copying it to the general pasteboard. That problem would not be hard to solve, and I'd welcome a patch.

    Nonetheless, I hope you find it useful.

  2. Thanks, Peter! I'll take a look when I get a chance.

    Funny, I was about to be lazy and say I could create a keyboard macro that brings up the Xcode Refactoring window, does a Copy, and closes the window, and that would suffice for many of my needs. But having a bunch of work already done by you makes the problem much more interesting. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.