From 6fdd841593298d63d57ba53ef187a6e1a01b03bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Granstr=C3=B6m?= Date: Tue, 28 Sep 2021 17:48:33 +0200 Subject: [PATCH 1/5] Be able to annotate function prototypes in header files --- lua/neogen/configurations/c.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/neogen/configurations/c.lua b/lua/neogen/configurations/c.lua index 393246b..53e8f70 100644 --- a/lua/neogen/configurations/c.lua +++ b/lua/neogen/configurations/c.lua @@ -52,12 +52,12 @@ end return { parent = { - func = { "function_declaration", "function_definition" }, + func = { "function_declaration", "function_definition", "declaration" }, }, data = { func = { - ["function_declaration|function_definition"] = { + ["function_declaration|function_definition|declaration"] = { ["0"] = { extract = c_function_extractor, }, From b92bb47dd8f33cc92001edf505803b9e074c30c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Granstr=C3=B6m?= Date: Tue, 28 Sep 2021 18:47:36 +0200 Subject: [PATCH 2/5] Insert "return" based on function return type --- lua/neogen/configurations/c.lua | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/lua/neogen/configurations/c.lua b/lua/neogen/configurations/c.lua index 53e8f70..e166144 100644 --- a/lua/neogen/configurations/c.lua +++ b/lua/neogen/configurations/c.lua @@ -32,6 +32,11 @@ local c_function_extractor = function(node) { retrieve = "first", node_type = "return_statement", extract = true }, }, }, + { + retrieve = "first", + node_type = "primitive_type", + extract = true, + }, c_params, } @@ -44,9 +49,21 @@ local c_function_extractor = function(node) res = vim.tbl_deep_extend("keep", res, subres) end + local has_return_statement = function() + -- function implementation + if res.return_statement then + return res.return_statement + -- function prototype + elseif res.function_declarator and res.primitive_type and res.primitive_type[1] ~= "void" then + return res.primitive_type + end + -- not found + return nil + end + return { parameters = res.identifier, - return_statement = res.return_statement, + return_statement = has_return_statement(), } end From b48752476c7c16cbfe376526e0c84d9ca1f21fdf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Granstr=C3=B6m?= Date: Tue, 28 Sep 2021 19:18:13 +0200 Subject: [PATCH 3/5] Format with stylua --- lua/neogen/configurations/c.lua | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/lua/neogen/configurations/c.lua b/lua/neogen/configurations/c.lua index e166144..0f518a4 100644 --- a/lua/neogen/configurations/c.lua +++ b/lua/neogen/configurations/c.lua @@ -33,9 +33,9 @@ local c_function_extractor = function(node) }, }, { - retrieve = "first", - node_type = "primitive_type", - extract = true, + retrieve = "first", + node_type = "primitive_type", + extract = true, }, c_params, } @@ -50,15 +50,15 @@ local c_function_extractor = function(node) end local has_return_statement = function() - -- function implementation - if res.return_statement then - return res.return_statement - -- function prototype - elseif res.function_declarator and res.primitive_type and res.primitive_type[1] ~= "void" then - return res.primitive_type - end - -- not found - return nil + -- function implementation + if res.return_statement then + return res.return_statement + -- function prototype + elseif res.function_declarator and res.primitive_type and res.primitive_type[1] ~= "void" then + return res.primitive_type + end + -- not found + return nil end return { From 7320b90119d1777bfa7178e41e7614664b6e794f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Granstr=C3=B6m?= Date: Tue, 28 Sep 2021 19:23:04 +0200 Subject: [PATCH 4/5] Move comments to align with stylua formatting --- lua/neogen/configurations/c.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/neogen/configurations/c.lua b/lua/neogen/configurations/c.lua index 0f518a4..28b7f9c 100644 --- a/lua/neogen/configurations/c.lua +++ b/lua/neogen/configurations/c.lua @@ -50,11 +50,11 @@ local c_function_extractor = function(node) end local has_return_statement = function() - -- function implementation if res.return_statement then + -- function implementation return res.return_statement - -- function prototype elseif res.function_declarator and res.primitive_type and res.primitive_type[1] ~= "void" then + -- function prototype return res.primitive_type end -- not found From c0cd1bc91f05533ac4905a8d3eed9655ed566ccc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Granstr=C3=B6m?= Date: Fri, 1 Oct 2021 13:30:40 +0200 Subject: [PATCH 5/5] Add test for pointer return value --- lua/neogen/configurations/c.lua | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/lua/neogen/configurations/c.lua b/lua/neogen/configurations/c.lua index 28b7f9c..4389e6f 100644 --- a/lua/neogen/configurations/c.lua +++ b/lua/neogen/configurations/c.lua @@ -37,6 +37,11 @@ local c_function_extractor = function(node) node_type = "primitive_type", extract = true, }, + { + retrieve = "first", + node_type = "pointer_declarator", + extract = true, + }, c_params, } @@ -53,9 +58,11 @@ local c_function_extractor = function(node) if res.return_statement then -- function implementation return res.return_statement - elseif res.function_declarator and res.primitive_type and res.primitive_type[1] ~= "void" then - -- function prototype - return res.primitive_type + elseif res.function_declarator and res.primitive_type then + if res.primitive_type[1] ~= "void" or res.pointer_declarator then + -- function prototype + return res.primitive_type + end end -- not found return nil