Last active
July 12, 2017 17:39
-
-
Save elliottwilliams/bbdb63c8addf1616e9484d56d2673698 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
USAGE = <<EOF | |
usage: xcode2vimr.rb [path_of_open_xcode_file] | |
This script talks to Xcode via osascript to determine its cursor's line number | |
for the given file, then opens VimR to the specified line. | |
If no path argument is given, it determines the path via the title of the | |
frontmost Xcode window. | |
EOF | |
require 'open3' | |
VIMR = '/usr/local/bin/vimr' | |
FILE = ARGV[0] || `osascript -e 'tell application "Xcode" to \ | |
return path of document 1 whose name ends with \ | |
(word -1 of (get name of window 1))'`.chomp | |
abort USAGE if FILE == "-h" or FILE == "--help" | |
def open_to line | |
`#{VIMR} #{FILE} --nvim +:#{line}` | |
end | |
def line_of pos | |
open(FILE) { |f| f.read(pos).count($/) + 1 } | |
end | |
def xcode_cursor_position | |
pos, status = Open3.capture2e "osascript", "-", FILE, stdin_data: <<-EOF | |
to findSourceDocument(targetedPath) | |
tell application "Xcode" | |
repeat with i from 1 to (source document count) by 1 | |
tell source document i | |
if its path is targetedPath then return it | |
end tell | |
end repeat | |
source document count | |
end tell | |
end findSourceDocument | |
to getLowestPosition(tuple) | |
if item 1 of tuple < item 2 of tuple then | |
return item 1 of tuple | |
else | |
return item 2 of tuple | |
end if | |
end getLowestPosition | |
on run argv | |
set sourceDocument to findSourceDocument(item 1 of argv) | |
tell application "Xcode" to set characterRange to selected character range of sourceDocument | |
return getLowestPosition(characterRange) | |
end run | |
EOF | |
pos.to_i if status.success? | |
end | |
open_to line_of xcode_cursor_position |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment