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 58da8982a..fe81a0edd 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,8 @@ +import { TYPESCRIPT_TEST_CASES } from "./testCases/typescript"; import { testRootPathContext } from "./testUtils"; const TEST_CASES = [ - { - nodeType: "function_declaration", - fileName: "file1.ts", - language: "TypeScript", - cursorPosition: { line: 10, character: 24 }, - definitionPositions: [ - { row: 9, column: 34 }, // Person - { row: 9, column: 44 }, // Address - ], - }, - { - nodeType: "method_declaration", - fileName: "file1.ts", - language: "TypeScript", - cursorPosition: { line: 22, character: 30 }, - definitionPositions: [ - { 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 - ], - }, + ...TYPESCRIPT_TEST_CASES, { nodeType: "function_definition", fileName: "file1.py", diff --git a/core/autocomplete/context/root-path-context/test/files/file1.ts b/core/autocomplete/context/root-path-context/test/files/class.ts similarity index 54% rename from core/autocomplete/context/root-path-context/test/files/file1.ts rename to core/autocomplete/context/root-path-context/test/files/class.ts index ba09c5709..017b592ae 100644 --- a/core/autocomplete/context/root-path-context/test/files/file1.ts +++ b/core/autocomplete/context/root-path-context/test/files/class.ts @@ -7,10 +7,6 @@ import { // @ts-ignore } from "./types"; -function getAddress(person: Person): Address { - return person.address; -} - class Group extends BaseClass implements FirstInterface, SecondInterface { people: Person[]; @@ -20,6 +16,18 @@ class Group extends BaseClass implements FirstInterface, SecondInterface { } getPersonAddress(person: Person): Address { - return getAddress(person); + return person.address; + } + + getHardcodedAddress(): Address { + return { street: "123 Main St", city: "Anytown" }; + } + + addPerson(person: Person) { + this.people = [...this.people, person]; + } + + addPeople(people: Person[]) { + this.people = [...this.people, ...people]; } } diff --git a/core/autocomplete/context/root-path-context/test/files/functions.ts b/core/autocomplete/context/root-path-context/test/files/functions.ts new file mode 100644 index 000000000..2b6125a74 --- /dev/null +++ b/core/autocomplete/context/root-path-context/test/files/functions.ts @@ -0,0 +1,25 @@ +import { + Address, + Person, + // @ts-ignore +} from "./types"; + +function getAddress(person: Person): Address { + return person.address; +} + +function getFirstAddress(people: Person[]): Address { + return people[0].address; +} + +function logPerson(person: Person) { + console.log(`Name: ${person.name}, Age: ${person.age}`); +} + +function getHardcodedAddress(): Address { + return { street: "123 Main St", city: "Anytown", zipCode: "12345" }; +} + +function getAddresses(people: Person[]): Address[] { + return people[0].address; +} diff --git a/core/autocomplete/context/root-path-context/test/testCases/typescript.ts b/core/autocomplete/context/root-path-context/test/testCases/typescript.ts new file mode 100644 index 000000000..6913cb8d3 --- /dev/null +++ b/core/autocomplete/context/root-path-context/test/testCases/typescript.ts @@ -0,0 +1,89 @@ +export const TYPESCRIPT_TEST_CASES = [ + { + nodeType: "function_declaration with a param and a return type", + fileName: "functions.ts", + language: "TypeScript", + cursorPosition: { line: 7, character: 24 }, + definitionPositions: [ + { row: 6, column: 34 }, // Person + { row: 6, column: 44 }, // Address + ], + }, + { + nodeType: "function_declaration with array param", + fileName: "functions.ts", + language: "TypeScript", + cursorPosition: { line: 11, character: 27 }, + definitionPositions: [ + { row: 10, column: 39 }, // Person + { row: 10, column: 51 }, // Address + ], + }, + { + nodeType: "function_declaration without return type", + fileName: "functions.ts", + language: "TypeScript", + cursorPosition: { line: 15, character: 58 }, + definitionPositions: [ + { row: 14, column: 33 }, // Person + ], + }, + { + nodeType: "function_declaration without params", + fileName: "functions.ts", + language: "TypeScript", + cursorPosition: { line: 19, character: 70 }, + definitionPositions: [ + { row: 18, column: 39 }, // Person + ], + }, + { + nodeType: "method_declaration", + fileName: "class.ts", + language: "TypeScript", + cursorPosition: { line: 18, character: 26 }, + definitionPositions: [ + { row: 9, column: 29 }, // BaseClass + { row: 9, column: 55 }, // FirstInterface + { row: 9, column: 72 }, // SecondInterface + { row: 17, column: 33 }, // Person + { row: 17, column: 43 }, // Address + ], + }, + { + nodeType: "method_declaration without arguments", + fileName: "class.ts", + language: "TypeScript", + cursorPosition: { line: 22, character: 54 }, + definitionPositions: [ + { row: 9, column: 29 }, // BaseClass + { row: 9, column: 55 }, // FirstInterface + { row: 9, column: 72 }, // SecondInterface + { row: 21, column: 32 }, // Address + ], + }, + { + nodeType: "method_declaration without return type", + fileName: "class.ts", + language: "TypeScript", + cursorPosition: { line: 26, character: 43 }, + definitionPositions: [ + { row: 9, column: 29 }, // BaseClass + { row: 9, column: 55 }, // FirstInterface + { row: 9, column: 72 }, // SecondInterface + { row: 25, column: 26 }, // Person + ], + }, + { + nodeType: "method_declaration with array type arguments", + fileName: "class.ts", + language: "TypeScript", + cursorPosition: { line: 30, character: 46 }, + definitionPositions: [ + { row: 9, column: 29 }, // BaseClass + { row: 9, column: 55 }, // FirstInterface + { row: 9, column: 72 }, // SecondInterface + { row: 29, column: 26 }, // Person + ], + }, +]; 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 ab10968a3..24dc04e22 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 @@ -1,10 +1,34 @@ +; Pattern for capturing the return type ( - (function_declaration - (formal_parameters - (_ - (type_annotation) @param_type - ) + (function_declaration + (type_annotation) @return_type + ) +) + +; Pattern for parameters with direct type_identifier +( + (function_declaration + (formal_parameters + (_ + (type_annotation + (type_identifier) @param_type ) - (type_annotation) @return_type + ) ) + ) +) + +; Pattern for parameters with one level of nesting +( + (function_declaration + (formal_parameters + (_ + (type_annotation + (_ + (type_identifier) @param_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 727db5090..9f8685385 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 @@ -1,10 +1,34 @@ +; Pattern for capturing the return type ( - (method_definition - (formal_parameters - (_ - (type_annotation) @param_type - ) - ) - (type_annotation) @return_type - ) + (method_definition + (type_annotation) @return_type + ) +) + +; Pattern for parameters with direct type_identifier +( + (method_definition + (formal_parameters + (_ + (type_annotation + (type_identifier) @param_type + ) + ) + ) + ) +) + +; Pattern for parameters with one level of nesting +( + (method_definition + (formal_parameters + (_ + (type_annotation + (_ + (type_identifier) @param_type + ) + ) + ) + ) + ) )