Skip to content

Commit

Permalink
Add “Open Package” (⇧⌘D), uses current line’s import path
Browse files Browse the repository at this point in the history
If the current line is not an import statement then it will prompt the user for the package to open. Default dialog text is the contents of the search clipboard.

The package is located via ‘GOPATH’ and ‘GOROOT’ (obtained via ‘go env’).

If the package contains a single go file (excluding tests) then that file will be opened, otherwise the package is opened as a new project.
  • Loading branch information
sorbits committed Jul 14, 2013
1 parent 5a3b308 commit 42b66fe
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
80 changes: 80 additions & 0 deletions Commands/Open Package.tmCommand
@@ -0,0 +1,80 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>beforeRunningCommand</key>
<string>nop</string>
<key>command</key>
<string>#!/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby
require "shellwords"
def import_path
if ENV.has_key? 'TM_SELECTED_TEXT'
ENV['TM_SELECTED_TEXT']
elsif ENV['TM_CURRENT_LINE'] =~ /^\s*(?:import\s+)?(?:\.|[[:alpha:]_][[:alnum:]_]*\s+)?(["`])(.*?)\1/;
$2
else
defaultText = %x{ /usr/bin/pbpaste -pboard find }
require "#{ENV['TM_SUPPORT_PATH']}/lib/ui.rb"
TextMate::UI.request_string :title =&gt; "Open Package", :default =&gt; defaultText, :prompt =&gt; "Which package do you wish to open?"
end
end
def go_path
env = %x{"${TM_GO:-go}" env}
if $? == 0
lcal, root = [], []
env.scan(/^GO(PATH|ROOT)="(.*)"/) do |key,value|
case key
when 'PATH': lcal = value.split(':').map { |dir| "#{dir}/src" }
when 'ROOT': root = value.split(':').map { |dir| "#{dir}/src/pkg" }
end
end
[ lcal, root ].flatten
else
ENV['GOPATH'].to_s.split(':').map { |dir| "#{dir}/src" }
end
end
def find_package_path(package)
go_path.each do |dir|
path = File.expand_path(package, dir)
if File.directory?(path)
files = Dir.entries(path).select { |file| file =~ /\.go$/ &amp;&amp; file !~ /_test\.go$/ }
return files.size == 1 ? File.join(path, files.first) : path
end
end
nil
end
if package = import_path()
if path = find_package_path(package)
%x{"$TM_MATE" #{path.shellescape}}
else
require "#{ENV['TM_SUPPORT_PATH']}/lib/exit_codes.rb"
TextMate.exit_show_tool_tip "Unable to locate package for import path ‘#{package}’."
end
end
</string>
<key>input</key>
<string>selection</string>
<key>inputFormat</key>
<string>text</string>
<key>keyEquivalent</key>
<string>@D</string>
<key>name</key>
<string>Open Package</string>
<key>outputCaret</key>
<string>afterOutput</string>
<key>outputFormat</key>
<string>text</string>
<key>outputLocation</key>
<string>discard</string>
<key>scope</key>
<string>source.go</string>
<key>uuid</key>
<string>D3CD6B51-3A7E-4356-85F4-B76B8336BEF2</string>
<key>version</key>
<integer>2</integer>
</dict>
</plist>
1 change: 1 addition & 0 deletions Support/gomate.rb
Expand Up @@ -24,6 +24,7 @@ def Go::go(command, options={})

if command == 'test' && ENV['TM_FILENAME'] =~ /(_test)?(\.go)$/
basename = $`
args.push("-v")
args.push("#{basename}.go")
args.push("#{basename}_test.go")
opts[:chdir] = ENV['TM_DIRECTORY']
Expand Down
2 changes: 2 additions & 0 deletions info.plist
Expand Up @@ -20,7 +20,9 @@
<string>0F6A8710-54FC-48F5-9D02-D093DA001D17</string>
<string>73628139-0077-4F09-9B72-77546D7C2D2D</string>
<string>------------------------------------</string>
<string>D3CD6B51-3A7E-4356-85F4-B76B8336BEF2</string>
<string>7BCFCFC8-9152-4638-8436-E17B0C754C8D</string>
<string>------------------------------------</string>
<string>B0271A46-F6EF-4D2F-95A6-EC067E69155C</string>
<string>------------------------------------</string>
<string>C64599DA-E362-4411-9782-58A9C7F1B05A</string>
Expand Down

0 comments on commit 42b66fe

Please sign in to comment.