| 1 | --- |
| 2 | title: "Gopls: Using Sublime Text" |
| 3 | --- |
| 4 | |
| 5 | Use the [LSP] package. After installing it using Package Control, do the following: |
| 6 | |
| 7 | * Open the **Command Palette** |
| 8 | * Find and run the command **LSP: Enable Language Server Globally** |
| 9 | * Select the **gopls** item. Be careful not to select the similarly named *golsp* by mistake. |
| 10 | |
| 11 | Finally, you should familiarise yourself with the LSP package's *Settings* and *Key Bindings*. Find them under the menu item **Preferences > Package Settings > LSP**. |
| 12 | |
| 13 | ## Examples |
| 14 | Minimal global LSP settings, that assume **gopls** and **go** appear on the PATH seen by Sublime Text:<br> |
| 15 | ``` |
| 16 | { |
| 17 | "clients": { |
| 18 | "gopls": { |
| 19 | "enabled": true, |
| 20 | } |
| 21 | } |
| 22 | } |
| 23 | ``` |
| 24 | |
| 25 | Global LSP settings that supply a specific PATH for finding **gopls** and **go**, as well as some settings for Sublime LSP itself: |
| 26 | ``` |
| 27 | { |
| 28 | "clients": { |
| 29 | "gopls": { |
| 30 | "enabled": true, |
| 31 | "env": { |
| 32 | "PATH": "/path/to/your/go/bin", |
| 33 | } |
| 34 | } |
| 35 | }, |
| 36 | // Recommended by https://agniva.me/gopls/2021/01/02/setting-up-gopls-sublime.html |
| 37 | // except log_stderr mentioned there is no longer recognized. |
| 38 | "show_references_in_quick_panel": true, |
| 39 | "log_debug": true, |
| 40 | // These two are recommended by LSP-json as replacement for deprecated only_show_lsp_completions |
| 41 | "inhibit_snippet_completions": true, |
| 42 | "inhibit_word_completions": true, |
| 43 | } |
| 44 | ``` |
| 45 | |
| 46 | LSP and gopls settings can also be adjusted on a per-project basis to override global settings. |
| 47 | ``` |
| 48 | { |
| 49 | "folders": [ |
| 50 | { |
| 51 | "path": "/path/to/a/folder/one" |
| 52 | }, |
| 53 | { |
| 54 | // If you happen to be working on Go itself, this can be helpful; go-dev/bin should be on PATH. |
| 55 | "path": "/path/to/your/go-dev/src/cmd" |
| 56 | } |
| 57 | ], |
| 58 | "settings": { |
| 59 | "LSP": { |
| 60 | "gopls": { |
| 61 | // To use a specific version of gopls with Sublime Text LSP (e.g., to try new features in development) |
| 62 | "command": [ |
| 63 | "/path/to/your/go/bin/gopls" |
| 64 | ], |
| 65 | "env": { |
| 66 | "PATH": "/path/to/your/go-dev/bin:/path/to/your/go/bin", |
| 67 | "GOPATH": "", |
| 68 | }, |
| 69 | "settings": { |
| 70 | "experimentalWorkspaceModule": true |
| 71 | } |
| 72 | } |
| 73 | }, |
| 74 | // This will apply for all languages in this project that have |
| 75 | // LSP servers, not just Go, however cannot enable just for Go. |
| 76 | "lsp_format_on_save": true, |
| 77 | } |
| 78 | } |
| 79 | ``` |
| 80 | |
| 81 | Usually changes to these settings are recognized after saving the project file, but it may sometimes be necessary to either restart the server(s) (**Tools > LSP > Restart Servers**) or quit and restart Sublime Text itself. |
| 82 | |
| 83 | [LSP]: https://packagecontrol.io/packages/LSP |