From 8a8a87889ec4ccf051c1771eb6995b850802e47a Mon Sep 17 00:00:00 2001 From: Nate Sesti Date: Sun, 3 Mar 2024 20:22:08 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20WIP=20on=20indexing=20for=20code?= =?UTF-8?q?=20context=20provider?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/indexing/CodeSnippetsIndex.ts | 104 +++++++++++++++++ core/tag-qry/tree-sitter-c-tags.scm | 9 ++ core/tag-qry/tree-sitter-c_sharp-tags.scm | 46 ++++++++ core/tag-qry/tree-sitter-cpp-tags.scm | 15 +++ core/tag-qry/tree-sitter-elisp-tags.scm | 8 ++ core/tag-qry/tree-sitter-elixir-tags.scm | 54 +++++++++ core/tag-qry/tree-sitter-elm-tags.scm | 19 +++ core/tag-qry/tree-sitter-go-tags.scm | 30 +++++ core/tag-qry/tree-sitter-java-tags.scm | 20 ++++ core/tag-qry/tree-sitter-javascript-tags.scm | 88 ++++++++++++++ core/tag-qry/tree-sitter-ocaml-tags.scm | 116 +++++++++++++++++++ core/tag-qry/tree-sitter-php-tags.scm | 26 +++++ core/tag-qry/tree-sitter-python-tags.scm | 12 ++ core/tag-qry/tree-sitter-ql-tags.scm | 26 +++++ core/tag-qry/tree-sitter-ruby-tags.scm | 64 ++++++++++ core/tag-qry/tree-sitter-rust-tags.scm | 60 ++++++++++ core/tag-qry/tree-sitter-typescript-tags.scm | 23 ++++ core/util/treeSitter.ts | 19 ++- 18 files changed, 737 insertions(+), 2 deletions(-) create mode 100644 core/indexing/CodeSnippetsIndex.ts create mode 100644 core/tag-qry/tree-sitter-c-tags.scm create mode 100644 core/tag-qry/tree-sitter-c_sharp-tags.scm create mode 100644 core/tag-qry/tree-sitter-cpp-tags.scm create mode 100644 core/tag-qry/tree-sitter-elisp-tags.scm create mode 100644 core/tag-qry/tree-sitter-elixir-tags.scm create mode 100644 core/tag-qry/tree-sitter-elm-tags.scm create mode 100644 core/tag-qry/tree-sitter-go-tags.scm create mode 100644 core/tag-qry/tree-sitter-java-tags.scm create mode 100644 core/tag-qry/tree-sitter-javascript-tags.scm create mode 100644 core/tag-qry/tree-sitter-ocaml-tags.scm create mode 100644 core/tag-qry/tree-sitter-php-tags.scm create mode 100644 core/tag-qry/tree-sitter-python-tags.scm create mode 100644 core/tag-qry/tree-sitter-ql-tags.scm create mode 100644 core/tag-qry/tree-sitter-ruby-tags.scm create mode 100644 core/tag-qry/tree-sitter-rust-tags.scm create mode 100644 core/tag-qry/tree-sitter-typescript-tags.scm diff --git a/core/indexing/CodeSnippetsIndex.ts b/core/indexing/CodeSnippetsIndex.ts new file mode 100644 index 000000000..55bddb88c --- /dev/null +++ b/core/indexing/CodeSnippetsIndex.ts @@ -0,0 +1,104 @@ +import fs from "fs"; +import path from "path"; +import { ChunkWithoutID, IDE, IndexingProgressUpdate } from ".."; +import { + getLanguageForFile, + getParserForFile, + supportedLanguages, +} from "../util/treeSitter"; +import { DatabaseConnection, SqliteDb } from "./refreshIndex"; +import { + CodebaseIndex, + IndexResultType, + IndexTag, + MarkCompleteCallback, + RefreshIndexResults, +} from "./types"; + +export class CodeSnippetsCodebaseIndex implements CodebaseIndex { + artifactId = "codeSnippets"; + + constructor(private readonly ide: IDE) {} + + private async _createTables(db: DatabaseConnection) { + await db.exec(`CREATE TABLE IF NOT EXISTS code_snippets ( + id INTEGER PRIMARY KEY, + path TEXT NOT NULL, + cacheKey TEXT NOT NULL, + content TEXT NOT NULL, + startLine INTEGER NOT NULL, + endLine INTEGER NOT NULL + )`); + } + + private getQuerySource(filepath: string) { + const fullLangName = supportedLanguages[filepath.split(".").pop() ?? ""]; + const sourcePath = path.join( + __dirname, + "..", + "tag-qry", + `tree-sitter-${fullLangName}-tags.scm`, + ); + return fs.readFileSync(sourcePath).toString(); + } + + async getSnippetsInFile( + filepath: string, + contents: string, + ): Promise { + const lang = await getLanguageForFile(filepath); + const parser = await getParserForFile(filepath); + const ast = parser.parse(contents); + const query = lang?.query(this.getQuerySource(filepath)); + const matches = query?.matches(ast.rootNode); + + return ( + matches?.flatMap((match) => { + const nodes = match.captures.map((capture) => capture.node); + const results = nodes.map((node) => ({ + content: node.text, + startLine: node.startPosition.row, + endLine: node.endPosition.row, + })); + return results; + }) ?? [] + ); + } + + async *update( + tag: IndexTag, + results: RefreshIndexResults, + markComplete: MarkCompleteCallback, + ): AsyncGenerator { + const db = await SqliteDb.get(); + await this._createTables(db); + + for (let i = 0; i < results.compute.length; i++) { + const compute = results.compute[i]; + const snippets = await this.getSnippetsInFile( + compute.path, + await this.ide.readFile(compute.path), + ); + + // Add snippets to sqlite + for (const snippet of snippets) { + await db.run( + `INSERT INTO code_snippets (path, cacheKey, content, startLine, endLine) VALUES (?, ?, ?, ?, ?)`, + [ + compute.path, + compute.cacheKey, + snippet.content, + snippet.startLine, + snippet.endLine, + ], + ); + } + + yield { + desc: `Indexing ${compute.path}`, + progress: i / results.compute.length, + }; + markComplete([compute], IndexResultType.Compute); + } + } +} diff --git a/core/tag-qry/tree-sitter-c-tags.scm b/core/tag-qry/tree-sitter-c-tags.scm new file mode 100644 index 000000000..1035aa224 --- /dev/null +++ b/core/tag-qry/tree-sitter-c-tags.scm @@ -0,0 +1,9 @@ +(struct_specifier name: (type_identifier) @name.definition.class body:(_)) @definition.class + +(declaration type: (union_specifier name: (type_identifier) @name.definition.class)) @definition.class + +(function_declarator declarator: (identifier) @name.definition.function) @definition.function + +(type_definition declarator: (type_identifier) @name.definition.type) @definition.type + +(enum_specifier name: (type_identifier) @name.definition.type) @definition.type diff --git a/core/tag-qry/tree-sitter-c_sharp-tags.scm b/core/tag-qry/tree-sitter-c_sharp-tags.scm new file mode 100644 index 000000000..58e9199a4 --- /dev/null +++ b/core/tag-qry/tree-sitter-c_sharp-tags.scm @@ -0,0 +1,46 @@ +(class_declaration + name: (identifier) @name.definition.class + ) @definition.class + +(class_declaration + bases: (base_list (_) @name.reference.class) + ) @reference.class + +(interface_declaration + name: (identifier) @name.definition.interface + ) @definition.interface + +(interface_declaration + bases: (base_list (_) @name.reference.interface) + ) @reference.interface + +(method_declaration + name: (identifier) @name.definition.method + ) @definition.method + +(object_creation_expression + type: (identifier) @name.reference.class + ) @reference.class + +(type_parameter_constraints_clause + target: (identifier) @name.reference.class + ) @reference.class + +(type_constraint + type: (identifier) @name.reference.class + ) @reference.class + +(variable_declaration + type: (identifier) @name.reference.class + ) @reference.class + +(invocation_expression + function: + (member_access_expression + name: (identifier) @name.reference.send + ) +) @reference.send + +(namespace_declaration + name: (identifier) @name.definition.module +) @definition.module diff --git a/core/tag-qry/tree-sitter-cpp-tags.scm b/core/tag-qry/tree-sitter-cpp-tags.scm new file mode 100644 index 000000000..7a7ad0b99 --- /dev/null +++ b/core/tag-qry/tree-sitter-cpp-tags.scm @@ -0,0 +1,15 @@ +(struct_specifier name: (type_identifier) @name.definition.class body:(_)) @definition.class + +(declaration type: (union_specifier name: (type_identifier) @name.definition.class)) @definition.class + +(function_declarator declarator: (identifier) @name.definition.function) @definition.function + +(function_declarator declarator: (field_identifier) @name.definition.function) @definition.function + +(function_declarator declarator: (qualified_identifier scope: (namespace_identifier) @scope name: (identifier) @name.definition.method)) @definition.method + +(type_definition declarator: (type_identifier) @name.definition.type) @definition.type + +(enum_specifier name: (type_identifier) @name.definition.type) @definition.type + +(class_specifier name: (type_identifier) @name.definition.class) @definition.class diff --git a/core/tag-qry/tree-sitter-elisp-tags.scm b/core/tag-qry/tree-sitter-elisp-tags.scm new file mode 100644 index 000000000..743c8d8a8 --- /dev/null +++ b/core/tag-qry/tree-sitter-elisp-tags.scm @@ -0,0 +1,8 @@ +;; defun/defsubst +(function_definition name: (symbol) @name.definition.function) @definition.function + +;; Treat macros as function definitions for the sake of TAGS. +(macro_definition name: (symbol) @name.definition.function) @definition.function + +;; Match function calls +(list (symbol) @name.reference.function) @reference.function diff --git a/core/tag-qry/tree-sitter-elixir-tags.scm b/core/tag-qry/tree-sitter-elixir-tags.scm new file mode 100644 index 000000000..9eb39d95d --- /dev/null +++ b/core/tag-qry/tree-sitter-elixir-tags.scm @@ -0,0 +1,54 @@ +; Definitions + +; * modules and protocols +(call + target: (identifier) @ignore + (arguments (alias) @name.definition.module) + (#match? @ignore "^(defmodule|defprotocol)$")) @definition.module + +; * functions/macros +(call + target: (identifier) @ignore + (arguments + [ + ; zero-arity functions with no parentheses + (identifier) @name.definition.function + ; regular function clause + (call target: (identifier) @name.definition.function) + ; function clause with a guard clause + (binary_operator + left: (call target: (identifier) @name.definition.function) + operator: "when") + ]) + (#match? @ignore "^(def|defp|defdelegate|defguard|defguardp|defmacro|defmacrop|defn|defnp)$")) @definition.function + +; References + +; ignore calls to kernel/special-forms keywords +(call + target: (identifier) @ignore + (#match? @ignore "^(def|defp|defdelegate|defguard|defguardp|defmacro|defmacrop|defn|defnp|defmodule|defprotocol|defimpl|defstruct|defexception|defoverridable|alias|case|cond|else|for|if|import|quote|raise|receive|require|reraise|super|throw|try|unless|unquote|unquote_splicing|use|with)$")) + +; ignore module attributes +(unary_operator + operator: "@" + operand: (call + target: (identifier) @ignore)) + +; * function call +(call + target: [ + ; local + (identifier) @name.reference.call + ; remote + (dot + right: (identifier) @name.reference.call) + ]) @reference.call + +; * pipe into function call +(binary_operator + operator: "|>" + right: (identifier) @name.reference.call) @reference.call + +; * modules +(alias) @name.reference.module @reference.module diff --git a/core/tag-qry/tree-sitter-elm-tags.scm b/core/tag-qry/tree-sitter-elm-tags.scm new file mode 100644 index 000000000..ad15d5e67 --- /dev/null +++ b/core/tag-qry/tree-sitter-elm-tags.scm @@ -0,0 +1,19 @@ +(value_declaration (function_declaration_left (lower_case_identifier) @name.definition.function)) @definition.function + +(function_call_expr (value_expr (value_qid) @name.reference.function)) @reference.function +(exposed_value (lower_case_identifier) @name.reference.function)) @reference.function +(type_annotation ((lower_case_identifier) @name.reference.function) (colon)) @reference.function + +(type_declaration ((upper_case_identifier) @name.definition.type) ) @definition.type + +(type_ref (upper_case_qid (upper_case_identifier) @name.reference.type)) @reference.type +(exposed_type (upper_case_identifier) @name.reference.type)) @reference.type + +(type_declaration (union_variant (upper_case_identifier) @name.definition.union)) @definition.union + +(value_expr (upper_case_qid (upper_case_identifier) @name.reference.union)) @reference.union + + +(module_declaration + (upper_case_qid (upper_case_identifier)) @name.definition.module +) @definition.module diff --git a/core/tag-qry/tree-sitter-go-tags.scm b/core/tag-qry/tree-sitter-go-tags.scm new file mode 100644 index 000000000..a32d03aa7 --- /dev/null +++ b/core/tag-qry/tree-sitter-go-tags.scm @@ -0,0 +1,30 @@ +( + (comment)* @doc + . + (function_declaration + name: (identifier) @name.definition.function) @definition.function + (#strip! @doc "^//\\s*") + (#set-adjacent! @doc @definition.function) +) + +( + (comment)* @doc + . + (method_declaration + name: (field_identifier) @name.definition.method) @definition.method + (#strip! @doc "^//\\s*") + (#set-adjacent! @doc @definition.method) +) + +(call_expression + function: [ + (identifier) @name.reference.call + (parenthesized_expression (identifier) @name.reference.call) + (selector_expression field: (field_identifier) @name.reference.call) + (parenthesized_expression (selector_expression field: (field_identifier) @name.reference.call)) + ]) @reference.call + +(type_spec + name: (type_identifier) @name.definition.type) @definition.type + +(type_identifier) @name.reference.type @reference.type diff --git a/core/tag-qry/tree-sitter-java-tags.scm b/core/tag-qry/tree-sitter-java-tags.scm new file mode 100644 index 000000000..3b7290d46 --- /dev/null +++ b/core/tag-qry/tree-sitter-java-tags.scm @@ -0,0 +1,20 @@ +(class_declaration + name: (identifier) @name.definition.class) @definition.class + +(method_declaration + name: (identifier) @name.definition.method) @definition.method + +(method_invocation + name: (identifier) @name.reference.call + arguments: (argument_list) @reference.call) + +(interface_declaration + name: (identifier) @name.definition.interface) @definition.interface + +(type_list + (type_identifier) @name.reference.implementation) @reference.implementation + +(object_creation_expression + type: (type_identifier) @name.reference.class) @reference.class + +(superclass (type_identifier) @name.reference.class) @reference.class diff --git a/core/tag-qry/tree-sitter-javascript-tags.scm b/core/tag-qry/tree-sitter-javascript-tags.scm new file mode 100644 index 000000000..3bc55c5c5 --- /dev/null +++ b/core/tag-qry/tree-sitter-javascript-tags.scm @@ -0,0 +1,88 @@ +( + (comment)* @doc + . + (method_definition + name: (property_identifier) @name.definition.method) @definition.method + (#not-eq? @name.definition.method "constructor") + (#strip! @doc "^[\\s\\*/]+|^[\\s\\*/]$") + (#select-adjacent! @doc @definition.method) +) + +( + (comment)* @doc + . + [ + (class + name: (_) @name.definition.class) + (class_declaration + name: (_) @name.definition.class) + ] @definition.class + (#strip! @doc "^[\\s\\*/]+|^[\\s\\*/]$") + (#select-adjacent! @doc @definition.class) +) + +( + (comment)* @doc + . + [ + (function + name: (identifier) @name.definition.function) + (function_declaration + name: (identifier) @name.definition.function) + (generator_function + name: (identifier) @name.definition.function) + (generator_function_declaration + name: (identifier) @name.definition.function) + ] @definition.function + (#strip! @doc "^[\\s\\*/]+|^[\\s\\*/]$") + (#select-adjacent! @doc @definition.function) +) + +( + (comment)* @doc + . + (lexical_declaration + (variable_declarator + name: (identifier) @name.definition.function + value: [(arrow_function) (function)]) @definition.function) + (#strip! @doc "^[\\s\\*/]+|^[\\s\\*/]$") + (#select-adjacent! @doc @definition.function) +) + +( + (comment)* @doc + . + (variable_declaration + (variable_declarator + name: (identifier) @name.definition.function + value: [(arrow_function) (function)]) @definition.function) + (#strip! @doc "^[\\s\\*/]+|^[\\s\\*/]$") + (#select-adjacent! @doc @definition.function) +) + +(assignment_expression + left: [ + (identifier) @name.definition.function + (member_expression + property: (property_identifier) @name.definition.function) + ] + right: [(arrow_function) (function)] +) @definition.function + +(pair + key: (property_identifier) @name.definition.function + value: [(arrow_function) (function)]) @definition.function + +( + (call_expression + function: (identifier) @name.reference.call) @reference.call + (#not-match? @name.reference.call "^(require)$") +) + +(call_expression + function: (member_expression + property: (property_identifier) @name.reference.call) + arguments: (_) @reference.call) + +(new_expression + constructor: (_) @name.reference.class) @reference.class diff --git a/core/tag-qry/tree-sitter-ocaml-tags.scm b/core/tag-qry/tree-sitter-ocaml-tags.scm new file mode 100644 index 000000000..1799f8acf --- /dev/null +++ b/core/tag-qry/tree-sitter-ocaml-tags.scm @@ -0,0 +1,116 @@ +; Modules +;-------- + +( + (comment)? @doc . + (module_definition (module_binding (module_name) @name.definition.module) @definition.module) + (#strip! @doc "^\\(\\*\\*?\\s*|\\s\\*\\)$") +) + +(module_path (module_name) @name.reference.module) @reference.module + +; Modules types +;-------------- + +( + (comment)? @doc . + (module_type_definition (module_type_name) @name.definition.interface) @definition.interface + (#strip! @doc "^\\(\\*\\*?\\s*|\\s\\*\\)$") +) + +(module_type_path (module_type_name) @name.reference.implementation) @reference.implementation + +; Functions +;---------- + +( + (comment)? @doc . + (value_definition + [ + (let_binding + pattern: (value_name) @name.definition.function + (parameter)) + (let_binding + pattern: (value_name) @name.definition.function + body: [(fun_expression) (function_expression)]) + ] @definition.function + ) + (#strip! @doc "^\\(\\*\\*?\\s*|\\s\\*\\)$") +) + +( + (comment)? @doc . + (external (value_name) @name.definition.function) @definition.function + (#strip! @doc "^\\(\\*\\*?\\s*|\\s\\*\\)$") +) + +(application_expression + function: (value_path (value_name) @name.reference.call)) @reference.call + +(infix_expression + left: (value_path (value_name) @name.reference.call) + (infix_operator) @reference.call + (#eq? @reference.call "@@")) + +(infix_expression + (infix_operator) @reference.call + right: (value_path (value_name) @name.reference.call) + (#eq? @reference.call "|>")) + +; Operator +;--------- + +( + (comment)? @doc . + (value_definition + (let_binding + pattern: (parenthesized_operator [ + (prefix_operator) + (infix_operator) + (hash_operator) + (indexing_operator) + (let_operator) + (and_operator) + (match_operator) + ] @name.definition.function)) @definition.function) + (#strip! @doc "^\\(\\*\\*?\\s*|\\s\\*\\)$") +) + +[ + (prefix_operator) + (sign_operator) + (infix_operator) + (hash_operator) + (indexing_operator) + (let_operator) + (and_operator) + (match_operator) +] @name.reference.call @reference.call + +; Classes +;-------- + +( + (comment)? @doc . + [ + (class_definition (class_binding (class_name) @name.definition.class) @definition.class) + (class_type_definition (class_type_binding (class_type_name) @name.definition.class) @definition.class) + ] + (#strip! @doc "^\\(\\*\\*?\\s*|\\s\\*\\)$") +) + +[ + (class_path (class_name) @name.reference.class) + (class_type_path (class_type_name) @name.reference.class) +] @reference.class + +; Methods +;-------- + +( + (comment)? @doc . + (method_definition (method_name) @name.definition.method) @definition.method + (#strip! @doc "^\\(\\*\\*?\\s*|\\s\\*\\)$") +) + +(method_invocation (method_name) @name.reference.call) @reference.call diff --git a/core/tag-qry/tree-sitter-php-tags.scm b/core/tag-qry/tree-sitter-php-tags.scm new file mode 100644 index 000000000..61c86fcbe --- /dev/null +++ b/core/tag-qry/tree-sitter-php-tags.scm @@ -0,0 +1,26 @@ +(class_declaration + name: (name) @name.definition.class) @definition.class + +(function_definition + name: (name) @name.definition.function) @definition.function + +(method_declaration + name: (name) @name.definition.function) @definition.function + +(object_creation_expression + [ + (qualified_name (name) @name.reference.class) + (variable_name (name) @name.reference.class) + ]) @reference.class + +(function_call_expression + function: [ + (qualified_name (name) @name.reference.call) + (variable_name (name)) @name.reference.call + ]) @reference.call + +(scoped_call_expression + name: (name) @name.reference.call) @reference.call + +(member_call_expression + name: (name) @name.reference.call) @reference.call diff --git a/core/tag-qry/tree-sitter-python-tags.scm b/core/tag-qry/tree-sitter-python-tags.scm new file mode 100644 index 000000000..3be5bed9f --- /dev/null +++ b/core/tag-qry/tree-sitter-python-tags.scm @@ -0,0 +1,12 @@ +(class_definition + name: (identifier) @name.definition.class) @definition.class + +(function_definition + name: (identifier) @name.definition.function) @definition.function + +(call + function: [ + (identifier) @name.reference.call + (attribute + attribute: (identifier) @name.reference.call) + ]) @reference.call diff --git a/core/tag-qry/tree-sitter-ql-tags.scm b/core/tag-qry/tree-sitter-ql-tags.scm new file mode 100644 index 000000000..3164aa25a --- /dev/null +++ b/core/tag-qry/tree-sitter-ql-tags.scm @@ -0,0 +1,26 @@ +(classlessPredicate + name: (predicateName) @name.definition.function) @definition.function + +(memberPredicate + name: (predicateName) @name.definition.method) @definition.method + +(aritylessPredicateExpr + name: (literalId) @name.reference.call) @reference.call + +(module + name: (moduleName) @name.definition.module) @definition.module + +(dataclass + name: (className) @name.definition.class) @definition.class + +(datatype + name: (className) @name.definition.class) @definition.class + +(datatypeBranch + name: (className) @name.definition.class) @definition.class + +(qualifiedRhs + name: (predicateName) @name.reference.call) @reference.call + +(typeExpr + name: (className) @name.reference.type) @reference.type diff --git a/core/tag-qry/tree-sitter-ruby-tags.scm b/core/tag-qry/tree-sitter-ruby-tags.scm new file mode 100644 index 000000000..79e71d2d6 --- /dev/null +++ b/core/tag-qry/tree-sitter-ruby-tags.scm @@ -0,0 +1,64 @@ +; Method definitions + +( + (comment)* @doc + . + [ + (method + name: (_) @name.definition.method) @definition.method + (singleton_method + name: (_) @name.definition.method) @definition.method + ] + (#strip! @doc "^#\\s*") + (#select-adjacent! @doc @definition.method) +) + +(alias + name: (_) @name.definition.method) @definition.method + +(setter + (identifier) @ignore) + +; Class definitions + +( + (comment)* @doc + . + [ + (class + name: [ + (constant) @name.definition.class + (scope_resolution + name: (_) @name.definition.class) + ]) @definition.class + (singleton_class + value: [ + (constant) @name.definition.class + (scope_resolution + name: (_) @name.definition.class) + ]) @definition.class + ] + (#strip! @doc "^#\\s*") + (#select-adjacent! @doc @definition.class) +) + +; Module definitions + +( + (module + name: [ + (constant) @name.definition.module + (scope_resolution + name: (_) @name.definition.module) + ]) @definition.module +) + +; Calls + +(call method: (identifier) @name.reference.call) @reference.call + +( + [(identifier) (constant)] @name.reference.call @reference.call + (#is-not? local) + (#not-match? @name.reference.call "^(lambda|load|require|require_relative|__FILE__|__LINE__)$") +) diff --git a/core/tag-qry/tree-sitter-rust-tags.scm b/core/tag-qry/tree-sitter-rust-tags.scm new file mode 100644 index 000000000..dadfa7acb --- /dev/null +++ b/core/tag-qry/tree-sitter-rust-tags.scm @@ -0,0 +1,60 @@ +; ADT definitions + +(struct_item + name: (type_identifier) @name.definition.class) @definition.class + +(enum_item + name: (type_identifier) @name.definition.class) @definition.class + +(union_item + name: (type_identifier) @name.definition.class) @definition.class + +; type aliases + +(type_item + name: (type_identifier) @name.definition.class) @definition.class + +; method definitions + +(declaration_list + (function_item + name: (identifier) @name.definition.method)) @definition.method + +; function definitions + +(function_item + name: (identifier) @name.definition.function) @definition.function + +; trait definitions +(trait_item + name: (type_identifier) @name.definition.interface) @definition.interface + +; module definitions +(mod_item + name: (identifier) @name.definition.module) @definition.module + +; macro definitions + +(macro_definition + name: (identifier) @name.definition.macro) @definition.macro + +; references + +(call_expression + function: (identifier) @name.reference.call) @reference.call + +(call_expression + function: (field_expression + field: (field_identifier) @name.reference.call)) @reference.call + +(macro_invocation + macro: (identifier) @name.reference.call) @reference.call + +; implementations + +(impl_item + trait: (type_identifier) @name.reference.implementation) @reference.implementation + +(impl_item + type: (type_identifier) @name.reference.implementation + !trait) @reference.implementation diff --git a/core/tag-qry/tree-sitter-typescript-tags.scm b/core/tag-qry/tree-sitter-typescript-tags.scm new file mode 100644 index 000000000..b1ab874dd --- /dev/null +++ b/core/tag-qry/tree-sitter-typescript-tags.scm @@ -0,0 +1,23 @@ +(function_signature + name: (identifier) @name.definition.function) @definition.function + +(method_signature + name: (property_identifier) @name.definition.method) @definition.method + +(abstract_method_signature + name: (property_identifier) @name.definition.method) @definition.method + +(abstract_class_declaration + name: (type_identifier) @name.definition.class) @definition.class + +(module + name: (identifier) @name.definition.module) @definition.module + +(interface_declaration + name: (type_identifier) @name.definition.interface) @definition.interface + +(type_annotation + (type_identifier) @name.reference.type) @reference.type + +(new_expression + constructor: (identifier) @name.reference.class) @reference.class diff --git a/core/util/treeSitter.ts b/core/util/treeSitter.ts index 3e82e0811..f0e9814b3 100644 --- a/core/util/treeSitter.ts +++ b/core/util/treeSitter.ts @@ -1,4 +1,5 @@ import * as path from "path"; +import { Language } from "web-tree-sitter"; const Parser = require("web-tree-sitter"); export const supportedLanguages: { [key: string]: string } = { @@ -71,6 +72,21 @@ export async function getParserForFile(filepath: string) { try { await Parser.init(); const parser = new Parser(); + + const language = await getLanguageForFile(filepath); + parser.setLanguage(language); + + return parser; + } catch (e) { + console.error("Unable to load language for file", filepath, e); + return undefined; + } +} + +export async function getLanguageForFile( + filepath: string, +): Promise { + try { const extension = path.extname(filepath).slice(1); if (!supportedLanguages[extension]) { @@ -89,8 +105,7 @@ export async function getParserForFile(filepath: string) { `tree-sitter-${supportedLanguages[extension]}.wasm`, ); const language = await Parser.Language.load(wasmPath); - parser.setLanguage(language); - return parser; + return language; } catch (e) { console.error("Unable to load language for file", filepath, e); return undefined;