Repository navigation
Add a 'tags' crate, for computing ctags-style code navigation tags - #583
Merged
Merged
Conversation
Co-Authored-By: Patrick Thomson <[email protected]>
Co-Authored-By: Patrick Thomson <[email protected]>
Co-Authored-By: Patrick Thomson <[email protected]>
Co-Authored-By: Patrick Thomson <[email protected]>
Co-Authored-By: Patrick Thomson <[email protected]>
Avoid adding and removing states for these patterns on every node in the tree by just skipping the wildcard step of the matching process
Contributor
|
Will there be bindings for JS similar to how tree sitter works at the moment and will there be docs on this? |
Contributor
Author
|
We'll write docs for this soon, but currently we're still changing the interface as we integrate this into github.com. |
Contributor
Author
|
We probably won't include this as part of the wasm library, but the actual code that implements this is pretty small: https://github.com/tree-sitter/tree-sitter/blob/master/tags/src/lib.rs. So once we stabilize and document the interface, I think it would be easy to implement an equivalent system in JavaScript, or another language. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
We're exploring ways to make GitHub's code navigation features more easily extensible by external contributors. Now that we've shipped our new syntax highlighting system based on tree queries, we think that we may be able to use a similar system to drive our "fuzzy" code navigation data as well.
This PR adds a new sub-crate to the project called
tree-sitter-tags, which provides functionality for computing ctags-style code navigation data based on tree queries.API
Like the
tree-sitter-highlightcrate,tree-sitter-tagshas two build targets: a rust library, and a static C library. The Rust library is consumed by a new subcommand in the CLI:tree-sitter tags. Right now, this subcommand is similar to thequerysubcommand, in that it's mainly useful for testing debugging.The C library is designed to easily be linked into a project written in another programming language. Its API is declared in
tags.h.Query Format
The idea is that, to enable tagging, you would add one new query to a tree-sitter grammar repository:
queries/tags.scm. This query has a pretty simple structure. The patterns should have the following captures:@name(required) - a node containing a symbol name@doc(optional) - a node whose text contains the documentation for a symbol. Multiple nodes can be captured in cases where the documentation is spread across multiple nodes.@call- a node that represents a function or method call@method- a node that represents a method definition@function- a node that represents a function definition@classa node that represents a class definition@modulea node that represents a module definitionPatterns can also have the following predicates:
(strip! @docs "some-regex")- This allows certain parts of the@docsnode's text to be removed from the captured documentation.(set-adjacent! @docs @some-node)- Specifies that docs should only be taken from nodes that are on adjacent lines as@some-node.Fundamental Query API Improvements
As part of this work, we needed to slightly increase the power of the tree query language. We have added a
+operator that can be used to capture an arbitrary number of repeating nodes. Examples:You can match a class declaration and capture one or more preceding comments. This is the use case we found in the
tree-sitter-tagscrate.But repetition also works for nodes with children. For example, you can match a method definition and and capture one or more decorators:
Future Work
Now that there is a
+operator in queries, which is analogous to+in regular expressions, it might make sense to make the*operator work like it does in regexes too. But the*is currently used as a "wildcard". It might be worth revisiting the query syntax as a whole, to try and make it as familiar and consistent as possible. I think that we can defer this to a later PR.