Skip to content

Commit 6575d4f

Browse files
committed
Def: Initial support for user-supplied package.
godef does an extremely fast and accurate job of locating identifiers, so we'll let godef have have the first attempt. If godef can't figure out what the user wants, we'll simply ask them for the package name. Splits Def's implementation out of gomate.rb because we must use TextMate::UI which requires TM's builtin ruby18.
1 parent 61ff4bb commit 6575d4f

File tree

2 files changed

+42
-39
lines changed

2 files changed

+42
-39
lines changed

Commands/Def.tmCommand

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,54 @@
55
<key>beforeRunningCommand</key>
66
<string>nop</string>
77
<key>command</key>
8-
<string>#!/usr/bin/env ruby
8+
<string>#!/usr/bin/env ruby18
99
10-
require "#{ENV['TM_BUNDLE_SUPPORT']}/gomate"
11-
Go::godef</string>
10+
require ENV['TM_SUPPORT_PATH'] + '/lib/textmate'
11+
require ENV['TM_SUPPORT_PATH'] + '/lib/tm/process'
12+
require ENV['TM_SUPPORT_PATH'] + '/lib/tm/save_current_document'
13+
14+
# TextMate's special TM_GODEF or expect 'godef' on PATH
15+
godef_cmd = ENV['TM_GODEF'] || 'godef'
16+
17+
TextMate.save_if_untitled('go')
18+
19+
# load current document from stdin
20+
document = []
21+
while line = $stdin.gets
22+
document.push(line)
23+
end
24+
25+
# byte offset of cursor position from the beginning of file
26+
cursor = document[ 0, ENV['TM_LINE_NUMBER'].to_i - 1].join().length + ENV['TM_LINE_INDEX'].to_i
27+
28+
args = []
29+
args.push(godef_cmd)
30+
args.push('-f')
31+
args.push(ENV['TM_FILEPATH'])
32+
args.push('-o')
33+
args.push("#{cursor}")
34+
35+
# /usr/local/Cellar/go/1.7.1/libexec/src/database/sql/sql.go:960:15
36+
out, err = TextMate::Process.run(*args)
37+
38+
if err.nil? || err == ''
39+
file_details = out.split(':')
40+
TextMate.go_to(:file =&gt; file_details[0],
41+
:line =&gt; file_details[1],
42+
:column =&gt; file_details[2].to_i)
43+
elsif err.casecmp "godef: no identifier found";
44+
require "#{ENV['TM_SUPPORT_PATH']}/lib/ui.rb"
45+
package = TextMate::UI.request_string :title =&gt; "Open Package", :default =&gt; '', :prompt =&gt; "Which package do you wish to open?"
46+
#TODO: Find and open package
47+
else
48+
TextMate.exit_show_tool_tip(err)
49+
end</string>
1250
<key>input</key>
1351
<string>selection</string>
1452
<key>inputFormat</key>
1553
<string>text</string>
1654
<key>keyEquivalent</key>
17-
<string>@g</string>
55+
<string>@D</string>
1856
<key>name</key>
1957
<string>Go to Definition</string>
2058
<key>outputCaret</key>

Support/gomate.rb

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -92,41 +92,6 @@ def Go::gogetdoc
9292

9393
end
9494

95-
def Go::godef
96-
# TextMate's special TM_GODEF or expect 'godef' on PATH
97-
godef_cmd = ENV['TM_GODEF'] || 'godef'
98-
99-
TextMate.save_if_untitled('go')
100-
101-
# load current document from stdin
102-
document = []
103-
while line = $stdin.gets
104-
document.push(line)
105-
end
106-
107-
# byte offset of cursor position from the beginning of file
108-
cursor = document[ 0, ENV['TM_LINE_NUMBER'].to_i - 1].join().length + ENV['TM_LINE_INDEX'].to_i
109-
110-
args = []
111-
args.push(godef_cmd)
112-
args.push('-f')
113-
args.push(ENV['TM_FILEPATH'])
114-
args.push('-o')
115-
args.push("#{cursor}")
116-
117-
# /usr/local/Cellar/go/1.7.1/libexec/src/database/sql/sql.go:960:15
118-
out, err = TextMate::Process.run(*args)
119-
120-
if err.nil? || err == ''
121-
file_details = out.split(':')
122-
TextMate.go_to(:file => file_details[0],
123-
:line => file_details[1],
124-
:column => file_details[2].to_i)
125-
else
126-
TextMate.exit_show_tool_tip(err)
127-
end
128-
end
129-
13095
def Go::golint
13196
golint = ENV['TM_GOLINT'] || 'golint'
13297
TextMate.save_if_untitled('go')

0 commit comments

Comments
 (0)