-
Notifications
You must be signed in to change notification settings - Fork 200
Add Dart support to codebase indexing #941
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
WebMad
wants to merge
3
commits into
Zoo-Code-Org:main
Choose a base branch
from
WebMad:feat/940-dart-codebase-indexing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+313
−3
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
src/services/tree-sitter/__tests__/fixtures/sample-dart.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| export default `abstract class Animal { | ||
| String speak(); | ||
|
|
||
| Future<String> describe({ | ||
| required bool verbose, | ||
| }); | ||
| } | ||
|
|
||
| class Point { | ||
| const Point(); | ||
| Point.named(); | ||
| factory Point.origin() => const Point(); | ||
|
|
||
| Point.fromCoordinates( | ||
| int x, | ||
| int y, | ||
| ) : assert(x >= 0), | ||
| assert(y >= 0); | ||
|
|
||
| factory Point.fromRecord( | ||
| ({int x, int y}) coordinates, | ||
| ) => Point.fromCoordinates( | ||
| coordinates.x, | ||
| coordinates.y, | ||
| ); | ||
|
|
||
| int get x => 0; | ||
| set x(int value) {} | ||
|
|
||
| Point operator +(Point other) => this; | ||
|
|
||
| Point operator [](int index) => this; | ||
|
|
||
| static List<T> emptyList<T extends Object>() { | ||
| return <T>[]; | ||
| } | ||
| } | ||
|
|
||
| mixin Runner { | ||
| void run() { | ||
| print("running"); | ||
| } | ||
| } | ||
|
|
||
| enum Status { | ||
| ready, | ||
| running; | ||
|
|
||
| const Status(); | ||
| } | ||
|
|
||
| class Dog extends Animal with Runner { | ||
| final String name; | ||
|
|
||
| Dog(this.name); | ||
|
|
||
| @override | ||
| String speak() { | ||
| return "\$name barks"; | ||
| } | ||
| } | ||
|
|
||
| extension StringTools on String { | ||
| String doubled() { | ||
| return this + this; | ||
| } | ||
| } | ||
|
|
||
| extension on int { | ||
| int squared() => this * this; | ||
| } | ||
|
|
||
| extension type UserId(int value) { | ||
| UserId.zero() : value = 0; | ||
| } | ||
|
|
||
| typedef Operation = int Function(int left, int right); | ||
|
|
||
| typedef AsyncOperation<T extends Object> = Future<T> Function( | ||
| T value, { | ||
| required Duration timeout, | ||
| }); | ||
|
|
||
| int get answer => 42; | ||
| set answer(int value) {} | ||
|
|
||
| int add(int left, int right) { | ||
| return left + right; | ||
| } | ||
|
|
||
| Future<T> retry<T extends Object>( | ||
| Future<T> Function() operation, { | ||
| int attempts = 3, | ||
| }) async { | ||
| return operation(); | ||
| } | ||
|
|
||
| Future<void> initialize() async { | ||
| await Future<void>.value(); | ||
| } | ||
|
|
||
| Iterable<int> countUpTo(int maximum) sync* { | ||
| for (var value = 0; value <= maximum; value++) { | ||
| yield value; | ||
| } | ||
| } | ||
|
|
||
| Stream<int> countPeriodically(int maximum) async* { | ||
| for (var value = 0; value <= maximum; value++) { | ||
| yield value; | ||
| } | ||
| } | ||
| ` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
src/services/tree-sitter/__tests__/parseSourceCodeDefinitions.dart.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| import { dartQuery } from "../queries" | ||
| import { testParseSourceCodeDefinitions } from "./helpers" | ||
| import sampleDartContent from "./fixtures/sample-dart" | ||
|
|
||
| const dartOptions = { | ||
| language: "dart", | ||
| wasmFile: "tree-sitter-dart.wasm", | ||
| queryString: dartQuery, | ||
| extKey: "dart", | ||
| } | ||
|
|
||
| describe("parseSourceCodeDefinitionsForFile with Dart", () => { | ||
| it("captures a redirecting factory constructor", async () => { | ||
| const content = `class Logger { | ||
| factory Logger() = ConsoleLogger; | ||
| } | ||
|
|
||
| class ConsoleLogger implements Logger {}` | ||
|
|
||
| const result = await testParseSourceCodeDefinitions("/test/file.dart", content, dartOptions) | ||
|
|
||
| expect(result).toMatch(/\d+--\d+ \|\s*factory Logger\(\) = ConsoleLogger/) | ||
| }) | ||
|
|
||
| it("captures a multiline generative constructor once", async () => { | ||
| const content = `class Point { | ||
| Point.fromCoordinates( | ||
| int x, | ||
| int y, | ||
| ); | ||
| }` | ||
|
|
||
| const result = await testParseSourceCodeDefinitions("/test/file.dart", content, dartOptions) | ||
|
|
||
| expect(result?.match(/\d+--\d+ \|\s*Point\.fromCoordinates\(/g)).toHaveLength(1) | ||
| }) | ||
|
|
||
| it("captures a mixin application class", async () => { | ||
| const content = `class Base {} | ||
| mixin Serializable {} | ||
| class SerializableBase = Base with Serializable;` | ||
|
|
||
| const result = await testParseSourceCodeDefinitions("/test/file.dart", content, dartOptions) | ||
|
|
||
| expect(result).toMatch(/\d+--\d+ \|\s*class SerializableBase = Base with Serializable/) | ||
| }) | ||
|
|
||
| it("captures external top-level functions and methods", async () => { | ||
| const content = `external int nativeVersion(); | ||
|
|
||
| class NativeApi { | ||
| external String platformName(); | ||
| }` | ||
|
|
||
| const result = await testParseSourceCodeDefinitions("/test/file.dart", content, dartOptions) | ||
|
|
||
| expect(result).toMatch(/\d+--\d+ \| external int nativeVersion\(\)/) | ||
| expect(result).toMatch(/\d+--\d+ \|\s*external String platformName\(\)/) | ||
| }) | ||
|
|
||
| it("does not report local functions as file-level definitions", async () => { | ||
| const content = `void outer() { | ||
| int inner() => 1; | ||
| print(inner()); | ||
| }` | ||
|
|
||
| const result = await testParseSourceCodeDefinitions("/test/file.dart", content, dartOptions) | ||
|
|
||
| expect(result).toMatch(/\d+--\d+ \| void outer\(\)/) | ||
| expect(result).not.toMatch(/int inner\(\)/) | ||
| }) | ||
|
|
||
| it("includes a multiline top-level function body without duplicating its declaration", async () => { | ||
| const content = `int add(int left, int right) { | ||
| final result = left + right; | ||
| return result; | ||
| }` | ||
|
|
||
| const result = await testParseSourceCodeDefinitions("/test/file.dart", content, dartOptions) | ||
| const addDefinitions = result?.split("\n").filter((line) => line.includes("int add(")) ?? [] | ||
|
|
||
| expect(addDefinitions).toEqual(["1--4 | int add(int left, int right) {"]) | ||
| }) | ||
|
|
||
| it("should capture common Dart declarations", async () => { | ||
| const result = await testParseSourceCodeDefinitions("/test/file.dart", sampleDartContent, dartOptions) | ||
| const definitionLines = result?.split("\n").filter((line) => line.includes(" | ")) ?? [] | ||
|
|
||
| expect(result).toMatch(/\d+--\d+ \| abstract class Animal/) | ||
| expect(result).toMatch(/\d+--\d+ \|\s*Future<String> describe/) | ||
| expect(result).toMatch(/\d+--\d+ \| class Point/) | ||
| expect(result).toMatch(/\d+--\d+ \|\s*const Point\(\)/) | ||
| expect(result).toMatch(/\d+--\d+ \|\s*Point\.named\(\)/) | ||
| expect(result).toMatch(/\d+--\d+ \|\s*factory Point\.origin\(\)/) | ||
| expect(result).toMatch(/\d+--\d+ \|\s*Point\.fromCoordinates\(/) | ||
| expect(result).toMatch(/\d+--\d+ \|\s*factory Point\.fromRecord\(/) | ||
| expect(result?.match(/\d+--\d+ \| Point\.fromCoordinates\(/g)).toHaveLength(1) | ||
| expect(result?.match(/\d+--\d+ \| factory Point\.fromRecord\(/g)).toHaveLength(1) | ||
| expect(result).toMatch(/\d+--\d+ \|\s*int get x/) | ||
| expect(result).toMatch(/\d+--\d+ \|\s*set x\(int value\)/) | ||
| expect(result).toMatch(/\d+--\d+ \|\s*Point operator \+/) | ||
| expect(result).toMatch(/\d+--\d+ \|\s*Point operator \[]/) | ||
| expect(result).toMatch(/\d+--\d+ \|\s*static List<T> emptyList/) | ||
| expect(result).toMatch(/\d+--\d+ \| mixin Runner/) | ||
| expect(result).toMatch(/\d+--\d+ \| enum Status/) | ||
| expect(result).toMatch(/\d+--\d+ \|\s*const Status\(\)/) | ||
| expect(result).toMatch(/\d+--\d+ \| class Dog extends Animal with Runner/) | ||
| expect(result).toMatch(/\d+--\d+ \| extension StringTools on String/) | ||
| expect(result).toMatch(/\d+--\d+ \| extension on int/) | ||
| expect(result).toMatch(/\d+--\d+ \| extension type UserId/) | ||
| expect(result).toMatch(/\d+--\d+ \| typedef Operation/) | ||
| expect(result).toMatch(/\d+--\d+ \| typedef AsyncOperation/) | ||
| expect(result).toMatch(/\d+--\d+ \| int get answer/) | ||
| expect(result).toMatch(/\d+--\d+ \| set answer\(int value\)/) | ||
| expect(result).toMatch(/\d+--\d+ \|\s*String speak\(\)/) | ||
| expect(result).toMatch(/\d+--\d+ \| int add\(int left, int right\)/) | ||
| expect(result).toMatch(/\d+--\d+ \| Future<T> retry<T extends Object>/) | ||
| expect(result).toMatch(/\d+--\d+ \| Future<void> initialize\(\) async/) | ||
| expect(result).toMatch(/\d+--\d+ \| Iterable<int> countUpTo\(int maximum\) sync\*/) | ||
| expect(result).toMatch(/\d+--\d+ \| Stream<int> countPeriodically\(int maximum\) async\*/) | ||
| expect(definitionLines).toHaveLength(37) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| // Definition captures adapted from tree-sitter-dart's canonical tags query: | ||
| // https://github.com/UserNobody14/tree-sitter-dart/blob/master/queries/tags.scm | ||
| export default ` | ||
| (class_definition | ||
| name: (identifier) @name) @definition.class | ||
|
|
||
| (class_definition | ||
| (mixin_application_class | ||
| (identifier) @name)) @definition.class | ||
|
|
||
| (type_alias | ||
| (type_identifier) @name) @definition.type | ||
|
|
||
| (declaration | ||
| (function_signature | ||
| name: (identifier) @name)) @definition.method | ||
|
|
||
| (redirecting_factory_constructor_signature | ||
| (identifier) @name) @definition.method | ||
|
|
||
| (method_signature) @definition.method | ||
|
|
||
| (constructor_signature | ||
| name: (identifier) @name) @definition.method | ||
|
|
||
| (constant_constructor_signature | ||
| (identifier) @name) @definition.method | ||
|
|
||
| (mixin_declaration | ||
| (mixin) | ||
| (identifier) @name) @definition.mixin | ||
|
|
||
| (extension_declaration | ||
| name: (identifier) @name) @definition.extension | ||
|
|
||
| (extension_type_declaration | ||
| name: (identifier) @name) @definition.extension | ||
|
|
||
| (enum_declaration | ||
| name: (identifier) @name) @definition.enum | ||
|
|
||
| (program | ||
| (getter_signature | ||
| name: (identifier) @name) @definition.function) | ||
|
|
||
| (program | ||
| (setter_signature | ||
| name: (identifier) @name) @definition.function) | ||
|
|
||
| (program | ||
| (function_signature | ||
| name: (identifier) @name) @definition.function) | ||
| ` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.