diff --git a/lua/null-ls/builtins/code_actions/gomodifytags.lua b/lua/null-ls/builtins/code_actions/gomodifytags.lua index db7db58c..0fc39e6d 100644 --- a/lua/null-ls/builtins/code_actions/gomodifytags.lua +++ b/lua/null-ls/builtins/code_actions/gomodifytags.lua @@ -140,9 +140,16 @@ return h.make_builtin({ -- End of Execution helpers -- Main + local has_parser, parser = pcall(vim.treesitter.get_parser, bufnr, "go", { error = false }) + if not has_parser or not parser then + log:debug("no parser available for lang go") + return + end + parser:parse() + local tsnode if u.has_version("0.9.0") then - tsnode = vim.treesitter.get_node({ bufnr = bufnr, row = row - 1, col = col - 1 }) + tsnode = vim.treesitter.get_node({ bufnr = bufnr, lang = "go", pos = { row - 1, col - 1 } }) else tsnode = vim.treesitter.get_node_at_pos(bufnr, row - 1, col - 1, {}) end @@ -193,7 +200,7 @@ return h.make_builtin({ return end - struct_name = treesitter_get_node_text(tsnode, 0) + struct_name = treesitter_get_node_text(tsnode, bufnr) if struct_name == nil then return end @@ -230,11 +237,11 @@ return h.make_builtin({ -- Ops on struct field if (tsnode:type()) == "field_identifier" then - local field_name = treesitter_get_node_text(tsnode, 0) + local field_name = treesitter_get_node_text(tsnode, bufnr) tspnode = tsnode:parent():parent():parent() if tspnode ~= nil and (tspnode:type()) == "struct_type" then tspnode = tspnode:parent():child(0) - struct_name = treesitter_get_node_text(tspnode, 0) + struct_name = treesitter_get_node_text(tspnode, bufnr) end if struct_name == nil or field_name == nil then diff --git a/lua/null-ls/builtins/code_actions/impl.lua b/lua/null-ls/builtins/code_actions/impl.lua index 13e23dc7..82fa3de6 100644 --- a/lua/null-ls/builtins/code_actions/impl.lua +++ b/lua/null-ls/builtins/code_actions/impl.lua @@ -186,9 +186,16 @@ return h.make_builtin({ } end + local has_parser, parser = pcall(vim.treesitter.get_parser, bufnr, "go", { error = false }) + if not has_parser or not parser then + log:debug("no parser available for lang go") + return + end + parser:parse() + local tsnode if u.has_version("0.9.0") then - tsnode = vim.treesitter.get_node({ bufnr = bufnr, row = row - 1, col = col - 1 }) + tsnode = vim.treesitter.get_node({ bufnr = bufnr, lang = "go", pos = { row - 1, col - 1 } }) else tsnode = vim.treesitter.get_node_at_pos(bufnr, row - 1, col - 1, {}) end diff --git a/test/spec/builtins/code_actions/gomodifytags_spec.lua b/test/spec/builtins/code_actions/gomodifytags_spec.lua new file mode 100644 index 00000000..27532a0e --- /dev/null +++ b/test/spec/builtins/code_actions/gomodifytags_spec.lua @@ -0,0 +1,228 @@ +local stub = require("luassert.stub") + +local code_actions = require("null-ls.builtins").code_actions + +local function make_node(node_type, range, opts) + opts = opts or {} + local children = opts.children or {} + local fields = opts.fields or {} + local parent = opts.parent + local node = {} + + function node:type() + return node_type + end + + function node:range() + return range[1], range[2], range[3], range[4] + end + + function node:parent() + return parent + end + + function node:set_parent(p) + parent = p + end + + function node:child_count() + return #children + end + + function node:child(idx) + return children[idx + 1] + end + + function node:field(name) + return fields[name] or {} + end + + for _, c in ipairs(children) do + c:set_parent(node) + end + + return node +end + +describe("code_actions gomodifytags", function() + local handler = code_actions.gomodifytags.generator.fn + local get_parser + local bufnr + local prev_bufnr + + before_each(function() + prev_bufnr = vim.api.nvim_get_current_buf() + bufnr = vim.api.nvim_create_buf(false, true) + vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, { + "package main", + "", + "type User struct {", + "\tName string", + "}", + }) + get_parser = stub(vim.treesitter, "get_parser") + end) + + after_each(function() + get_parser:revert() + if vim.api.nvim_buf_is_valid(prev_bufnr) then + vim.api.nvim_set_current_buf(prev_bufnr) + end + if vim.api.nvim_buf_is_valid(bufnr) then + vim.api.nvim_buf_delete(bufnr, { force = true }) + end + end) + + local function build_struct_tree() + -- Line 2 (0-indexed): "type User struct {" + -- Line 3 (0-indexed): "\tName string" + local type_id = make_node("type_identifier", { 2, 5, 2, 9 }) + local field_id = make_node("field_identifier", { 3, 1, 3, 5 }) + local field_type = make_node("type_identifier", { 3, 6, 3, 12 }) + local field_decl = make_node("field_declaration", { 3, 1, 3, 12 }, { + children = { field_id, field_type }, + }) + local field_list = make_node("field_declaration_list", { 2, 17, 4, 1 }, { + children = { field_decl }, + }) + local struct_type = make_node("struct_type", { 2, 10, 4, 1 }, { + children = { field_list }, + }) + local type_spec = make_node("type_spec", { 2, 5, 4, 1 }, { + children = { type_id, struct_type }, + fields = { type = { struct_type } }, + }) + make_node("type_declaration", { 2, 0, 4, 1 }, { + children = { make_node("type", { 2, 0, 2, 4 }), type_spec }, + }) + + return { + type_id = type_id, + field_id = field_id, + } + end + + it("should parse syntax tree and return struct actions at target position", function() + local nodes = build_struct_tree() + local parsed = false + local parser = { + parse = function() + parsed = true + return {} + end, + named_node_for_range = function(_, range) + if not parsed then + return nil + end + if range[1] == 2 and range[2] == 6 then + return nodes.type_id + end + return nil + end, + } + get_parser.returns(parser) + + local actions = handler({ + bufnr = bufnr, + bufname = "user.go", + range = { row = 3, col = 7 }, + }) + + assert.is_true(parsed) + assert.is_table(actions) + assert.equals(7, #actions) + assert.equals("gomodifytags: add tags", actions[1].title) + end) + + it("should parse syntax tree and execute field action with struct and field names", function() + local nodes = build_struct_tree() + local parsed = false + local parser = { + parse = function() + parsed = true + return {} + end, + named_node_for_range = function(_, range) + if not parsed then + return nil + end + if range[1] == 3 and range[2] == 2 then + return nodes.field_id + end + return nil + end, + } + get_parser.returns(parser) + + local fn_execute = stub(vim.fn, "execute") + local fn_system = stub(vim.fn, "system") + local get_vvar = stub(vim.api, "nvim_get_vvar") + get_vvar.returns(0) + + local actions = handler({ + bufnr = bufnr, + bufname = "user.go", + range = { row = 4, col = 3 }, + }) + + assert.is_true(parsed) + assert.is_table(actions) + assert.equals(7, #actions) + assert.equals("gomodifytags: clear tags", actions[4].title) + + actions[4].action() + assert + .stub(fn_system) + .was_called_with("gomodifytags -quiet -w -file user.go -struct User -field Name -clear-tags") + + fn_execute:revert() + fn_system:revert() + get_vvar:revert() + end) + + it("should use params.range instead of window cursor on current buffer", function() + vim.api.nvim_set_current_buf(bufnr) + vim.api.nvim_win_set_cursor(0, { 1, 0 }) + + local nodes = build_struct_tree() + local parsed = false + local parser = { + parse = function() + parsed = true + return {} + end, + named_node_for_range = function(_, range) + if not parsed then + return nil + end + if range[1] == 2 and range[2] == 6 then + return nodes.type_id + end + return nil + end, + } + get_parser.returns(parser) + + local actions = handler({ + bufnr = bufnr, + bufname = "user.go", + range = { row = 3, col = 7 }, + }) + + assert.is_true(parsed) + assert.is_table(actions) + assert.equals(7, #actions) + end) + + it("should return nil when go parser is unavailable", function() + get_parser.returns(nil) + + local actions = handler({ + bufnr = bufnr, + bufname = "user.go", + range = { row = 3, col = 7 }, + }) + + assert.is_nil(actions) + end) +end) diff --git a/test/spec/builtins/code_actions/impl_spec.lua b/test/spec/builtins/code_actions/impl_spec.lua new file mode 100644 index 00000000..87bcb2d7 --- /dev/null +++ b/test/spec/builtins/code_actions/impl_spec.lua @@ -0,0 +1,152 @@ +local stub = require("luassert.stub") + +local code_actions = require("null-ls.builtins").code_actions + +local function make_node(node_type, range, opts) + opts = opts or {} + local children = opts.children or {} + local parent = opts.parent + local node = {} + + function node:type() + return node_type + end + + function node:range() + return range[1], range[2], range[3], range[4] + end + + function node:parent() + return parent + end + + function node:set_parent(p) + parent = p + end + + for _, c in ipairs(children) do + c:set_parent(node) + end + + return node +end + +describe("code_actions impl", function() + local handler = code_actions.impl.generator.fn + local get_parser + local bufnr + local prev_bufnr + + before_each(function() + prev_bufnr = vim.api.nvim_get_current_buf() + bufnr = vim.api.nvim_create_buf(false, true) + vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, { + "package main", + "", + "type Handler struct{}", + }) + get_parser = stub(vim.treesitter, "get_parser") + end) + + after_each(function() + get_parser:revert() + if vim.api.nvim_buf_is_valid(prev_bufnr) then + vim.api.nvim_set_current_buf(prev_bufnr) + end + if vim.api.nvim_buf_is_valid(bufnr) then + vim.api.nvim_buf_delete(bufnr, { force = true }) + end + end) + + it("should parse syntax tree and return implement interface action at target position", function() + local type_id = make_node("type_identifier", { 2, 5, 2, 12 }) + local struct_type = make_node("struct_type", { 2, 13, 2, 21 }) + local type_spec = make_node("type_spec", { 2, 5, 2, 21 }, { + children = { type_id, struct_type }, + }) + make_node("type_declaration", { 2, 0, 2, 21 }, { + children = { type_spec }, + }) + + local parsed = false + local parser = { + parse = function() + parsed = true + return {} + end, + named_node_for_range = function(_, range) + if not parsed then + return nil + end + if range[1] == 2 and range[2] == 6 then + return type_id + end + return nil + end, + } + get_parser.returns(parser) + + local actions = handler({ + bufnr = bufnr, + range = { row = 3, col = 7 }, + }) + + assert.is_true(parsed) + assert.is_table(actions) + assert.equals(1, #actions) + assert.equals("impl: implement interface", actions[1].title) + end) + + it("should use params.range instead of window cursor on current buffer", function() + vim.api.nvim_set_current_buf(bufnr) + vim.api.nvim_win_set_cursor(0, { 1, 0 }) + + local type_id = make_node("type_identifier", { 2, 5, 2, 12 }) + local struct_type = make_node("struct_type", { 2, 13, 2, 21 }) + local type_spec = make_node("type_spec", { 2, 5, 2, 21 }, { + children = { type_id, struct_type }, + }) + make_node("type_declaration", { 2, 0, 2, 21 }, { + children = { type_spec }, + }) + + local parsed = false + local parser = { + parse = function() + parsed = true + return {} + end, + named_node_for_range = function(_, range) + if not parsed then + return nil + end + if range[1] == 2 and range[2] == 6 then + return type_id + end + return nil + end, + } + get_parser.returns(parser) + + local actions = handler({ + bufnr = bufnr, + range = { row = 3, col = 7 }, + }) + + assert.is_true(parsed) + assert.is_table(actions) + assert.equals(1, #actions) + assert.equals("impl: implement interface", actions[1].title) + end) + + it("should return nil when go parser is unavailable", function() + get_parser.returns(nil) + + local actions = handler({ + bufnr = bufnr, + range = { row = 3, col = 7 }, + }) + + assert.is_nil(actions) + end) +end)