Skip to content

Commit

Permalink
Merge pull request #36 from Badhi/global_functions
Browse files Browse the repository at this point in the history
feat: Supporting global functions
  • Loading branch information
Badhi committed Jan 29, 2024
2 parents 49da4fe + f0dce1f commit 3ac14c9
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 51 deletions.
45 changes: 30 additions & 15 deletions lua/nt-cpp-tools/internal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ local function run_on_nodes(query, runner, sel_start_row, sel_end_row)
local parser = parsers.get_parser(bufnr, ft)
local root = parser:parse()[1]:root()

local matches = query:iter_matches(root, bufnr, sel_start_row, sel_end_row)
local matches = query:iter_matches(root, bufnr, sel_start_row, sel_end_row + 1)

while true do
local pattern, match = matches()
Expand Down Expand Up @@ -206,8 +206,22 @@ end
local function find_class_details(member_node, member_data)
member_data.class_details = {}
local end_row
local class_node = member_node:parent():type() == 'template_declaration' and
member_node:parent():parent():parent() or member_node:parent():parent()

if member_node:parent():type() == 'template_declaration' then
member_node = member_node:parent()
end
-- print(member_node:parent():type())

-- If global function, member node is the highest, no class data available
-- but function requires the scope end row to return
if member_node:parent():type() == 'translation_unit' then --TODO namespaces
_, _, end_row, _ = member_node:range()
return end_row
end

-- the function could be a template, therefore going an extra parent higher
local class_node = member_node:parent():parent()

while class_node and
(class_node:type() == 'class_specifier' or
class_node:type() == 'struct_specifier' or
Expand Down Expand Up @@ -269,19 +283,20 @@ function M.imp_func(range_start, range_end, custom_cb)
local classes_name
local classes_template_statemets

for h = #fun.class_details, 1, -1 do
local templ_class_name = fun.class_details[h].name ..
(fun.class_details[h].class_template_params or '') .. '::'
classes_name = (h == #fun.class_details) and templ_class_name or classes_name .. templ_class_name
if not classes_template_statemets then
classes_template_statemets = fun.class_details[h].class_template_statement
else
classes_template_statemets = classes_template_statemets .. ' '
.. fun.class_details[h].class_template_statement
end
if fun.class_details then
for h = #fun.class_details, 1, -1 do
local templ_class_name = fun.class_details[h].name ..
(fun.class_details[h].class_template_params or '') .. '::'
classes_name = (h == #fun.class_details) and templ_class_name or classes_name .. templ_class_name
if not classes_template_statemets then
classes_template_statemets = fun.class_details[h].class_template_statement
else
classes_template_statemets = classes_template_statemets .. ' '
.. fun.class_details[h].class_template_statement
end
end
end


local template_statements
if classes_template_statemets and fun.template then
template_statements = classes_template_statemets .. ' ' .. fun.template
Expand All @@ -293,7 +308,7 @@ function M.imp_func(range_start, range_end, custom_cb)

output = output .. (template_statements and template_statements .. '\n' or '') ..
(fun.ret_type and fun.ret_type .. ' ' or '' ) ..
classes_name
(classes_name and classes_name or '')
.. fun.fun_dec .. '\n{\n}\n'
end
end
Expand Down
54 changes: 19 additions & 35 deletions queries/cpp/outside_class_def.scm
Original file line number Diff line number Diff line change
@@ -1,36 +1,20 @@
(_
name: (type_identifier)
body: (field_declaration_list
[
(template_declaration
(declaration
[
(function_declarator)
(reference_declarator
(function_declarator))
(pointer_declarator
(function_declarator))
]
)@member_function
)
(field_declaration
[
(function_declarator)
(reference_declarator
(function_declarator))
(pointer_declarator
(function_declarator))
]
)
(declaration
[
(function_declarator)
(reference_declarator
(function_declarator))
(pointer_declarator
(function_declarator))
]
)
]@member_function
[
(field_declaration
[
(function_declarator)
(reference_declarator
(function_declarator))
(pointer_declarator
(function_declarator))
]
)
(declaration
[
(function_declarator)
(reference_declarator
(function_declarator))
(pointer_declarator
(function_declarator))
]
)
)
]@member_function
4 changes: 3 additions & 1 deletion scripts/minimal_setup.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
--vim.cmd [[set runtimepath+=.]]
vim.cmd [[set runtimepath+=.]]
vim.cmd [[set runtimepath+=/home/bhashith/lazy/nvim-treesitter/]]
vim.cmd [[set runtimepath+=/home/bhashith/lazy/plenary.nvim/]]
vim.cmd [[runtime! plugin/plenary.vim]]
vim.cmd [[runtime! plugin/nvim-treesitter.lua]]
vim.cmd [[runtime! plugin/nt-cpp-tools.vim]]
Expand Down
67 changes: 67 additions & 0 deletions test/implement_functions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -466,3 +466,70 @@ int* C::test2() const noexcept
}
-----------------------------
5,5

============================================
simple_global_functions
============================================
void test();
-----------------------------
void test()
{
}
-----------------------------
1,1
============================================
global_functions_with_args
============================================
void test(int a, int b, std::vector<int> d);
-----------------------------
void test(int a, int b, std::vector<int> d)
{
}
-----------------------------
1,1
============================================
global_functions_with_args_with_return
============================================
std::map<int, std::string> test(int a, int b, std::vector<int> d);
-----------------------------
std::map<int, std::string> test(int a, int b, std::vector<int> d)
{
}
-----------------------------
1,1
============================================
template_global_functions_with_args
============================================
template<typename T>
void test(int a, int b, std::vector<int> d);
-----------------------------
template <typename T>
void test(int a, int b, std::vector<int> d)
{
}
-----------------------------
1,2
============================================
template_global_functions_with_args_with_return
============================================
template<typename T>
std::map<int, std::string> test(int a, int b, std::vector<int> d);
-----------------------------
template <typename T>
std::map<int, std::string> test(int a, int b, std::vector<int> d)
{
}
-----------------------------
1,2
============================================
template_global_functions
============================================
template<typename T>
void test();
-----------------------------
template <typename T>
void test()
{
}
-----------------------------
1,2

0 comments on commit 3ac14c9

Please sign in to comment.