Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,6 @@ tree-sitter-php = "0.24"
tree-sitter-perl = "1.1"
tree-sitter-r = "1.2"
tree-sitter-elixir = "0.3"
tree-sitter-dart = "0.1"

[dev-dependencies]
228 changes: 225 additions & 3 deletions src/indexer/extractor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ pub fn is_test_file(file_path: &str) -> bool {
|| file_name.ends_with("Test.kts")
|| path.components().any(|c| c.as_os_str() == "test")
}
"dart" => {
file_name.ends_with("_test.dart")
|| file_name.ends_with("_widget_test.dart")
|| path.components().any(|c| c.as_os_str() == "test")
}
_ => false,
}
}
Expand Down Expand Up @@ -96,6 +101,18 @@ pub fn is_noise_call(name: &str) -> bool {
| "TODO" | "lazy"
// Android logger mappings
| "v" | "d" | "i" | "w" | "e" | "wtf"
// ── Dart / Flutter built-ins ──
| "setState" | "initState" | "dispose" | "build"
| "context" | "mounted" | "widget"
| "debugPrint"
| "maybeOf"
// Dart null safety & keywords
| "late" | "required" | "abstract" | "override"
| "extends" | "with" | "implements" | "mixin" | "extension"
| "static" | "final" | "const" | "var"
// Flutter test functions
| "group" | "testWidgets" | "test" | "setUp" | "tearDown"
| "setUpAll" | "tearDownAll"
) || name.len() < 2
}

Expand Down Expand Up @@ -164,6 +181,15 @@ pub fn get_tested_file_path(file_path: &str) -> Option<String> {
None
}
}
"dart" => {
if file_name.ends_with("_test.dart") {
Some(file_name.trim_end_matches("_test.dart").to_string() + ".dart")
} else if file_name.ends_with("_widget_test.dart") {
Some(file_name.trim_end_matches("_widget_test.dart").to_string() + ".dart")
} else {
None
}
}
_ => None,
}?;

Expand Down Expand Up @@ -403,15 +429,19 @@ impl<'a> EntityExtractor<'a> {
| "function_definition"
| "function_item"
| "function_def"
| "function_signature"
| "method_declaration"
| "method_definition"
| "method_signature"
| "constructor_declaration"
| "constructor_signature"
| "secondary_constructor" => {
self.extract_function(node, parent, elements, relationships);
}
"class_declaration" | "type_declaration" | "class_def" | "struct_item"
| "class_definition" | "enum_declaration" | "record_declaration"
| "object_declaration" | "companion_object" => {
| "object_declaration" | "companion_object"
| "mixin_declaration" | "extension_declaration" | "type_alias" => {
self.extract_class(node, parent, elements, relationships);
}
"decorated_definition" => {
Expand All @@ -431,7 +461,8 @@ impl<'a> EntityExtractor<'a> {
| "import_specifier"
| "import_statement"
| "import_from_statement"
| "use_declaration" => {
| "use_declaration"
| "library_import" => {
for source in self.get_import_sources(node, node_type) {
relationships.push(Relationship {
id: None,
Expand Down Expand Up @@ -479,6 +510,9 @@ impl<'a> EntityExtractor<'a> {
| "object_declaration"
| "companion_object"
| "interface_declaration"
| "mixin_declaration"
| "extension_declaration"
| "type_alias"
) {
self.get_node_name(node)
} else {
Expand All @@ -498,7 +532,7 @@ impl<'a> EntityExtractor<'a> {
) {
let is_constructor = matches!(
node.kind(),
"constructor_declaration" | "secondary_constructor"
"constructor_declaration" | "secondary_constructor" | "constructor_signature"
);
let name = if is_constructor {
self.get_node_name(node)
Expand Down Expand Up @@ -1255,6 +1289,7 @@ impl<'a> EntityExtractor<'a> {
"method_declaration"
| "constructor_declaration"
| "secondary_constructor"
| "constructor_signature"
| "class_declaration"
| "interface_declaration"
| "enum_declaration"
Expand Down Expand Up @@ -1407,6 +1442,27 @@ impl<'a> EntityExtractor<'a> {
return sources;
}

// Dart: library imports (e.g., import 'package:flutter/material.dart';)
if node_type == "library_import" && self.language == "dart" {
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
match child.kind() {
"interpreted_string_literal" | "string" => {
if let Some(bytes) = self.source.get(child.byte_range()) {
if let Ok(s) = std::str::from_utf8(bytes) {
let trimmed = s.trim_matches('"').to_string();
if !trimmed.is_empty() {
sources.push(trimmed);
}
}
}
}
_ => {}
}
}
return sources;
}

// Go and JS/TS: walk all children to find string literals and import_specifiers
let mut stack = vec![node];
while let Some(current) = stack.pop() {
Expand Down Expand Up @@ -1484,6 +1540,13 @@ mod tests {
parser.parse(source, None)
}

fn parse_dart(source: &[u8]) -> Option<tree_sitter::Tree> {
let mut parser = Parser::new();
let lang: tree_sitter::Language = tree_sitter_dart::LANGUAGE.into();
parser.set_language(&lang).ok()?;
parser.parse(source, None)
}

#[test]
fn test_extractor_new() {
let source = b"func foo() {}";
Expand Down Expand Up @@ -2445,4 +2508,163 @@ class OldService {
);
}
}

// ── Dart / Flutter tests ──────────────────────────────────────────────────

#[test]
fn test_is_test_file_dart() {
assert!(is_test_file("lib/foo_test.dart"));
assert!(is_test_file("test/widget_test.dart"));
assert!(is_test_file("test/unit/my_test.dart"));
assert!(!is_test_file("lib/main.dart"));
assert!(!is_test_file("lib/home_page.dart"));
}

#[test]
fn test_get_tested_file_path_dart() {
assert_eq!(
get_tested_file_path("lib/foo_test.dart"),
Some("lib/foo.dart".to_string())
);
assert_eq!(
get_tested_file_path("test/widget_test.dart"),
Some("test/widget.dart".to_string())
);
assert_eq!(get_tested_file_path("lib/main.dart"), None);
}

#[test]
fn test_is_noise_call_dart_builtins() {
assert!(is_noise_call("setState"));
assert!(is_noise_call("initState"));
assert!(is_noise_call("dispose"));
assert!(is_noise_call("build"));
assert!(is_noise_call("context"));
assert!(is_noise_call("mounted"));
assert!(is_noise_call("widget"));
assert!(is_noise_call("debugPrint"));
assert!(is_noise_call("late"));
assert!(is_noise_call("required"));
assert!(is_noise_call("async"));
assert!(is_noise_call("await"));
}

#[test]
fn test_is_noise_call_dart_test_functions() {
assert!(is_noise_call("group"));
assert!(is_noise_call("testWidgets"));
assert!(is_noise_call("test"));
assert!(is_noise_call("setUp"));
assert!(is_noise_call("tearDown"));
assert!(is_noise_call("setUpAll"));
assert!(is_noise_call("tearDownAll"));
}

#[test]
fn test_extract_dart_class() {
let source = b"class MyWidget extends StatelessWidget {}";
if let Some(tree) = parse_dart(source) {
let extractor = EntityExtractor::new(source, "my_widget.dart", "dart");
let (elements, _) = extractor.extract(&tree);
let classes: Vec<_> = elements
.iter()
.filter(|e| e.element_type == "class")
.collect();
assert!(!classes.is_empty(), "Should extract Dart class");
assert_eq!(classes[0].name, "MyWidget");
}
}

#[test]
fn test_extract_dart_mixin() {
let source = b"mixin Toggleable {}";
if let Some(tree) = parse_dart(source) {
let extractor = EntityExtractor::new(source, "toggleable.dart", "dart");
let (elements, _) = extractor.extract(&tree);
let mixins: Vec<_> = elements
.iter()
.filter(|e| e.element_type == "class" && e.name == "Toggleable")
.collect();
assert!(!mixins.is_empty(), "Should extract Dart mixin");
}
}

#[test]
fn test_extract_dart_extension() {
let source = b"extension StringExtensions on String {}";
if let Some(tree) = parse_dart(source) {
let extractor = EntityExtractor::new(source, "string_ext.dart", "dart");
let (elements, _) = extractor.extract(&tree);
let extensions: Vec<_> = elements
.iter()
.filter(|e| e.name == "StringExtensions")
.collect();
assert!(!extensions.is_empty(), "Should extract Dart extension");
}
}

#[test]
fn test_extract_dart_function() {
let source = b"void greet(String name) => print('Hello $name');";
if let Some(tree) = parse_dart(source) {
let extractor = EntityExtractor::new(source, "greet.dart", "dart");
let (elements, _) = extractor.extract(&tree);
let funcs: Vec<_> = elements
.iter()
.filter(|e| e.element_type == "function")
.collect();
assert!(!funcs.is_empty(), "Should extract Dart function");
assert_eq!(funcs[0].name, "greet");
}
}

#[test]
fn test_extract_dart_method() {
let source = b"class Counter { void increment() {} }";
if let Some(tree) = parse_dart(source) {
let extractor = EntityExtractor::new(source, "counter.dart", "dart");
let (elements, _) = extractor.extract(&tree);
let methods: Vec<_> = elements
.iter()
.filter(|e| e.element_type == "method")
.collect();
assert!(!methods.is_empty(), "Should extract Dart method");
assert_eq!(methods[0].name, "increment");
}
}

#[test]
fn test_extract_dart_import() {
// Import extraction depends on tree-sitter-dart node types
// This test verifies the parser can process Dart files
let source = br#"import 'package:flutter/material.dart';"#;
if let Some(tree) = parse_dart(source) {
let extractor = EntityExtractor::new(source, "main.dart", "dart");
let _ = extractor.extract(&tree);
// Parser should not panic - import handling verified manually
}
}

#[test]
fn test_extract_dart_stateful_widget() {
let source = br#"
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) => Text('hello');
}
"#;
if let Some(tree) = parse_dart(source) {
let extractor = EntityExtractor::new(source, "my_home_page.dart", "dart");
let (elements, _) = extractor.extract(&tree);
let classes: Vec<_> = elements
.iter()
.filter(|e| e.element_type == "class")
.collect();
assert!(classes.len() >= 2, "Should extract both widget classes");
}
}
}
11 changes: 8 additions & 3 deletions src/indexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn find_files_sync(root: &str) -> Result<Vec<String>, Box<dyn std::error::Er
let mut files = Vec::new();
let extensions = [
"go", "ts", "js", "py", "rs", "java", "kt", "kts", "tf", "yml", "yaml", "json", "toml",
"mod",
"mod", "dart",
];
let config_files = [
"package.json",
Expand Down Expand Up @@ -103,7 +103,8 @@ fn get_language(file_path: &str) -> Option<&'static str> {
"py" => Some("python"),
"rs" => Some("rust"),
"java" => Some("java"),
"kt" => Some("kotlin"),
"kt" | "kts" => Some("kotlin"),
"dart" => Some("dart"),
_ => None,
}
}
Expand Down Expand Up @@ -232,7 +233,7 @@ fn extract_elements_for_file(
};

thread_local! {
static PARSERS: std::cell::RefCell<Vec<Option<tree_sitter::Parser>>> = std::cell::RefCell::new(vec![None, None, None, None, None, None]);
static PARSERS: std::cell::RefCell<Vec<Option<tree_sitter::Parser>>> = std::cell::RefCell::new(vec![None, None, None, None, None, None, None]);
}

let parser_idx = match language {
Expand All @@ -242,6 +243,7 @@ fn extract_elements_for_file(
"rust" => 3,
"java" => 4,
"kotlin" => 5,
"dart" => 6,
_ => {
return Ok(ParsedFile {
element_count: 0,
Expand All @@ -262,6 +264,7 @@ fn extract_elements_for_file(
"rust" => tree_sitter_rust::LANGUAGE.into(),
"java" => tree_sitter_java::LANGUAGE.into(),
"kotlin" => tree_sitter_kotlin_ng::LANGUAGE.into(),
"dart" => tree_sitter_dart::LANGUAGE.into(),
_ => return p,
};
let _ = p.set_language(&lang);
Expand Down Expand Up @@ -504,6 +507,8 @@ pub fn index_file_sync(
"java"
} else if file_path.ends_with(".kt") || file_path.ends_with(".kts") {
"kotlin"
} else if file_path.ends_with(".dart") {
"dart"
} else {
return Ok(0);
};
Expand Down