Skip to content

Commit

Permalink
feat(cpp): add support for virtual functions (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
kkoomen committed Oct 24, 2020
1 parent 09c393e commit a06e8e9
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 20 deletions.
7 changes: 6 additions & 1 deletion ftplugin/cpp.vim
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ unlet s:class_pattern['parameters']
" Matches (template) function- and class method declarations.
" ------------------------------------------------------------------------------
let s:function_pattern = doge#helpers#deepextend(s:pattern_base, {
\ 'nodeTypes': ['function_definition', 'declaration', 'template_declaration'],
\ 'nodeTypes': [
\ 'function_definition',
\ 'declaration',
\ 'template_declaration',
\ 'function_declarator',
\ ],
\})

" ------------------------------------------------------------------------------
Expand Down
14 changes: 11 additions & 3 deletions src/parsers/cpp.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ enum NodeType {
FIELD_DECLARATION = 'field_declaration',
CLASS_SPECIFIER = 'class_specifier',
TEMPLATE_DECLARATION = 'template_declaration',
FUNCTION_DECLARATOR = 'function_declarator',
}

export class CppParserService
Expand Down Expand Up @@ -41,6 +42,7 @@ export class CppParserService
break;
}

case NodeType.FUNCTION_DECLARATOR:
case NodeType.FUNCTION_DEFINITION:
case NodeType.DECLARATION: {
this.result = {
Expand All @@ -52,7 +54,11 @@ export class CppParserService
parameters: [],
returnType: null,
};
this.runNodeParser(this.parseFunction, node);
if (node.type === NodeType.FUNCTION_DECLARATOR) {
this.runNodeParser(this.parseFunction, node.parent as SyntaxNode);
} else {
this.runNodeParser(this.parseFunction, node);
}
break;
}

Expand Down Expand Up @@ -122,10 +128,12 @@ export class CppParserService
// Method name.
childNode.children
.filter((n: SyntaxNode) =>
['identifier', 'scoped_identifier'].includes(n.type),
['identifier', 'scoped_identifier', 'field_identifier'].includes(
n.type,
),
)
.forEach((n: SyntaxNode) => {
if (n.type === 'identifier') {
if (['identifier', 'field_identifier'].includes(n.type)) {
this.result.name = n.text;
}

Expand Down
115 changes: 99 additions & 16 deletions test/filetypes/cpp/virtual-functions.vader
Original file line number Diff line number Diff line change
@@ -1,18 +1,101 @@
# ==============================================================================
# Virtual functions with parameters with return type.
# Virtual function declarations with parameters with return type.
# ==============================================================================
# TODO: Fix tests after https://github.com/tree-sitter/tree-sitter-cpp/issues/88
" Given cpp (virtual function with parameters with return type):
" virtual bool my_virtual_func(bool val) = 0;
"
" Do (trigger doge):
" \<C-d>
"
" Expect cpp (generated comment with @brief, @param and @return tags):
" /**
" * @brief [TODO:description]
" *
" * @param val [TODO:description]
" * @return [TODO:description]
" */
" virtual bool my_virtual_func(bool val) = 0;
Given cpp (virtual function declarations with parameters with return type):
class Test {
virtual bool my_virtual_function(bool val);
virtual bool my_pure_virtual_function(bool val) = 0;
};

class Test2: public Test {
bool my_virtual_function(bool val) override;
bool my_pure_virtual_function(bool val) final;
}

Do (trigger doge):
:2\<CR>
\<C-d>
:9\<CR>
\<C-d>
:19\<CR>
\<C-d>
:26\<CR>
\<C-d>

Expect cpp (generated comment with @brief, @param and @return tags):
class Test {
/**
* @brief [TODO:description]
*
* @param val [TODO:description]
* @return [TODO:description]
*/
virtual bool my_virtual_function(bool val);
/**
* @brief [TODO:description]
*
* @param val [TODO:description]
* @return [TODO:description]
*/
virtual bool my_pure_virtual_function(bool val) = 0;
};

class Test2: public Test {
/**
* @brief [TODO:description]
*
* @param val [TODO:description]
* @return [TODO:description]
*/
bool my_virtual_function(bool val) override;
/**
* @brief [TODO:description]
*
* @param val [TODO:description]
* @return [TODO:description]
*/
bool my_pure_virtual_function(bool val) final;
}

# ==============================================================================
# Virtual function definitions with parameters with return type.
# ==============================================================================
Given cpp (virtual function definitions with parameters with return type):
class base {
public:
bool fun_1(bool val) { cout << "base-1\n"; }
};

class derived : public base {
public:
bool fun_1(bool val) { cout << "derived-1\n"; }
};

Do (trigger doge):
:3\<CR>
\<C-d>
:14\<CR>
\<C-d>

Expect cpp (generated comment with @brief, @param and @return tags):
class base {
public:
/**
* @brief [TODO:description]
*
* @param val [TODO:description]
* @return [TODO:description]
*/
bool fun_1(bool val) { cout << "base-1\n"; }
};

class derived : public base {
public:
/**
* @brief [TODO:description]
*
* @param val [TODO:description]
* @return [TODO:description]
*/
bool fun_1(bool val) { cout << "derived-1\n"; }
};

0 comments on commit a06e8e9

Please sign in to comment.