From 698db54ee0cf5cdb771a7f156d1045bf8ac019cf Mon Sep 17 00:00:00 2001 From: tomasz-io Date: Thu, 14 Nov 2024 16:26:24 -0800 Subject: [PATCH] improv: handle generics in class definitons --- .../root-path-context/test/files/classes.ts | 6 ++ .../test/testCases/typescript.ts | 56 +++++++++++++++++++ .../typescript/class_declaration.scm | 38 +++++++++++-- 3 files changed, 94 insertions(+), 6 deletions(-) diff --git a/core/autocomplete/context/root-path-context/test/files/classes.ts b/core/autocomplete/context/root-path-context/test/files/classes.ts index 2f6b76cda..8bb829986 100644 --- a/core/autocomplete/context/root-path-context/test/files/classes.ts +++ b/core/autocomplete/context/root-path-context/test/files/classes.ts @@ -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 implements FirstInterface {} diff --git a/core/autocomplete/context/root-path-context/test/testCases/typescript.ts b/core/autocomplete/context/root-path-context/test/testCases/typescript.ts index 9c490ae23..f051668fc 100644 --- a/core/autocomplete/context/root-path-context/test/testCases/typescript.ts +++ b/core/autocomplete/context/root-path-context/test/testCases/typescript.ts @@ -216,8 +216,64 @@ const CLASS_METHODS = [ }, ]; +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, ...ARROW_FUNCTIONS, ...CLASS_METHODS, + ...CLASS_DEFINITIONS, ]; diff --git a/extensions/vscode/tree-sitter/root-path-context-queries/typescript/class_declaration.scm b/extensions/vscode/tree-sitter/root-path-context-queries/typescript/class_declaration.scm index bf38152b7..d164b05a8 100644 --- a/extensions/vscode/tree-sitter/root-path-context-queries/typescript/class_declaration.scm +++ b/extensions/vscode/tree-sitter/root-path-context-queries/typescript/class_declaration.scm @@ -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 + ) + ) + ) + ) ) \ No newline at end of file