feat(parser): Implement C/C++ get_definition and get_signature

This commit is contained in:
2026-05-05 19:42:14 -04:00
parent b8460107b9
commit 799feb0f94
2 changed files with 281 additions and 0 deletions
+101
View File
@@ -204,3 +204,104 @@ public:
assert '[Class] MyClass (Lines 2-6)' in outline
assert ' [Method] myMethod (Lines 4-5)' in outline
def test_ast_parser_get_definition_c() -> None:
"""Verify get_definition for C."""
parser = ASTParser(language="c")
code = """
void my_func() {
printf("hello\\n");
}
struct MyStruct {
int x;
};
"""
def1 = parser.get_definition(code, "my_func")
assert 'void my_func() {' in def1
assert 'printf("hello\\n");' in def1
def2 = parser.get_definition(code, "MyStruct")
assert 'struct MyStruct {' in def2
assert 'int x;' in def2
def test_ast_parser_get_definition_cpp() -> None:
"""Verify get_definition for C++ including scoped methods."""
parser = ASTParser(language="cpp")
code = """
class MyClass {
public:
void myMethod() {
int x = 1;
}
};
namespace MyNamespace {
void nsFunc() {}
}
"""
# Scoped lookup
def1 = parser.get_definition(code, "MyClass::myMethod")
assert 'void myMethod() {' in def1
assert 'int x = 1;' in def1
# Just name lookup
def2 = parser.get_definition(code, "myMethod")
assert 'void myMethod() {' in def2
# Namespace lookup
def3 = parser.get_definition(code, "MyNamespace::nsFunc")
assert 'void nsFunc() {}' in def3
def test_ast_parser_get_definition_cpp_template() -> None:
"""Verify get_definition for C++ templates."""
parser = ASTParser(language="cpp")
code = """
template <typename T>
void myTemplateFunc(T x) {
}
"""
def1 = parser.get_definition(code, "myTemplateFunc")
assert 'template <typename T>' in def1
assert 'void myTemplateFunc(T x) {' in def1
def test_ast_parser_get_signature_c() -> None:
"""Verify get_signature for C."""
parser = ASTParser(language="c")
code = """
void my_func(int a,
char* b) {
printf("hello\\n");
}
"""
sig = parser.get_signature(code, "my_func")
assert 'void my_func(int a,' in sig
assert 'char* b)' in sig
assert '{' not in sig
assert 'printf' not in sig
def test_ast_parser_get_signature_cpp() -> None:
"""Verify get_signature for C++ templates and methods."""
parser = ASTParser(language="cpp")
code = """
class MyClass {
public:
template <typename T>
T myTemplateMethod(T x) {
return x;
}
void normalMethod() {
}
};
"""
# Template method
sig1 = parser.get_signature(code, "MyClass::myTemplateMethod")
assert 'template <typename T>' in sig1
assert 'T myTemplateMethod(T x)' in sig1
assert '{' not in sig1
# Normal method
sig2 = parser.get_signature(code, "MyClass::normalMethod")
assert 'void normalMethod()' in sig2
assert '{' not in sig2