[mirror] Make your go dev experience better github.com/olexsmir/gopher.nvim
neovim golang

feat: automatically restart lsp server (opt-in)

olexsmir.xyz 7c198a1b 440a7cc4

verified
Changed files
+105 -69
doc
lua
+6
README.md
··· 201 201 -- timeout for running internal commands 202 202 timeout = 2000, 203 203 204 + -- timeout for running installer commands(e.g :GoDepsInstall, :GoDepsInstallSync) 205 + installer_timeout = 999999, 206 + 207 + -- restart gopls server after commands like `:GoMod`, `:GoGet`, `:GoWork` 208 + restart_lsp = false, 209 + 204 210 commands = { 205 211 go = "go", 206 212 gomodifytags = "gomodifytags",
+12 -1
doc/gopher.nvim.txt
··· 45 45 46 46 ============================================================================== 47 47 ------------------------------------------------------------------------------ 48 + *config* 49 + `config` 50 + Type ~ 51 + `(gopher.Config)` 52 + 53 + ------------------------------------------------------------------------------ 48 54 *gopher.nvim-config* 49 55 `default_config` 50 56 >lua ··· 57 63 ---@type number 58 64 timeout = 2000, 59 65 60 - --- timeout for running installer commands(e.g :GoDepsInstall, :GoDepsInstallSync) 66 + -- timeout for running installer commands(e.g :GoDepsInstall, :GoDepsInstallSync) 61 67 installer_timeout = 999999, 68 + 69 + -- restart gopls server after commands like `:GoMod`, `:GoGet`, `:GoWork` 70 + restart_lsp = false, 62 71 63 72 -- user specified paths to binaries 64 73 ---@class gopher.ConfigCommand ··· 96 105 < 97 106 Class ~ 98 107 {gopher.Config} 108 + Fields ~ 109 + {setup} `(fun(user_config?: gopher.Config))` 99 110 100 111 101 112 ==============================================================================
-51
lua/gopher/_utils/gocmd.lua
··· 1 - local r = require "gopher._utils.runner" 2 - local c = require("gopher.config").commands 3 - local u = require "gopher._utils" 4 - local gocmd = {} 5 - 6 - ---@param args string[] 7 - ---@return string[] 8 - local function if_get(args) 9 - for i, arg in ipairs(args) do 10 - local m = string.match(arg, "^https://(.*)$") or string.match(arg, "^http://(.*)$") or arg 11 - table.remove(args, i) 12 - table.insert(args, i, m) 13 - end 14 - return args 15 - end 16 - 17 - ---@param args unknown[] 18 - ---@return string[] 19 - local function if_generate(args) 20 - if #args == 1 and args[1] == "%" then 21 - args[1] = vim.fn.expand "%" 22 - end 23 - return args 24 - end 25 - 26 - ---@param subcmd string 27 - ---@param args string[] 28 - ---@return string 29 - function gocmd.run(subcmd, args) 30 - if #args == 0 and subcmd ~= "generate" then 31 - error "please provide any arguments" 32 - end 33 - 34 - if subcmd == "get" then 35 - args = if_get(args) 36 - end 37 - 38 - if subcmd == "generate" then 39 - args = if_generate(args) 40 - end 41 - 42 - local rs = r.sync { c.go, subcmd, unpack(args) } 43 - if rs.code ~= 0 then 44 - error("go " .. subcmd .. " failed: " .. rs.stderr) 45 - end 46 - 47 - u.notify(c.go .. " " .. subcmd .. " ran successful") 48 - return rs.stdout 49 - end 50 - 51 - return gocmd
+11
lua/gopher/_utils/lsp.lua
··· 1 + local lsp = {} 2 + 3 + local gopls = "gopls" 4 + 5 + -- Restarts gopls server (see: `:h lsp-restart`) 6 + function lsp.restart() 7 + vim.lsp.enable(gopls, false) 8 + vim.lsp.enable(gopls, true) 9 + end 10 + 11 + return lsp
+5 -1
lua/gopher/config.lua
··· 25 25 ---@type number 26 26 timeout = 2000, 27 27 28 - --- timeout for running installer commands(e.g :GoDepsInstall, :GoDepsInstallSync) 28 + -- timeout for running installer commands(e.g :GoDepsInstall, :GoDepsInstallSync) 29 29 installer_timeout = 999999, 30 + 31 + -- restart gopls server after commands like `:GoMod`, `:GoGet`, `:GoWork` 32 + restart_lsp = false, 30 33 31 34 -- user specified paths to binaries 32 35 ---@class gopher.ConfigCommand ··· 84 87 vim.validate("log_level", _config.log_level, "number") 85 88 vim.validate("timeout", _config.timeout, "number") 86 89 vim.validate("installer_timeout", _config.timeout, "number") 90 + vim.validate("restart_lsp", _config.restart_lsp, "boolean") 87 91 vim.validate("commands", _config.commands, "table") 88 92 vim.validate("commands.go", _config.commands.go, "string") 89 93 vim.validate("commands.gomodifytags", _config.commands.gomodifytags, "string")
+66
lua/gopher/go.lua
··· 1 + local c = require "gopher.config" 2 + local u = require "gopher._utils" 3 + local lsp = require "gopher._utils.lsp" 4 + local r = require "gopher._utils.runner" 5 + local go = {} 6 + 7 + local function run(subcmd, args) 8 + local rs = r.sync { c.commands.go, subcmd, unpack(args) } 9 + if rs.code ~= 0 then 10 + error("go " .. subcmd .. " failed: " .. rs.stderr) 11 + end 12 + 13 + u.notify(c.commands.go .. " " .. subcmd .. " ran successful") 14 + return rs.stdout 15 + end 16 + 17 + local function restart_lsp() 18 + if c.restart_lsp then 19 + lsp.restart() 20 + end 21 + end 22 + 23 + ---@param args string[] 24 + function go.get(args) 25 + for i, arg in ipairs(args) do 26 + local m = string.match(arg, "^https://(.*)$") or string.match(arg, "^http://(.*)$") or arg 27 + table.remove(args, i) 28 + table.insert(args, i, m) 29 + end 30 + 31 + run("get", args) 32 + restart_lsp() 33 + end 34 + 35 + ---@param args string[] 36 + function go.mod(args) 37 + run("mod", args) 38 + restart_lsp() 39 + end 40 + 41 + ---@param args string[] 42 + function go.work(args) 43 + -- TODO: use `gopls.tidy` 44 + 45 + run("work", args) 46 + restart_lsp() 47 + end 48 + 49 + ---Executes `go generate` 50 + ---If only argument is `%` it's going to be equivalent to `go generate <path to current file>` 51 + ---@param args string[] 52 + function go.generate(args) 53 + -- TODO: use `gopls.generate` 54 + 55 + if #args == 0 then 56 + error "please provide arguments" 57 + end 58 + 59 + if #args == 1 and args[1] == "%" then 60 + args[1] = vim.fn.expand "%" 61 + end 62 + 63 + run("generate", args) 64 + end 65 + 66 + return go
+5 -16
lua/gopher/init.lua
··· 13 13 local log = require "gopher._utils.log" 14 14 local tags = require "gopher.struct_tags" 15 15 local tests = require "gopher.gotests" 16 - local gocmd = require("gopher._utils.gocmd").run 16 + local go = require "gopher.go" 17 17 local gopher = {} 18 18 19 19 ---@toc_entry Setup ··· 58 58 all = tests.all_tests, 59 59 } 60 60 61 - gopher.get = function(...) 62 - gocmd("get", ...) 63 - end 64 - 65 - gopher.mod = function(...) 66 - gocmd("mod", ...) 67 - end 68 - 69 - gopher.generate = function(...) 70 - gocmd("generate", ...) 71 - end 72 - 73 - gopher.work = function(...) 74 - gocmd("work", ...) 75 - end 61 + gopher.get = go.get 62 + gopher.mod = go.mod 63 + gopher.work = go.work 64 + gopher.generate = go.generate 76 65 77 66 return gopher