Gopls:在 Sublime Text 中使用

請使用 LSP 外掛。在透過 Package Control 安裝該外掛後,請執行以下操作:

  • 開啟 命令面板
  • 找到並執行命令 LSP: Enable Language Server Globally(全域性啟用語言伺服器)
  • 選擇 gopls 條目。請注意不要錯誤地選擇名稱相似的 golsp

最後,您應該熟悉 LSP 外掛的 設定快捷鍵。您可以在選單項 Preferences > Package Settings > LSP(偏好設定 > 外掛設定 > LSP)下找到它們。

示例

最小化的全域性 LSP 設定,假設 goplsgo 出現在 Sublime Text 可見的 PATH 中。

{
    "clients": {
        "gopls": {
             "enabled": true,
         }
    }
}

全域性 LSP 設定,為查詢 goplsgo 提供了一個特定的 PATH,以及一些 Sublime LSP 本身的的設定。

{
    "clients": {
        "gopls": {
            "enabled": true,
            "env": {
                "PATH": "/path/to/your/go/bin",
            }
        }
    },
    // Recommended by https://agniva.me/gopls/2021/01/02/setting-up-gopls-sublime.html
    // except log_stderr mentioned there is no longer recognized.
    "show_references_in_quick_panel": true,
    "log_debug": true,
    // These two are recommended by LSP-json as replacement for deprecated only_show_lsp_completions
    "inhibit_snippet_completions": true,
    "inhibit_word_completions": true,
 }

LSP 和 gopls 的設定也可以針對每個專案進行調整,以覆蓋全域性設定。

{
    "folders": [
        {
            "path": "/path/to/a/folder/one"
        },
        {
            // If you happen to be working on Go itself, this can be helpful; go-dev/bin should be on PATH.
            "path": "/path/to/your/go-dev/src/cmd"
        }
     ],
    "settings": {
        "LSP": {
            "gopls": {
                // To use a specific version of gopls with Sublime Text LSP (e.g., to try new features in development)
                "command": [
                    "/path/to/your/go/bin/gopls"
                ],
                "env": {
                    "PATH": "/path/to/your/go-dev/bin:/path/to/your/go/bin",
                    "GOPATH": "",
                },
                "settings": {
                    "experimentalWorkspaceModule": true
                }
            }
        },
        // This will apply for all languages in this project that have
        // LSP servers, not just Go, however cannot enable just for Go.
        "lsp_format_on_save": true,
    }
}

通常,在儲存專案檔案後,這些設定的更改就會生效,但有時可能需要重新啟動伺服器(Tools > LSP > Restart Servers)(工具 > LSP > 重啟伺服器)或退出並重新啟動 Sublime Text 本身。


本文件的原始碼可以在 golang.org/x/tools/gopls/doc 下找到。