diff --git a/lua/gopher/_utils/init.lua b/lua/gopher/_utils/init.lua index 58a17b2..ad27fb4 100644 --- a/lua/gopher/_utils/init.lua +++ b/lua/gopher/_utils/init.lua @@ -1,10 +1,11 @@ -local c = require "gopher.config" -local log = require "gopher._utils.log" local utils = {} ---@param msg string ---@param lvl? integer by default `vim.log.levels.INFO` function utils.notify(msg, lvl) + local c = require "gopher.config" + local log = require "gopher._utils.log" + lvl = lvl or vim.log.levels.INFO vim.notify(msg, lvl, { ---@diagnostic disable-next-line:undefined-field diff --git a/lua/gopher/_utils/runner.lua b/lua/gopher/_utils/runner.lua index 15eaa42..23e5f73 100644 --- a/lua/gopher/_utils/runner.lua +++ b/lua/gopher/_utils/runner.lua @@ -1,4 +1,3 @@ -local c = require "gopher.config" local runner = {} ---@class gopher.RunnerOpts @@ -12,6 +11,7 @@ local runner = {} ---@param opts? gopher.RunnerOpts ---@return vim.SystemObj function runner.async(cmd, on_exit, opts) + local c = require "gopher.config" opts = opts or {} return vim.system(cmd, { cwd = opts.cwd or nil, @@ -25,6 +25,7 @@ end ---@param opts? gopher.RunnerOpts ---@return vim.SystemCompleted function runner.sync(cmd, opts) + local c = require "gopher.config" opts = opts or {} return vim .system(cmd, { diff --git a/lua/gopher/comment.lua b/lua/gopher/comment.lua index 4ce264c..7c4f410 100644 --- a/lua/gopher/comment.lua +++ b/lua/gopher/comment.lua @@ -7,9 +7,6 @@ --- Set cursor on line with function/method/struct/etc and --- run `:GoCmt` to generate a comment. -local ts = require "gopher._utils.ts" -local log = require "gopher._utils.log" -local u = require "gopher._utils" local comment = {} --- NOTE: The order of functions executed inside this function is IMPORTANT. @@ -20,6 +17,9 @@ local comment = {} ---@return string ---@dochide local function generate(bufnr, line) + local ts = require "gopher._utils.ts" + local u = require "gopher._utils" + local sf_ok, sf_res = pcall(ts.get_struct_field_under_cursor, bufnr) if sf_ok then return u.indent(line, sf_res.indent) .. "// " .. sf_res.name .. " " @@ -54,6 +54,8 @@ local function generate(bufnr, line) end function comment.comment() + local log = require "gopher._utils.log" + local bufnr = vim.api.nvim_get_current_buf() local lnum = vim.fn.getcurpos()[2] local line = vim.fn.getline(lnum) diff --git a/lua/gopher/go.lua b/lua/gopher/go.lua index ca57068..7e36895 100644 --- a/lua/gopher/go.lua +++ b/lua/gopher/go.lua @@ -1,9 +1,10 @@ -local c = require "gopher.config" -local u = require "gopher._utils" -local r = require "gopher._utils.runner" local go = {} local function run(subcmd, args) + local c = require "gopher.config" + local u = require "gopher._utils" + local r = require "gopher._utils.runner" + local rs = r.sync { c.commands.go, subcmd, unpack(args) } if rs.code ~= 0 then error("go " .. subcmd .. " failed: " .. rs.stderr) diff --git a/lua/gopher/gotests.lua b/lua/gopher/gotests.lua index 0a9eaf4..578252b 100644 --- a/lua/gopher/gotests.lua +++ b/lua/gopher/gotests.lua @@ -18,16 +18,16 @@ --- --- If you prefer named tests, you can enable them in |gopher.nvim-config|. -local c = require "gopher.config" -local ts_utils = require "gopher._utils.ts" -local r = require "gopher._utils.runner" -local u = require "gopher._utils" -local log = require "gopher._utils.log" local gotests = {} ---@param args table ---@dochide local function add_test(args) + local c = require "gopher.config" + local r = require "gopher._utils.runner" + local u = require "gopher._utils" + local log = require "gopher._utils.log" + if c.gotests.named then table.insert(args, "-named") end @@ -57,8 +57,10 @@ end -- generate unit test for one function function gotests.func_test() + local ts = require "gopher._utils.ts" + local bufnr = vim.api.nvim_get_current_buf() - local func = ts_utils.get_func_under_cursor(bufnr) + local func = ts.get_func_under_cursor(bufnr) add_test { "-only", func.name } end diff --git a/lua/gopher/iferr.lua b/lua/gopher/iferr.lua index 79ebc0d..6a2bfcc 100644 --- a/lua/gopher/iferr.lua +++ b/lua/gopher/iferr.lua @@ -24,14 +24,15 @@ --- } --- -local c = require "gopher.config" -local u = require "gopher._utils" -local r = require "gopher._utils.runner" -local log = require "gopher._utils.log" local iferr = {} ---@param message? string Optional custom error message to use instead of config default function iferr.iferr(message) + local c = require "gopher.config" + local u = require "gopher._utils" + local r = require "gopher._utils.runner" + local log = require "gopher._utils.log" + local curb = vim.fn.wordcount().cursor_bytes local pos = vim.fn.getcurpos()[2] local fpath = vim.fn.expand "%" diff --git a/lua/gopher/impl.lua b/lua/gopher/impl.lua index 907bbee..9dd1d4c 100644 --- a/lua/gopher/impl.lua +++ b/lua/gopher/impl.lua @@ -34,13 +34,14 @@ --- } --- < -local c = require "gopher.config" -local r = require "gopher._utils.runner" -local ts_utils = require "gopher._utils.ts" -local u = require "gopher._utils" local impl = {} function impl.impl(...) + local c = require "gopher.config" + local r = require "gopher._utils.runner" + local ts = require "gopher._utils.ts" + local u = require "gopher._utils" + local args = { ... } local iface, recv = "", "" local bufnr = vim.api.nvim_get_current_buf() @@ -50,12 +51,12 @@ function impl.impl(...) u.notify("arguments not provided. usage: :GoImpl f *File io.Reader", vim.log.levels.ERROR) return elseif #args == 1 then -- :GoImpl io.Reader - local st = ts_utils.get_struct_under_cursor(bufnr) + local st = ts.get_struct_under_cursor(bufnr) iface = args[1] recv = string.lower(st.name) .. " *" .. st.name append_after = st.end_ elseif #args == 2 then -- :GoImpl w io.Writer - local st = ts_utils.get_struct_under_cursor(bufnr) + local st = ts.get_struct_under_cursor(bufnr) iface = args[2] recv = args[1] .. " *" .. st.name append_after = st.end_ diff --git a/lua/gopher/init.lua b/lua/gopher/init.lua index dddd6a2..5e3ed0e 100644 --- a/lua/gopher/init.lua +++ b/lua/gopher/init.lua @@ -10,9 +10,6 @@ --- Table of Contents ---@toc -local tags = require "gopher.struct_tags" -local tests = require "gopher.gotests" -local go = require "gopher.go" local gopher = {} ---@toc_entry Setup @@ -25,7 +22,7 @@ local gopher = {} ---@usage >lua --- require("gopher").setup {} -- use default config or replace {} with your own --- < ----@param user_config gopher.Config See |gopher.nvim-config| +---@param user_config? gopher.Config See |gopher.nvim-config| gopher.setup = function(user_config) require("gopher.config").setup(user_config) end @@ -37,27 +34,68 @@ end --- install plugin's dependencies, you can run: --- `:GoInstallDeps` or `:GoInstallDepsSync` --- or use `require("gopher").install_deps()` if you prefer lua api. -gopher.install_deps = require("gopher.installer").install_deps +---@param opts? {sync:boolean} +gopher.install_deps = function(opts) + return require("gopher.installer").install_deps(opts) +end + +gopher.impl = function(...) + return require("gopher.impl").impl(...) +end -gopher.impl = require("gopher.impl").impl -gopher.iferr = require("gopher.iferr").iferr -gopher.comment = require("gopher.comment").comment +---@param message? string Optional custom error message to use instead of config default +gopher.iferr = function(message) + return require("gopher.iferr").iferr(message) +end + +gopher.comment = function() + return require("gopher.comment").comment() +end gopher.tags = { - add = tags.add, - rm = tags.remove, - clear = tags.clear, + ---@param opts gopher.StructTagInput + add = function(opts) + return require("gopher.struct_tags").add(opts) + end, + ---@param opts gopher.StructTagInput + rm = function(opts) + return require("gopher.struct_tags").remove(opts) + end, + clear = function() + return require("gopher.struct_tags").clear() + end, } gopher.test = { - add = tests.func_test, - exported = tests.all_exported_tests, - all = tests.all_tests, + add = function() + return require("gopher.gotests").func_test() + end, + exported = function() + return require("gopher.gotests").all_exported_tests() + end, + all = function() + return require("gopher.gotests").all_tests() + end, } -gopher.get = go.get -gopher.mod = go.mod -gopher.work = go.work -gopher.generate = go.generate +---@param args string[] +gopher.get = function(args) + return require("gopher.go").get(args) +end + +---@param args string[] +gopher.mod = function(args) + return require("gopher.go").mod(args) +end + +---@param args string[] +gopher.work = function(args) + return require("gopher.go").work(args) +end + +---@param args string[] +gopher.generate = function(args) + return require("gopher.go").generate(args) +end return gopher diff --git a/lua/gopher/installer.lua b/lua/gopher/installer.lua index 61e1788..4871e9b 100644 --- a/lua/gopher/installer.lua +++ b/lua/gopher/installer.lua @@ -1,7 +1,3 @@ -local c = require "gopher.config" -local r = require "gopher._utils.runner" -local u = require "gopher._utils" -local log = require "gopher._utils.log" local installer = {} local urls = { @@ -15,6 +11,9 @@ local urls = { ---@param opt vim.SystemCompleted ---@param url string local function handle_intall_exit(opt, url) + local u = require "gopher._utils" + local log = require "gopher._utils.log" + if opt.code ~= 0 then vim.schedule(function() u.notify("go install failed: " .. url) @@ -31,6 +30,10 @@ end ---@param url string local function install(url) + local c = require "gopher.config" + local r = require "gopher._utils.runner" + local u = require "gopher._utils" + vim.schedule(function() u.notify("go install-ing: " .. url) end) @@ -42,6 +45,10 @@ end ---@param url string local function install_sync(url) + local c = require "gopher.config" + local r = require "gopher._utils.runner" + local u = require "gopher._utils" + vim.schedule(function() u.notify("go install-ing: " .. url) end) diff --git a/lua/gopher/json2go.lua b/lua/gopher/json2go.lua index 40d0e9f..c92292c 100644 --- a/lua/gopher/json2go.lua +++ b/lua/gopher/json2go.lua @@ -13,10 +13,6 @@ --- :GoJson {"name": "Alice", "age": 30} --- < -local c = require "gopher.config" -local log = require "gopher._utils.log" -local u = require "gopher._utils" -local r = require "gopher._utils.runner" local json2go = {} ---@dochide @@ -24,6 +20,7 @@ local json2go = {} ---@param cpos integer ---@param type_ string local function apply(bufnr, cpos, type_) + local u = require "gopher._utils" local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false) local input_lines = u.remove_empty_lines(vim.split(type_, "\n")) for i, line in pairs(input_lines) do @@ -37,6 +34,11 @@ end ---@param json_str string Json string that is going to be converted to go type. ---@return string? Go type, or nil if failed. function json2go.transform(json_str) + local c = require "gopher.config" + local log = require "gopher._utils.log" + local u = require "gopher._utils" + local r = require "gopher._utils.runner" + local cmd = { c.commands.json2go } if c.json2go.type_name then table.insert(cmd, "-type", c.json2go.type_name) @@ -54,6 +56,8 @@ end ---@dochide ---@param ocpos integer local function interactive(ocpos) + local c = require "gopher.config" + local obuf = vim.api.nvim_get_current_buf() local owin = vim.api.nvim_get_current_win() diff --git a/lua/gopher/struct_tags.lua b/lua/gopher/struct_tags.lua index 9e7094c..76081a4 100644 --- a/lua/gopher/struct_tags.lua +++ b/lua/gopher/struct_tags.lua @@ -35,11 +35,6 @@ --- } --- < -local ts = require "gopher._utils.ts" -local r = require "gopher._utils.runner" -local c = require "gopher.config" -local u = require "gopher._utils" -local log = require "gopher._utils.log" local struct_tags = {} ---@dochide @@ -58,6 +53,12 @@ local struct_tags = {} ---@param user_args string[] ---@dochide local function handle_tags(fpath, bufnr, range, user_args) + local c = require "gopher.config" + local u = require "gopher._utils" + local log = require "gopher._utils.log" + local r = require "gopher._utils.runner" + local ts = require "gopher._utils.ts" + local st = ts.get_struct_under_cursor(bufnr) -- stylua: ignore @@ -134,6 +135,8 @@ end ---@param args string[] ---@return gopher.StructTagsArgs function struct_tags.parse_args(args) + local u = require "gopher._utils" + args = unwrap_if_needed(args) local tags, options = {}, {} @@ -156,6 +159,9 @@ end ---@param opts gopher.StructTagInput ---@dochide function struct_tags.add(opts) + local c = require "gopher.config" + local log = require "gopher._utils.log" + log.debug("adding tags", opts) local fpath = vim.fn.expand "%" @@ -174,6 +180,9 @@ end ---@dochide ---@param opts gopher.StructTagInput function struct_tags.remove(opts) + local c = require "gopher.config" + local log = require "gopher._utils.log" + log.debug("removing tags", opts) local fpath = vim.fn.expand "%"