Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding tag queries #791

Merged
merged 4 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions docs/src/content/docs/reference/scripts/parsers.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,15 @@ library. It returns an AST (Abstract Syntax Tree) that can be used to analyze th

```js
// the whole tree
const [tree] = await parsers.code(file)
const { captures } = await parsers.code(file)
// with a query
const captures = await parsers.code(file, "(interface_declaration) @i")
const { captures } = await parsers.code(file, "(interface_declaration) @i")
```

The `tags` query is a built-in alias for the [tree-sitter `tags` query](https://tree-sitter.github.io/tree-sitter/code-navigation-systems) that is made available in most tree-sitter libraries.

````js
const { captures } = await parsers.code(file, 'tags')
```

## Math expression
Expand All @@ -222,7 +228,7 @@ The `parsers.math` function uses [mathjs](https://mathjs.org/) to parse a math e

```js
const res = await parsers.math("1 + 1")
```
````

## .env

Expand Down
41 changes: 41 additions & 0 deletions genaisrc/update-tree-sitter-queries.genai.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Octokit } from "octokit"

const files: Record<string, string> = {}
const downloadScm = async (repo: string, name: string) => {
const repoName = repo.replace(/^tree-sitter-/, "")
try {
const res = await client.rest.repos.getContent({
owner: "tree-sitter",
repo,
path: `queries/${name}.scm`,
mediaType: { format: "raw" },
})
if (res.status === 200) {
// download and stream content to a file
await workspace.writeText(
`packages/core/src/queries/${repoName}/${name}.scm`,
res.data
)
files[`${repoName}/${name}`] = res.data
}
} catch (e) {}
}

const { client } = await github.client()

const repos = await client.rest.repos.listForOrg({
org: "tree-sitter",
})
const tsRepos = repos.data.filter((repo) =>
repo.name.startsWith("tree-sitter-")
)
console.log(`Found ${tsRepos.length} tree-sitter repos`)
for (const repo of tsRepos) {
console.log(`Updating queries for ${repo.name}`)
await downloadScm(repo.name, "tags")
}

await workspace.writeText(
"packages/core/src/treesitterqueries.json",
JSON.stringify(files, null, 2)
)
6 changes: 4 additions & 2 deletions packages/cli/src/codequery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { logVerbose } from "../../core/src/util"
* @param files - A glob pattern to match files for querying.
* @param query - The Tree-sitter query to be executed on each file.
*/
export async function codeQuery(files: string, query: string) {
export async function codeQuery(files: string, query: OptionsOrString<"tags">) {
// Find files matching the given pattern, respecting .gitignore rules.
const ffs = await host.findFiles(files, {
applyGitIgnore: true, // Ensure .gitignore rules are applied when finding files
Expand All @@ -40,7 +40,9 @@ export async function codeQuery(files: string, query: string) {
const res = await treeSitterQuery(f, query)

// Serialize and collect the query capture results
captures.push(...res.map((r) => serializeQueryCapture(f.filename, r)))
captures.push(
...res.captures.map((r) => serializeQueryCapture(f.filename, r))
)
}

// Output the collected captures in YAML format
Expand Down
23 changes: 23 additions & 0 deletions packages/core/src/queries/c-sharp/tags.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
(class_declaration name: (identifier) @name) @definition.class

(class_declaration (base_list (_) @name)) @reference.class

(interface_declaration name: (identifier) @name) @definition.interface

(interface_declaration (base_list (_) @name)) @reference.interface

(method_declaration name: (identifier) @name) @definition.method

(object_creation_expression type: (identifier) @name) @reference.class

(type_parameter_constraints_clause (identifier) @name) @reference.class

(type_parameter_constraint (type type: (identifier) @name)) @reference.class

(variable_declaration type: (identifier) @name) @reference.class

(invocation_expression function: (member_access_expression name: (identifier) @name)) @reference.send

(namespace_declaration name: (identifier) @name) @definition.module

(namespace_declaration name: (identifier) @name) @module
9 changes: 9 additions & 0 deletions packages/core/src/queries/c/tags.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(struct_specifier name: (type_identifier) @name body:(_)) @definition.class

(declaration type: (union_specifier name: (type_identifier) @name)) @definition.class

(function_declarator declarator: (identifier) @name) @definition.function

(type_definition declarator: (type_identifier) @name) @definition.type

(enum_specifier name: (type_identifier) @name) @definition.type
15 changes: 15 additions & 0 deletions packages/core/src/queries/cpp/tags.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
(struct_specifier name: (type_identifier) @name body:(_)) @definition.class

(declaration type: (union_specifier name: (type_identifier) @name)) @definition.class

(function_declarator declarator: (identifier) @name) @definition.function

(function_declarator declarator: (field_identifier) @name) @definition.function

(function_declarator declarator: (qualified_identifier scope: (namespace_identifier) @local.scope name: (identifier) @name)) @definition.method

(type_definition declarator: (type_identifier) @name) @definition.type

(enum_specifier name: (type_identifier) @name) @definition.type

(class_specifier name: (type_identifier) @name) @definition.class
30 changes: 30 additions & 0 deletions packages/core/src/queries/go/tags.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
(
(comment)* @doc
.
(function_declaration
name: (identifier) @name) @definition.function
(#strip! @doc "^//\\s*")
(#set-adjacent! @doc @definition.function)
)

(
(comment)* @doc
.
(method_declaration
name: (field_identifier) @name) @definition.method
(#strip! @doc "^//\\s*")
(#set-adjacent! @doc @definition.method)
)

(call_expression
function: [
(identifier) @name
(parenthesized_expression (identifier) @name)
(selector_expression field: (field_identifier) @name)
(parenthesized_expression (selector_expression field: (field_identifier) @name))
]) @reference.call

(type_spec
name: (type_identifier) @name) @definition.type

(type_identifier) @name @reference.type
20 changes: 20 additions & 0 deletions packages/core/src/queries/java/tags.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
(class_declaration
name: (identifier) @name) @definition.class

(method_declaration
name: (identifier) @name) @definition.method

(method_invocation
name: (identifier) @name
arguments: (argument_list) @reference.call)

(interface_declaration
name: (identifier) @name) @definition.interface

(type_list
(type_identifier) @name) @reference.implementation

(object_creation_expression
type: (type_identifier) @name) @reference.class

(superclass (type_identifier) @name) @reference.class
99 changes: 99 additions & 0 deletions packages/core/src/queries/javascript/tags.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
(
(comment)* @doc
.
(method_definition
name: (property_identifier) @name) @definition.method
(#not-eq? @name "constructor")
(#strip! @doc "^[\\s\\*/]+|^[\\s\\*/]$")
(#select-adjacent! @doc @definition.method)
)

(
(comment)* @doc
.
[
(class
name: (_) @name)
(class_declaration
name: (_) @name)
] @definition.class
(#strip! @doc "^[\\s\\*/]+|^[\\s\\*/]$")
(#select-adjacent! @doc @definition.class)
)

(
(comment)* @doc
.
[
(function_expression
name: (identifier) @name)
(function_declaration
name: (identifier) @name)
(generator_function
name: (identifier) @name)
(generator_function_declaration
name: (identifier) @name)
] @definition.function
(#strip! @doc "^[\\s\\*/]+|^[\\s\\*/]$")
(#select-adjacent! @doc @definition.function)
)

(
(comment)* @doc
.
(lexical_declaration
(variable_declarator
name: (identifier) @name
value: [(arrow_function) (function_expression)]) @definition.function)
(#strip! @doc "^[\\s\\*/]+|^[\\s\\*/]$")
(#select-adjacent! @doc @definition.function)
)

(
(comment)* @doc
.
(variable_declaration
(variable_declarator
name: (identifier) @name
value: [(arrow_function) (function_expression)]) @definition.function)
(#strip! @doc "^[\\s\\*/]+|^[\\s\\*/]$")
(#select-adjacent! @doc @definition.function)
)

(assignment_expression
left: [
(identifier) @name
(member_expression
property: (property_identifier) @name)
]
right: [(arrow_function) (function_expression)]
) @definition.function

(pair
key: (property_identifier) @name
value: [(arrow_function) (function_expression)]) @definition.function

(
(call_expression
function: (identifier) @name) @reference.call
(#not-match? @name "^(require)$")
)

(call_expression
function: (member_expression
property: (property_identifier) @name)
arguments: (_) @reference.call)

(new_expression
constructor: (_) @name) @reference.class

(export_statement value: (assignment_expression left: (identifier) @name right: ([
(number)
(string)
(identifier)
(undefined)
(null)
(new_expression)
(binary_expression)
(call_expression)
]))) @definition.constant
Loading
Loading