Gopls: Vim または Neovim を使用する
vim-go
vim-go バージョン 1.20+ を以下の設定で使用します
let g:go_def_mode='gopls'
let g:go_info_mode='gopls'
LanguageClient-neovim
LanguageClient-neovim を以下の設定で使用します
" Launch gopls when Go files are in use
let g:LanguageClient_serverCommands = {
\ 'go': ['gopls']
\ }
" Run gofmt on save
autocmd BufWritePre *.go :call LanguageClient#textDocument_formatting_sync()
Ale
ale を使用します
let g:ale_linters = {
\ 'go': ['gopls'],
\}
この問題を参照してください
vim-lsp
prabirshrestha/vim-lsp を以下の設定で使用します
augroup LspGo
au!
autocmd User lsp_setup call lsp#register_server({
\ 'name': 'gopls',
\ 'cmd': {server_info->['gopls']},
\ 'whitelist': ['go'],
\ })
autocmd FileType go setlocal omnifunc=lsp#complete
"autocmd FileType go nmap <buffer> gd <plug>(lsp-definition)
"autocmd FileType go nmap <buffer> ,n <plug>(lsp-next-error)
"autocmd FileType go nmap <buffer> ,p <plug>(lsp-previous-error)
augroup END
vim-lsc
natebosch/vim-lsc を以下の設定で使用します
let g:lsc_server_commands = {
\ "go": {
\ "command": "gopls serve",
\ "log_level": -1,
\ "suppress_stderr": v:true,
\ },
\}
log_level と suppress_stderr の部分は、ログによる破損を防ぐために必要です。#180 と #213 の問題を参照してください。
coc.nvim
coc.nvim を以下の coc-settings.json 設定で使用します
"languageserver": {
"go": {
"command": "gopls",
"rootPatterns": ["go.work", "go.mod", ".vim/", ".git/", ".hg/"],
"filetypes": ["go"],
"initializationOptions": {
"usePlaceholders": true
}
}
}
go.work ファイルを使用する場合、workspace.workspaceFolderCheckCwd オプションを設定するとよいでしょう。これにより、現在の開いているディレクトリに go.mod ファイルがある場合でも、coc.nvim が親ディレクトリで go.work ファイルを強制的に検索します。詳細については、coc.nvim のドキュメントを参照してください。
その他の設定も initializationOptions に追加できます。
editor.action.organizeImport コードアクションは、コードの自動整形と不足しているインポートの追加を行います。これを保存時に自動的に実行するには、以下の行を init.vim に追加します
autocmd BufWritePre *.go :call CocAction('runCommand', 'editor.action.organizeImport')
govim
従来のVimのみで、実験的な govim を使用するには、インストール手順に単純に従ってください。
Neovim v0.5.0+
Neovim で新しいネイティブ LSP クライアントを使用するには、Neovim v.0.5.0+、nvim-lspconfig 設定ヘルパープラグインをインストールし、そこのgopls 設定セクションを確認してください。
インストール
Neovim のネイティブプラグインシステムを使用できます。Unix システムでは、nvim-lspconfig リポジトリを正しいディレクトリにクローンすることでそれを行うことができます。
dir="${HOME}/.local/share/nvim/site/pack/nvim-lspconfig/opt/nvim-lspconfig/"
mkdir -p "$dir"
cd "$dir"
git clone 'https://github.com/neovim/nvim-lspconfig.git' .
設定
nvim-lspconfig は合理的なデフォルト値を提供するよう努めているため、セットアップは非常に簡潔です。
local lspconfig = require("lspconfig")
lspconfig.gopls.setup({})
ただし、好みに応じて gopls を設定することもできます。以下は unusedparams、staticcheck、gofumpt を有効にする例です。
local lspconfig = require("lspconfig")
lspconfig.gopls.setup({
settings = {
gopls = {
analyses = {
unusedparams = true,
},
staticcheck = true,
gofumpt = true,
},
},
})
インポートとフォーマット
以下の設定を使用すると、goimports のロジックを使用して保存時にインポートが整理され、コードがフォーマットされます。
autocmd("BufWritePre", {
pattern = "*.go",
callback = function()
local params = vim.lsp.util.make_range_params()
params.context = {only = {"source.organizeImports"}}
-- buf_request_sync defaults to a 1000ms timeout. Depending on your
-- machine and codebase, you may want longer. Add an additional
-- argument after params if you find that you have to write the file
-- twice for changes to be saved.
-- E.g., vim.lsp.buf_request_sync(0, "textDocument/codeAction", params, 3000)
local result = vim.lsp.buf_request_sync(0, "textDocument/codeAction", params)
for cid, res in pairs(result or {}) do
for _, r in pairs(res.result or {}) do
if r.edit then
local enc = (vim.lsp.get_client_by_id(cid) or {}).offset_encoding or "utf-16"
vim.lsp.util.apply_workspace_edit(r.edit, enc)
end
end
end
vim.lsp.buf.format({async = false})
end
})
Omnifunc
Neovim v0.8.1 以降では、omnifunc オプションを設定しない場合、v:lua.vim.lsp.omnifunc に自動設定されます。それ以前のバージョンを使用している場合は、手動で設定できます。
local on_attach = function(client, bufnr)
-- Enable completion triggered by <c-x><c-o>
vim.api.nvim_buf_set_option(bufnr, 'omnifunc', 'v:lua.vim.lsp.omnifunc')
end
require('lspconfig').gopls.setup({
on_attach = on_attach
})
追加リンク
このドキュメントのソースファイルは、golang.org/x/tools/gopls/doc の下にあります。