ctrl+shift+p filters: :st2 :st3 :win :osx :linux
Browse

Golang Tools Integration

by liuhewei ST3

Golang tools (gofmt, gocode, golint, guru, gorename, ...) integration for Sublime Text 3

Details

Installs

  • Total 11K
  • Win 4K
  • OS X 5K
  • Linux 3K
Sep 19 Sep 18 Sep 17 Sep 16 Sep 15 Sep 14 Sep 13 Sep 12 Sep 11 Sep 10 Sep 9 Sep 8 Sep 7 Sep 6 Sep 5 Sep 4 Sep 3 Sep 2 Sep 1 Aug 31 Aug 30 Aug 29 Aug 28 Aug 27 Aug 26 Aug 25 Aug 24 Aug 23 Aug 22 Aug 21 Aug 20 Aug 19 Aug 18 Aug 17 Aug 16 Aug 15 Aug 14 Aug 13 Aug 12 Aug 11 Aug 10 Aug 9 Aug 8 Aug 7 Aug 6 Aug 5
Windows 9 6 7 13 23 21 14 13 4 5 5 12 6 7 10 10 6 6 4 2 9 7 6 4 7 8 18 10 8 11 4 5 9 3 7 11 10 6 13 11 10 13 8 11 3 3
OS X 14 17 20 19 30 49 39 15 7 2 5 11 11 17 4 5 4 9 11 6 11 6 2 3 6 11 8 7 15 11 4 11 6 6 9 5 10 7 7 4 6 9 6 8 12 7
Linux 2 17 6 5 15 29 14 8 5 6 4 6 7 9 9 5 2 9 5 3 5 3 8 9 9 8 5 3 8 7 7 5 8 10 8 8 6 3 3 8 7 4 5 5 2 4

Readme

Source
raw.​githubusercontent.​com

Golang Tools Integration

It's a fork version of GoTools by ironcladlou. I create this project because ironcladlou has decided to abandon GoTools project, and I want to use this plugin together with Golang Build (by wbond) (discuss-1; discuss-2).

Features inherited from GoTools:

  • Auto-Completion: with gocode
  • Auto-Format: with gofmt or goimports
  • Go-to Definition: with guru
  • Rename: with gorename

Features removed(replaced) from GoTools:

  • Go syntax highlight: replaced by sublime's native support
  • Go build & test system: replaced by “Golang Build” (search in package-control)
  • GoTools Settings: replaced by “golangconfig
  • godef support: only use guru for “Go to definition”

Features added:

  • Auto-Lint: with golint or govet
  • Doc tips over selected text or cursor: with godoc

Usage

  • Step 1: Install golang and go tools by yourself: gocode, goimports, guru, gorename, golint, and make sure the $GOPATH/bin is added into $PATH (Or you can set them in Golang's global “Settings - User” later).

    go get -u -v github.com/nsf/gocode
    # OR mdempsky/gocode for better performance
    go get -u -v github.com/mdempsky/gocode
    
    go get -u -v github.com/golang/lint/golint
    go get -u -v golang.org/x/tools/cmd/guru
    go get -u -v golang.org/x/tools/cmd/goimports
    go get -u -v golang.org/x/tools/cmd/gorename
    
  • Step 2: Search and install “Golang Tools Integration” from package control.

  • Step 3(optional): Configure the Settings for golang and your project following the golang.sublime-settings and ExampleProject.sublime-project. Typically, the full features of 'guru' need use the configuration of the project.

Tips

  • If you want to trigger auto-completion after “.”, you can add below into Settings - Syntax specific - User (a.k.a. User/Go.sublime-settings):

    {
        "auto_complete_triggers": [{"selector": "source.go - string - comment - constant.numeric", "characters": "."}]
    }
    
  • If you want to ignore auto-completion when in comments, constant strings, and numbers, you can add below into Settings - Syntax specific - User (a.k.a. User/Go.sublime-settings):

    {
        "auto_complete_selector": "meta.tag - punctuation.definition.tag.begin, source - comment - string - constant.numeric"
    }
    

Introduction below comes from GoTools project

GoTools

GoTools is a Go programming language plugin for Sublime Text 3 inspired by vim-go. Rather than attempting to reinvent various supporting IDE components, it provides integration with existing community-supported tools.

Features

  • Jump to symbol/declaration using guru
  • Format and syntax check on save, including gutter marks (using gofmt)
  • Autocompletion (using gocode)
  • Build and test integration
  • Source analysis (using guru)
  • Identifier renaming (using gorename)
  • Improved syntax support (borrowed from GoSublime)

Prerequisites

GoTools will attempt to find all external Go tools (guru, gofmt, gocode, etc.) using GOPATH and GOROOT (not PATH). If you don't have these binaries, use go get to install them:

go get -u -v github.com/nsf/gocode
go get -u -v golang.org/x/tools/cmd/goimports
go get -u -v golang.org/x/tools/cmd/guru
go get -u -v golang.org/x/tools/cmd/gorename

GoTools is only tested with Go 1.4. Note that gofmt is now included with the Go distribution, and any gofmt installed to GOPATH is likely from an old Go version and should probably be removed.

Installing

The easiest way to install GoTools is to use Package Control. Simply install Package Control, and then install the “GoTools” package using Package Control: Install Package from the command palette.

If you want to install GoTools manually, download the latest release and extract it to ~/.config/sublime-text-3/Packages/GoTools on Linux, or ~/Library/Application\ Support/Sublime\ Text\ 3/Packages/GoTools on OSX.

Configuring GoTools

Create a GoTools settings file through the Sublime Text preferences menu at Package Settings -> GoTools -> Settings -> User.

Default settings are provided and can be accessed through the Sublime Text preferences menu at Package Settings -> GoTools -> Settings - Default. Each option is documented in the settings file itself.

Configuring Your Project

Create a GoTools settings key in a Sublime Text .sublime-project file (through the menu at Project -> Edit Project).

A documented example project file is provided.

Using GoTools

NOTE: Most GoTools commands are available via the Sublime Text command palette. Open the palette when viewing a Go source file and search for “GoTools” to see what's available.

Many of the build commands are also available via the context menu.

Format on Save

GoTools will format Go source buffers each time they're saved. To disable automatic formatting, set format_on_save in your GoTools settings.

Here's an example key binding which formats a source file when <ctrl>+<alt>+f is pressed:

{"keys": ["ctrl+alt+f"], "command": "gotools_format"}

By default gofmt is used for formatting. To change the backend, set format_backend in your GoTools settings. goimports is also available, as well as the option to first run goimports, then gofmt. This third option is useful when you want the automatic import resolution as well as the simplification (-s) feature from gofmt at the same time.

Go to Definition

GoTools provides a gotools_goto_def Sublime Text command which will jump to the symbol definition at the cursor.

Here's an example key binding which will go to a definition when <ctrl+g> is pressed:

{"keys": ["ctrl+g"], "command": "gotools_goto_def"}

Here's an example sublime-mousemap entry which will go to a definition using <ctrl>+<left mouse>:

{"button": "button1", "count": 1, "modifiers": ["ctrl"], "command": "gotools_goto_def"}

Autocomplete

GoTools integrates the Sublime Text autocompletion engine with gocode.

Here's an example key binding which autocompletes when <ctrl>+<space> is pressed:

{"keys": ["ctrl+space"], "command": "auto_complete"}

When suggestions are available, a specially formatted suggestion list will appear, including type information for each suggestion.

To disable autocompletion integration, set autocomplete in your GoTools settings.

Builds

GoTools integrates the Sublime Text build system with go build.

Activate the GoTools build system from the Sublime Text menu by selecting it from Tools -> Build System. If the build system is set to Automatic, GoTools will be automatically used for builds when editing Go source files.

There are several ways to perform a build:

  • From the Sublime Text menu at Tools -> Build
  • A key bound to the build command
  • The command palette, as Build: Build

A “Clean Build” command variant is also provided which recursively deletes all GOPATH/pkg directory contents prior to executing the build as usual.

Build results are placed in the Sublime Text build output panel which can be toggled with a command such as:

{ "keys" : ["ctrl+m"], "command" : "show_panel" , "args" : {"panel": "output.exec", "toggle": true}},

Here's an example key binding which runs a build when <ctrl>+b is pressed:

{ "keys": ["ctrl+b"], "command": "build" },

Here's an example key binding which runs “Clean Build” when <ctrl>+<alt>+b is pressed:

{ "keys": ["ctrl+alt+b"], "command": "build", "args": {"variant": "Clean Build"}},

Tests

GoTools integrates the Sublime Text build system with go test.

GoTools attempts to “do what you mean” depending on context. For instance, when using “Run Test at Cursor” in a test file which requires an integration Go build tag, GoTools will notice this and automatically add -tags integration to the test execution.

The following GoTools build variants are available:

Variant Description
Run Tests Discovers test packages based on the project_package and test_packages settings relative to the project gopath and executes them.
Run Test at Cursor Runs a single test method at or surrounding the cursor.
Run Current Package Tests Runs tests for the package containing the current file.
Run Tagged Tests Like “Run Tests” but for the packages specified in the tagged_packages setting.
Run Last Test Runs the last test variant that was executed.

Test results are placed in the built-in Sublime Text build output panel which can be toggled with a command such as:

{ "keys" : ["ctrl+m"], "command" : "show_panel" , "args" : {"panel": "output.exec", "toggle": true}},

Here's an example key binding which runs the test at the cursor when <ctrl>+<alt>+t is pressed:

{ "keys": ["ctrl+alt+t"], "command": "build", "args": {"variant": "Run Test at Cursor"}},

Replace variant in the command with any variant name from the preceding table for other bindings.

Oracle Analysis (experimental)

GoTools integrates Sublime Text with guru. Oracle is invoked with the gotools_guru Sublime Text command.

Here's an example which runs the guru “implements” command when <ctrl+alt+i> is pressed:

{ "keys" : ["ctrl+alt+i"], "command" : "gotools_guru" , "args" : {"command": "implements"}},

The following guru operations are supported as arguments to the gotools_guru command:

Command Notes
callers Slow on large codebases.
callees Slow on large codebases.
callstack Slow on large codebases.
describe
freevars Requires a selection.
implements
peers
referrers

Oracle results are placed in a Sublime Text output panel which can be toggled with a command such as:

{ "keys" : ["ctrl+m"], "command" : "show_panel" , "args" : {"panel": "output.gotools_guru", "toggle": true}},

Rename (experimental)

GoTools provides a gotools_rename command supported by gorename which supports type-safe renaming of identifiers.

When the gotools_rename command is executed, an input panel labeled Go rename: will appear. Rename results are placed in a Sublime Text output panel which can be toggled with a command such as:

{ "keys" : ["ctrl+m"], "command" : "show_panel" , "args" : {"panel": "output.gotools_rename", "toggle": true}},

Important: The gorename tool writes files in-place with no option for a dry-run. Changes might be destructive, and the tool is known to have bugs.

Gocode Caveats

Important: Using gocode support will modify the lib-path setting in the gocode daemon. The change will affect all clients, including other Sublime Text sessions, Vim instances, etc. Don't use this setting if you're concerned about interoperability with other tools which integrate with gocode.

Some projects make use of a dependency isolation tool such as Godep, and many projects use some sort of custom build script. Additionally, gocode uses a client/server architecture, and at present relies on a global server-side setting to resolve Go package paths for suggestion computation. By default, gocode will only search GOROOT and GOPATH/pkg for packages, which may be insufficient if the project compiles source to multiple GOPATH entries (such as Godeps/_workspace/pkg).

With such a project, to get the best suggestions from gocode, it's necessary to configure the gocode daemon prior to client suggestion requests to inform gocode about the locations of compiled packages for the project.

GoTools will infer the correct gocode lib-path by constructing a path which incorporates all project GOPATH entries.

GoSublime Caveats

Installing GoTools alongside GoSublime isn't tested or supported, so YMMV.