From df2bbc89da7f7f9ee406a8fe6d0c4fd244abd488 Mon Sep 17 00:00:00 2001 From: tomasz-io Date: Mon, 11 Nov 2024 12:25:59 -0800 Subject: [PATCH] feat: capture base class and interfaces implemented --- .../RootPathContextService.ts | 1 + .../test/RootPathContextService.test.ts | 59 +++++++++++-------- .../test/typescript/file1.ts | 12 +++- .../test/typescript/types.ts | 11 ---- .../class_declaration/typescript.scm | 19 ++++++ 5 files changed, 66 insertions(+), 36 deletions(-) delete mode 100644 core/autocomplete/context/root-path-context/test/typescript/types.ts create mode 100644 extensions/vscode/tree-sitter/root-path-context-queries/class_declaration/typescript.scm diff --git a/core/autocomplete/context/root-path-context/RootPathContextService.ts b/core/autocomplete/context/root-path-context/RootPathContextService.ts index cc8ca0636..a9ca7fe2d 100644 --- a/core/autocomplete/context/root-path-context/RootPathContextService.ts +++ b/core/autocomplete/context/root-path-context/RootPathContextService.ts @@ -25,6 +25,7 @@ export class RootPathContextService { "program", "function_declaration", "method_definition", + "class_declaration", ]); /** diff --git a/core/autocomplete/context/root-path-context/test/RootPathContextService.test.ts b/core/autocomplete/context/root-path-context/test/RootPathContextService.test.ts index c7ede49f8..597d37d12 100644 --- a/core/autocomplete/context/root-path-context/test/RootPathContextService.test.ts +++ b/core/autocomplete/context/root-path-context/test/RootPathContextService.test.ts @@ -1,29 +1,42 @@ import { testRootPathContext } from "./testUtils"; +const TEST_CASES = [ + { + description: "function", + fileName: "file1.ts", + range: { + start: { line: 10, character: 2 }, + end: { line: 10, character: 24 }, + }, + positions: [ + { row: 9, column: 34 }, // Person + { row: 9, column: 44 }, // Address + ], + }, + { + description: "class method", + fileName: "file1.ts", + range: { + start: { line: 22, character: 4 }, + end: { line: 22, character: 30 }, + }, + positions: [ + { row: 13, column: 29 }, // BaseClass + { row: 13, column: 55 }, // FirstInterface + { row: 13, column: 72 }, // SecondInterface + { row: 21, column: 33 }, // Person + { row: 21, column: 43 }, // Address + ], + }, +]; + describe("RootPathContextService", () => { describe("TypeScript should return expected snippets when editing inside a:", () => { - test("function", async () => { - await testRootPathContext( - "typescript", - "file1.ts", - { start: { line: 3, character: 2 }, end: { line: 3, character: 24 } }, - [ - { row: 2, column: 34 }, - { row: 2, column: 44 }, - ], - ); - }); - - test("class method", async () => { - await testRootPathContext( - "typescript", - "file1.ts", - { start: { line: 14, character: 4 }, end: { line: 14, character: 30 } }, - [ - { row: 13, column: 33 }, - { row: 13, column: 43 }, - ], - ); - }); + test.each(TEST_CASES)( + "should look for correct type definitions when editing inside a $description", + async ({ fileName, range, positions }) => { + await testRootPathContext("typescript", fileName, range, positions); + }, + ); }); }); diff --git a/core/autocomplete/context/root-path-context/test/typescript/file1.ts b/core/autocomplete/context/root-path-context/test/typescript/file1.ts index b1e0cf6ce..ba09c5709 100644 --- a/core/autocomplete/context/root-path-context/test/typescript/file1.ts +++ b/core/autocomplete/context/root-path-context/test/typescript/file1.ts @@ -1,13 +1,21 @@ -import { Address, Person } from "./types"; +import { + Address, + Person, + BaseClass, + FirstInterface, + SecondInterface, + // @ts-ignore +} from "./types"; function getAddress(person: Person): Address { return person.address; } -class Group { +class Group extends BaseClass implements FirstInterface, SecondInterface { people: Person[]; constructor(people: Person[]) { + super(); this.people = people; } diff --git a/core/autocomplete/context/root-path-context/test/typescript/types.ts b/core/autocomplete/context/root-path-context/test/typescript/types.ts deleted file mode 100644 index 272ec27d7..000000000 --- a/core/autocomplete/context/root-path-context/test/typescript/types.ts +++ /dev/null @@ -1,11 +0,0 @@ -export interface Person { - firstName: string; - lastName: string; - age: number; - address: Address; -} - -export interface Address { - street: string; - city: string; -} diff --git a/extensions/vscode/tree-sitter/root-path-context-queries/class_declaration/typescript.scm b/extensions/vscode/tree-sitter/root-path-context-queries/class_declaration/typescript.scm new file mode 100644 index 000000000..bf38152b7 --- /dev/null +++ b/extensions/vscode/tree-sitter/root-path-context-queries/class_declaration/typescript.scm @@ -0,0 +1,19 @@ +( + (class_declaration + (class_heritage + (extends_clause + (identifier) @base-class + ) + ) + ) +) + +( + (class_declaration + (class_heritage + (implements_clause + (type_identifier) @interface + ) + ) + ) +) \ No newline at end of file