Skip to content

Commit

Permalink
support for dynamic gopath, package, and package path; display packag…
Browse files Browse the repository at this point in the history
…e as TM noun where appropriate; support for golint + goimports

Cherrypick febf522 from fmccann, plus additional hand merges from the launchpad project
  • Loading branch information
fmccann authored and syscrusher committed Sep 24, 2015
1 parent 8e6a71b commit 2653890
Show file tree
Hide file tree
Showing 8 changed files with 204 additions and 26 deletions.
14 changes: 12 additions & 2 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,16 @@ Planned:
- Rework some snippets, like a single import without an alias
- Add support for all go tools like fix, vet, lint, oracle

###3.2.0 / 2015-09-18
- [Support for dynamically determining gopath, package name, and package path @fmccann](https://github.com/syscrusher/golang.tmbundle/commit/49440460f058e80ccf0f2bd44891b48164091128)
- [Support for golint and goimports @fmccann](https://github.com/syscrusher/golang.tmbundle/commit/49440460f058e80ccf0f2bd44891b48164091128)
- [Display package name when running command against package name @fmccann](https://github.com/syscrusher/golang.tmbundle/commit/49440460f058e80ccf0f2bd44891b48164091128)
- [Allow run and build on unsaved files @tg](https://github.com/syscrusher/golang.tmbundle/commit/4809d74ea36654bdd9a2475ded6f729eb3082b65)
- [Fmt updates document without saving the current version @tg](https://github.com/syscrusher/golang.tmbundle/commit/998d17a9f8024b2c6571f242d2d93f44723c3e87)
- [Automatically format documents on save @tg](https://github.com/syscrusher/golang.tmbundle/commit/8e6a71b1f8e986b7644c3286c1f4c538dc1345ec)
- [Add fmt.Print snippet @jish](https://github.com/syscrusher/golang.tmbundle/commit/f73850c2774b1bbe6c7ecec40e4bbbb376fa1225)
- [Add fmt.Fprintf snippet](https://github.com/syscrusher/golang.tmbundle/commit/ead451fa74a98628ddeb50ab6d901be18b60bba7)

###3.1.0 / 2015-02-04
- [Run all non-run commands against current directory @tg](https://github.com/syscrusher/golang.tmbundle/commit/d3f09ee3bbe5fba76964e1bdc23e7d7247b733ee)
- [Allow run and build on unsaved files @tg](https://github.com/syscrusher/golang.tmbundle/commit/4809d74ea36654bdd9a2475ded6f729eb3082b65)
Expand Down Expand Up @@ -106,6 +116,6 @@ Imported some community additions and bugfixes to bring compatibility with OS X
- Variable initialization is FINALLY matching correctly. Should work for every style & number of variables, even in-line in loop statements.
- Matches exported variable names correctly (i.e. those beginning with an uppercase letter).
- Dot-accessed variable match no longer consumes the preceding '.' character.

###0.1.0 / 2009-11-14
- Initial Revision
- Initial Revision
2 changes: 1 addition & 1 deletion Commands/Fmt.tmCommand
Expand Up @@ -14,7 +14,7 @@
<key>inputFormat</key>
<string>text</string>
<key>keyEquivalent</key>
<string>^H</string>
<string></string>
<key>name</key>
<string>Fmt</string>
<key>outputCaret</key>
Expand Down
34 changes: 34 additions & 0 deletions Commands/Imports.tmCommand
@@ -0,0 +1,34 @@
<?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>saveModifiedFiles</string>
<key>command</key>
<string>#!/usr/bin/env ruby18
require "#{ENV['TM_BUNDLE_SUPPORT']}/gomate"
Go::goimports
</string>
<key>input</key>
<string>document</string>
<key>inputFormat</key>
<string>text</string>
<key>keyEquivalent</key>
<string>^H</string>
<key>name</key>
<string>Imports</string>
<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>5509FB1E-C780-44ED-8BCF-4300ABC30C32</string>
<key>version</key>
<integer>2</integer>
</dict>
</plist>
34 changes: 34 additions & 0 deletions Commands/Lint.tmCommand
@@ -0,0 +1,34 @@
<?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>saveActiveFile</string>
<key>command</key>
<string>#!/usr/bin/env ruby18
require "#{ENV['TM_BUNDLE_SUPPORT']}/gomate"
Go::golint
</string>
<key>input</key>
<string>none</string>
<key>inputFormat</key>
<string>text</string>
<key>keyEquivalent</key>
<string>^L</string>
<key>name</key>
<string>Lint</string>
<key>outputCaret</key>
<string>interpolateByLine</string>
<key>outputFormat</key>
<string>html</string>
<key>outputLocation</key>
<string>newWindow</string>
<key>scope</key>
<string>source.go</string>
<key>uuid</key>
<string>7E52594B-80CA-4EDD-B2B8-C54EB3844E95</string>
<key>version</key>
<integer>2</integer>
</dict>
</plist>
34 changes: 20 additions & 14 deletions Commands/Open Package.tmCommand
Expand Up @@ -6,37 +6,43 @@
<string>nop</string>
<key>command</key>
<string>#!/usr/bin/env ruby18
require "shellwords"
require &quot;shellwords&quot;
require &quot;#{ENV[&apos;TM_BUNDLE_SUPPORT&apos;]}/gomate&quot; # import to get dynamic gopath if set
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/;
if ENV.has_key? &apos;TM_SELECTED_TEXT&apos;
ENV[&apos;TM_SELECTED_TEXT&apos;]
elsif ENV[&apos;TM_CURRENT_LINE&apos;] =~ /^\s*(?:import\s+)?(?:\.|[[:alpha:]_][[:alnum:]_]*\s+)?([&quot;`])(.*?)\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?"
require &quot;#{ENV[&apos;TM_SUPPORT_PATH&apos;]}/lib/ui.rb&quot;
TextMate::UI.request_string :title =&gt; &quot;Open Package&quot;, :default =&gt; defaultText, :prompt =&gt; &quot;Which package do you wish to open?&quot;
end
end
def go_path
env = %x{"${TM_GO:-go}" env}
env = %x{&quot;${TM_GO:-go}&quot; env}
if $? == 0
lcal, root = [], []
env.scan(/^GO(PATH|ROOT)="(.*)"/) do |key,value|
env.scan(/^GO(PATH|ROOT)=&quot;(.*)&quot;/) do |key,value|
case key
when 'PATH': lcal = value.split(':').map { |dir| "#{dir}/src" }
when 'ROOT': root = value.split(':').map { |dir| "#{dir}/src/pkg" }
when &apos;PATH&apos;: lcal = value.split(&apos;:&apos;).map { |dir| &quot;#{dir}/src&quot; }
when &apos;ROOT&apos;: root = value.split(&apos;:&apos;).map { |dir| &quot;#{dir}/src/pkg&quot; }
end
end
[ lcal, root ].flatten
else
ENV['GOPATH'].to_s.split(':').map { |dir| "#{dir}/src" }
ENV[&apos;GOPATH&apos;].to_s.split(&apos;:&apos;).map { |dir| &quot;#{dir}/src&quot; }
end
end
def find_package_path(package)
if ENV.has_key?(&apos;TM_GO_DYNAMIC_PKG_PATH&apos;)
path = `#{ENV[&apos;TM_GO_DYNAMIC_PKG_PATH&apos;]} #{package}`.chomp
return path if path != nil &amp;&amp; !path.empty?
end
go_path.each do |dir|
path = File.expand_path(package, dir)
if File.directory?(path)
Expand All @@ -49,10 +55,10 @@ end
if package = import_path()
if path = find_package_path(package)
%x{"$TM_MATE" #{path.shellescape}}
%x{&quot;$TM_MATE&quot; #{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}’."
require &quot;#{ENV[&apos;TM_SUPPORT_PATH&apos;]}/lib/exit_codes.rb&quot;
TextMate.exit_show_tool_tip &quot;Unable to locate package for import path ‘#{package}’.&quot;
end
end
</string>
Expand Down
27 changes: 21 additions & 6 deletions README.md
@@ -1,20 +1,23 @@
## golang.tmbundle v3.1.0
## golang.tmbundle v3.2.0
(a TextMate 2 bundle for the go programming language)

### Installation
TextMate by default will detect .go files and load [Jim Dovey's bundle](https://github.com/AlanQuatermain/go-tmbundle). This is a fork with additional improvements merged from around the community.

Big changes from the default version:
- all non-run go commands operate on the current directory instead of per file (thanks [tg](https://github.com/tg))
- Support for goimports (thanks [fmccann](https://github.com/fmccann))
- Support for golint (thanks [fmccann](https://github.com/fmccann))
- Users can supply commands via ENV variables (TM\_GO\_DYNAMIC\_GOPATH, TM\_GO\_DYNAMIC\_PKG, TM\_GO\_DYNAMIC\_PKG\_PATH). The bundle will consult these commands if defined to dynamically change the gopath or package based on the current directory. (thanks [fmccann](https://github.com/fmccann))
- all non-run go commands operate on the current directory instead of per file if the package is not defined dynamically. (thanks [tg](https://github.com/tg)).
- run and build work on unsaved files (thanks [tg](https://github.com/tg))
- added print, println, printf, and fprintf snippets (thanks [jish](https://github.com/jish))
- bugfixes

To install this bundle manually, open a Terminal window and do:

```Shell
mkdir ~/Library/Application\ Support/Avian/Bundles
cd ~/Library/Application\ Support/Avian/Bundles
mkdir -p ~/Library/Application\ Support/Avian/Pristine\ Copy/Bundles
cd ~/Library/Application\ Support/Avian/Pristine\ Copy/Bundles
git clone git://github.com/syscrusher/golang.tmbundle.git
```

Expand All @@ -26,8 +29,20 @@ go get -u github.com/nsf/gocode
go get -u code.google.com/p/go.tools/cmd/godoc
```

This bundle uses goimports for cleaning up imports and reformatting code, which can be installed with:

```Shell
go get -u go get golang.org/x/tools/cmd/goimports
```

This bundle uses golint for linting code, which can be installed with:

```Shell
go get -u github.com/golang/lint/golint
```

### Features
The bundle implements language syntax, completion via gocode, some snippets, and some compile/format/documentation commands for the [Go language](http://golang.org/).
The bundle implements language syntax, completion via gocode, some snippets, and some compile/format/documentation commands for the [Go language](http://golang.org/).

### Snippets

Expand Down Expand Up @@ -113,4 +128,4 @@ This is a fork from [Jim Dovey](https://github.com/AlanQuatermain) who has done
>Thanks be to lasersox and Infininight over at the [#textmate room on IRC](irc://irc.freenode.net/textmate) for all their help in cleaning up this here bundle, and for helping me to optimize my regex use in the language grammar.
Thanks to Martin Kühl for his extensive additions to this project's snippets and commands. Also Infininight's work on updating the bundle to use the TextMate's new Ruby interface and Jeremy & Sylvain's work on supporting Go 1.0 has been invaluable. Their assistance and stewardship while I've been deep in the world of Objective-C is very much appreciated.

Happy coding :)
Happy coding :)
81 changes: 79 additions & 2 deletions Support/gomate.rb
Expand Up @@ -9,11 +9,20 @@

# 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'] : '').sub(/^:+/,'')
(ENV.has_key?('GOPATH') ? ':' + ENV['GOPATH'] : '')

# Call tool to determine gopath
if ENV.has_key?('TM_GO_DYNAMIC_GOPATH')
Dir.chdir(ENV['TM_DIRECTORY']) do
ENV['GOPATH'] = `#{ENV['TM_GO_DYNAMIC_GOPATH']}`.chomp
end
end

module Go
def Go::go(command, options={})
# TextMate's special TM_GO or expect 'go' on PATH
go_cmd = ENV['TM_GO'] || 'go'

TextMate.save_if_untitled('go')
TextMate::Executor.make_project_master_current_document

Expand All @@ -29,10 +38,21 @@ def Go::go(command, options={})
opts[:chdir] = directory
end

if command == 'run' || !directory
# Call tool to determine package; default to directory name
pkg = directory
if ENV.has_key?('TM_GO_DYNAMIC_PKG')
Dir.chdir(ENV['TM_DIRECTORY']) do
pkg = `#{ENV['TM_GO_DYNAMIC_PKG']}`.chomp
pkg = nil if pkg == nil || pkg.empty?
end
end

if command == 'run' || !pkg
args.push(ENV['TM_FILEPATH'])
else
args.push("-v") # list packages being operated on
args.push(pkg)
opts[:noun] = pkg
end
args.push(opts)

Expand Down Expand Up @@ -70,4 +90,61 @@ def Go::godoc
TextMate.exit_show_tool_tip(err)
end
end

def Go::gofmt
# TextMate's special TM_GOFMT or expect 'gofmt' on PATH
gofmt_cmd = ENV['TM_GOFMT'] || 'gofmt'
TextMate.save_if_untitled('go')

args = []
args.push(gofmt_cmd)
args.push(ENV['TM_FILEPATH'])

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

if err.nil? || err == ''
puts out
else
args << {:use_hashbang => false, :version_args => ['version'], :version_regex => /\Ago version (.*)/}
TextMate::Executor.run(*args)
TextMate.exit_show_html
end
end

def Go::goimports
goimport_cmd = ENV['TM_GOIMPORTS'] || 'goimports'
TextMate.save_if_untitled('go')

args = []
args.push(goimport_cmd)
args.push(ENV['TM_FILEPATH'])

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

if err.nil? || err == ''
puts out
else
args << {:use_hashbang => false}
TextMate::Executor.run(*args)
TextMate.exit_show_html
end
end

def Go::golint
golint = ENV['TM_GOLINT'] || 'golint'
TextMate.save_if_untitled('go')
TextMate::Executor.make_project_master_current_document

args = Array.new
opts = {:use_hashbang => false, :verb => 'Linting', :version_replace => 'golint'}

file_length = ENV['TM_DIRECTORY'].length + 1
go_file = ENV['TM_FILEPATH'][file_length..-1]
opts[:chdir] = ENV['TM_DIRECTORY']

args.push(go_file)
args.push(opts)

TextMate::Executor.run(golint, *args)
end
end
4 changes: 3 additions & 1 deletion info.plist
Expand Up @@ -7,7 +7,7 @@
<key>contactName</key>
<string>syscrusher</string>
<key>description</key>
<string>Support for the &lt;a href="http://golang.org"&gt;Go programming language&lt;/a&gt;.</string>
<string>Support for the &lt;a href=&quot;http://golang.org&quot;&gt;Go programming language&lt;/a&gt;.</string>
<key>mainMenu</key>
<dict>
<key>excludedItems</key>
Expand All @@ -25,6 +25,8 @@
<string>7BCFCFC8-9152-4638-8436-E17B0C754C8D</string>
<string>------------------------------------</string>
<string>B0271A46-F6EF-4D2F-95A6-EC067E69155C</string>
<string>5509FB1E-C780-44ED-8BCF-4300ABC30C32</string>
<string>7E52594B-80CA-4EDD-B2B8-C54EB3844E95</string>
<string>------------------------------------</string>
<string>C64599DA-E362-4411-9782-58A9C7F1B05A</string>
<string>88695E43-E330-4A5A-9492-C604DBD10A27</string>
Expand Down

0 comments on commit 2653890

Please sign in to comment.