feat: automatically restart lsp server (opt-in)

This commit is contained in:
Oleksandr Smirnov 2025-09-10 15:55:43 +03:00
parent 440a7cc432
commit 7c198a1b36
No known key found for this signature in database
7 changed files with 105 additions and 69 deletions

View file

@ -201,6 +201,12 @@ require("gopher").setup {
-- timeout for running internal commands
timeout = 2000,
-- timeout for running installer commands(e.g :GoDepsInstall, :GoDepsInstallSync)
installer_timeout = 999999,
-- restart gopls server after commands like `:GoMod`, `:GoGet`, `:GoWork`
restart_lsp = false,
commands = {
go = "go",
gomodifytags = "gomodifytags",

View file

@ -44,6 +44,12 @@ to install them synchronously pass `{sync = true}` as an argument.
==============================================================================
------------------------------------------------------------------------------
*config*
`config`
Type ~
`(gopher.Config)`
------------------------------------------------------------------------------
*gopher.nvim-config*
`default_config`
@ -57,9 +63,12 @@ to install them synchronously pass `{sync = true}` as an argument.
---@type number
timeout = 2000,
--- timeout for running installer commands(e.g :GoDepsInstall, :GoDepsInstallSync)
-- timeout for running installer commands(e.g :GoDepsInstall, :GoDepsInstallSync)
installer_timeout = 999999,
-- restart gopls server after commands like `:GoMod`, `:GoGet`, `:GoWork`
restart_lsp = false,
-- user specified paths to binaries
---@class gopher.ConfigCommand
commands = {
@ -96,6 +105,8 @@ to install them synchronously pass `{sync = true}` as an argument.
<
Class ~
{gopher.Config}
Fields ~
{setup} `(fun(user_config?: gopher.Config))`
==============================================================================

View file

@ -1,51 +0,0 @@
local r = require "gopher._utils.runner"
local c = require("gopher.config").commands
local u = require "gopher._utils"
local gocmd = {}
---@param args string[]
---@return string[]
local function if_get(args)
for i, arg in ipairs(args) do
local m = string.match(arg, "^https://(.*)$") or string.match(arg, "^http://(.*)$") or arg
table.remove(args, i)
table.insert(args, i, m)
end
return args
end
---@param args unknown[]
---@return string[]
local function if_generate(args)
if #args == 1 and args[1] == "%" then
args[1] = vim.fn.expand "%"
end
return args
end
---@param subcmd string
---@param args string[]
---@return string
function gocmd.run(subcmd, args)
if #args == 0 and subcmd ~= "generate" then
error "please provide any arguments"
end
if subcmd == "get" then
args = if_get(args)
end
if subcmd == "generate" then
args = if_generate(args)
end
local rs = r.sync { c.go, subcmd, unpack(args) }
if rs.code ~= 0 then
error("go " .. subcmd .. " failed: " .. rs.stderr)
end
u.notify(c.go .. " " .. subcmd .. " ran successful")
return rs.stdout
end
return gocmd

11
lua/gopher/_utils/lsp.lua Normal file
View file

@ -0,0 +1,11 @@
local lsp = {}
local gopls = "gopls"
-- Restarts gopls server (see: `:h lsp-restart`)
function lsp.restart()
vim.lsp.enable(gopls, false)
vim.lsp.enable(gopls, true)
end
return lsp

View file

@ -25,9 +25,12 @@ local default_config = {
---@type number
timeout = 2000,
--- timeout for running installer commands(e.g :GoDepsInstall, :GoDepsInstallSync)
-- timeout for running installer commands(e.g :GoDepsInstall, :GoDepsInstallSync)
installer_timeout = 999999,
-- restart gopls server after commands like `:GoMod`, `:GoGet`, `:GoWork`
restart_lsp = false,
-- user specified paths to binaries
---@class gopher.ConfigCommand
commands = {
@ -84,6 +87,7 @@ function config.setup(user_config)
vim.validate("log_level", _config.log_level, "number")
vim.validate("timeout", _config.timeout, "number")
vim.validate("installer_timeout", _config.timeout, "number")
vim.validate("restart_lsp", _config.restart_lsp, "boolean")
vim.validate("commands", _config.commands, "table")
vim.validate("commands.go", _config.commands.go, "string")
vim.validate("commands.gomodifytags", _config.commands.gomodifytags, "string")

66
lua/gopher/go.lua Normal file
View file

@ -0,0 +1,66 @@
local c = require "gopher.config"
local u = require "gopher._utils"
local lsp = require "gopher._utils.lsp"
local r = require "gopher._utils.runner"
local go = {}
local function run(subcmd, args)
local rs = r.sync { c.commands.go, subcmd, unpack(args) }
if rs.code ~= 0 then
error("go " .. subcmd .. " failed: " .. rs.stderr)
end
u.notify(c.commands.go .. " " .. subcmd .. " ran successful")
return rs.stdout
end
local function restart_lsp()
if c.restart_lsp then
lsp.restart()
end
end
---@param args string[]
function go.get(args)
for i, arg in ipairs(args) do
local m = string.match(arg, "^https://(.*)$") or string.match(arg, "^http://(.*)$") or arg
table.remove(args, i)
table.insert(args, i, m)
end
run("get", args)
restart_lsp()
end
---@param args string[]
function go.mod(args)
run("mod", args)
restart_lsp()
end
---@param args string[]
function go.work(args)
-- TODO: use `gopls.tidy`
run("work", args)
restart_lsp()
end
---Executes `go generate`
---If only argument is `%` it's going to be equivalent to `go generate <path to current file>`
---@param args string[]
function go.generate(args)
-- TODO: use `gopls.generate`
if #args == 0 then
error "please provide arguments"
end
if #args == 1 and args[1] == "%" then
args[1] = vim.fn.expand "%"
end
run("generate", args)
end
return go

View file

@ -13,7 +13,7 @@
local log = require "gopher._utils.log"
local tags = require "gopher.struct_tags"
local tests = require "gopher.gotests"
local gocmd = require("gopher._utils.gocmd").run
local go = require "gopher.go"
local gopher = {}
---@toc_entry Setup
@ -58,20 +58,9 @@ gopher.test = {
all = tests.all_tests,
}
gopher.get = function(...)
gocmd("get", ...)
end
gopher.mod = function(...)
gocmd("mod", ...)
end
gopher.generate = function(...)
gocmd("generate", ...)
end
gopher.work = function(...)
gocmd("work", ...)
end
gopher.get = go.get
gopher.mod = go.mod
gopher.work = go.work
gopher.generate = go.generate
return gopher