From abb0c56b2d79701a5bcd95df172c048cd57303b6 Mon Sep 17 00:00:00 2001 From: Allan Odgaard Date: Thu, 11 Jul 2013 13:31:16 +0200 Subject: [PATCH 1/8] =?UTF-8?q?Add=20snippet=20to=20create=20test=20boiler?= =?UTF-8?q?plate=20(test=E2=87=A5)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use this in a new empty test file to get started. This goes well together with the new behavior of “Go to Counterpart” (alpha.9447) which allow ⌥⌘↑ to be used to create new test files. --- Snippets/Test.tmSnippet | 28 ++++++++++++++++++++++++++++ info.plist | 1 + 2 files changed, 29 insertions(+) create mode 100644 Snippets/Test.tmSnippet diff --git a/Snippets/Test.tmSnippet b/Snippets/Test.tmSnippet new file mode 100644 index 0000000..6d9bb65 --- /dev/null +++ b/Snippets/Test.tmSnippet @@ -0,0 +1,28 @@ + + + + + content + package ${1:`base="${TM_FILEPATH%_test.go}.go" +if [[ -f "$base" ]] && grep -q '^package ' "$base" + then grep -m1 '^package ' "$base"|cut -d' ' -f2 + else echo 'main' +fi`} + +import ( + "testing" +) + +func Test${2:${TM_DISPLAYNAME/^(.*?)(_test)?(\.go)?$/${1:/capitalize}/}}(t *testing.T) { + $0t.Fatal("no test body") +} + name + Test + scope + source.go + tabTrigger + test + uuid + C6F66AFB-B50B-45AF-B2B2-819CC3464E56 + + diff --git a/info.plist b/info.plist index eb920cb..27592a6 100644 --- a/info.plist +++ b/info.plist @@ -104,6 +104,7 @@ CFE21C25-71DE-48A9-8FE6-2A40D8DF16EF 2DFA9510-6F88-4BC6-A409-DA4075DEA8FF 5E6327B4-8BD5-4079-8B25-D8D3EF9E89FB + C6F66AFB-B50B-45AF-B2B2-819CC3464E56 name Organization From 3ab1de149941a7131bd6e3c10a818b6b92cf064a Mon Sep 17 00:00:00 2001 From: Allan Odgaard Date: Sat, 13 Jul 2013 15:22:49 +0200 Subject: [PATCH 2/8] Enable folding for case/default (switch blocks) --- Preferences/Folding.tmPreferences | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Preferences/Folding.tmPreferences b/Preferences/Folding.tmPreferences index bfca2c7..0eba128 100644 --- a/Preferences/Folding.tmPreferences +++ b/Preferences/Folding.tmPreferences @@ -8,6 +8,8 @@ source.go settings + foldingIndentedBlockStart + ^\s*(case|default)\b foldingStartMarker (?x) /\*\*(?!\*) # opening C-style comment with 2 asterisks but no third later on From cfeec6385b75e40100748a1d9d0c1a75b2b6c650 Mon Sep 17 00:00:00 2001 From: Allan Odgaard Date: Sat, 13 Jul 2013 15:24:04 +0200 Subject: [PATCH 3/8] Improve indentation rules MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit For the code I tested, TextMate’s applied indentation is now identical to what you get from ‘gofmt’. --- Preferences/Indentation Rules.tmPreferences | 24 +++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/Preferences/Indentation Rules.tmPreferences b/Preferences/Indentation Rules.tmPreferences index acd806b..18867dc 100644 --- a/Preferences/Indentation Rules.tmPreferences +++ b/Preferences/Indentation Rules.tmPreferences @@ -9,13 +9,25 @@ settings decreaseIndentPattern - ^(.*\*/)?(\s*\}([^}{"']*\{)?[;\s]*?|\s*\)([^)("']*\()?[;\s]*?|(?:\s*(case|default).*:))(//.*|/\*.*\*/\s*)*? + (?x)^ + ( .* [*]/ )? + ( \s* \} ( [^}{"'\n]* \{ )? [;,()\s]* + | \s* \) ( [^)("'\n]* \( )? [;,\s]* + | ( \s* (case|default)\b .* ) + ) + ( \s* ( //.* | /[*] .*? [*]/ ) )* + \s* $ increaseIndentPattern - ^(?:.*\*\/)?(?:(.*\{[^}"'\n]*)|(?:\s*(case|default).*:)|(.*\([^)"'\n]*))(\/\/.*|\/\*.*\*\/\s*)*?$ - indentNextLinePattern - ^(?!(.*[});:\w\d"'`])?\s*(\/\/|\/\*.*\*\/\s*$)).*[^\s;:{})"'`\w\d]\s*$ - unIndentedLinePattern - ^\s*((\/\*|\*\/|\/\/|import\b.*|package\b.*).*)?$ + (?x)^ + ( .* [*]/ )? + ( ( .* \{ [^}"'\n]* ) + | ( .* \( [^)"'\n]* ) + | ( \s* (case|default)\b .* ) + ) + ( \s* ( //.* | /[*] .*? [*]/ ) )* + \s* $ + zeroIndentPattern + ^\s*(?!default:)\w+:.*$ uuid 160118A4-208D-4422-AFF0-0C21B5B78AAF From 45bdac6a3958456aa8d292c46ace3771e349ba3c Mon Sep 17 00:00:00 2001 From: Allan Odgaard Date: Sat, 13 Jul 2013 15:25:28 +0200 Subject: [PATCH 4/8] =?UTF-8?q?Fix=20links=20in=20potential=20output=20fro?= =?UTF-8?q?m=20=E2=80=98Reformat=20Document=E2=80=99=20(=E2=8C=83=E2=87=A7?= =?UTF-8?q?H)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When there are errors we re-run the command via TextMate::Executor, which then takes care of hyperlinking errors in the output. --- Support/goerrs.rb | 40 ---------------------------------------- Support/gomate.rb | 10 ++-------- 2 files changed, 2 insertions(+), 48 deletions(-) delete mode 100755 Support/goerrs.rb diff --git a/Support/goerrs.rb b/Support/goerrs.rb deleted file mode 100755 index 5fd0a7f..0000000 --- a/Support/goerrs.rb +++ /dev/null @@ -1,40 +0,0 @@ -#!/usr/bin/env ruby - -require "#{ENV['TM_SUPPORT_PATH']}/lib/escape" -require "pathname" - -module Go - def Go::normalize_file(file) - return nil if file == 'untitled' - return file if Pathname.new(file).absolute? - base = ENV['TM_PROJECT_DIRECTORY'] || ENV['TM_DIRECTORY'] || Dir.getwd - File.join(base, file) - end - - def Go::href(file, line) - file = normalize_file(file) - link = "txmt://open?line=#{line}" - link << "&url=file://#{e_url(file)}" if file - link - end - - def Go::link_errs(str, type) - if str.count(':') == 2 - str.gsub!(/^(.+):(\d+):\s+(.+)$/) do - file, line, msg = $1, $2, $3 - text = "" - text << "#{htmlize(File.basename(file))}:#{line}" - text << ": #{htmlize(msg)}
" - text - end - else - str.gsub!(/^(.+):(\d+):(\d+):\s+(.+)$/) do - file, line, col, msg = $1, $2, $3, $4 - text = "" - text << "#{htmlize(File.basename(file))}:#{line}" - text << ": #{htmlize(msg)}
" - text - end - end - end -end diff --git a/Support/gomate.rb b/Support/gomate.rb index 5e765e6..461684f 100755 --- a/Support/gomate.rb +++ b/Support/gomate.rb @@ -6,7 +6,6 @@ require "#{ENV['TM_SUPPORT_PATH']}/lib/web_preview" require "#{ENV['TM_SUPPORT_PATH']}/lib/tm/executor" 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'] : '') + @@ -90,13 +89,8 @@ def Go::gofmt 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 => '') - puts '
'
-      puts Go::link_errs(err, :err)
-      puts '
' - html_footer + args << {:use_hashbang => false, :version_args => ['version'], :version_regex => /\Ago version (.*)/} + TextMate::Executor.run(*args) TextMate.exit_show_html end end From 5a3b308e06a8421be3ccde93a08b714afa08747e Mon Sep 17 00:00:00 2001 From: Allan Odgaard Date: Sat, 13 Jul 2013 15:30:44 +0200 Subject: [PATCH 5/8] =?UTF-8?q?Remove=20erroneous=20escapes=20in=20package?= =?UTF-8?q?=20snippet=20(pkg=E2=87=A5)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit These were required in TextMate 1.x but are inserted as literal backslashes in 2.0. --- Snippets/Package.tmSnippet | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Snippets/Package.tmSnippet b/Snippets/Package.tmSnippet index 7d85ce1..58cfcbd 100644 --- a/Snippets/Package.tmSnippet +++ b/Snippets/Package.tmSnippet @@ -3,9 +3,9 @@ content - ${1/^main$|(.+)/(?1:\/* + ${1/^main$|(.+)/(?1:/* The $1 package implements ... bananas? -*\/ +*/ )/}package ${1:main} name Package From 42b66feefe42a33b1bd419a92ff7134a8a436d0d Mon Sep 17 00:00:00 2001 From: Allan Odgaard Date: Sun, 14 Jul 2013 17:12:48 +0200 Subject: [PATCH 6/8] =?UTF-8?q?Add=20=E2=80=9COpen=20Package=E2=80=9D=20(?= =?UTF-8?q?=E2=87=A7=E2=8C=98D),=20uses=20current=20line=E2=80=99s=20impor?= =?UTF-8?q?t=20path?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- Commands/Open Package.tmCommand | 80 +++++++++++++++++++++++++++++++++ Support/gomate.rb | 1 + info.plist | 2 + 3 files changed, 83 insertions(+) create mode 100644 Commands/Open Package.tmCommand diff --git a/Commands/Open Package.tmCommand b/Commands/Open Package.tmCommand new file mode 100644 index 0000000..fd92b93 --- /dev/null +++ b/Commands/Open Package.tmCommand @@ -0,0 +1,80 @@ + + + + + beforeRunningCommand + nop + command + #!/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 => "Open Package", :default => defaultText, :prompt => "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$/ && 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 + + input + selection + inputFormat + text + keyEquivalent + @D + name + Open Package + outputCaret + afterOutput + outputFormat + text + outputLocation + discard + scope + source.go + uuid + D3CD6B51-3A7E-4356-85F4-B76B8336BEF2 + version + 2 + + diff --git a/Support/gomate.rb b/Support/gomate.rb index 461684f..6c2b5a2 100755 --- a/Support/gomate.rb +++ b/Support/gomate.rb @@ -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'] diff --git a/info.plist b/info.plist index 27592a6..8ea3728 100644 --- a/info.plist +++ b/info.plist @@ -20,7 +20,9 @@ 0F6A8710-54FC-48F5-9D02-D093DA001D17 73628139-0077-4F09-9B72-77546D7C2D2D ------------------------------------ + D3CD6B51-3A7E-4356-85F4-B76B8336BEF2 7BCFCFC8-9152-4638-8436-E17B0C754C8D + ------------------------------------ B0271A46-F6EF-4D2F-95A6-EC067E69155C ------------------------------------ C64599DA-E362-4411-9782-58A9C7F1B05A From 3f96983585b2f932d7285986c1701758417c1874 Mon Sep 17 00:00:00 2001 From: Allan Odgaard Date: Mon, 15 Jul 2013 22:21:20 +0200 Subject: [PATCH 7/8] Lines with an initial closing brace now always decrease indent MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The issue is when code has been partially typed, e.g.: } e‸ Here the added ‘e’ (as in ‘else’) would previously cause the line to no longer be matched as decreasing the indent, which lead to suboptimal user experience. --- Preferences/Indentation Rules.tmPreferences | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Preferences/Indentation Rules.tmPreferences b/Preferences/Indentation Rules.tmPreferences index 18867dc..70b6d29 100644 --- a/Preferences/Indentation Rules.tmPreferences +++ b/Preferences/Indentation Rules.tmPreferences @@ -11,7 +11,7 @@ decreaseIndentPattern (?x)^ ( .* [*]/ )? - ( \s* \} ( [^}{"'\n]* \{ )? [;,()\s]* + ( \s* \} .* | \s* \) ( [^)("'\n]* \( )? [;,\s]* | ( \s* (case|default)\b .* ) ) From f3b6c46ada574e19104b4eeb029649e1214f2ea4 Mon Sep 17 00:00:00 2001 From: Allan Odgaard Date: Mon, 15 Jul 2013 22:25:51 +0200 Subject: [PATCH 8/8] Keys in map literals no longer matched as having zero indent MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The rule was intended to match labels, which look the same as key literals. Now ‘foo:’ is only matched as a label if it has no leading whitespace. Given the simplicity of the Go grammar, we could actually match map literals and give them a different scope, and thus have different indent patterns for when we are inside a map literal. --- Preferences/Indentation Rules.tmPreferences | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Preferences/Indentation Rules.tmPreferences b/Preferences/Indentation Rules.tmPreferences index 70b6d29..b82604b 100644 --- a/Preferences/Indentation Rules.tmPreferences +++ b/Preferences/Indentation Rules.tmPreferences @@ -27,7 +27,7 @@ ( \s* ( //.* | /[*] .*? [*]/ ) )* \s* $ zeroIndentPattern - ^\s*(?!default:)\w+:.*$ + ^(?!default:)\w+:.*$ uuid 160118A4-208D-4422-AFF0-0C21B5B78AAF