Skip to content

ColinKennedy/tree-sitter-usd

Repository files navigation

tree-sitter-usd

This library parses USD ASCII files using tree-sitter to produce a light-weight grammar of the file.

For those who don't know what tree-sitter is and why you'd care to use it, see Why Tree-sitter?. For install / usage instructions, see below.

Disclaimer

This repository's parsing rules are subject to change.

Building + Using

Neovim

Make sure you include the following somewhere in your init.lua file.

require("nvim-treesitter.configs").setup {
    ensure_installed = {"usd"},
    parser_install_dir = installation_directory,
    highlight = { enable = true },

    -- More stuff
}

Why Tree-sitter?

In the beginning, Tree-sitter was made to give text editors better syntax highlighting.

Most text editors today create syntax highlighting with regex patterns. On large files with long line counts, this approach is slow and error-prone.

In contrast to regex, Tree-sitter actually knows about your file. It can convert a USD file like:

#usda 1.0

def "root"
{
    custom uniform int value = 10
}

Into a tokenized tree like this:

(prim_definition) ; [3:1 - 2:5]
 (prim_type) ; [3:1 - 4:2]
 (string) ; [3:5 - 11:2]
 (block) ; [4:1 - 2:5]
  (attribute_assignment) ; [5:5 - 34:4]
   (custom) ; [5:5 - 11:4]
   (uniform) ; [5:12 - 19:4]
   (attribute_type) ; [5:20 - 23:4]
   (identifier) ; [5:24 - 29:4]
   (integer) ; [5:32 - 34:4]

That tree is built sparsely, interactively, and even works with WIP files where you may be missing a } or two. Tree-sitter is accurate, fast, and getting better all the time.

Having this tree is really powerful. It became clear very quickly to others that Tree-sitter can be used for a lot more than just syntax highlighting. Here's some of the fun plug-ins showing off what you can do using this USD parser.

Neovim

aerial.nvim - Navigate USD Files Effortlessly

aerial.nvim

Display And Move Through A Prim Tree

Effortlessly move in, out, or around any USD Prim, no matter how large it is.

navigate_within_prim_tree.mp4
Prim Tree Based On Your Current Position

Many times I find myself thinking "I'm in a nested Prim but I actually need to go one down, and over". This aerial.nvim view is super good at moving around.

move_up_and_drop_the_prim_hiearchy.mp4

Syntax Highlighting

Tree-sitter is an incremental parser. That means

  • Parsing is lightning quick
  • Making edits to the file doesn't require a full re-parse of the file
  • WIP files with syntax errors still parse

usd_treesitter_syntax_highlighting

And the results are pretty good. My Neovim theme is hybrid2.nvim. If you desire even more colors (e.g. coloring uniform as blue, instead of white), there's already an out-of-box highlight group for that over at nvim-treesitter-highlights-usd. In the future, this might get upstreamed to nvim-treesitter, maybe.

Maintain The Current Prim Context

prim_treesitter_context.mp4

nvim-treesitter-context

Have you ever been viewing a huge USD file and, in the middle of viewing some Prim, forget the name / tree of the Prim that you're viewing? This fun plug-in keeps the Prim name pinned as you scroll so you never lose your place.

Prim Statusline

nvim-gps + winbar.nvim

treesitter_usd_statusline.mp4

The top bar tracks your location in the file.

Auto-Folding

prim_auto_folding.mp4

nvim-treesitter

Text Objects

nvim-treesitter-textobjects

Select, move, delete, comment, edit anything easily, using whatever mappings you desire.

In truth, most people don't have much need to edit USD files directly. But it's a testiment to tree-sitter that the same mappings do as you expect across all languages.

Need A Parser? Look No Further

USD of course has parsing capabilities but, at the time of writing, most of the parsing classes and functions are private. On top of that, it's a multi-million like repository written in C++.

In contrast, tree-sitter

Tree-sitter is easy to embed and extend, making it very attractive for plug-in authors.

Future Improvements

Plug-Ins

There's a bunch of open-source momentum behind tree-sitter. New tools and plug-ins may come out that further expands upon the list of reasons above.

Some other plug-ins that could be useful in the future

And others

Neovim 0.10+

I spotted a couple Neovim roadmap items that seem to want to make tree-sitter faster and more async. It's already fast but more speed is definitely welcome on larger USD files. Needless to say I'll be keeping an eye on those!

Testing

Unittests

cd {root}
tree-sitter test

All tests should pass.

Highlighting

If everything worked correctly, you should be able to highlight any USD file from the tree-sitter CLI like so:

tree-sitter highlight /path/to/file.usda

You should see something like this

tree-sitter_example_usd_hightlighting

And the next time you run tree-sitter test, highlighting information will be in the output.

syntax highlighting:
  ✓ payload.usda (N assertions)
  ✓ references.usda (N assertions)
  ✓ relationship.usda (N assertions)
  ✓ specializes.usda (N assertions)
  ✓ string.usda (N assertions)

  ...

Actual USD Files

The best way to test tree-sitter-usd is to parse USD files in-action.

The basic steps are

  • Download from any of the links above
  • Install the tree-sitter-cli
  • Find + parse the files. e.g.
find /path/to/your/root/usd_files/folder -name "*.usda" -type f | xargs tree-sitter parse

tree-sitter-usd parses all of the files, everywhere, without errors.

Contributing

If you find a bug in a USD file, please submit an issue or pull request specifying the expected parse and the actual results.