From eadc96ebef6653a996dff32095fd87a86703058d Mon Sep 17 00:00:00 2001 From: tomasz-io Date: Tue, 12 Nov 2024 13:31:13 -0800 Subject: [PATCH 1/2] feat: php definitions --- .../RootPathContextService.ts | 23 +++++ .../test/RootPathContextService.test.ts | 90 ++++++++++--------- .../root-path-context/test/files/file1.php | 32 +++++++ .../function_definition/php.scm | 10 +++ 4 files changed, 115 insertions(+), 40 deletions(-) create mode 100644 core/autocomplete/context/root-path-context/test/files/file1.php create mode 100644 extensions/vscode/tree-sitter/root-path-context-queries/function_definition/php.scm diff --git a/core/autocomplete/context/root-path-context/RootPathContextService.ts b/core/autocomplete/context/root-path-context/RootPathContextService.ts index accd4f6df..6f163c265 100644 --- a/core/autocomplete/context/root-path-context/RootPathContextService.ts +++ b/core/autocomplete/context/root-path-context/RootPathContextService.ts @@ -9,6 +9,21 @@ import { AstPath } from "../../util/ast"; import { ImportDefinitionsService } from "../ImportDefinitionsService"; import { AutocompleteSnippet } from "../ranking"; +function getSyntaxTreeString( + node: Parser.SyntaxNode, + indent: string = "", +): string { + let result = ""; + const nodeInfo = `${node.type} [${node.startPosition.row}:${node.startPosition.column} - ${node.endPosition.row}:${node.endPosition.column}]`; + result += `${indent}${nodeInfo}\n`; + + for (const child of node.children) { + result += getSyntaxTreeString(child, indent + " "); + } + + return result; +} + export class RootPathContextService { private cache = new LRUCache({ max: 100, @@ -58,12 +73,17 @@ export class RootPathContextService { this.importDefinitionsService.get(filepath); break; default: + console.log(getSyntaxTreeString(node)); + query = await getQueryForFile( filepath, `root-path-context-queries/${node.type}`, ); break; } + const type = node.type; + + debugger; if (!query) { return snippets; @@ -115,6 +135,9 @@ export class RootPathContextService { RootPathContextService.TYPES_TO_USE.has(node.type), )) { const key = RootPathContextService.keyFromNode(parentKey, astNode); + const type = astNode.type; + + debugger; const foundInCache = this.cache.get(key); const newSnippets = 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 196a7b643..f0af54a45 100644 --- a/core/autocomplete/context/root-path-context/test/RootPathContextService.test.ts +++ b/core/autocomplete/context/root-path-context/test/RootPathContextService.test.ts @@ -1,49 +1,59 @@ 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 - ], - }, + // { + // 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 + // ], + // }, + // { + // 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.py", - language: "Python", - cursorPosition: { line: 4, character: 25 }, + fileName: "file1.php", + language: "PHP", + cursorPosition: { line: 12, character: 32 }, 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 + { row: 10, column: 26 }, // Person + { row: 10, column: 44 }, // Address ], }, ]; diff --git a/core/autocomplete/context/root-path-context/test/files/file1.php b/core/autocomplete/context/root-path-context/test/files/file1.php new file mode 100644 index 000000000..a72f805e0 --- /dev/null +++ b/core/autocomplete/context/root-path-context/test/files/file1.php @@ -0,0 +1,32 @@ +getAddress(); +} + +class Group extends BaseClass implements FirstInterface, SecondInterface +{ + private array $people; + + public function __construct(array $people) + { + parent::__construct(); + $this->people = $people; + } + + public function getPersonAddress(Person $person): Address + { + return getAddress($person); + } +} + +?> \ No newline at end of file diff --git a/extensions/vscode/tree-sitter/root-path-context-queries/function_definition/php.scm b/extensions/vscode/tree-sitter/root-path-context-queries/function_definition/php.scm new file mode 100644 index 000000000..3f9303ae1 --- /dev/null +++ b/extensions/vscode/tree-sitter/root-path-context-queries/function_definition/php.scm @@ -0,0 +1,10 @@ +( + (function_definition + parameters: (formal_parameters + (simple_parameter + type: (named_type) @param_type + )* + ) + return_type: (named_type) @return_type + ) +) \ No newline at end of file From 8da703f74525213dc212efa6b763d4be06dee38b Mon Sep 17 00:00:00 2001 From: tomasz-io Date: Wed, 13 Nov 2024 10:23:37 -0800 Subject: [PATCH 2/2] feat: php definitions --- .../RootPathContextService.ts | 12 +- .../test/RootPathContextService.test.ts | 103 ++++++++++-------- .../class_declaration/php.scm | 13 +++ .../method_declaration/php.scm | 10 ++ 4 files changed, 87 insertions(+), 51 deletions(-) create mode 100644 extensions/vscode/tree-sitter/root-path-context-queries/class_declaration/php.scm create mode 100644 extensions/vscode/tree-sitter/root-path-context-queries/method_declaration/php.scm diff --git a/core/autocomplete/context/root-path-context/RootPathContextService.ts b/core/autocomplete/context/root-path-context/RootPathContextService.ts index 6f163c265..9c47138cd 100644 --- a/core/autocomplete/context/root-path-context/RootPathContextService.ts +++ b/core/autocomplete/context/root-path-context/RootPathContextService.ts @@ -43,6 +43,7 @@ export class RootPathContextService { "function_declaration", "function_definition", "method_definition", + "method_declaration", "class_declaration", "class_definition", ]); @@ -73,7 +74,9 @@ export class RootPathContextService { this.importDefinitionsService.get(filepath); break; default: - console.log(getSyntaxTreeString(node)); + // const type = node.type; + // debugger; + // console.log(getSyntaxTreeString(node)); query = await getQueryForFile( filepath, @@ -83,8 +86,6 @@ export class RootPathContextService { } const type = node.type; - debugger; - if (!query) { return snippets; } @@ -135,9 +136,8 @@ export class RootPathContextService { RootPathContextService.TYPES_TO_USE.has(node.type), )) { const key = RootPathContextService.keyFromNode(parentKey, astNode); - const type = astNode.type; - - debugger; + // const type = astNode.type; + // debugger; const foundInCache = this.cache.get(key); const newSnippets = 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 f0af54a45..4bc099c28 100644 --- a/core/autocomplete/context/root-path-context/test/RootPathContextService.test.ts +++ b/core/autocomplete/context/root-path-context/test/RootPathContextService.test.ts @@ -1,51 +1,51 @@ 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 - // ], - // }, - // { - // 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_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 + ], + }, + { + 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", @@ -56,6 +56,19 @@ const TEST_CASES = [ { row: 10, column: 44 }, // Address ], }, + { + nodeType: "function_definition (inside a class)", + fileName: "file1.php", + language: "PHP", + cursorPosition: { line: 26, character: 35 }, + definitionPositions: [ + { row: 15, column: 29 }, // BaseClass + { row: 15, column: 55 }, // FirstInterface + { row: 15, column: 72 }, // SecondInterface + { row: 25, column: 43 }, // Person + { row: 25, column: 61 }, // Address + ], + }, ]; describe("RootPathContextService", () => { diff --git a/extensions/vscode/tree-sitter/root-path-context-queries/class_declaration/php.scm b/extensions/vscode/tree-sitter/root-path-context-queries/class_declaration/php.scm new file mode 100644 index 000000000..5e9a9c13e --- /dev/null +++ b/extensions/vscode/tree-sitter/root-path-context-queries/class_declaration/php.scm @@ -0,0 +1,13 @@ +( + (class_declaration + (base_clause + (name) @superclass) + ) +) + +( + (class_declaration + (class_interface_clause + (name) @interface) + ) +) \ No newline at end of file diff --git a/extensions/vscode/tree-sitter/root-path-context-queries/method_declaration/php.scm b/extensions/vscode/tree-sitter/root-path-context-queries/method_declaration/php.scm new file mode 100644 index 000000000..825324e01 --- /dev/null +++ b/extensions/vscode/tree-sitter/root-path-context-queries/method_declaration/php.scm @@ -0,0 +1,10 @@ +( + (method_declaration + (formal_parameters + (simple_parameter + type: (named_type) @param_type + )* + ) + return_type: (named_type) @return_type + ) +) \ No newline at end of file