feat(parser): Implement C/C++ skeleton and outline extraction

This commit is contained in:
2026-05-05 18:51:56 -04:00
parent 0b819b29c1
commit d3cd7cf75a
2 changed files with 137 additions and 3 deletions
+74
View File
@@ -36,6 +36,49 @@ class MyClass:
assert 'def method(self):' in skeleton
assert '"""Method docstring."""' in skeleton
def test_ast_parser_get_skeleton_c() -> None:
"""Verify that get_skeleton replaces function bodies with '...' for C while preserving structs."""
parser = ASTParser(language="c")
code = """
struct MyStruct {
int x;
};
void my_func() {
printf("hello\\n");
}
"""
skeleton = parser.get_skeleton(code)
assert 'struct MyStruct {' in skeleton
assert 'int x;' in skeleton
assert 'void my_func()' in skeleton
assert '...' in skeleton
assert 'printf("hello\\n");' not in skeleton
def test_ast_parser_get_skeleton_cpp() -> None:
"""Verify that get_skeleton replaces function and method bodies with '...' for C++."""
parser = ASTParser(language="cpp")
code = """
class MyClass {
public:
void myMethod() {
int x = 1;
}
};
template <typename T>
void myTemplateFunc(T x) {
x.doSomething();
}
"""
skeleton = parser.get_skeleton(code)
assert 'class MyClass {' in skeleton
assert 'void myMethod() ...' in skeleton
assert 'template <typename T>' in skeleton
assert 'void myTemplateFunc(T x) ...' in skeleton
assert 'int x = 1;' not in skeleton
assert 'x.doSomething();' not in skeleton
def test_ast_parser_invalid_language() -> None:
"""Verify handling of unsupported or invalid languages."""
# Currently ASTParser defaults to Python if language not supported or just fails tree-sitter init
@@ -130,3 +173,34 @@ class MyClass:
assert 'def dep1():' in view2
assert 'def dep2():' not in view2
assert 'def method2(self):' not in view2
def test_ast_parser_get_code_outline_c() -> None:
"""Verify that get_code_outline works for C."""
parser = ASTParser(language="c")
code = """
struct MyStruct {
int x;
};
void my_func() {
printf("hello\\n");
}
"""
outline = parser.get_code_outline(code)
assert '[Struct] MyStruct (Lines 2-4)' in outline
assert '[Func] my_func (Lines 6-8)' in outline
def test_ast_parser_get_code_outline_cpp() -> None:
"""Verify that get_code_outline works for C++."""
parser = ASTParser(language="cpp")
code = """
class MyClass {
public:
void myMethod() {
}
};
"""
outline = parser.get_code_outline(code)
assert '[Class] MyClass (Lines 2-6)' in outline
assert ' [Method] myMethod (Lines 4-5)' in outline