Skip to content

Commit

Permalink
Updated all commands to use TM_* variables to locate necessary execut…
Browse files Browse the repository at this point in the history
…ables and TM_GOPATH.

* Commands/Build.tmCommand: Deleted as it's functionality is identical to
   Compile.tmCommand.  I discussed this with Alan Q. before doing so.
* Commands/Tidy.tmCommand: Deleted.
* Commands/Reformat Document.tmCommand: Renamed from location/name above.
* Support/goerrs
   (Go::link_errs): Fixed bug where the file location in the link was wrong and updated
    to work with both compile errors and syntax errors when formatting.
* Support/gomate.rb
   (Go::get_package_name, get_target_name): Deleted as for now, we should
    start simple and only run 'go [build|run]' against single files.  (Later
    we could create new commands that do stuff at the package/application
    level but until we make that distinction, keep it simple.)
   (Go::launch): Deleted.
   (Go::go): Renamed from Go::launch and updated to use TM_GO when available.
   (Go::format): Deleted.
   (Go::gofmt): Renamed from Go::format, updated to use TM_GOFMT when available
    and updated to properly handle parser errors when formatting.
   (Go::godoc): Added, uses TM_GODOC when available and updated to properly
    handle errors when retrieving documentation.
  • Loading branch information
whitlockjc authored and infininight committed Oct 19, 2012
1 parent 0ff2ffb commit ca55b4c
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 114 deletions.
26 changes: 0 additions & 26 deletions Commands/Build.tmCommand

This file was deleted.

14 changes: 11 additions & 3 deletions Commands/Compile.tmCommand
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,27 @@
<string>#!/usr/bin/env ruby
require "#{ENV['TM_BUNDLE_SUPPORT']}/gomate"
Go::launch "build", :verb =&gt; "Compiling", :scope =&gt; :file
Go::go "build", :verb =&gt; "Compiling"
</string>
<key>input</key>
<string>document</string>
<key>inputFormat</key>
<string>text</string>
<key>keyEquivalent</key>
<string>@k</string>
<key>name</key>
<string>Compile</string>
<key>output</key>
<string>showAsHTML</string>
<key>outputCaret</key>
<string>afterOutput</string>
<key>outputFormat</key>
<string>html</string>
<key>outputLocation</key>
<string>newWindow</string>
<key>scope</key>
<string>source.go</string>
<key>uuid</key>
<string>73628139-0077-4F09-9B72-77546D7C2D2D</string>
<key>version</key>
<integer>2</integer>
</dict>
</plist>
34 changes: 15 additions & 19 deletions Commands/Documentation for Word : Selection.tmCommand
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,35 @@
<!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>#!/bin/bash
<string>#!/usr/bin/env ruby
source "$TM_SUPPORT_PATH/lib/bash_init.sh"
source "$TM_SUPPORT_PATH/lib/webpreview.sh"
require_cmd godoc
word="$(cat)"
terms="$(echo "${word}"|sed 's:\.: :g')"
if godoc ${terms} &gt;/dev/null 2&gt;&amp;1; then
html_header "Documentation for “${word}”"
godoc -html ${terms}
html_footer
exit_show_html
else
echo "Couldn't find documentation for “${word}”"
fi
require "#{ENV['TM_BUNDLE_SUPPORT']}/gomate"
Go::godoc
</string>
<key>fallbackInput</key>
<string>word</string>
<key>input</key>
<string>selection</string>
<key>inputFormat</key>
<string>text</string>
<key>keyEquivalent</key>
<string>^h</string>
<key>name</key>
<string>Documentation for Word / Selection</string>
<key>output</key>
<string>showAsTooltip</string>
<key>outputCaret</key>
<string>afterOutput</string>
<key>outputFormat</key>
<string>html</string>
<key>outputLocation</key>
<string>toolTip</string>
<key>scope</key>
<string>source.go</string>
<key>uuid</key>
<string>7BCFCFC8-9152-4638-8436-E17B0C754C8D</string>
<key>version</key>
<integer>2</integer>
</dict>
</plist>
12 changes: 10 additions & 2 deletions Commands/Tidy.tmCommand → Commands/Reformat Document.tmCommand
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,27 @@
<string>#!/usr/bin/env ruby
require "#{ENV['TM_BUNDLE_SUPPORT']}/gomate"
Go::format
Go::gofmt
</string>
<key>input</key>
<string>document</string>
<key>inputFormat</key>
<string>text</string>
<key>keyEquivalent</key>
<string>^H</string>
<key>name</key>
<string>Reformat Document</string>
<key>output</key>
<key>outputCaret</key>
<string>interpolateByLine</string>
<key>outputFormat</key>
<string>text</string>
<key>outputLocation</key>
<string>replaceDocument</string>
<key>scope</key>
<string>source.go</string>
<key>uuid</key>
<string>B0271A46-F6EF-4D2F-95A6-EC067E69155C</string>
<key>version</key>
<integer>2</integer>
</dict>
</plist>
14 changes: 11 additions & 3 deletions Commands/Run.tmCommand
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,27 @@
<string>#!/usr/bin/env ruby
require "#{ENV['TM_BUNDLE_SUPPORT']}/gomate"
Go::launch "run", :scope =&gt; :file
Go::go "run", :verb =&gt; "Running"
</string>
<key>input</key>
<string>document</string>
<key>inputFormat</key>
<string>text</string>
<key>keyEquivalent</key>
<string>@r</string>
<key>name</key>
<string>Run</string>
<key>output</key>
<string>showAsHTML</string>
<key>outputCaret</key>
<string>afterOutput</string>
<key>outputFormat</key>
<string>html</string>
<key>outputLocation</key>
<string>newWindow</string>
<key>scope</key>
<string>source.go</string>
<key>uuid</key>
<string>0B3C3EB0-9F51-4997-A87D-ECA507D8E31E</string>
<key>version</key>
<integer>2</integer>
</dict>
</plist>
14 changes: 11 additions & 3 deletions Commands/Test.tmCommand
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,27 @@
<string>#!/usr/bin/env ruby
require "#{ENV['TM_BUNDLE_SUPPORT']}/gomate"
Go::launch "test", :verb =&gt; "Testing", :scope =&gt; :package
Go::go "test", :verb =&gt; "Testing"
</string>
<key>input</key>
<string>document</string>
<key>inputFormat</key>
<string>text</string>
<key>keyEquivalent</key>
<string>~@t</string>
<key>name</key>
<string>Test</string>
<key>output</key>
<string>showAsHTML</string>
<key>outputCaret</key>
<string>afterOutput</string>
<key>outputFormat</key>
<string>html</string>
<key>outputLocation</key>
<string>newWindow</string>
<key>scope</key>
<string>source.go</string>
<key>uuid</key>
<string>0F6A8710-54FC-48F5-9D02-D093DA001D17</string>
<key>version</key>
<integer>2</integer>
</dict>
</plist>
22 changes: 16 additions & 6 deletions Support/goerrs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,22 @@ def Go::href(file, line)
end

def Go::link_errs(str, type)
str.gsub!(/^(.+):(\d+):\s+(.+)$/) do
file, line, msg = $1, $2, $3
text = "<a href=\"#{href(file, line)}\">"
text << "#{htmlize(File.basename(file))}:#{line}</a>"
text << ": <span class=\"err\">#{htmlize(msg)}</span><br>"
text
if str.count(':') == 2
str.gsub!(/^(.+):(\d+):\s+(.+)$/) do
file, line, msg = $1, $2, $3
text = "<a href=\"#{href(File.basename(file), line)}\">"
text << "#{htmlize(File.basename(file))}:#{line}</a>"
text << ": <span class=\"err\">#{htmlize(msg)}</span><br>"
text
end
else
str.gsub!(/^(.+):(\d+):(\d+):\s+(.+)$/) do
file, line, col, msg = $1, $2, $3, $4
text = "<a href=\"#{href(File.basename(file), line)}\">"
text << "#{htmlize(File.basename(file))}:#{line}</a>"
text << ": <span class=\"err\">#{htmlize(msg)}</span><br>"
text
end
end
end
end
127 changes: 75 additions & 52 deletions Support/gomate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,82 +2,105 @@

require "#{ENV['TM_SUPPORT_PATH']}/lib/escape"
require "#{ENV['TM_SUPPORT_PATH']}/lib/exit_codes"
require "#{ENV['TM_SUPPORT_PATH']}/lib/ui"
require "#{ENV['TM_SUPPORT_PATH']}/lib/web_preview"
require "#{ENV['TM_SUPPORT_PATH']}/lib/tm/executor"
require "#{ENV['TM_SUPPORT_PATH']}/lib/tm/require_cmd"
require "#{ENV['TM_SUPPORT_PATH']}/lib/tm/save_current_document"
require "#{ENV['TM_BUNDLE_SUPPORT']}/goerrs"

# TextMate's special GOPATH used in .tm_properties files prepended to the environment's GOPATH
ENV['GOPATH'] = (ENV.has_key?('TM_GOPATH') ? ENV['TM_GOPATH'] : '') +
(ENV.has_key?('GOPATH') ? ':' + ENV['GOPATH'] : '')

module Go
def Go::get_package_name(filename)
if ENV['GOPATH']
dirname = File.dirname File.expand_path filename
package = ENV['GOPATH'].split(/:/).map do |path|
root = (File.expand_path path) + '/src/'
dirname[root.length..-1] if dirname.start_with? root
end.compact.first
return package if package
end
def Go::go(command, options={})
# TextMate's special TM_GO or expect 'go' on PATH
go_cmd = ENV['TM_GO'] || 'go'
TextMate.require_cmd(go_cmd)
TextMate.save_current_document('go')
TextMate::Executor.make_project_master_current_document

title = "Can't find package name for file"
TextMate::HTMLOutput.show(:title => title) do |io|
gopath = "<code>GOPATH</code>"
io << "<p>The current #{gopath} is:</p>"
io << "<blockquote>"
(ENV['GOPATH'] or '').split(/:/).each do |path|
io << htmlize(path + "\n")
end
io << "</blockquote>"
io << "<p>Please consult <a href=\"http://golang.org/doc/code.html#GOPATH\">"
io << "#{gopath} and workspaces</a> in the Go documentation.</p>"
end
TextMate.exit_show_html
end
args = options[:args] ? options[:args] : []
opts = {:interactive_input => false, :use_hashbang => false, :version_args => ['version'], :version_regex => /\Ago version (.*)/}
opts[:verb] = options[:verb] if options[:verb]

def Go::get_target_name(filename, scope)
case scope
when :package
package = get_package_name(filename)
ENV['TM_DISPLAYNAME'] = package
return package
else
return filename
end
end
# At this time, we will always run 'go' against a single file. In the future there may be new
# commands that will invalidate this but until then, might as well start simple.
args.push(ENV['TM_FILEPATH'])
args.push(opts)

def Go::execute(tool, options={})
TextMate::Executor.run(go_cmd, command, *args) do |str, type|
Go::link_errs(str, type)
end
end

def Go::launch(command, options={})
TextMate.require_cmd("go")
TextMate.save_current_document("go")
def Go::godoc
# TextMate's special TM_GODOC or expect 'godoc' on PATH
godoc_cmd = ENV['TM_GODOC'] || 'godoc'
term = STDIN.read.strip
TextMate.require_cmd(godoc_cmd)
TextMate.save_current_document('go')
TextMate::Executor.make_project_master_current_document

args = options[:args] ? options[:args] : []
name = get_target_name(ENV['TM_FILEPATH'], options[:scope])
opts = {:interactive_input => false, :use_hashbang => false, :version_args => ["version"], :version_regex => /\Ago version (.*)/}
opts[:verb] = options[:verb] if options[:verb]
if term.nil? || term.empty?
term = TextMate::UI.request_string( :title => 'Go Documentation Search',
:prompt => 'Enter a term to search for:',
:button1 => 'Search')
end

args.push(name)
args.push(opts)
TextMate.exit_show_tool_tip('Please select a term to look up.') if term.nil? || term.empty?

TextMate::Executor.run("go", command, *args) do |str, type|
Go::link_errs(str, type)
args = []
args.push(godoc_cmd)
args.push('-html')
args.push('-tabwidth=0')
args.concat term.split('.')
args.push({:interactive_input => false, :use_hashbang => false})

out, err = TextMate::Process.run(*args)

if err.nil? || err == ''
html_header("Documentation for #{term}", "go")
puts out
html_footer
TextMate.exit_show_html
else
TextMate.exit_show_tool_tip(err)
end
end

def Go::format
TextMate.require_cmd("gofmt")
TextMate.save_current_document("go")
def Go::gofmt
# TextMate's special TM_GOFMT or expect 'gofmt' on PATH
gofmt_cmd = ENV['TM_GOFMT'] || 'gofmt'
TextMate.require_cmd(gofmt_cmd)
TextMate.save_current_document('go')
TextMate::Executor.make_project_master_current_document

args = []
args.push("gofmt")
args.push(gofmt_cmd)
args.push("-tabwidth=#{ENV['TM_TAB_SIZE']}")
args.push("-tabs=#{ENV['TM_SOFT_TABS'] != 'YES'}")
if ENV['TM_SOFT_TABS'] && ENV['TM_SOFT_TABS'] == 'YES'
args.push('-tabs=true')

This comment has been minimized.

Copy link
@rowland

rowland Dec 9, 2012

Tabs options are reversed. Formats with hard tabs when soft tabs are selected in editor and vice versa. Swapping -tabs=true and -tabs=false fixes it.

else
args.push('-tabs=false')
end
args.push(ENV['TM_FILEPATH'])
args.push({:interactive_input => false, :use_hashbang => false})
TextMate::Process.run(*args) do |str, type|
STDOUT << str if type == :out

out, err = TextMate::Process.run(*args)

if err.nil? || err == ''
puts out
else
html_header("Formatting \"#{ENV['TM_FILENAME']}\"...", "go",
# html_head below is used to style the error lines like those displayed when a compiler error occurs
:html_head => '<style type="text/css">.err { color: red; } pre { font-style: normal; white-space: normal; }</style>')
puts '<pre>'
puts Go::link_errs(err, :err)
puts '</pre>'
html_footer
TextMate.exit_show_html
end
end
end
Expand Down

0 comments on commit ca55b4c

Please sign in to comment.