Merge pull request #2844 from continuedev/tomasz/context-definitions
Set up root path context tests, add tree sitter definitions for typescript return types
This commit is contained in:
commit
17d3dca175
9 changed files with 138 additions and 69 deletions
|
|
@ -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<AutocompleteSnippet[]> {
|
||||
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,
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
},
|
||||
);
|
||||
});
|
||||
});
|
||||
|
|
@ -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"],
|
||||
);
|
||||
});
|
||||
});
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +0,0 @@
|
|||
export interface Person {
|
||||
firstName: string;
|
||||
lastName: string;
|
||||
age: number;
|
||||
address: Address;
|
||||
}
|
||||
|
||||
export interface Address {
|
||||
street: string;
|
||||
city: string;
|
||||
}
|
||||
|
|
@ -139,7 +139,7 @@ export enum TSQueryType {
|
|||
|
||||
export async function getQueryForFile(
|
||||
filepath: string,
|
||||
queryType: TSQueryType,
|
||||
queryPath: string,
|
||||
): Promise<Parser.Query | undefined> {
|
||||
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)) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,19 @@
|
|||
(
|
||||
(class_declaration
|
||||
(class_heritage
|
||||
(extends_clause
|
||||
(identifier) @base-class
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
(
|
||||
(class_declaration
|
||||
(class_heritage
|
||||
(implements_clause
|
||||
(type_identifier) @interface
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
|
@ -2,8 +2,9 @@
|
|||
(function_declaration
|
||||
(formal_parameters
|
||||
(_
|
||||
(type_annotation) @type
|
||||
(type_annotation) @param_type
|
||||
)
|
||||
)
|
||||
(type_annotation) @return_type
|
||||
)
|
||||
)
|
||||
|
|
@ -2,8 +2,9 @@
|
|||
(method_definition
|
||||
(formal_parameters
|
||||
(_
|
||||
(type_annotation) @type
|
||||
(type_annotation) @param_type
|
||||
)
|
||||
)
|
||||
(type_annotation) @return_type
|
||||
)
|
||||
)
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue