Skip to content

Commit

Permalink
fix(cpp): add support for declared functions (#314)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Oct 16, 2023
1 parent f10d29e commit 340d019
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 11 deletions.
16 changes: 15 additions & 1 deletion lua/aerial/backends/treesitter/extensions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
9 changes: 5 additions & 4 deletions queries/cpp/aerial.scm
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
(function_definition
(#set! "kind" "Function")
) @root @type

(struct_specifier
name: (type_identifier) @name
body: (field_declaration_list)
Expand All @@ -16,6 +12,11 @@
(#set! "kind" "Struct")
)

(function_declarator
declarator: (_) @name
(#set! "kind" "Function")
) @type

(enum_specifier
name: (type_identifier) @name
(#set! "kind" "Enum")
Expand Down
14 changes: 8 additions & 6 deletions tests/test_util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
29 changes: 29 additions & 0 deletions tests/treesitter/cpp_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
6 changes: 6 additions & 0 deletions tests/treesitter/cpp_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};

0 comments on commit 340d019

Please sign in to comment.