NSLogger icon indicating copy to clipboard operation
NSLogger copied to clipboard

Jump into XCode to log emitting line

Open holtwick opened this issue 14 years ago • 19 comments
trafficstars

May I remember the 'jump to code' feature ;) It was part of this ticket. https://github.com/fpillet/NSLogger/issues/closed#issue/10

I think it should be a straight forward implementation using some Apple Script magic. Maybe someone who knows Apple Script better than me could prepare the code to be used in NSLogger?

Would be great if I could have double click on a log entry to jump to the code. Maybe worth a settings option to let the user choose if he prefers jump to code or logging details?

holtwick avatar Jan 13 '11 16:01 holtwick

I don't forget about you ... I just didn't have the time to do it yet -- need to intercept mouve clicks in the cell and over the filename/function line and call the xed command line to open the file. Will do soon :-)

As to differentiating simple and double clicks, that's relatively easy - there is a flag for that in events.

fpillet avatar Jan 13 '11 16:01 fpillet

Sorry, I didn't want to urge you :)

Maybe it could be added as a button to the details window as a first step.

But I don't need the detail window at all, therefore it would be also ok for me to get into the code on double click directly ;)

holtwick avatar Jan 13 '11 16:01 holtwick

Argh... by accident I closed this ticket. I think only you can reopen it again. Sorry.

holtwick avatar Jan 13 '11 17:01 holtwick

Congratulations to 1.0 release. I would really love to see this feature in NSLogger though. I made some investigation and you can use the following XCode command line tool to jump to a file and line:

xed --line 100 ~/work/xyz/MainWindowController.m

It should be very straight forward to implement this feature e.g. using NSTask to call this. It seems that XCode is entelligent enough to open the correct Workspace or Project.

holtwick avatar Oct 31 '11 12:10 holtwick

I pushed an experimental implementation to the 'development' branch. Alt-doubleclick to open the file in Xcode. Unfortunately, xed doesn't seem to direct Xcode to select the proper line (there's an OSA error, try it in console with the same arguments to see it).

If you want to help refine the implementation, look at -logCellDoubleClicked: in LoggerWindowController.m. Ultimately, what I want is an arrow that shows up when you hover a log entry that has a valid filename attached to it. Lacking the time to implement this, though.

fpillet avatar Oct 31 '11 15:10 fpillet

Indeed. I have no idea how to solve it. I posted a question to StackOverflow where hopefully some genius may help us out with a solution. http://stackoverflow.com/questions/7957016/jump-to-file-and-line-from-outside-xcode-4-2

Thanks for implementing the stuff so far.

holtwick avatar Oct 31 '11 17:10 holtwick

Bug reported to Apple as rdar://10626303 - See a copy of it at http://openradar.appspot.com/radar?id=1480404

gcerquant avatar Dec 26 '11 11:12 gcerquant

If you want to you could replace xed with the following shell/applescript combo:

#!/bin/bash

if [ "$1" = "-l" ] || [ "$1" = "--line" ] ; then
        line=$2
        file=$3
else
        line=1
        file=$1
fi

osascript &>/dev/null <<EOF
tell application "Xcode"
        open "$file"
        activate
        tell application "System Events"
                tell process "Xcode"
                        keystroke "l" using command down
                        repeat until window "Jump" exists
                        end repeat
                        click text field 1 of window "Jump"
                        set value of text field 1 of window "Jump" to "$line"
                        keystroke return
                end tell
        end tell
end tell
EOF

Warning:

  • no error checking,
  • crude commandline parsing
  • not the full functionality of the original xed
  • "Jump in 'filename.m'" in the "Navigate" Menu in Xcode must have command+L as shortcut
  • Universal Access has to be turned on for this to work

dunkelstern avatar Mar 30 '12 08:03 dunkelstern

Awesome! The open line did not work for me and since I'm not very good at AppleScript I made a little workaround ;)

#!/bin/bash

if [ "$1" = "-l" ] || [ "$1" = "--line" ] ; then
        line=$2
        file=$3
else
        line=1
        file=$1
fi

xed "$file"

osascript &>/dev/null <<EOF
tell application "Xcode"        
        activate
        tell application "System Events"
                tell process "Xcode"
                        keystroke "l" using command down
                        repeat until window "Jump" exists
                        end repeat
                        click text field 1 of window "Jump"
                        set value of text field 1 of window "Jump" to "$line"
                        keystroke return
                end tell
        end tell
end tell
EOF

holtwick avatar Mar 30 '12 10:03 holtwick

Fantastic! Thanks guys - BTW I'm now using AppCode most of the time instead of Xcode, which starts sucking balls. Need to find a way to do this for AppCode too :-)

Thanks for the awesome script, will get this integrated in NSLogger ASAP.

fpillet avatar Mar 30 '12 10:03 fpillet

if you omit the "open" thing, it jumps to the line in the current open file in Xcode so that would not be wise to do.

Perhaps that's some file name quoting issue, if i hit the problem too i'll update.

Current Code in this gist: https://gist.github.com/2250729

dunkelstern avatar Mar 30 '12 10:03 dunkelstern

Indeed, your script works perfectly. The problem was that I did not use an absolute file path. Sorry, my fault. This will be a killer feature for NSLogger :)

holtwick avatar Mar 30 '12 12:03 holtwick

I added the feature from the development branch again. So far it seems to work fine. On ALT + Double Click it jumps into Xcode. It just does not show the cursor or hilite the line somehow, but this may just be little tweaks in the Apple Script. Hope you like it: https://github.com/holtwick/NSLogger/commit/22e21507bd569edb066a596be885aca43b42b633

holtwick avatar Apr 08 '12 10:04 holtwick

That's a really nice future. Great job guys.

I noticed two bugs though:

  • I have several osascript processes running on my machine after using the feature
  • Double-clicking does not always do what expected: sometimes the "Jump" stays open, sometimes I have line of code deleted, sometimes it does not jump to the line. I guess this is a "synchronization" problem.

I had a rapid look into it but did not find any solution for now.

pbernery avatar May 01 '12 19:05 pbernery

thanks, guys! this is great, much appreciated.

one note, in case it helps anyone else: i have a global key binding for Command-L, so the keystroke "l" using command down AppleScript didn't work for me. I replaced it with click menu item index 36 of menu "Navigate" of menu bar item "Navigate" of menu bar 1. Works in Xcode 4.6. It'll need to be updated if/when the Navigate menu changes, or replaced with something that iterates through the menu items and checks for the "Jump in " substring, but it works for me for now.

snarfed avatar Apr 04 '13 05:04 snarfed

I optimized this script to make it get to the line in the source file much faster. It could probably use some fallback logic in case for some reason you don't already have the project open (though.. that's what mainly seemed to slow everything down...)

Code is here → Made "⌥ + double click" Open Quickly

PaulCapestany avatar Apr 10 '13 09:04 PaulCapestany

awesome, thanks!

snarfed avatar Apr 10 '13 14:04 snarfed

I also realized that some of the weird behavior people may have been noticing with this is most likely caused by having to press ⌥ when double clicking: if you don't let go of the ⌥ key pretty darn fast, then it can interfere with the AppleScript keystroke commands, resulting in Xcode not doing what you want it to do.

Code that I think addresses this issue: https://github.com/fpillet/NSLogger/pull/102

PaulCapestany avatar Apr 10 '13 20:04 PaulCapestany

For whoever may be interested, I came up with what I think are some more improvements: https://github.com/fpillet/NSLogger/pull/114

PaulCapestany avatar Jul 05 '13 21:07 PaulCapestany