fix(cpp): add support for declared functions (#314)

This commit is contained in:
Steven Arcangeli 2023-10-15 17:55:52 -07:00
parent f10d29edb4
commit 340d0197d7
5 changed files with 63 additions and 11 deletions

View file

@ -309,7 +309,21 @@ M.c = {
postprocess = c_postprocess,
}
M.cpp = {
postprocess = c_postprocess,
postprocess = function(bufnr, item, match)
if item.kind ~= "Function" then
return
end
local parent = (utils.get_at_path(match, "type") or {}).node
local stop_types = { "function_definition", "declaration", "field_declaration" }
while parent and not vim.tbl_contains(stop_types, parent:type()) do
parent = parent:parent()
end
if parent then
for k, v in pairs(helpers.range_from_nodes(parent, parent)) do
item[k] = v
end
end
end,
}
M.rst = {

View file

@ -1,7 +1,3 @@
(function_definition
(#set! "kind" "Function")
) @root @type
(struct_specifier
name: (type_identifier) @name
body: (field_declaration_list)
@ -16,6 +12,11 @@
(#set! "kind" "Struct")
)
(function_declarator
declarator: (_) @name
(#set! "kind" "Function")
) @type
(enum_specifier
name: (type_identifier) @name
(#set! "kind" "Enum")

View file

@ -69,19 +69,20 @@ M.assert_tree_equals = function(received, expected, path)
for i, child in ipairs(received) do
local exp_child = expected[i]
local lines = { "Symbol mismatch: {" }
local fields = { "kind", "name", "level", "lnum", "col", "end_lnum", "end_col" }
local fields =
{ "kind", "name", "level", "lnum", "col", "end_lnum", "end_col", "selection_range" }
for _, field in ipairs(fields) do
local s_field = string.rep(" ", 8 - string.len(field)) .. field
local line = string.format("%s = %s", s_field, exp_child[field])
if child[field] ~= exp_child[field] then
line = line .. string.format(" [%s]", child[field])
local s_field = string.rep(" ", 17 - string.len(field)) .. field
local line = string.format("%s = %s", s_field, vim.inspect(exp_child[field]))
if not vim.deep_equal(child[field], exp_child[field]) then
line = line .. string.format(" [%s]", vim.inspect(child[field]))
end
table.insert(lines, line)
end
table.insert(lines, "}")
local err_msg = table.concat(lines, "\n")
for _, field in ipairs(fields) do
assert.equals(exp_child[field], child[field], err_msg)
assert.same(exp_child[field], child[field], err_msg)
end
table.insert(path, exp_child.name)
M.assert_tree_equals(child.children, exp_child.children, path)
@ -91,6 +92,7 @@ end
M.reset_editor = function()
require("aerial").setup({})
require("aerial").sync_load()
vim.cmd.tabonly({ mods = { silent = true } })
for i, winid in ipairs(vim.api.nvim_tabpage_list_wins(0)) do
if i > 1 then

View file

@ -113,6 +113,35 @@ describe("treesitter cpp", function()
end_lnum = 28,
end_col = 1,
},
{
kind = "Function",
name = "declaredFunction",
level = 0,
lnum = 30,
col = 0,
end_lnum = 30,
end_col = 23,
},
{
kind = "Class",
name = "A",
level = 0,
lnum = 32,
col = 0,
end_lnum = 34,
end_col = 1,
children = {
{
kind = "Function",
name = "clsDeclaredFunction",
level = 1,
lnum = 33,
col = 2,
end_lnum = 33,
end_col = 28,
},
},
},
})
end)
end)

View file

@ -26,3 +26,9 @@ void fn_4() {
struct Point p1; // This should not show up as a symbol
struct Point *p2 = new struct Point; // This should not show up as a symbol
}
int declaredFunction();
class A {
int clsDeclaredFunction();
};