diff --git a/README.md b/README.md index d0c9515..273ae89 100644 --- a/README.md +++ b/README.md @@ -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", diff --git a/doc/gopher.nvim.txt b/doc/gopher.nvim.txt index 59c6b4a..5652b80 100644 --- a/doc/gopher.nvim.txt +++ b/doc/gopher.nvim.txt @@ -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))` ============================================================================== diff --git a/lua/gopher/_utils/gocmd.lua b/lua/gopher/_utils/gocmd.lua deleted file mode 100644 index a091c4f..0000000 --- a/lua/gopher/_utils/gocmd.lua +++ /dev/null @@ -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 diff --git a/lua/gopher/_utils/lsp.lua b/lua/gopher/_utils/lsp.lua new file mode 100644 index 0000000..8a2e373 --- /dev/null +++ b/lua/gopher/_utils/lsp.lua @@ -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 diff --git a/lua/gopher/config.lua b/lua/gopher/config.lua index 7754b8e..9020b89 100644 --- a/lua/gopher/config.lua +++ b/lua/gopher/config.lua @@ -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") diff --git a/lua/gopher/go.lua b/lua/gopher/go.lua new file mode 100644 index 0000000..459053f --- /dev/null +++ b/lua/gopher/go.lua @@ -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 ` +---@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 diff --git a/lua/gopher/init.lua b/lua/gopher/init.lua index 9afac0c..54dc3ad 100644 --- a/lua/gopher/init.lua +++ b/lua/gopher/init.lua @@ -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