From 2f84ef0ab9a87aa6300415d30db02e3460a07195 Mon Sep 17 00:00:00 2001 From: tomasz-io Date: Fri, 15 Nov 2024 15:29:54 -0800 Subject: [PATCH] feat: capture python definitions --- .../RootPathContextService.ts | 7 +- .../test/RootPathContextService.test.ts | 25 +-- .../root-path-context/test/files/file1.py | 13 -- .../test/files/python/classes.py | 11 ++ .../test/files/python/functions.py | 42 +++++ .../files/{ => typescript}/arrowFunctions.ts | 0 .../files/{ => typescript}/classMethods.ts | 0 .../test/files/{ => typescript}/classes.ts | 0 .../test/files/{ => typescript}/functions.ts | 0 .../test/files/{ => typescript}/generators.ts | 0 .../root-path-context/test/testCases/index.ts | 2 + .../test/testCases/python.ts | 145 ++++++++++++++++++ .../test/testCases/typescript.ts | 72 ++++----- .../python/class_definition.scm | 27 +++- .../python/function_definition.scm | 71 +++++++-- 15 files changed, 322 insertions(+), 93 deletions(-) delete mode 100644 core/autocomplete/context/root-path-context/test/files/file1.py create mode 100644 core/autocomplete/context/root-path-context/test/files/python/classes.py create mode 100644 core/autocomplete/context/root-path-context/test/files/python/functions.py rename core/autocomplete/context/root-path-context/test/files/{ => typescript}/arrowFunctions.ts (100%) rename core/autocomplete/context/root-path-context/test/files/{ => typescript}/classMethods.ts (100%) rename core/autocomplete/context/root-path-context/test/files/{ => typescript}/classes.ts (100%) rename core/autocomplete/context/root-path-context/test/files/{ => typescript}/functions.ts (100%) rename core/autocomplete/context/root-path-context/test/files/{ => typescript}/generators.ts (100%) create mode 100644 core/autocomplete/context/root-path-context/test/testCases/index.ts create mode 100644 core/autocomplete/context/root-path-context/test/testCases/python.ts diff --git a/core/autocomplete/context/root-path-context/RootPathContextService.ts b/core/autocomplete/context/root-path-context/RootPathContextService.ts index 0147df151..8571acd0a 100644 --- a/core/autocomplete/context/root-path-context/RootPathContextService.ts +++ b/core/autocomplete/context/root-path-context/RootPathContextService.ts @@ -82,9 +82,9 @@ export class RootPathContextService { this.importDefinitionsService.get(filepath); break; default: - // const type = node.type; - // console.log(getSyntaxTreeString(node)); - // debugger; + const type = node.type; + console.log(getSyntaxTreeString(node)); + debugger; query = await getQueryForFile( filepath, @@ -176,6 +176,7 @@ export class RootPathContextService { parentKey = key; } + debugger; return snippets; } } 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 78aed69a7..a954dda84 100644 --- a/core/autocomplete/context/root-path-context/test/RootPathContextService.test.ts +++ b/core/autocomplete/context/root-path-context/test/RootPathContextService.test.ts @@ -1,30 +1,9 @@ -import { TYPESCRIPT_TEST_CASES } from "./testCases/typescript"; +import { PYTHON_TEST_CASES, TYPESCRIPT_TEST_CASES } from "./testCases"; import { testRootPathContext } from "./testUtils"; const TEST_CASES = [ + ...PYTHON_TEST_CASES, ...TYPESCRIPT_TEST_CASES, - { - nodeType: "function_definition", - fileName: "file1.py", - language: "Python", - cursorPosition: { line: 4, character: 25 }, - definitionPositions: [ - { row: 3, column: 30 }, // Person - { row: 3, column: 42 }, // Address - ], - }, - { - nodeType: "function_definition (inside a class)", - fileName: "file1.py", - language: "Python", - cursorPosition: { line: 12, character: 33 }, - definitionPositions: [ - { row: 6, column: 21 }, // BaseClass - { row: 6, column: 33 }, // Collection - { row: 11, column: 47 }, // Person - { row: 11, column: 59 }, // Address - ], - }, { nodeType: "function_definition", fileName: "file1.php", diff --git a/core/autocomplete/context/root-path-context/test/files/file1.py b/core/autocomplete/context/root-path-context/test/files/file1.py deleted file mode 100644 index 55c3d9c25..000000000 --- a/core/autocomplete/context/root-path-context/test/files/file1.py +++ /dev/null @@ -1,13 +0,0 @@ -from .base_module import BaseClass, Collection, Person, Address -from typing import List - -def get_address(person: Person) -> Address: - return person.address - -class Group(BaseClass, Collection): - def __init__(self, people: List[Person]) -> None: - super().__init__() - self.people = people - - def get_person_address(self, person: Person) -> Address: - return get_address(person) diff --git a/core/autocomplete/context/root-path-context/test/files/python/classes.py b/core/autocomplete/context/root-path-context/test/files/python/classes.py new file mode 100644 index 000000000..d752d8433 --- /dev/null +++ b/core/autocomplete/context/root-path-context/test/files/python/classes.py @@ -0,0 +1,11 @@ +class Group(BaseClass, Person): + pass + +class Group(metaclass=MetaGroup): + pass + +class Group(BaseClass[Address], Gathering[Person]): + pass + +class Group(List[Address], Person[str]): + pass \ No newline at end of file diff --git a/core/autocomplete/context/root-path-context/test/files/python/functions.py b/core/autocomplete/context/root-path-context/test/files/python/functions.py new file mode 100644 index 000000000..74a9d8b58 --- /dev/null +++ b/core/autocomplete/context/root-path-context/test/files/python/functions.py @@ -0,0 +1,42 @@ +from typing import List, Union, TypeVar, Generic + +T = TypeVar('T') + +class Address: + pass + +class Person: + pass + +class PersonWithGeneric(Generic[T]): + pass + + +def get_address(person: Person) -> Address: + pass + +def get_group_address(people: Group[Person]) -> Group[Address]: + pass + +def log_person(person: Person) -> None: + pass + +def get_hardcoded_address() -> Address: + pass + +def log_person_or_address(value: Union[Person, Address]) -> Union[Person, Address]: + pass + +def log_person_and_address(person: Person, address: Address) -> None: + pass + +def get_address_generator(person: Person) -> Generator[Address, None, None]: + yield + + +class Group: + def log_person_and_address(self, person: Person, address: Address) -> None: + pass + +async def get_person(address: Address) -> Person: + pass \ No newline at end of file diff --git a/core/autocomplete/context/root-path-context/test/files/arrowFunctions.ts b/core/autocomplete/context/root-path-context/test/files/typescript/arrowFunctions.ts similarity index 100% rename from core/autocomplete/context/root-path-context/test/files/arrowFunctions.ts rename to core/autocomplete/context/root-path-context/test/files/typescript/arrowFunctions.ts diff --git a/core/autocomplete/context/root-path-context/test/files/classMethods.ts b/core/autocomplete/context/root-path-context/test/files/typescript/classMethods.ts similarity index 100% rename from core/autocomplete/context/root-path-context/test/files/classMethods.ts rename to core/autocomplete/context/root-path-context/test/files/typescript/classMethods.ts diff --git a/core/autocomplete/context/root-path-context/test/files/classes.ts b/core/autocomplete/context/root-path-context/test/files/typescript/classes.ts similarity index 100% rename from core/autocomplete/context/root-path-context/test/files/classes.ts rename to core/autocomplete/context/root-path-context/test/files/typescript/classes.ts diff --git a/core/autocomplete/context/root-path-context/test/files/functions.ts b/core/autocomplete/context/root-path-context/test/files/typescript/functions.ts similarity index 100% rename from core/autocomplete/context/root-path-context/test/files/functions.ts rename to core/autocomplete/context/root-path-context/test/files/typescript/functions.ts diff --git a/core/autocomplete/context/root-path-context/test/files/generators.ts b/core/autocomplete/context/root-path-context/test/files/typescript/generators.ts similarity index 100% rename from core/autocomplete/context/root-path-context/test/files/generators.ts rename to core/autocomplete/context/root-path-context/test/files/typescript/generators.ts diff --git a/core/autocomplete/context/root-path-context/test/testCases/index.ts b/core/autocomplete/context/root-path-context/test/testCases/index.ts new file mode 100644 index 000000000..7859fc23d --- /dev/null +++ b/core/autocomplete/context/root-path-context/test/testCases/index.ts @@ -0,0 +1,2 @@ +export * from "./python"; +export * from "./typescript"; diff --git a/core/autocomplete/context/root-path-context/test/testCases/python.ts b/core/autocomplete/context/root-path-context/test/testCases/python.ts new file mode 100644 index 000000000..9c8d46e43 --- /dev/null +++ b/core/autocomplete/context/root-path-context/test/testCases/python.ts @@ -0,0 +1,145 @@ +export const FUNCTIONS = [ + { + nodeType: "function_definition with argument and return type", + fileName: "python/functions.py", + language: "Python", + cursorPosition: { line: 15, character: 8 }, + definitionPositions: [ + { row: 14, column: 30 }, // Person + { row: 14, column: 42 }, // Address + ], + }, + { + nodeType: + "function_definition with generic argument and generic return type", + fileName: "python/functions.py", + language: "Python", + cursorPosition: { line: 18, character: 8 }, + definitionPositions: [ + { row: 17, column: 35 }, // Group + { row: 17, column: 42 }, // Person + { row: 17, column: 53 }, // Group + { row: 17, column: 61 }, // Address + ], + }, + { + nodeType: "function_definition with single argument and None return type", + fileName: "python/functions.py", + language: "Python", + cursorPosition: { line: 21, character: 8 }, + definitionPositions: [ + { row: 20, column: 29 }, // Person + ], + }, + { + nodeType: "function_definition with no arguments and single return type", + fileName: "python/functions.py", + language: "Python", + cursorPosition: { line: 24, character: 8 }, + definitionPositions: [ + { row: 23, column: 38 }, // Address + ], + }, + { + nodeType: "function_definition with Union arguments and Union return type", + fileName: "python/functions.py", + language: "Python", + cursorPosition: { line: 27, character: 8 }, + definitionPositions: [ + { row: 26, column: 45 }, // Person + { row: 26, column: 54 }, // Address + { row: 26, column: 72 }, // Person + { row: 26, column: 81 }, // Address + ], + }, + { + nodeType: + "function_definition with multiple arguments and None return type", + fileName: "python/functions.py", + language: "Python", + cursorPosition: { line: 30, character: 8 }, + definitionPositions: [ + { row: 29, column: 41 }, // Person + { row: 29, column: 59 }, // Address + ], + }, + { + nodeType: "function_definition with one argument and Generator return type", + fileName: "python/functions.py", + language: "Python", + cursorPosition: { line: 33, character: 9 }, + definitionPositions: [ + { row: 32, column: 40 }, // Person + { row: 32, column: 62 }, // Address + ], + }, + { + nodeType: "function_definition inside a class", + fileName: "python/functions.py", + language: "Python", + cursorPosition: { line: 38, character: 12 }, + definitionPositions: [ + { row: 37, column: 51 }, // Person + { row: 37, column: 69 }, // Address + ], + }, + { + nodeType: "function_definition of an async function", + fileName: "python/functions.py", + language: "Python", + cursorPosition: { line: 41, character: 8 }, + definitionPositions: [ + { row: 40, column: 37 }, // Address + { row: 40, column: 48 }, // Person + ], + }, +]; + +export const CLASSES = [ + { + nodeType: "class_definition with multiple superclasses", + fileName: "python/classes.py", + language: "Python", + cursorPosition: { line: 1, character: 8 }, + definitionPositions: [ + { row: 0, column: 21 }, // BaseClass + { row: 0, column: 29 }, // Person + ], + }, + { + nodeType: "class_definition with multiple superclasses", + fileName: "python/classes.py", + language: "Python", + cursorPosition: { line: 4, character: 8 }, + definitionPositions: [ + { row: 3, column: 31 }, // MetaGroup + ], + }, + { + nodeType: "class_definition with generic superclasses", + fileName: "python/classes.py", + language: "Python", + cursorPosition: { line: 7, character: 8 }, + definitionPositions: [ + { row: 6, column: 21 }, // BaseClass + { row: 6, column: 29 }, // Address + { row: 6, column: 41 }, // Gathering + { row: 6, column: 48 }, // Person + ], + }, + { + nodeType: "class_definition with generic superclasses (built in types)", + fileName: "python/classes.py", + language: "Python", + cursorPosition: { line: 10, character: 8 }, + definitionPositions: [ + { row: 9, column: 24 }, // Address + { row: 9, column: 33 }, // Person + ], + }, +]; + +export const PYTHON_TEST_CASES = [ + // ...FUNCTIONS, + ...CLASSES, +]; 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 f945a02a9..ddda2bfde 100644 --- a/core/autocomplete/context/root-path-context/test/testCases/typescript.ts +++ b/core/autocomplete/context/root-path-context/test/testCases/typescript.ts @@ -1,7 +1,7 @@ const FUNCTIONS = [ { nodeType: "function_declaration with a param and a return type", - fileName: "functions.ts", + fileName: "typescript/functions.ts", language: "TypeScript", cursorPosition: { line: 3, character: 9 }, definitionPositions: [ @@ -11,7 +11,7 @@ const FUNCTIONS = [ }, { nodeType: "function_declaration with array param", - fileName: "functions.ts", + fileName: "typescript/functions.ts", language: "TypeScript", cursorPosition: { line: 7, character: 9 }, definitionPositions: [ @@ -21,7 +21,7 @@ const FUNCTIONS = [ }, { nodeType: "function_declaration without return type", - fileName: "functions.ts", + fileName: "typescript/functions.ts", language: "TypeScript", cursorPosition: { line: 11, character: 9 }, definitionPositions: [ @@ -30,7 +30,7 @@ const FUNCTIONS = [ }, { nodeType: "function_declaration without params", - fileName: "functions.ts", + fileName: "typescript/functions.ts", language: "TypeScript", cursorPosition: { line: 15, character: 9 }, definitionPositions: [ @@ -39,7 +39,7 @@ const FUNCTIONS = [ }, { nodeType: "function_declaration with array params and array return type", - fileName: "functions.ts", + fileName: "typescript/functions.ts", language: "TypeScript", cursorPosition: { line: 19, character: 9 }, definitionPositions: [ @@ -50,7 +50,7 @@ const FUNCTIONS = [ { nodeType: "function_declaration with generic params and generic return type", - fileName: "functions.ts", + fileName: "typescript/functions.ts", language: "TypeScript", cursorPosition: { line: 23, character: 9 }, definitionPositions: [ @@ -63,7 +63,7 @@ const FUNCTIONS = [ { nodeType: "function_declaration with union type params and union return type", - fileName: "functions.ts", + fileName: "typescript/functions.ts", language: "TypeScript", cursorPosition: { line: 27, character: 9 }, definitionPositions: [ @@ -75,7 +75,7 @@ const FUNCTIONS = [ }, { nodeType: "function_declaration with two arguments", - fileName: "functions.ts", + fileName: "typescript/functions.ts", language: "TypeScript", cursorPosition: { line: 31, character: 9 }, definitionPositions: [ @@ -88,7 +88,7 @@ const FUNCTIONS = [ const GENERATORS = [ { nodeType: "function_declaration with a param and a return type", - fileName: "generators.ts", + fileName: "typescript/generators.ts", language: "TypeScript", cursorPosition: { line: 3, character: 9 }, definitionPositions: [ @@ -98,7 +98,7 @@ const GENERATORS = [ }, { nodeType: "function_declaration with array param", - fileName: "generators.ts", + fileName: "typescript/generators.ts", language: "TypeScript", cursorPosition: { line: 7, character: 9 }, definitionPositions: [ @@ -108,7 +108,7 @@ const GENERATORS = [ }, { nodeType: "function_declaration without return type", - fileName: "generators.ts", + fileName: "typescript/generators.ts", language: "TypeScript", cursorPosition: { line: 11, character: 9 }, definitionPositions: [ @@ -117,7 +117,7 @@ const GENERATORS = [ }, { nodeType: "function_declaration without params", - fileName: "generators.ts", + fileName: "typescript/generators.ts", language: "TypeScript", cursorPosition: { line: 15, character: 9 }, definitionPositions: [ @@ -126,7 +126,7 @@ const GENERATORS = [ }, { nodeType: "function_declaration with array params and array return type", - fileName: "generators.ts", + fileName: "typescript/generators.ts", language: "TypeScript", cursorPosition: { line: 19, character: 9 }, definitionPositions: [ @@ -137,7 +137,7 @@ const GENERATORS = [ { nodeType: "function_declaration with generic params and generic return type", - fileName: "generators.ts", + fileName: "typescript/generators.ts", language: "TypeScript", cursorPosition: { line: 23, character: 9 }, definitionPositions: [ @@ -150,7 +150,7 @@ const GENERATORS = [ { nodeType: "function_declaration with union type params and union return type", - fileName: "generators.ts", + fileName: "typescript/generators.ts", language: "TypeScript", cursorPosition: { line: 27, character: 9 }, definitionPositions: [ @@ -162,7 +162,7 @@ const GENERATORS = [ }, { nodeType: "function_declaration with two arguments", - fileName: "generators.ts", + fileName: "typescript/generators.ts", language: "TypeScript", cursorPosition: { line: 31, character: 9 }, definitionPositions: [ @@ -175,7 +175,7 @@ const GENERATORS = [ const ARROW_FUNCTIONS = [ { nodeType: "arrow_function with a param and a return type", - fileName: "arrowFunctions.ts", + fileName: "typescript/arrowFunctions.ts", language: "TypeScript", cursorPosition: { line: 3, character: 9 }, definitionPositions: [ @@ -185,7 +185,7 @@ const ARROW_FUNCTIONS = [ }, { nodeType: "arrow_function without return type", - fileName: "arrowFunctions.ts", + fileName: "typescript/arrowFunctions.ts", language: "TypeScript", cursorPosition: { line: 7, character: 9 }, definitionPositions: [ @@ -194,7 +194,7 @@ const ARROW_FUNCTIONS = [ }, { nodeType: "arrow_function without params", - fileName: "arrowFunctions.ts", + fileName: "typescript/arrowFunctions.ts", language: "TypeScript", cursorPosition: { line: 11, character: 9 }, definitionPositions: [ @@ -203,7 +203,7 @@ const ARROW_FUNCTIONS = [ }, { nodeType: "arrow_function with array params and array return type", - fileName: "arrowFunctions.ts", + fileName: "typescript/arrowFunctions.ts", language: "TypeScript", cursorPosition: { line: 15, character: 9 }, definitionPositions: [ @@ -213,7 +213,7 @@ const ARROW_FUNCTIONS = [ }, { nodeType: "arrow_function with generic params and generic return type", - fileName: "arrowFunctions.ts", + fileName: "typescript/arrowFunctions.ts", language: "TypeScript", cursorPosition: { line: 19, character: 9 }, definitionPositions: [ @@ -225,7 +225,7 @@ const ARROW_FUNCTIONS = [ }, { nodeType: "arrow_function with union type params and union return type", - fileName: "arrowFunctions.ts", + fileName: "typescript/arrowFunctions.ts", language: "TypeScript", cursorPosition: { line: 23, character: 9 }, definitionPositions: [ @@ -237,7 +237,7 @@ const ARROW_FUNCTIONS = [ }, { nodeType: "arrow_function with two arguments", - fileName: "arrowFunctions.ts", + fileName: "typescript/arrowFunctions.ts", language: "TypeScript", cursorPosition: { line: 27, character: 11 }, definitionPositions: [ @@ -250,7 +250,7 @@ const ARROW_FUNCTIONS = [ const CLASS_METHODS = [ { nodeType: "method_declaration with a param and a return type", - fileName: "classMethods.ts", + fileName: "typescript/classMethods.ts", language: "TypeScript", cursorPosition: { line: 4, character: 11 }, definitionPositions: [ @@ -260,7 +260,7 @@ const CLASS_METHODS = [ }, { nodeType: "method_declaration without arguments", - fileName: "classMethods.ts", + fileName: "typescript/classMethods.ts", language: "TypeScript", cursorPosition: { line: 8, character: 11 }, definitionPositions: [ @@ -269,7 +269,7 @@ const CLASS_METHODS = [ }, { nodeType: "method_declaration without return type", - fileName: "classMethods.ts", + fileName: "typescript/classMethods.ts", language: "TypeScript", cursorPosition: { line: 12, character: 11 }, definitionPositions: [ @@ -278,7 +278,7 @@ const CLASS_METHODS = [ }, { nodeType: "method_declaration with array type arguments", - fileName: "classMethods.ts", + fileName: "typescript/classMethods.ts", language: "TypeScript", cursorPosition: { line: 16, character: 11 }, definitionPositions: [ @@ -288,7 +288,7 @@ const CLASS_METHODS = [ { nodeType: "method_declaration with array type arguments and array type return", - fileName: "classMethods.ts", + fileName: "typescript/classMethods.ts", language: "TypeScript", cursorPosition: { line: 20, character: 11 }, definitionPositions: [ @@ -299,7 +299,7 @@ const CLASS_METHODS = [ { nodeType: "method_declaration with with generic params and generic return type", - fileName: "classMethods.ts", + fileName: "typescript/classMethods.ts", language: "TypeScript", cursorPosition: { line: 24, character: 11 }, definitionPositions: [ @@ -311,7 +311,7 @@ const CLASS_METHODS = [ }, { nodeType: "method_declaration with union type params and union return type", - fileName: "classMethods.ts", + fileName: "typescript/classMethods.ts", language: "TypeScript", cursorPosition: { line: 28, character: 11 }, definitionPositions: [ @@ -323,7 +323,7 @@ const CLASS_METHODS = [ }, { nodeType: "method_declaration with two arguments", - fileName: "classMethods.ts", + fileName: "typescript/classMethods.ts", language: "TypeScript", cursorPosition: { line: 32, character: 11 }, definitionPositions: [ @@ -336,7 +336,7 @@ const CLASS_METHODS = [ const CLASS_DEFINITIONS = [ { nodeType: "class_declaration with base class", - fileName: "classes.ts", + fileName: "typescript/classes.ts", language: "TypeScript", cursorPosition: { line: 2, character: 31 }, definitionPositions: [ @@ -345,7 +345,7 @@ const CLASS_DEFINITIONS = [ }, { nodeType: "class_declaration with interface", - fileName: "classes.ts", + fileName: "typescript/classes.ts", language: "TypeScript", cursorPosition: { line: 4, character: 39 }, definitionPositions: [ @@ -354,7 +354,7 @@ const CLASS_DEFINITIONS = [ }, { nodeType: "class_declaration with base class and multiple interfaces", - fileName: "classes.ts", + fileName: "typescript/classes.ts", language: "TypeScript", cursorPosition: { line: 6, character: 74 }, definitionPositions: [ @@ -365,7 +365,7 @@ const CLASS_DEFINITIONS = [ }, { nodeType: "class_declaration with base class and multiple interfaces", - fileName: "classes.ts", + fileName: "typescript/classes.ts", language: "TypeScript", cursorPosition: { line: 6, character: 74 }, definitionPositions: [ @@ -376,7 +376,7 @@ const CLASS_DEFINITIONS = [ }, { nodeType: "class_declaration with generic base class and generic interface", - fileName: "classes.ts", + fileName: "typescript/classes.ts", language: "TypeScript", cursorPosition: { line: 8, character: 69 }, definitionPositions: [ diff --git a/extensions/vscode/tree-sitter/root-path-context-queries/python/class_definition.scm b/extensions/vscode/tree-sitter/root-path-context-queries/python/class_definition.scm index 5a9e34b86..61fdb29ec 100644 --- a/extensions/vscode/tree-sitter/root-path-context-queries/python/class_definition.scm +++ b/extensions/vscode/tree-sitter/root-path-context-queries/python/class_definition.scm @@ -1,5 +1,24 @@ -( - (class_definition - superclasses: (argument_list - (identifier) @superclass)) +; Match superclass identifiers in a simple inheritance +(class_definition + superclasses: (argument_list + (identifier) @catch) + (#not-match? @catch "^(str|int|float|bool|list|dict|tuple|set|frozenset|complex|bytes|bytearray|memoryview|range|slice|object|type|NoneType|List|Dict|Tuple|Set|FrozenSet|Union|Optional|Any|Callable|Iterable|Iterator|Generator|Coroutine|AsyncIterable|AsyncIterator|Awaitable|ContextManager|Pattern|Match|TypeVar|Generic|Sequence|Mapping|MutableMapping|MutableSequence|ByteString|Reversible|Sized|Container|Collection|AbstractSet|MutableSet|KeysView|ItemsView|ValuesView|Hashable|Sized|SupportsInt|SupportsFloat|SupportsComplex|SupportsBytes|SupportsAbs|SupportsRound|ChainMap|Counter|OrderedDict|defaultdict|deque|namedtuple|TypedDict)$") +) + +; Match the value of a keyword argument in class inheritance +(class_definition + superclasses: (argument_list + (keyword_argument + (_) + (identifier) @catch)) + (#not-match? @catch "^(str|int|float|bool|list|dict|tuple|set|frozenset|complex|bytes|bytearray|memoryview|range|slice|object|type|NoneType|List|Dict|Tuple|Set|FrozenSet|Union|Optional|Any|Callable|Iterable|Iterator|Generator|Coroutine|AsyncIterable|AsyncIterator|Awaitable|ContextManager|Pattern|Match|TypeVar|Generic|Sequence|Mapping|MutableMapping|MutableSequence|ByteString|Reversible|Sized|Container|Collection|AbstractSet|MutableSet|KeysView|ItemsView|ValuesView|Hashable|Sized|SupportsInt|SupportsFloat|SupportsComplex|SupportsBytes|SupportsAbs|SupportsRound|ChainMap|Counter|OrderedDict|defaultdict|deque|namedtuple|TypedDict)$") +) + +; Match identifiers within subscripts in class inheritance +(class_definition + superclasses: (argument_list + (subscript + (identifier) @catch + )) + (#not-match? @catch "^(str|int|float|bool|list|dict|tuple|set|frozenset|complex|bytes|bytearray|memoryview|range|slice|object|type|NoneType|List|Dict|Tuple|Set|FrozenSet|Union|Optional|Any|Callable|Iterable|Iterator|Generator|Coroutine|AsyncIterable|AsyncIterator|Awaitable|ContextManager|Pattern|Match|TypeVar|Generic|Sequence|Mapping|MutableMapping|MutableSequence|ByteString|Reversible|Sized|Container|Collection|AbstractSet|MutableSet|KeysView|ItemsView|ValuesView|Hashable|Sized|SupportsInt|SupportsFloat|SupportsComplex|SupportsBytes|SupportsAbs|SupportsRound|ChainMap|Counter|OrderedDict|defaultdict|deque|namedtuple|TypedDict)$") ) \ No newline at end of file diff --git a/extensions/vscode/tree-sitter/root-path-context-queries/python/function_definition.scm b/extensions/vscode/tree-sitter/root-path-context-queries/python/function_definition.scm index 864666a4a..535882bfa 100644 --- a/extensions/vscode/tree-sitter/root-path-context-queries/python/function_definition.scm +++ b/extensions/vscode/tree-sitter/root-path-context-queries/python/function_definition.scm @@ -1,17 +1,60 @@ -( - (function_definition - (parameters - (_ - (type) @a - (#not-match? @a "^(str|int|float|bool|list|dict|tuple)$") - ) - ) - ) +; When the type is a simple identifier: +(function_definition + parameters: (parameters + (typed_parameter + type: (type + (identifier) @type_identifier))) + + (#not-match? @type_identifier "^(str|int|float|bool|list|dict|tuple|set|frozenset|complex|bytes|bytearray|memoryview|range|slice|object|type|NoneType|List|Dict|Tuple|Set|FrozenSet|Union|Optional|Any|Callable|Iterable|Iterator|Generator|Coroutine|AsyncIterable|AsyncIterator|Awaitable|ContextManager|Pattern|Match|TypeVar|Generic|Sequence|Mapping|MutableMapping|MutableSequence|ByteString|Reversible|Sized|Container|Collection|AbstractSet|MutableSet|KeysView|ItemsView|ValuesView|Hashable|Sized|SupportsInt|SupportsFloat|SupportsComplex|SupportsBytes|SupportsAbs|SupportsRound|ChainMap|Counter|OrderedDict|defaultdict|deque|namedtuple|TypedDict)$") ) -( - (function_definition - (type) @b - (#not-match? @b "^(str|int|float|bool|list|dict|tuple)$") - ) +; When the type is a generic type with one identifier +(function_definition + parameters: (parameters + (typed_parameter + type: (type + (generic_type + (identifier) @type_identifier)))) + + (#not-match? @type_identifier "^(str|int|float|bool|list|dict|tuple|set|frozenset|complex|bytes|bytearray|memoryview|range|slice|object|type|NoneType|List|Dict|Tuple|Set|FrozenSet|Union|Optional|Any|Callable|Iterable|Iterator|Generator|Coroutine|AsyncIterable|AsyncIterator|Awaitable|ContextManager|Pattern|Match|TypeVar|Generic|Sequence|Mapping|MutableMapping|MutableSequence|ByteString|Reversible|Sized|Container|Collection|AbstractSet|MutableSet|KeysView|ItemsView|ValuesView|Hashable|Sized|SupportsInt|SupportsFloat|SupportsComplex|SupportsBytes|SupportsAbs|SupportsRound|ChainMap|Counter|OrderedDict|defaultdict|deque|namedtuple|TypedDict)$") +) + +; When the generic type has type parameters with one identifier +(function_definition + parameters: (parameters + (typed_parameter + type: (type + (generic_type + (identifier) + (type_parameter + (type + (identifier) @type_identifier)))))) + + (#not-match? @type_identifier "^(str|int|float|bool|list|dict|tuple|set|frozenset|complex|bytes|bytearray|memoryview|range|slice|object|type|NoneType|List|Dict|Tuple|Set|FrozenSet|Union|Optional|Any|Callable|Iterable|Iterator|Generator|Coroutine|AsyncIterable|AsyncIterator|Awaitable|ContextManager|Pattern|Match|TypeVar|Generic|Sequence|Mapping|MutableMapping|MutableSequence|ByteString|Reversible|Sized|Container|Collection|AbstractSet|MutableSet|KeysView|ItemsView|ValuesView|Hashable|Sized|SupportsInt|SupportsFloat|SupportsComplex|SupportsBytes|SupportsAbs|SupportsRound|ChainMap|Counter|OrderedDict|defaultdict|deque|namedtuple|TypedDict)$") +) + +; When the return type is a simple identifier +(function_definition + return_type: (type + (identifier) @type_identifier) + (#not-match? @type_identifier "^(str|int|float|bool|list|dict|tuple|set|frozenset|complex|bytes|bytearray|memoryview|range|slice|object|type|NoneType|List|Dict|Tuple|Set|FrozenSet|Union|Optional|Any|Callable|Iterable|Iterator|Generator|Coroutine|AsyncIterable|AsyncIterator|Awaitable|ContextManager|Pattern|Match|TypeVar|Generic|Sequence|Mapping|MutableMapping|MutableSequence|ByteString|Reversible|Sized|Container|Collection|AbstractSet|MutableSet|KeysView|ItemsView|ValuesView|Hashable|Sized|SupportsInt|SupportsFloat|SupportsComplex|SupportsBytes|SupportsAbs|SupportsRound|ChainMap|Counter|OrderedDict|defaultdict|deque|namedtuple|TypedDict)$") +) + +; When the return type is a generic type with one identifier +(function_definition + return_type: (type + (generic_type + (identifier) @type_identifier)) + (#not-match? @type_identifier "^(str|int|float|bool|list|dict|tuple|set|frozenset|complex|bytes|bytearray|memoryview|range|slice|object|type|NoneType|List|Dict|Tuple|Set|FrozenSet|Union|Optional|Any|Callable|Iterable|Iterator|Generator|Coroutine|AsyncIterable|AsyncIterator|Awaitable|ContextManager|Pattern|Match|TypeVar|Generic|Sequence|Mapping|MutableMapping|MutableSequence|ByteString|Reversible|Sized|Container|Collection|AbstractSet|MutableSet|KeysView|ItemsView|ValuesView|Hashable|Sized|SupportsInt|SupportsFloat|SupportsComplex|SupportsBytes|SupportsAbs|SupportsRound|ChainMap|Counter|OrderedDict|defaultdict|deque|namedtuple|TypedDict)$") +) + +; When the generic type has type parameters with one identifier +(function_definition + return_type: (type + (generic_type + (identifier) + (type_parameter + (type + (identifier) @type_identifier)))) + (#not-match? @type_identifier "^(str|int|float|bool|list|dict|tuple|set|frozenset|complex|bytes|bytearray|memoryview|range|slice|object|type|NoneType|List|Dict|Tuple|Set|FrozenSet|Union|Optional|Any|Callable|Iterable|Iterator|Generator|Coroutine|AsyncIterable|AsyncIterator|Awaitable|ContextManager|Pattern|Match|TypeVar|Generic|Sequence|Mapping|MutableMapping|MutableSequence|ByteString|Reversible|Sized|Container|Collection|AbstractSet|MutableSet|KeysView|ItemsView|ValuesView|Hashable|Sized|SupportsInt|SupportsFloat|SupportsComplex|SupportsBytes|SupportsAbs|SupportsRound|ChainMap|Counter|OrderedDict|defaultdict|deque|namedtuple|TypedDict)$") ) \ No newline at end of file