From 32b4c162f08b745e9c38a69066da6690c74b68b8 Mon Sep 17 00:00:00 2001 From: ray-x Date: Tue, 25 Oct 2022 20:16:50 +1100 Subject: [PATCH] gomvp support #176 --- README.md | 10 ++++++- doc/go.txt | 3 ++ lua/go/commands.lua | 4 +++ lua/go/gomvp.lua | 50 +++++++++++++++++++++++++++++++ lua/go/install.lua | 1 + lua/go/ts/go.lua | 71 +++++++++++++++++++++++++++++---------------- 6 files changed, 113 insertions(+), 26 deletions(-) create mode 100644 lua/go/gomvp.lua diff --git a/README.md b/README.md index 14e0e61..9f60497 100644 --- a/README.md +++ b/README.md @@ -564,9 +564,17 @@ if err != nil { ``` +### Rename modules +* Gomvp +Rename module name in under cursor +e.g. +Gomvp +Gomvp old_mod_name +Gomvp old_mod_name new_mod_name -### Commands + +### Debug Commands | Command | Description | | -------------- | ----------------------------------------------------------------------------------------------- | diff --git a/doc/go.txt b/doc/go.txt index 56eb4b1..fa3601a 100644 --- a/doc/go.txt +++ b/doc/go.txt @@ -221,6 +221,9 @@ COMMANDS *go-nvim-commands* :GoRename *:GoRename* Rename the identifier under the cursor. +:Gomvp *:Gomvp* + Rename the module name under the cursor. + :{range}GoAddTag [flags] *:GoAddTags* Add, modify, or remove struct tags. Will only apply to the fields in {range} if it's given, or applied to all fields in the struct if it's diff --git a/lua/go/commands.lua b/lua/go/commands.lua index 35b376f..f5ea868 100644 --- a/lua/go/commands.lua +++ b/lua/go/commands.lua @@ -354,5 +354,9 @@ return { end, range = true, }) + + create_cmd('Gomvp', function(opts) + require('go.gomvp').run(opts.fargs) + end, { nargs = '*' }) end, } diff --git a/lua/go/gomvp.lua b/lua/go/gomvp.lua new file mode 100644 index 0000000..c463969 --- /dev/null +++ b/lua/go/gomvp.lua @@ -0,0 +1,50 @@ +local runner = require("go.runner") +local utils = require("go.utils") +local log = utils.log +local M = {} + +function M.run(args) + require("go.install").install('gomvp') + args = args or {} + + local input = vim.ui.input + + local guihua = utils.load_plugin("guihua.lua", "guihua.input") + if guihua then + input = guihua.input + end + local cmd = { "gomvp" } + local old_mod = require('go.ts.go').get_module_at_pos() + if old_mod == nil then + if #args == 0 then + utils.warn("please provide a module name or put cursor on a module name") + return + end + old_mod = args[1] + end + local new_module + if #args == 2 then + new_module = args[2] + else + new_module = input({ + prompt = "new module name: ", + default = old_mod, + on_confirm = function(input) + new_module = input + end}) + end + vim.list_extend(cmd, { old_mod, new_module }) + local opts = { + update_buffer = true, + on_exit = function() + vim.schedule(function() + utils.restart() + end) + end, + } + log('running', cmd) + runner.run(cmd, opts) + return cmd, opts +end + +return M diff --git a/lua/go/install.lua b/lua/go/install.lua index a5d4901..be8a71f 100644 --- a/lua/go/install.lua +++ b/lua/go/install.lua @@ -24,6 +24,7 @@ local url = { gotestsum = 'gotest.tools/gotestsum', mockgen = 'github.com/golang/mock', ['json-to-struct'] = 'github.com/tmc/json-to-struct', + gomvp = 'https://github.com/abenz1267/gomvp', } local tools = {} diff --git a/lua/go/ts/go.lua b/lua/go/ts/go.lua index 72a2804..5d9a337 100644 --- a/lua/go/ts/go.lua +++ b/lua/go/ts/go.lua @@ -1,16 +1,17 @@ -local nodes = require("go.ts.nodes") +local nodes = require('go.ts.nodes') -local log = require("go.utils").log -local warn = require("go.utils").warn -local info = require("go.utils").info -local debug = require("go.utils").debug -debug = log +local tsutil = require('nvim-treesitter.ts_utils') +local log = require('go.utils').log +local warn = require('go.utils').warn +local info = require('go.utils').info +local debug = require('go.utils').debug +-- debug = log local M = { - query_struct = "(type_spec name:(type_identifier) @definition.struct type: (struct_type))", - query_package = "(package_clause (package_identifier)@package.name)@package.clause", - query_struct_id = "(type_spec name:(type_identifier) @definition.struct (struct_type))", - query_em_struct_id = "(field_declaration name:(field_identifier) @definition.struct (struct_type))", + query_struct = '(type_spec name:(type_identifier) @definition.struct type: (struct_type))', + query_package = '(package_clause (package_identifier)@package.name)@package.clause', + query_struct_id = '(type_spec name:(type_identifier) @definition.struct (struct_type))', + query_em_struct_id = '(field_declaration name:(field_identifier) @definition.struct (struct_type))', query_struct_block = [[((type_declaration (type_spec name:(type_identifier) @struct.name type: (struct_type)))@struct.declaration)]], query_type_declaration = [[((type_declaration (type_spec name:(type_identifier)@type_decl.name type:(type_identifier)@type_decl.type))@type_decl.declaration)]], -- rename to gotype so not confuse with type query_em_struct_block = [[(field_declaration name:(field_identifier)@struct.name type: (struct_type)) @struct.declaration]], @@ -18,8 +19,8 @@ local M = { -- query_em_struct = "(field_declaration name:(field_identifier) @definition.struct type: (struct_type))", query_interface_id = [[((type_declaration (type_spec name:(type_identifier) @interface.name type:(interface_type)))@interface.declaration)]], query_interface_method = [[((method_spec name: (field_identifier)@method.name)@interface.method.declaration)]], - query_func = "((function_declaration name: (identifier)@function.name) @function.declaration)", - query_method = "(method_declaration receiver: (parameter_list (parameter_declaration name:(identifier)@method.receiver.name type:(type_identifier)@method.receiver.type)) name:(field_identifier)@method.name)@method.declaration", + query_func = '((function_declaration name: (identifier)@function.name) @function.declaration)', + query_method = '(method_declaration receiver: (parameter_list (parameter_declaration name:(identifier)@method.receiver.name type:(type_identifier)@method.receiver.type)) name:(field_identifier)@method.name)@method.declaration', query_method_name = [[((method_declaration receiver: (parameter_list)@method.receiver name: (field_identifier)@method.name @@ -103,17 +104,17 @@ local M = { } local function get_name_defaults() - return { ["func"] = "function", ["if"] = "if", ["else"] = "else", ["for"] = "for" } + return { ['func'] = 'function', ['if'] = 'if', ['else'] = 'else', ['for'] = 'for' } end M.get_struct_node_at_pos = function(bufnr) - local query = M.query_struct_block .. " " .. M.query_em_struct_block + local query = M.query_struct_block .. ' ' .. M.query_em_struct_block local bufn = bufnr or vim.api.nvim_get_current_buf() local ns = nodes.nodes_at_cursor(query, get_name_defaults(), bufn) if ns == nil then - debug("struct not found") + debug('struct not found') else - log("struct node", ns) + log('struct node', ns) return ns[#ns] end end @@ -123,9 +124,9 @@ M.get_type_node_at_pos = function(bufnr) local bufn = bufnr or vim.api.nvim_get_current_buf() local ns = nodes.nodes_at_cursor(query, get_name_defaults(), bufn) if ns == nil then - debug("type not found") + debug('type not found') else - log("type node", ns) + log('type node', ns) return ns[#ns] end end @@ -136,7 +137,7 @@ M.get_interface_node_at_pos = function(bufnr) local bufn = bufnr or vim.api.nvim_get_current_buf() local ns = nodes.nodes_at_cursor(query, get_name_defaults(), bufn) if ns == nil then - debug("interface not found") + debug('interface not found') else return ns[#ns] end @@ -148,14 +149,14 @@ M.get_interface_method_node_at_pos = function(bufnr) local ns = nodes.nodes_at_cursor(query, get_name_defaults(), bufnr) if ns == nil then - warn("interface method not found") + warn('interface method not found') else return ns[#ns] end end M.get_func_method_node_at_pos = function(bufnr) - local query = M.query_func .. " " .. M.query_method_name + local query = M.query_func .. ' ' .. M.query_method_name -- local query = require("go.ts.go").query_method_name local bufn = bufnr or vim.api.nvim_get_current_buf() @@ -165,12 +166,32 @@ M.get_func_method_node_at_pos = function(bufnr) return nil end if ns == nil then - warn("function not found") + warn('function not found') else return ns[#ns] end end +M.get_import_node_at_pos = function(bufnr) + local bufn = bufnr or vim.api.nvim_get_current_buf() + + local cur_node = tsutil.get_node_at_cursor() + + if cur_node and (cur_node:type() == 'import_spec' or cur_node:parent():type() == 'import_spec') then + return cur_node + end +end + +M.get_module_at_pos = function(bufnr) + local node = M.get_import_node_at_pos(bufnr) + if node then + local module = vim.treesitter.query.get_node_text(node, vim.api.nvim_get_current_buf()) + -- log + module = string.gsub(module, '"', '') + return module + end +end + M.get_package_node_at_pos = function(bufnr) local row, col = unpack(vim.api.nvim_win_get_cursor(0)) row, col = row, col + 1 @@ -187,14 +208,14 @@ M.get_package_node_at_pos = function(bufnr) return nil end if ns == nil then - warn("package not found") + warn('package not found') else return ns[#ns] end end function M.in_func() - local ok, ts_utils = pcall(require, "nvim-treesitter.ts_utils") + local ok, ts_utils = pcall(require, 'nvim-treesitter.ts_utils') if not ok then return false end @@ -205,7 +226,7 @@ function M.in_func() local expr = current_node while expr do - if expr:type() == "function_declaration" or expr:type() == "method_declaration" then + if expr:type() == 'function_declaration' or expr:type() == 'method_declaration' then return true end expr = expr:parent()