Skip to content

Commit 8b19e9b

Browse files
committed
fix(tree-sitter): address Dart query review feedback
1 parent 5f71794 commit 8b19e9b

4 files changed

Lines changed: 163 additions & 43 deletions

File tree

‎src/services/tree-sitter/__tests__/fixtures/sample-dart.ts‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,39 @@
11
export default `abstract class Animal {
22
String speak();
3+
4+
Future<String> describe({
5+
required bool verbose,
6+
});
37
}
48
59
class Point {
610
const Point();
711
Point.named();
812
factory Point.origin() => const Point();
913
14+
Point.fromCoordinates(
15+
int x,
16+
int y,
17+
) : assert(x >= 0),
18+
assert(y >= 0);
19+
20+
factory Point.fromRecord(
21+
({int x, int y}) coordinates,
22+
) => Point.fromCoordinates(
23+
coordinates.x,
24+
coordinates.y,
25+
);
26+
1027
int get x => 0;
1128
set x(int value) {}
1229
1330
Point operator +(Point other) => this;
31+
32+
Point operator [](int index) => this;
33+
34+
static List<T> emptyList<T extends Object>() {
35+
return <T>[];
36+
}
1437
}
1538
1639
mixin Runner {
@@ -43,16 +66,48 @@ extension StringTools on String {
4366
}
4467
}
4568
69+
extension on int {
70+
int squared() => this * this;
71+
}
72+
4673
extension type UserId(int value) {
4774
UserId.zero() : value = 0;
4875
}
4976
5077
typedef Operation = int Function(int left, int right);
5178
79+
typedef AsyncOperation<T extends Object> = Future<T> Function(
80+
T value, {
81+
required Duration timeout,
82+
});
83+
5284
int get answer => 42;
5385
set answer(int value) {}
5486
5587
int add(int left, int right) {
5688
return left + right;
5789
}
90+
91+
Future<T> retry<T extends Object>(
92+
Future<T> Function() operation, {
93+
int attempts = 3,
94+
}) async {
95+
return operation();
96+
}
97+
98+
Future<void> initialize() async {
99+
await Future<void>.value();
100+
}
101+
102+
Iterable<int> countUpTo(int maximum) sync* {
103+
for (var value = 0; value <= maximum; value++) {
104+
yield value;
105+
}
106+
}
107+
108+
Stream<int> countPeriodically(int maximum) async* {
109+
for (var value = 0; value <= maximum; value++) {
110+
yield value;
111+
}
112+
}
58113
`

‎src/services/tree-sitter/__tests__/parseSourceCodeDefinitions.dart.spec.ts‎

Lines changed: 94 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,102 @@ const dartOptions = {
1010
}
1111

1212
describe("parseSourceCodeDefinitionsForFile with Dart", () => {
13+
it("captures a redirecting factory constructor", async () => {
14+
const content = `class Logger {
15+
factory Logger() = ConsoleLogger;
16+
}
17+
18+
class ConsoleLogger implements Logger {}`
19+
20+
const result = await testParseSourceCodeDefinitions("/test/file.dart", content, dartOptions)
21+
22+
expect(result).toMatch(/\d+--\d+ \|\s*factory Logger\(\) = ConsoleLogger/)
23+
})
24+
25+
it("captures a multiline generative constructor once", async () => {
26+
const content = `class Point {
27+
Point.fromCoordinates(
28+
int x,
29+
int y,
30+
);
31+
}`
32+
33+
const result = await testParseSourceCodeDefinitions("/test/file.dart", content, dartOptions)
34+
35+
expect(result?.match(/\d+--\d+ \|\s*Point\.fromCoordinates\(/g)).toHaveLength(1)
36+
})
37+
38+
it("captures a mixin application class", async () => {
39+
const content = `class Base {}
40+
mixin Serializable {}
41+
class SerializableBase = Base with Serializable;`
42+
43+
const result = await testParseSourceCodeDefinitions("/test/file.dart", content, dartOptions)
44+
45+
expect(result).toMatch(/\d+--\d+ \|\s*class SerializableBase = Base with Serializable/)
46+
})
47+
48+
it("captures external top-level functions and methods", async () => {
49+
const content = `external int nativeVersion();
50+
51+
class NativeApi {
52+
external String platformName();
53+
}`
54+
55+
const result = await testParseSourceCodeDefinitions("/test/file.dart", content, dartOptions)
56+
57+
expect(result).toMatch(/\d+--\d+ \| external int nativeVersion\(\)/)
58+
expect(result).toMatch(/\d+--\d+ \|\s*external String platformName\(\)/)
59+
})
60+
61+
it("does not report local functions as file-level definitions", async () => {
62+
const content = `void outer() {
63+
int inner() => 1;
64+
print(inner());
65+
}`
66+
67+
const result = await testParseSourceCodeDefinitions("/test/file.dart", content, dartOptions)
68+
69+
expect(result).toMatch(/\d+--\d+ \| void outer\(\)/)
70+
expect(result).not.toMatch(/int inner\(\)/)
71+
})
72+
1373
it("should capture common Dart declarations", async () => {
1474
const result = await testParseSourceCodeDefinitions("/test/file.dart", sampleDartContent, dartOptions)
75+
const definitionLines = result?.split("\n").filter((line) => line.includes(" | ")) ?? []
1576

16-
expect(result).toMatch(/abstract class Animal/)
17-
expect(result).toMatch(/class Point/)
18-
expect(result).toMatch(/const Point\(\)/)
19-
expect(result).toMatch(/Point\.named\(\)/)
20-
expect(result).toMatch(/factory Point\.origin\(\)/)
21-
expect(result).toMatch(/int get x/)
22-
expect(result).toMatch(/set x\(int value\)/)
23-
expect(result).toMatch(/Point operator \+/)
24-
expect(result).toMatch(/mixin Runner/)
25-
expect(result).toMatch(/enum Status/)
26-
expect(result).toMatch(/const Status\(\)/)
27-
expect(result).toMatch(/class Dog extends Animal with Runner/)
28-
expect(result).toMatch(/extension StringTools on String/)
29-
expect(result).toMatch(/extension type UserId/)
30-
expect(result).toMatch(/typedef Operation/)
31-
expect(result).toMatch(/int get answer/)
32-
expect(result).toMatch(/set answer\(int value\)/)
33-
expect(result).toMatch(/String speak\(\)/)
34-
expect(result).toMatch(/int add\(int left, int right\)/)
77+
expect(result).toMatch(/\d+--\d+ \| abstract class Animal/)
78+
expect(result).toMatch(/\d+--\d+ \|\s*Future<String> describe/)
79+
expect(result).toMatch(/\d+--\d+ \| class Point/)
80+
expect(result).toMatch(/\d+--\d+ \|\s*const Point\(\)/)
81+
expect(result).toMatch(/\d+--\d+ \|\s*Point\.named\(\)/)
82+
expect(result).toMatch(/\d+--\d+ \|\s*factory Point\.origin\(\)/)
83+
expect(result).toMatch(/\d+--\d+ \|\s*Point\.fromCoordinates\(/)
84+
expect(result).toMatch(/\d+--\d+ \|\s*factory Point\.fromRecord\(/)
85+
expect(result?.match(/\d+--\d+ \| Point\.fromCoordinates\(/g)).toHaveLength(1)
86+
expect(result?.match(/\d+--\d+ \| factory Point\.fromRecord\(/g)).toHaveLength(1)
87+
expect(result).toMatch(/\d+--\d+ \|\s*int get x/)
88+
expect(result).toMatch(/\d+--\d+ \|\s*set x\(int value\)/)
89+
expect(result).toMatch(/\d+--\d+ \|\s*Point operator \+/)
90+
expect(result).toMatch(/\d+--\d+ \|\s*Point operator \[]/)
91+
expect(result).toMatch(/\d+--\d+ \|\s*static List<T> emptyList/)
92+
expect(result).toMatch(/\d+--\d+ \| mixin Runner/)
93+
expect(result).toMatch(/\d+--\d+ \| enum Status/)
94+
expect(result).toMatch(/\d+--\d+ \|\s*const Status\(\)/)
95+
expect(result).toMatch(/\d+--\d+ \| class Dog extends Animal with Runner/)
96+
expect(result).toMatch(/\d+--\d+ \| extension StringTools on String/)
97+
expect(result).toMatch(/\d+--\d+ \| extension on int/)
98+
expect(result).toMatch(/\d+--\d+ \| extension type UserId/)
99+
expect(result).toMatch(/\d+--\d+ \| typedef Operation/)
100+
expect(result).toMatch(/\d+--\d+ \| typedef AsyncOperation/)
101+
expect(result).toMatch(/\d+--\d+ \| int get answer/)
102+
expect(result).toMatch(/\d+--\d+ \| set answer\(int value\)/)
103+
expect(result).toMatch(/\d+--\d+ \|\s*String speak\(\)/)
104+
expect(result).toMatch(/\d+--\d+ \| int add\(int left, int right\)/)
105+
expect(result).toMatch(/\d+--\d+ \| Future<T> retry<T extends Object>/)
106+
expect(result).toMatch(/\d+--\d+ \| Future<void> initialize\(\) async/)
107+
expect(result).toMatch(/\d+--\d+ \| Iterable<int> countUpTo\(int maximum\) sync\*/)
108+
expect(result).toMatch(/\d+--\d+ \| Stream<int> countPeriodically\(int maximum\) async\*/)
109+
expect(definitionLines).toHaveLength(37)
35110
})
36111
})

‎src/services/tree-sitter/index.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,11 @@ function processCaptures(captures: QueryCapture[], lines: string[], language: st
272272
if (node.parent && node.parent.lastChild) {
273273
const contextEnd = node.parent.lastChild.endPosition.row
274274
const contextSpan = contextEnd - node.parent.startPosition.row + 1
275+
const hasDistinctContextStart = node.parent.startPosition.row !== startLine
275276

276-
// Only include context if it spans multiple lines
277-
if (contextSpan >= getMinComponentLines()) {
277+
// Only include context when it adds a distinct source line. A parent
278+
// starting on the definition line would echo the same declaration.
279+
if (hasDistinctContextStart && contextSpan >= getMinComponentLines()) {
278280
// Add the full range first
279281
const rangeKey = `${node.parent.startPosition.row}-${contextEnd}`
280282
if (!processedLines.has(rangeKey)) {

‎src/services/tree-sitter/queries/dart.ts‎

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,21 @@ export default `
44
(class_definition
55
name: (identifier) @name) @definition.class
66
7-
(method_signature
8-
(function_signature)) @definition.method
7+
(class_definition
8+
(mixin_application_class
9+
(identifier) @name)) @definition.class
910
1011
(type_alias
1112
(type_identifier) @name) @definition.type
1213
13-
(method_signature
14-
(getter_signature
15-
name: (identifier) @name)) @definition.method
16-
17-
(method_signature
18-
(setter_signature
19-
name: (identifier) @name)) @definition.method
20-
21-
(method_signature
14+
(declaration
2215
(function_signature
2316
name: (identifier) @name)) @definition.method
2417
25-
(method_signature
26-
(factory_constructor_signature
27-
(identifier) @name)) @definition.method
28-
29-
(method_signature
30-
(constructor_signature
31-
name: (identifier) @name)) @definition.method
18+
(redirecting_factory_constructor_signature
19+
(identifier) @name) @definition.method
3220
33-
(method_signature
34-
(operator_signature)) @definition.method
21+
(method_signature) @definition.method
3522
3623
(constructor_signature
3724
name: (identifier) @name) @definition.method
@@ -60,6 +47,7 @@ export default `
6047
(setter_signature
6148
name: (identifier) @name) @definition.function)
6249
63-
(function_signature
64-
name: (identifier) @name) @definition.function
50+
(program
51+
(function_signature
52+
name: (identifier) @name) @definition.function)
6553
`

0 commit comments

Comments
 (0)