fix(ts)(c/cpp): detect functions that return pointers (fix #25)

Side note: this unfortunately moves the symbol location forward in the
line to where the name starts, where before it was at the beginning of
the return type. Every time you add a pointer to the return type (e.g.
int* fn() or int** fn()) it creates a new treesitter wrapper node of
"pointer_declaration". Unfortunately there does not seem to currently be
a way to write a query for function_definition -> <any number for
children> -> function_declaration. Because of that, we must either
explicitly query for each level of pointer indirection, or just query
directly for the function_declaration. I have opted for the latter
because it is cleaner, but it has the side effect of changing the symbol
location slightly.
This commit is contained in:
Steven Arcangeli 2021-11-30 11:12:39 -08:00
parent f584e82db5
commit e8366e30b4
7 changed files with 41 additions and 23 deletions

View file

@ -7,7 +7,7 @@ return {
cpp = {
class_specifier = "Class",
enum_specifier = "Enum",
function_definition = "Function",
function_declarator = "Function",
struct_specifier = "Struct",
},
c_sharp = {

View file

@ -2,13 +2,5 @@
type: [(enum_specifier) (struct_specifier)] @type
declarator: (type_identifier) @name) @location
(_
declarator: (pointer_declarator
declarator: (function_declarator
declarator: (identifier) @name) @type)
) @location
(_
declarator: (function_declarator
declarator: (identifier) @name) @type
) @location
(function_declarator
declarator: (identifier) @name) @type

View file

@ -1,6 +1,5 @@
(function_definition
declarator: (function_declarator
declarator: [(identifier) (field_identifier) (qualified_identifier)] @name)) @type
(function_declarator
declarator: [(identifier) (field_identifier) (qualified_identifier)] @name) @type
(struct_specifier
name: (type_identifier) @name

View file

@ -8,41 +8,48 @@ describe("treesitter c", function()
name = "fn_1",
level = 0,
lnum = 3,
col = 0,
col = 5,
},
{
kind = "Function",
name = "fn_2",
level = 0,
lnum = 5,
col = 0,
col = 6,
},
{
kind = "Function",
name = "fn_3",
level = 0,
lnum = 7,
col = 0,
col = 5,
},
{
kind = "Function",
name = "fn_4",
level = 0,
lnum = 9,
col = 0,
col = 6,
},
{
kind = "Function",
name = "fn_5",
level = 0,
lnum = 11,
col = 7,
},
{
kind = "Enum",
name = "kEnum",
level = 0,
lnum = 11,
lnum = 13,
col = 0,
},
{
kind = "Struct",
name = "St_1",
level = 0,
lnum = 15,
lnum = 17,
col = 0,
},
})

View file

@ -8,6 +8,8 @@ void fn_3() {}
void *fn_4() { return 0; }
void **fn_5() { return 0; }
typedef enum {
kVal,
} kEnum;

View file

@ -8,7 +8,7 @@ describe("treesitter cpp", function()
name = "fn_1",
level = 0,
lnum = 1,
col = 0,
col = 5,
},
{
kind = "Struct",
@ -43,7 +43,7 @@ describe("treesitter cpp", function()
name = "meth_1",
level = 1,
lnum = 12,
col = 2,
col = 7,
},
},
},
@ -52,7 +52,21 @@ describe("treesitter cpp", function()
name = "A::bar",
level = 0,
lnum = 15,
col = 0,
col = 5,
},
{
kind = "Function",
name = "fn_2",
level = 0,
lnum = 17,
col = 5,
},
{
kind = "Function",
name = "fn_3",
level = 0,
lnum = 19,
col = 6,
},
})
end)

View file

@ -13,3 +13,7 @@ public:
};
void A::bar() {}
int *fn_2() {}
int **fn_3() {}