gomvp support #176

pull/234/head
ray-x 2 years ago
parent a5b307c35c
commit 32b4c162f0

@ -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 |
| -------------- | ----------------------------------------------------------------------------------------------- |

@ -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

@ -354,5 +354,9 @@ return {
end,
range = true,
})
create_cmd('Gomvp', function(opts)
require('go.gomvp').run(opts.fargs)
end, { nargs = '*' })
end,
}

@ -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

@ -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 = {}

@ -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()

Loading…
Cancel
Save