Merge pull request #2957 from continuedev/tomasz/even-more-ts-definitions

TypeScript generics definitions
This commit is contained in:
Nate Sesti 2024-11-15 16:16:22 -08:00 • committed by GitHub
commit 5d86b03a12
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 485 additions and 101 deletions

View file

@ -4,7 +4,12 @@ import { LRUCache } from "lru-cache";
import Parser from "web-tree-sitter";
import { IDE } from "../../..";
import { getFullLanguageName, getQueryForFile } from "../../../util/treeSitter";
import {
getFullLanguageName,
getQueryForFile,
IGNORE_PATH_PATTERNS,
LanguageName,
} from "../../../util/treeSitter";
import { AstPath } from "../../util/ast";
import { ImportDefinitionsService } from "../ImportDefinitionsService";
import { AutocompleteSnippet } from "../ranking";
@ -40,6 +45,7 @@ export class RootPathContextService {
private static TYPES_TO_USE = new Set([
"arrow_function",
"generator_function_declaration",
"program",
"function_declaration",
"function_definition",
@ -68,6 +74,7 @@ export class RootPathContextService {
node: Parser.SyntaxNode,
): Promise<AutocompleteSnippet[]> {
const snippets: AutocompleteSnippet[] = [];
const language = getFullLanguageName(filepath);
let query: Parser.Query | undefined;
switch (node.type) {
@ -76,9 +83,8 @@ export class RootPathContextService {
break;
default:
// const type = node.type;
// debugger;
// console.log(getSyntaxTreeString(node));
const language = getFullLanguageName(filepath);
// debugger;
query = await getQueryForFile(
filepath,
@ -86,7 +92,6 @@ export class RootPathContextService {
);
break;
}
const type = node.type;
if (!query) {
return snippets;
@ -96,7 +101,11 @@ export class RootPathContextService {
for (const item of match.captures) {
try {
const endPosition = item.node.endPosition;
const newSnippets = await this.getSnippets(filepath, endPosition);
const newSnippets = await this.getSnippets(
filepath,
endPosition,
language,
);
snippets.push(...newSnippets);
} catch (e) {
debugger;
@ -113,6 +122,7 @@ export class RootPathContextService {
private async getSnippets(
filepath: string,
endPosition: Parser.Point,
language: LanguageName,
): Promise<AutocompleteSnippet[]> {
const definitions = await this.ide.gotoDefinition({
filepath,
@ -122,10 +132,18 @@ export class RootPathContextService {
},
});
const newSnippets = await Promise.all(
definitions.map(async (def) => ({
...def,
contents: await this.ide.readRangeInFile(def.filepath, def.range),
})),
definitions
.filter((definition) => {
const isIgnoredPath = IGNORE_PATH_PATTERNS[language]?.some(
(pattern) => pattern.test(definition.filepath),
);
return !isIgnoredPath;
})
.map(async (def) => ({
...def,
contents: await this.ide.readRangeInFile(def.filepath, def.range),
})),
);
return newSnippets;
@ -157,7 +175,7 @@ export class RootPathContextService {
parentKey = key;
}
debugger;
return snippets;
}
}

View file

@ -61,15 +61,17 @@ const TEST_CASES = [
];
describe("RootPathContextService", () => {
test.each(TEST_CASES)(
"Should look for correct type definitions when editing inside a $nodeType in $language",
async ({ fileName, cursorPosition, definitionPositions }) => {
await testRootPathContext(
"files",
fileName,
cursorPosition,
definitionPositions,
);
},
);
describe("should look for correct type definitions", () => {
test.each(TEST_CASES)(
"$language: $nodeType",
async ({ fileName, cursorPosition, definitionPositions }) => {
await testRootPathContext(
"files",
fileName,
cursorPosition,
definitionPositions,
);
},
);
});
});

View file

@ -23,3 +23,7 @@ const logPersonWithAddres = (person: Person<Address>): Person<Address> => {
const logPersonOrAddress = (person: Person | Address): Person | Address => {
// TODO
};
const logPersonAndAddress = (person: Person, address: Address) => {
// TODO
};

View file

@ -28,4 +28,8 @@ class Group {
logPersonOrAddress(person: Person | Address): Person | Address {
// TODO
}
logPersonAndAddress(person: Person, address: Address) {
// TODO
}
}

View file

@ -1,3 +1,9 @@
// @ts-nocheck
class Group extends BaseClass {}
class Group implements FirstInterface {}
class Group extends BaseClass implements FirstInterface, SecondInterface {}
class Group extends BaseClass<User> implements FirstInterface<User> {}

View file

@ -27,3 +27,7 @@ function logPersonWithAddress(person: Person<Address>): Person<Address> {
function logPersonOrAddress(person: Person | Address): Person | Address {
// TODO
}
function logPersonAndAddress(person: Person, address: Address) {
// TODO
}

View file

@ -0,0 +1,33 @@
// @ts-nocheck
function* getAddress(person: Person): Address {
// TODO
}
function* getFirstAddress(people: Person[]): Address {
// TODO
}
function* logPerson(person: Person) {
// TODO
}
function* getHardcodedAddress(): Address {
// TODO
}
function* getAddresses(people: Person[]): Address[] {
// TODO
}
function* logPersonWithAddress(person: Person<Address>): Person<Address> {
// TODO
}
function* logPersonOrAddress(person: Person | Address): Person | Address {
// TODO
}
function* logPersonAndAddress(person: Person, address: Address) {
// TODO
}

View file

@ -73,6 +73,103 @@ const FUNCTIONS = [
{ row: 26, column: 71 }, // Address
],
},
{
nodeType: "function_declaration with two arguments",
fileName: "functions.ts",
language: "TypeScript",
cursorPosition: { line: 31, character: 9 },
definitionPositions: [
{ row: 30, column: 43 }, // Person
{ row: 30, column: 61 }, // Address
],
},
];
const GENERATORS = [
{
nodeType: "function_declaration with a param and a return type",
fileName: "generators.ts",
language: "TypeScript",
cursorPosition: { line: 3, character: 9 },
definitionPositions: [
{ row: 2, column: 35 }, // Person
{ row: 2, column: 45 }, // Address
],
},
{
nodeType: "function_declaration with array param",
fileName: "generators.ts",
language: "TypeScript",
cursorPosition: { line: 7, character: 9 },
definitionPositions: [
{ row: 6, column: 40 }, // Person
{ row: 6, column: 52 }, // Address
],
},
{
nodeType: "function_declaration without return type",
fileName: "generators.ts",
language: "TypeScript",
cursorPosition: { line: 11, character: 9 },
definitionPositions: [
{ row: 10, column: 34 }, // Person
],
},
{
nodeType: "function_declaration without params",
fileName: "generators.ts",
language: "TypeScript",
cursorPosition: { line: 15, character: 9 },
definitionPositions: [
{ row: 14, column: 40 }, // Person
],
},
{
nodeType: "function_declaration with array params and array return type",
fileName: "generators.ts",
language: "TypeScript",
cursorPosition: { line: 19, character: 9 },
definitionPositions: [
{ row: 18, column: 37 }, // Person
{ row: 18, column: 49 }, // Address
],
},
{
nodeType:
"function_declaration with generic params and generic return type",
fileName: "generators.ts",
language: "TypeScript",
cursorPosition: { line: 23, character: 9 },
definitionPositions: [
{ row: 22, column: 45 }, // Person
{ row: 22, column: 53 }, // Address
{ row: 22, column: 63 }, // Person
{ row: 22, column: 71 }, // Address
],
},
{
nodeType:
"function_declaration with union type params and union return type",
fileName: "generators.ts",
language: "TypeScript",
cursorPosition: { line: 27, character: 9 },
definitionPositions: [
{ row: 26, column: 43 }, // Person
{ row: 26, column: 53 }, // Address
{ row: 26, column: 62 }, // Person
{ row: 26, column: 72 }, // Address
],
},
{
nodeType: "function_declaration with two arguments",
fileName: "generators.ts",
language: "TypeScript",
cursorPosition: { line: 31, character: 9 },
definitionPositions: [
{ row: 30, column: 44 }, // Person
{ row: 30, column: 62 }, // Address
],
},
];
const ARROW_FUNCTIONS = [
@ -138,6 +235,16 @@ const ARROW_FUNCTIONS = [
{ row: 22, column: 71 }, // Address
],
},
{
nodeType: "arrow_function with two arguments",
fileName: "arrowFunctions.ts",
language: "TypeScript",
cursorPosition: { line: 27, character: 11 },
definitionPositions: [
{ row: 26, column: 43 }, // Person
{ row: 26, column: 61 }, // Address
],
},
];
const CLASS_METHODS = [
@ -214,10 +321,77 @@ const CLASS_METHODS = [
{ row: 27, column: 64 }, // Address
],
},
{
nodeType: "method_declaration with two arguments",
fileName: "classMethods.ts",
language: "TypeScript",
cursorPosition: { line: 32, character: 11 },
definitionPositions: [
{ row: 31, column: 36 }, // Person
{ row: 31, column: 54 }, // Address
],
},
];
const CLASS_DEFINITIONS = [
{
nodeType: "class_declaration with base class",
fileName: "classes.ts",
language: "TypeScript",
cursorPosition: { line: 2, character: 31 },
definitionPositions: [
{ row: 2, column: 29 }, // BaseClass
],
},
{
nodeType: "class_declaration with interface",
fileName: "classes.ts",
language: "TypeScript",
cursorPosition: { line: 4, character: 39 },
definitionPositions: [
{ row: 4, column: 37 }, // FirstInterface
],
},
{
nodeType: "class_declaration with base class and multiple interfaces",
fileName: "classes.ts",
language: "TypeScript",
cursorPosition: { line: 6, character: 74 },
definitionPositions: [
{ row: 6, column: 29 }, // BaseClass
{ row: 6, column: 55 }, // FirstInterface
{ row: 6, column: 72 }, // SecondInterface
],
},
{
nodeType: "class_declaration with base class and multiple interfaces",
fileName: "classes.ts",
language: "TypeScript",
cursorPosition: { line: 6, character: 74 },
definitionPositions: [
{ row: 6, column: 29 }, // BaseClass
{ row: 6, column: 55 }, // FirstInterface
{ row: 6, column: 72 }, // SecondInterface
],
},
{
nodeType: "class_declaration with generic base class and generic interface",
fileName: "classes.ts",
language: "TypeScript",
cursorPosition: { line: 8, character: 69 },
definitionPositions: [
{ row: 8, column: 29 }, // BaseClass
{ row: 8, column: 61 }, // FirstInterface
{ row: 8, column: 34 }, // User
{ row: 8, column: 66 }, // User
],
},
];
export const TYPESCRIPT_TEST_CASES = [
...FUNCTIONS,
...GENERATORS,
...ARROW_FUNCTIONS,
...CLASS_METHODS,
...CLASS_DEFINITIONS,
];

View file

@ -84,6 +84,7 @@ export async function testRootPathContext(
index + 1,
expect.any(String), // filepath argument
position,
expect.any(String), // language argument
);
});
}

View file

@ -3,81 +3,116 @@ import * as path from "node:path";
import Parser, { Language } from "web-tree-sitter";
export const supportedLanguages: { [key: string]: string } = {
cpp: "cpp",
hpp: "cpp",
cc: "cpp",
cxx: "cpp",
hxx: "cpp",
cp: "cpp",
hh: "cpp",
inc: "cpp",
// Depended on this PR: https://github.com/tree-sitter/tree-sitter-cpp/pull/173
// ccm: "cpp",
// c++m: "cpp",
// cppm: "cpp",
// cxxm: "cpp",
cs: "c_sharp",
c: "c",
h: "c",
css: "css",
php: "php",
phtml: "php",
php3: "php",
php4: "php",
php5: "php",
php7: "php",
phps: "php",
"php-s": "php",
bash: "bash",
sh: "bash",
json: "json",
ts: "typescript",
mts: "typescript",
cts: "typescript",
tsx: "tsx",
// vue: "vue", // tree-sitter-vue parser is broken
// The .wasm file being used is faulty, and yaml is split line-by-line anyway for the most part
// yaml: "yaml",
// yml: "yaml",
elm: "elm",
js: "javascript",
jsx: "javascript",
mjs: "javascript",
cjs: "javascript",
py: "python",
ipynb: "python",
pyw: "python",
pyi: "python",
el: "elisp",
emacs: "elisp",
ex: "elixir",
exs: "elixir",
go: "go",
eex: "embedded_template",
heex: "embedded_template",
leex: "embedded_template",
html: "html",
htm: "html",
java: "java",
lua: "lua",
ocaml: "ocaml",
ml: "ocaml",
mli: "ocaml",
ql: "ql",
res: "rescript",
resi: "rescript",
rb: "ruby",
erb: "ruby",
rs: "rust",
rdl: "systemrdl",
toml: "toml",
sol: "solidity",
export enum LanguageName {
CPP = "cpp",
C_SHARP = "c_sharp",
C = "c",
CSS = "css",
PHP = "php",
BASH = "bash",
JSON = "json",
TYPESCRIPT = "typescript",
TSX = "tsx",
ELM = "elm",
JAVASCRIPT = "javascript",
PYTHON = "python",
ELISP = "elisp",
ELIXIR = "elixir",
GO = "go",
EMBEDDED_TEMPLATE = "embedded_template",
HTML = "html",
JAVA = "java",
LUA = "lua",
OCAML = "ocaml",
QL = "ql",
RESCRIPT = "rescript",
RUBY = "ruby",
RUST = "rust",
SYSTEMRDL = "systemrdl",
TOML = "toml",
SOLIDITY = "solidity",
}
// jl: "julia",
// swift: "swift",
// kt: "kotlin",
// scala: "scala",
export const supportedLanguages: { [key: string]: LanguageName } = {
cpp: LanguageName.CPP,
hpp: LanguageName.CPP,
cc: LanguageName.CPP,
cxx: LanguageName.CPP,
hxx: LanguageName.CPP,
cp: LanguageName.CPP,
hh: LanguageName.CPP,
inc: LanguageName.CPP,
// Depended on this PR: https://github.com/tree-sitter/tree-sitter-cpp/pull/173
// ccm: LanguageName.CPP,
// c++m: LanguageName.CPP,
// cppm: LanguageName.CPP,
// cxxm: LanguageName.CPP,
cs: LanguageName.C_SHARP,
c: LanguageName.C,
h: LanguageName.C,
css: LanguageName.CSS,
php: LanguageName.PHP,
phtml: LanguageName.PHP,
php3: LanguageName.PHP,
php4: LanguageName.PHP,
php5: LanguageName.PHP,
php7: LanguageName.PHP,
phps: LanguageName.PHP,
"php-s": LanguageName.PHP,
bash: LanguageName.BASH,
sh: LanguageName.BASH,
json: LanguageName.JSON,
ts: LanguageName.TYPESCRIPT,
mts: LanguageName.TYPESCRIPT,
cts: LanguageName.TYPESCRIPT,
tsx: LanguageName.TSX,
// vue: LanguageName.VUE, // tree-sitter-vue parser is broken
// The .wasm file being used is faulty, and yaml is split line-by-line anyway for the most part
// yaml: LanguageName.YAML,
// yml: LanguageName.YAML,
elm: LanguageName.ELM,
js: LanguageName.JAVASCRIPT,
jsx: LanguageName.JAVASCRIPT,
mjs: LanguageName.JAVASCRIPT,
cjs: LanguageName.JAVASCRIPT,
py: LanguageName.PYTHON,
ipynb: LanguageName.PYTHON,
pyw: LanguageName.PYTHON,
pyi: LanguageName.PYTHON,
el: LanguageName.ELISP,
emacs: LanguageName.ELISP,
ex: LanguageName.ELIXIR,
exs: LanguageName.ELIXIR,
go: LanguageName.GO,
eex: LanguageName.EMBEDDED_TEMPLATE,
heex: LanguageName.EMBEDDED_TEMPLATE,
leex: LanguageName.EMBEDDED_TEMPLATE,
html: LanguageName.HTML,
htm: LanguageName.HTML,
java: LanguageName.JAVA,
lua: LanguageName.LUA,
ocaml: LanguageName.OCAML,
ml: LanguageName.OCAML,
mli: LanguageName.OCAML,
ql: LanguageName.QL,
res: LanguageName.RESCRIPT,
resi: LanguageName.RESCRIPT,
rb: LanguageName.RUBY,
erb: LanguageName.RUBY,
rs: LanguageName.RUST,
rdl: LanguageName.SYSTEMRDL,
toml: LanguageName.TOML,
sol: LanguageName.SOLIDITY,
// jl: LanguageName.JULIA,
// swift: LanguageName.SWIFT,
// kt: LanguageName.KOTLIN,
// scala: LanguageName.SCALA,
};
export const IGNORE_PATH_PATTERNS: Partial<Record<LanguageName, RegExp[]>> = {
[LanguageName.TYPESCRIPT]: [/.*node_modules/],
[LanguageName.JAVASCRIPT]: [/.*node_modules/],
};
export async function getParserForFile(filepath: string) {

View file

@ -1,13 +1,21 @@
(
(class_declaration
(class_heritage
(extends_clause
(identifier) @base-class
)
; Match a class declaration that extends a base class
; Returns:
; @base-class: The identifier of the base class being extended
; @generic_argument: The type identifier of the generic argument, if present
(class_declaration
(class_heritage
(extends_clause
(identifier) @base-class
(type_arguments
(type_identifier) @generic_argument
)?
)
)
)
; Match a class declaration that implements an interface
; Returns:
; @interface: The type identifier of the interface being implemented
(
(class_declaration
(class_heritage
@ -16,4 +24,22 @@
)
)
)
)
; Match a class declaration that implements a generic interface
; Returns:
; @interface: The type identifier of the generic interface being implemented
; @generic_argument: The type identifier of the generic argument
(
class_declaration
(class_heritage
(implements_clause
(generic_type
(type_identifier) @interface
(type_arguments
(type_identifier) @generic_argument
)
)
)
)
)

View file

@ -0,0 +1,77 @@
; Pattern for return type with direct type_identifier
(
(generator_function_declaration
(type_annotation
(type_identifier) @return_type
)
)
)
; Pattern for return type with one level of nesting
(
(generator_function_declaration
(type_annotation
(_
(type_identifier) @return_type
)
)
)
)
; Pattern for return type with two levels of nesting
(
(generator_function_declaration
(type_annotation
(_
(_
(type_identifier) @return_type
)
)
)
)
)
; Pattern for parameters with direct type_identifier
(
(generator_function_declaration
(formal_parameters
(_
(type_annotation
(type_identifier) @param_type
)
)
)
)
)
; Pattern for parameters with one level of nesting
(
(generator_function_declaration
(formal_parameters
(_
(type_annotation
(_
(type_identifier) @param_type
)
)
)
)
)
)
; Pattern for parameters with two levels of nesting
(
(generator_function_declaration
(formal_parameters
(_
(type_annotation
(_
(_
(type_identifier) @param_type
)
)
)
)
)
)
)