From 37a50d674a233a0036d2a935170b52b6f58830a0 Mon Sep 17 00:00:00 2001 From: tomasz-io Date: Fri, 8 Nov 2024 11:17:43 +0700 Subject: [PATCH 1/3] improv: capture typescript return types, update root path context tests --- .../RootPathContextService.ts | 56 +++++++++---------- .../test/RootPathContextService.test.ts | 23 ++++++++ .../testUtils.ts} | 52 +++++++++-------- .../test/typescript/file1.ts | 4 ++ core/util/treeSitter.ts | 4 +- .../function_declaration/typescript.scm | 3 +- .../method_definition/typescript.scm | 5 +- 7 files changed, 91 insertions(+), 56 deletions(-) create mode 100644 core/autocomplete/context/root-path-context/test/RootPathContextService.test.ts rename core/autocomplete/context/root-path-context/{RootPathContextService.test.ts => test/testUtils.ts} (70%) diff --git a/core/autocomplete/context/root-path-context/RootPathContextService.ts b/core/autocomplete/context/root-path-context/RootPathContextService.ts index ba99b2e83..cc8ca0636 100644 --- a/core/autocomplete/context/root-path-context/RootPathContextService.ts +++ b/core/autocomplete/context/root-path-context/RootPathContextService.ts @@ -52,23 +52,12 @@ export class RootPathContextService { case "program": this.importDefinitionsService.get(filepath); break; - case "function_declaration": + default: query = await getQueryForFile( filepath, - TSQueryType.FunctionDeclaration, + `root-path-context-queries/${node.type}`, ); break; - case "method_definition": - query = await getQueryForFile(filepath, TSQueryType.MethodDefinition); - break; - case "function_definition": - query = await getQueryForFile(filepath, TSQueryType.FunctionDefinition); - break; - case "method_declaration": - query = await getQueryForFile(filepath, TSQueryType.MethodDeclaration); - break; - default: - break; } if (!query) { @@ -77,28 +66,37 @@ export class RootPathContextService { await Promise.all( query.matches(node).map(async (match) => { - const startPosition = match.captures[0].node.startPosition; - const endPosition = match.captures[0].node.endPosition; - const definitions = await this.ide.gotoDefinition({ - filepath, - position: { - line: endPosition.row, - character: endPosition.column, - }, - }); - const newSnippets = await Promise.all( - definitions.map(async (def) => ({ - ...def, - contents: await this.ide.readRangeInFile(def.filepath, def.range), - })), - ); - snippets.push(...newSnippets); + for (const item of match.captures) { + const endPosition = item.node.endPosition; + const newSnippets = await this.getSnippets(filepath, endPosition); + snippets.push(...newSnippets); + } }), ); return snippets; } + private async getSnippets( + filepath: string, + endPosition: Parser.Point, + ): Promise { + const definitions = await this.ide.gotoDefinition({ + filepath, + position: { + line: endPosition.row, + character: endPosition.column, + }, + }); + const newSnippets = await Promise.all( + definitions.map(async (def) => ({ + ...def, + contents: await this.ide.readRangeInFile(def.filepath, def.range), + })), + ); + return newSnippets; + } + async getContextForPath( filepath: string, astPath: AstPath, diff --git a/core/autocomplete/context/root-path-context/test/RootPathContextService.test.ts b/core/autocomplete/context/root-path-context/test/RootPathContextService.test.ts new file mode 100644 index 000000000..ac52727b2 --- /dev/null +++ b/core/autocomplete/context/root-path-context/test/RootPathContextService.test.ts @@ -0,0 +1,23 @@ +import { testRootPathContext } from "./testUtils"; + +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 } }, + ["EXPECTED SNIPPET 1", "EXPECTED SNIPPET 2"], + ); + }); + + test("class method", async () => { + await testRootPathContext( + "typescript", + "file1.ts", + { start: { line: 14, character: 4 }, end: { line: 14, character: 30 } }, + ["EXPECTED SNIPPET 1", "EXPECTED SNIPPET 2"], + ); + }); + }); +}); diff --git a/core/autocomplete/context/root-path-context/RootPathContextService.test.ts b/core/autocomplete/context/root-path-context/test/testUtils.ts similarity index 70% rename from core/autocomplete/context/root-path-context/RootPathContextService.test.ts rename to core/autocomplete/context/root-path-context/test/testUtils.ts index ccce0b6ac..5a3a51a5a 100644 --- a/core/autocomplete/context/root-path-context/RootPathContextService.test.ts +++ b/core/autocomplete/context/root-path-context/test/testUtils.ts @@ -1,10 +1,17 @@ import fs from "fs"; import path from "path"; -import { Range } from "../../.."; -import { testIde } from "../../../test/util/fixtures"; -import { getAst, getTreePathAtCursor } from "../../util/ast"; -import { ImportDefinitionsService } from "../ImportDefinitionsService"; -import { RootPathContextService } from "./RootPathContextService"; +import { jest } from "@jest/globals"; + +import { Range } from "../../../.."; +import { testIde } from "../../../../test/util/fixtures"; +import { getAst, getTreePathAtCursor } from "../../../util/ast"; +import { ImportDefinitionsService } from "../../ImportDefinitionsService"; +import { RootPathContextService } from "../RootPathContextService"; + +const MOCK_HARDCODED_RANGE = { + start: { line: 0, character: 0 }, + end: { line: 0, character: 0 }, +}; function splitTextAtRange(fileContent: string, range: Range): [string, string] { const lines = fileContent.split("\n"); @@ -42,10 +49,27 @@ export async function testRootPathContext( rangeToFill: Range, expectedSnippets: string[], ) { + // Create a mocked instance of RootPathContextService const ide = testIde; const importDefinitionsService = new ImportDefinitionsService(ide); const service = new RootPathContextService(importDefinitionsService, ide); + expectedSnippets.forEach((snippet) => { + jest + // @ts-ignore + .spyOn(service, "getSnippets") + // @ts-ignore + .mockImplementationOnce(async (_filepath, _endPosition) => { + return [ + { + range: MOCK_HARDCODED_RANGE, + filepath: "NOT USED", + contents: snippet, + }, + ]; + }); + }); + // Copy the folder to the test directory const folderPath = path.join( __dirname, @@ -77,21 +101,5 @@ export async function testRootPathContext( const treePath = await getTreePathAtCursor(ast, prefix.length); const snippets = await service.getContextForPath(startPath, treePath); - expectedSnippets.forEach((expectedSnippet) => { - const found = snippets.find((snippet) => - snippet.contents.includes(expectedSnippet), - ); - expect(found).toBeDefined(); - }); + expect(snippets.map((snippet) => snippet.contents)).toEqual(expectedSnippets); } - -describe("RootPathContextService", () => { - it("should be true", async () => { - await testRootPathContext( - "typescript", - "file1.ts", - { start: { line: 3, character: 2 }, end: { line: 3, character: 24 } }, - ["export interface Person", "export interface Address"], - ); - }); -}); 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 c67ae91e2..b1e0cf6ce 100644 --- a/core/autocomplete/context/root-path-context/test/typescript/file1.ts +++ b/core/autocomplete/context/root-path-context/test/typescript/file1.ts @@ -10,4 +10,8 @@ class Group { constructor(people: Person[]) { this.people = people; } + + getPersonAddress(person: Person): Address { + return getAddress(person); + } } diff --git a/core/util/treeSitter.ts b/core/util/treeSitter.ts index bf6891f23..97fd32b66 100644 --- a/core/util/treeSitter.ts +++ b/core/util/treeSitter.ts @@ -139,7 +139,7 @@ export enum TSQueryType { export async function getQueryForFile( filepath: string, - queryType: TSQueryType, + queryPath: string, ): Promise { const language = await getLanguageForFile(filepath); if (!language) { @@ -153,7 +153,7 @@ export async function getQueryForFile( ...(process.env.NODE_ENV === "test" ? ["extensions", "vscode", "tree-sitter"] : ["tree-sitter"]), - queryType, + queryPath, `${fullLangName}.scm`, ); if (!fs.existsSync(sourcePath)) { diff --git a/extensions/vscode/tree-sitter/root-path-context-queries/function_declaration/typescript.scm b/extensions/vscode/tree-sitter/root-path-context-queries/function_declaration/typescript.scm index c0d936f5e..ab10968a3 100644 --- a/extensions/vscode/tree-sitter/root-path-context-queries/function_declaration/typescript.scm +++ b/extensions/vscode/tree-sitter/root-path-context-queries/function_declaration/typescript.scm @@ -2,8 +2,9 @@ (function_declaration (formal_parameters (_ - (type_annotation) @type + (type_annotation) @param_type ) ) + (type_annotation) @return_type ) ) \ No newline at end of file diff --git a/extensions/vscode/tree-sitter/root-path-context-queries/method_definition/typescript.scm b/extensions/vscode/tree-sitter/root-path-context-queries/method_definition/typescript.scm index 10ee28346..727db5090 100644 --- a/extensions/vscode/tree-sitter/root-path-context-queries/method_definition/typescript.scm +++ b/extensions/vscode/tree-sitter/root-path-context-queries/method_definition/typescript.scm @@ -2,8 +2,9 @@ (method_definition (formal_parameters (_ - (type_annotation) @type + (type_annotation) @param_type ) ) + (type_annotation) @return_type ) -) \ No newline at end of file +) From da627974155c688358b38653b855c5daf89de6d9 Mon Sep 17 00:00:00 2001 From: tomasz-io Date: Mon, 11 Nov 2024 12:02:46 -0800 Subject: [PATCH 2/3] improv: test that correct range is used for definition call --- .../test/RootPathContextService.test.ts | 10 ++++- .../root-path-context/test/testUtils.ts | 44 +++++++++---------- 2 files changed, 29 insertions(+), 25 deletions(-) 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 ac52727b2..c7ede49f8 100644 --- a/core/autocomplete/context/root-path-context/test/RootPathContextService.test.ts +++ b/core/autocomplete/context/root-path-context/test/RootPathContextService.test.ts @@ -7,7 +7,10 @@ describe("RootPathContextService", () => { "typescript", "file1.ts", { start: { line: 3, character: 2 }, end: { line: 3, character: 24 } }, - ["EXPECTED SNIPPET 1", "EXPECTED SNIPPET 2"], + [ + { row: 2, column: 34 }, + { row: 2, column: 44 }, + ], ); }); @@ -16,7 +19,10 @@ describe("RootPathContextService", () => { "typescript", "file1.ts", { start: { line: 14, character: 4 }, end: { line: 14, character: 30 } }, - ["EXPECTED SNIPPET 1", "EXPECTED SNIPPET 2"], + [ + { row: 13, column: 33 }, + { row: 13, column: 43 }, + ], ); }); }); diff --git a/core/autocomplete/context/root-path-context/test/testUtils.ts b/core/autocomplete/context/root-path-context/test/testUtils.ts index 5a3a51a5a..f4e79e968 100644 --- a/core/autocomplete/context/root-path-context/test/testUtils.ts +++ b/core/autocomplete/context/root-path-context/test/testUtils.ts @@ -7,11 +7,7 @@ import { testIde } from "../../../../test/util/fixtures"; import { getAst, getTreePathAtCursor } from "../../../util/ast"; import { ImportDefinitionsService } from "../../ImportDefinitionsService"; import { RootPathContextService } from "../RootPathContextService"; - -const MOCK_HARDCODED_RANGE = { - start: { line: 0, character: 0 }, - end: { line: 0, character: 0 }, -}; +import Parser from "web-tree-sitter"; function splitTextAtRange(fileContent: string, range: Range): [string, string] { const lines = fileContent.split("\n"); @@ -47,28 +43,20 @@ export async function testRootPathContext( folderName: string, relativeFilepath: string, rangeToFill: Range, - expectedSnippets: string[], + expectedDefinitionPositions: Parser.Point[], ) { // Create a mocked instance of RootPathContextService const ide = testIde; const importDefinitionsService = new ImportDefinitionsService(ide); const service = new RootPathContextService(importDefinitionsService, ide); - expectedSnippets.forEach((snippet) => { - jest - // @ts-ignore - .spyOn(service, "getSnippets") - // @ts-ignore - .mockImplementationOnce(async (_filepath, _endPosition) => { - return [ - { - range: MOCK_HARDCODED_RANGE, - filepath: "NOT USED", - contents: snippet, - }, - ]; - }); - }); + const getSnippetsMock = jest + // @ts-ignore + .spyOn(service, "getSnippets") + // @ts-ignore + .mockImplementation(async (_filepath, _endPosition) => { + return []; + }); // Copy the folder to the test directory const folderPath = path.join( @@ -99,7 +87,17 @@ export async function testRootPathContext( } const treePath = await getTreePathAtCursor(ast, prefix.length); - const snippets = await service.getContextForPath(startPath, treePath); + await service.getContextForPath(startPath, treePath); - expect(snippets.map((snippet) => snippet.contents)).toEqual(expectedSnippets); + expect(getSnippetsMock).toHaveBeenCalledTimes( + expectedDefinitionPositions.length, + ); + + expectedDefinitionPositions.forEach((position, index) => { + expect(getSnippetsMock).toHaveBeenNthCalledWith( + index + 1, + expect.any(String), // filepath argument + position, + ); + }); } From df2bbc89da7f7f9ee406a8fe6d0c4fd244abd488 Mon Sep 17 00:00:00 2001 From: tomasz-io Date: Mon, 11 Nov 2024 12:25:59 -0800 Subject: [PATCH 3/3] 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