Skip to content

Commit 2653890

Browse files
fmccannsyscrusher
authored andcommitted
support for dynamic gopath, package, and package path; display package as TM noun where appropriate; support for golint + goimports
Cherrypick febf522 from fmccann, plus additional hand merges from the launchpad project
1 parent 8e6a71b commit 2653890

File tree

8 files changed

+204
-26
lines changed

8 files changed

+204
-26
lines changed

CHANGELOG.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@ Planned:
22
- Rework some snippets, like a single import without an alias
33
- Add support for all go tools like fix, vet, lint, oracle
44

5+
###3.2.0 / 2015-09-18
6+
- [Support for dynamically determining gopath, package name, and package path @fmccann](https://github.com/syscrusher/golang.tmbundle/commit/49440460f058e80ccf0f2bd44891b48164091128)
7+
- [Support for golint and goimports @fmccann](https://github.com/syscrusher/golang.tmbundle/commit/49440460f058e80ccf0f2bd44891b48164091128)
8+
- [Display package name when running command against package name @fmccann](https://github.com/syscrusher/golang.tmbundle/commit/49440460f058e80ccf0f2bd44891b48164091128)
9+
- [Allow run and build on unsaved files @tg](https://github.com/syscrusher/golang.tmbundle/commit/4809d74ea36654bdd9a2475ded6f729eb3082b65)
10+
- [Fmt updates document without saving the current version @tg](https://github.com/syscrusher/golang.tmbundle/commit/998d17a9f8024b2c6571f242d2d93f44723c3e87)
11+
- [Automatically format documents on save @tg](https://github.com/syscrusher/golang.tmbundle/commit/8e6a71b1f8e986b7644c3286c1f4c538dc1345ec)
12+
- [Add fmt.Print snippet @jish](https://github.com/syscrusher/golang.tmbundle/commit/f73850c2774b1bbe6c7ecec40e4bbbb376fa1225)
13+
- [Add fmt.Fprintf snippet](https://github.com/syscrusher/golang.tmbundle/commit/ead451fa74a98628ddeb50ab6d901be18b60bba7)
14+
515
###3.1.0 / 2015-02-04
616
- [Run all non-run commands against current directory @tg](https://github.com/syscrusher/golang.tmbundle/commit/d3f09ee3bbe5fba76964e1bdc23e7d7247b733ee)
717
- [Allow run and build on unsaved files @tg](https://github.com/syscrusher/golang.tmbundle/commit/4809d74ea36654bdd9a2475ded6f729eb3082b65)
@@ -106,6 +116,6 @@ Imported some community additions and bugfixes to bring compatibility with OS X
106116
- Variable initialization is FINALLY matching correctly. Should work for every style & number of variables, even in-line in loop statements.
107117
- Matches exported variable names correctly (i.e. those beginning with an uppercase letter).
108118
- Dot-accessed variable match no longer consumes the preceding '.' character.
109-
119+
110120
###0.1.0 / 2009-11-14
111-
- Initial Revision
121+
- Initial Revision

Commands/Fmt.tmCommand

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<key>inputFormat</key>
1515
<string>text</string>
1616
<key>keyEquivalent</key>
17-
<string>^H</string>
17+
<string></string>
1818
<key>name</key>
1919
<string>Fmt</string>
2020
<key>outputCaret</key>

Commands/Imports.tmCommand

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>beforeRunningCommand</key>
6+
<string>saveModifiedFiles</string>
7+
<key>command</key>
8+
<string>#!/usr/bin/env ruby18
9+
10+
require "#{ENV['TM_BUNDLE_SUPPORT']}/gomate"
11+
Go::goimports
12+
</string>
13+
<key>input</key>
14+
<string>document</string>
15+
<key>inputFormat</key>
16+
<string>text</string>
17+
<key>keyEquivalent</key>
18+
<string>^H</string>
19+
<key>name</key>
20+
<string>Imports</string>
21+
<key>outputCaret</key>
22+
<string>interpolateByLine</string>
23+
<key>outputFormat</key>
24+
<string>text</string>
25+
<key>outputLocation</key>
26+
<string>replaceDocument</string>
27+
<key>scope</key>
28+
<string>source.go</string>
29+
<key>uuid</key>
30+
<string>5509FB1E-C780-44ED-8BCF-4300ABC30C32</string>
31+
<key>version</key>
32+
<integer>2</integer>
33+
</dict>
34+
</plist>

Commands/Lint.tmCommand

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>beforeRunningCommand</key>
6+
<string>saveActiveFile</string>
7+
<key>command</key>
8+
<string>#!/usr/bin/env ruby18
9+
10+
require "#{ENV['TM_BUNDLE_SUPPORT']}/gomate"
11+
Go::golint
12+
</string>
13+
<key>input</key>
14+
<string>none</string>
15+
<key>inputFormat</key>
16+
<string>text</string>
17+
<key>keyEquivalent</key>
18+
<string>^L</string>
19+
<key>name</key>
20+
<string>Lint</string>
21+
<key>outputCaret</key>
22+
<string>interpolateByLine</string>
23+
<key>outputFormat</key>
24+
<string>html</string>
25+
<key>outputLocation</key>
26+
<string>newWindow</string>
27+
<key>scope</key>
28+
<string>source.go</string>
29+
<key>uuid</key>
30+
<string>7E52594B-80CA-4EDD-B2B8-C54EB3844E95</string>
31+
<key>version</key>
32+
<integer>2</integer>
33+
</dict>
34+
</plist>

Commands/Open Package.tmCommand

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,43 @@
66
<string>nop</string>
77
<key>command</key>
88
<string>#!/usr/bin/env ruby18
9-
require "shellwords"
9+
require &quot;shellwords&quot;
10+
require &quot;#{ENV[&apos;TM_BUNDLE_SUPPORT&apos;]}/gomate&quot; # import to get dynamic gopath if set
1011
1112
def import_path
12-
if ENV.has_key? 'TM_SELECTED_TEXT'
13-
ENV['TM_SELECTED_TEXT']
14-
elsif ENV['TM_CURRENT_LINE'] =~ /^\s*(?:import\s+)?(?:\.|[[:alpha:]_][[:alnum:]_]*\s+)?(["`])(.*?)\1/;
13+
if ENV.has_key? &apos;TM_SELECTED_TEXT&apos;
14+
ENV[&apos;TM_SELECTED_TEXT&apos;]
15+
elsif ENV[&apos;TM_CURRENT_LINE&apos;] =~ /^\s*(?:import\s+)?(?:\.|[[:alpha:]_][[:alnum:]_]*\s+)?([&quot;`])(.*?)\1/;
1516
$2
1617
else
1718
defaultText = %x{ /usr/bin/pbpaste -pboard find }
18-
require "#{ENV['TM_SUPPORT_PATH']}/lib/ui.rb"
19-
TextMate::UI.request_string :title =&gt; "Open Package", :default =&gt; defaultText, :prompt =&gt; "Which package do you wish to open?"
19+
require &quot;#{ENV[&apos;TM_SUPPORT_PATH&apos;]}/lib/ui.rb&quot;
20+
TextMate::UI.request_string :title =&gt; &quot;Open Package&quot;, :default =&gt; defaultText, :prompt =&gt; &quot;Which package do you wish to open?&quot;
2021
end
2122
end
2223
2324
def go_path
24-
env = %x{"${TM_GO:-go}" env}
25+
env = %x{&quot;${TM_GO:-go}&quot; env}
2526
if $? == 0
2627
lcal, root = [], []
27-
env.scan(/^GO(PATH|ROOT)="(.*)"/) do |key,value|
28+
env.scan(/^GO(PATH|ROOT)=&quot;(.*)&quot;/) do |key,value|
2829
case key
29-
when 'PATH': lcal = value.split(':').map { |dir| "#{dir}/src" }
30-
when 'ROOT': root = value.split(':').map { |dir| "#{dir}/src/pkg" }
30+
when &apos;PATH&apos;: lcal = value.split(&apos;:&apos;).map { |dir| &quot;#{dir}/src&quot; }
31+
when &apos;ROOT&apos;: root = value.split(&apos;:&apos;).map { |dir| &quot;#{dir}/src/pkg&quot; }
3132
end
3233
end
3334
[ lcal, root ].flatten
3435
else
35-
ENV['GOPATH'].to_s.split(':').map { |dir| "#{dir}/src" }
36+
ENV[&apos;GOPATH&apos;].to_s.split(&apos;:&apos;).map { |dir| &quot;#{dir}/src&quot; }
3637
end
3738
end
3839
3940
def find_package_path(package)
41+
if ENV.has_key?(&apos;TM_GO_DYNAMIC_PKG_PATH&apos;)
42+
path = `#{ENV[&apos;TM_GO_DYNAMIC_PKG_PATH&apos;]} #{package}`.chomp
43+
return path if path != nil &amp;&amp; !path.empty?
44+
end
45+
4046
go_path.each do |dir|
4147
path = File.expand_path(package, dir)
4248
if File.directory?(path)
@@ -49,10 +55,10 @@ end
4955
5056
if package = import_path()
5157
if path = find_package_path(package)
52-
%x{"$TM_MATE" #{path.shellescape}}
58+
%x{&quot;$TM_MATE&quot; #{path.shellescape}}
5359
else
54-
require "#{ENV['TM_SUPPORT_PATH']}/lib/exit_codes.rb"
55-
TextMate.exit_show_tool_tip "Unable to locate package for import path ‘#{package}’."
60+
require &quot;#{ENV[&apos;TM_SUPPORT_PATH&apos;]}/lib/exit_codes.rb&quot;
61+
TextMate.exit_show_tool_tip &quot;Unable to locate package for import path ‘#{package}’.&quot;
5662
end
5763
end
5864
</string>

README.md

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
1-
## golang.tmbundle v3.1.0
1+
## golang.tmbundle v3.2.0
22
(a TextMate 2 bundle for the go programming language)
33

44
### Installation
55
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.
66

77
Big changes from the default version:
8-
- all non-run go commands operate on the current directory instead of per file (thanks [tg](https://github.com/tg))
8+
- Support for goimports (thanks [fmccann](https://github.com/fmccann))
9+
- Support for golint (thanks [fmccann](https://github.com/fmccann))
10+
- 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))
11+
- 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)).
912
- run and build work on unsaved files (thanks [tg](https://github.com/tg))
1013
- added print, println, printf, and fprintf snippets (thanks [jish](https://github.com/jish))
1114
- bugfixes
1215

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

1518
```Shell
16-
mkdir ~/Library/Application\ Support/Avian/Bundles
17-
cd ~/Library/Application\ Support/Avian/Bundles
19+
mkdir -p ~/Library/Application\ Support/Avian/Pristine\ Copy/Bundles
20+
cd ~/Library/Application\ Support/Avian/Pristine\ Copy/Bundles
1821
git clone git://github.com/syscrusher/golang.tmbundle.git
1922
```
2023

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

32+
This bundle uses goimports for cleaning up imports and reformatting code, which can be installed with:
33+
34+
```Shell
35+
go get -u go get golang.org/x/tools/cmd/goimports
36+
```
37+
38+
This bundle uses golint for linting code, which can be installed with:
39+
40+
```Shell
41+
go get -u github.com/golang/lint/golint
42+
```
43+
2944
### Features
30-
The bundle implements language syntax, completion via gocode, some snippets, and some compile/format/documentation commands for the [Go language](http://golang.org/).
45+
The bundle implements language syntax, completion via gocode, some snippets, and some compile/format/documentation commands for the [Go language](http://golang.org/).
3146

3247
### Snippets
3348

@@ -113,4 +128,4 @@ This is a fork from [Jim Dovey](https://github.com/AlanQuatermain) who has done
113128
>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.
114129
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.
115130

116-
Happy coding :)
131+
Happy coding :)

Support/gomate.rb

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,20 @@
99

1010
# TextMate's special GOPATH used in .tm_properties files prepended to the environment's GOPATH
1111
ENV['GOPATH'] = (ENV.has_key?('TM_GOPATH') ? ENV['TM_GOPATH'] : '') +
12-
(ENV.has_key?('GOPATH') ? ':' + ENV['GOPATH'] : '').sub(/^:+/,'')
12+
(ENV.has_key?('GOPATH') ? ':' + ENV['GOPATH'] : '')
13+
14+
# Call tool to determine gopath
15+
if ENV.has_key?('TM_GO_DYNAMIC_GOPATH')
16+
Dir.chdir(ENV['TM_DIRECTORY']) do
17+
ENV['GOPATH'] = `#{ENV['TM_GO_DYNAMIC_GOPATH']}`.chomp
18+
end
19+
end
20+
1321
module Go
1422
def Go::go(command, options={})
1523
# TextMate's special TM_GO or expect 'go' on PATH
1624
go_cmd = ENV['TM_GO'] || 'go'
25+
1726
TextMate.save_if_untitled('go')
1827
TextMate::Executor.make_project_master_current_document
1928

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

32-
if command == 'run' || !directory
41+
# Call tool to determine package; default to directory name
42+
pkg = directory
43+
if ENV.has_key?('TM_GO_DYNAMIC_PKG')
44+
Dir.chdir(ENV['TM_DIRECTORY']) do
45+
pkg = `#{ENV['TM_GO_DYNAMIC_PKG']}`.chomp
46+
pkg = nil if pkg == nil || pkg.empty?
47+
end
48+
end
49+
50+
if command == 'run' || !pkg
3351
args.push(ENV['TM_FILEPATH'])
3452
else
3553
args.push("-v") # list packages being operated on
54+
args.push(pkg)
55+
opts[:noun] = pkg
3656
end
3757
args.push(opts)
3858

@@ -70,4 +90,61 @@ def Go::godoc
7090
TextMate.exit_show_tool_tip(err)
7191
end
7292
end
93+
94+
def Go::gofmt
95+
# TextMate's special TM_GOFMT or expect 'gofmt' on PATH
96+
gofmt_cmd = ENV['TM_GOFMT'] || 'gofmt'
97+
TextMate.save_if_untitled('go')
98+
99+
args = []
100+
args.push(gofmt_cmd)
101+
args.push(ENV['TM_FILEPATH'])
102+
103+
out, err = TextMate::Process.run(*args)
104+
105+
if err.nil? || err == ''
106+
puts out
107+
else
108+
args << {:use_hashbang => false, :version_args => ['version'], :version_regex => /\Ago version (.*)/}
109+
TextMate::Executor.run(*args)
110+
TextMate.exit_show_html
111+
end
112+
end
113+
114+
def Go::goimports
115+
goimport_cmd = ENV['TM_GOIMPORTS'] || 'goimports'
116+
TextMate.save_if_untitled('go')
117+
118+
args = []
119+
args.push(goimport_cmd)
120+
args.push(ENV['TM_FILEPATH'])
121+
122+
out, err = TextMate::Process.run(*args)
123+
124+
if err.nil? || err == ''
125+
puts out
126+
else
127+
args << {:use_hashbang => false}
128+
TextMate::Executor.run(*args)
129+
TextMate.exit_show_html
130+
end
131+
end
132+
133+
def Go::golint
134+
golint = ENV['TM_GOLINT'] || 'golint'
135+
TextMate.save_if_untitled('go')
136+
TextMate::Executor.make_project_master_current_document
137+
138+
args = Array.new
139+
opts = {:use_hashbang => false, :verb => 'Linting', :version_replace => 'golint'}
140+
141+
file_length = ENV['TM_DIRECTORY'].length + 1
142+
go_file = ENV['TM_FILEPATH'][file_length..-1]
143+
opts[:chdir] = ENV['TM_DIRECTORY']
144+
145+
args.push(go_file)
146+
args.push(opts)
147+
148+
TextMate::Executor.run(golint, *args)
149+
end
73150
end

info.plist

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<key>contactName</key>
88
<string>syscrusher</string>
99
<key>description</key>
10-
<string>Support for the &lt;a href="http://golang.org"&gt;Go programming language&lt;/a&gt;.</string>
10+
<string>Support for the &lt;a href=&quot;http://golang.org&quot;&gt;Go programming language&lt;/a&gt;.</string>
1111
<key>mainMenu</key>
1212
<dict>
1313
<key>excludedItems</key>
@@ -25,6 +25,8 @@
2525
<string>7BCFCFC8-9152-4638-8436-E17B0C754C8D</string>
2626
<string>------------------------------------</string>
2727
<string>B0271A46-F6EF-4D2F-95A6-EC067E69155C</string>
28+
<string>5509FB1E-C780-44ED-8BCF-4300ABC30C32</string>
29+
<string>7E52594B-80CA-4EDD-B2B8-C54EB3844E95</string>
2830
<string>------------------------------------</string>
2931
<string>C64599DA-E362-4411-9782-58A9C7F1B05A</string>
3032
<string>88695E43-E330-4A5A-9492-C604DBD10A27</string>

0 commit comments

Comments
 (0)