diff --git a/core/autocomplete/context/root-path-context/RootPathContextService.ts b/core/autocomplete/context/root-path-context/RootPathContextService.ts index ba99b2e83..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", ]); /** @@ -52,23 +53,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 +67,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..597d37d12 --- /dev/null +++ b/core/autocomplete/context/root-path-context/test/RootPathContextService.test.ts @@ -0,0 +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.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/RootPathContextService.test.ts b/core/autocomplete/context/root-path-context/test/testUtils.ts similarity index 68% rename from core/autocomplete/context/root-path-context/RootPathContextService.test.ts rename to core/autocomplete/context/root-path-context/test/testUtils.ts index ccce0b6ac..f4e79e968 100644 --- a/core/autocomplete/context/root-path-context/RootPathContextService.test.ts +++ b/core/autocomplete/context/root-path-context/test/testUtils.ts @@ -1,10 +1,13 @@ 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"; +import Parser from "web-tree-sitter"; function splitTextAtRange(fileContent: string, range: Range): [string, string] { const lines = fileContent.split("\n"); @@ -40,12 +43,21 @@ 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); + 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( __dirname, @@ -75,23 +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); - expectedSnippets.forEach((expectedSnippet) => { - const found = snippets.find((snippet) => - snippet.contents.includes(expectedSnippet), + expect(getSnippetsMock).toHaveBeenCalledTimes( + expectedDefinitionPositions.length, + ); + + expectedDefinitionPositions.forEach((position, index) => { + expect(getSnippetsMock).toHaveBeenNthCalledWith( + index + 1, + expect.any(String), // filepath argument + position, ); - expect(found).toBeDefined(); }); } - -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..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,25 @@ -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; } + + getPersonAddress(person: Person): Address { + return getAddress(person); + } } 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/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/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 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 +)