From 4c35c0b75d678d3f901d8dd37428ae834c4bd689 Mon Sep 17 00:00:00 2001 From: Colin Kennedy Date: Sat, 6 May 2023 16:00:17 -0700 Subject: [PATCH] Added USD as a language + some queries Added USD highlight unittests Added USD indentation Added USD indents.scm file Added USD indent rules Simplified USD highlight groups Added explicit highlight groups Added newline Removed after/ftdetect/usda.lua usd/indents.scm - Added inline comments explaining the nodes Fixed USD highlight group unittests Added indentation unittests Simplified USD `@identifier` highlighting Added USD documentation highlight group tests Simplified USD highlight groups Moved USD `@identifier` and `@namespace` highlights into one place Removed and simplified tests/query/highlights/usd Simplified tests/query/highlights/usd more Removed trailing newline Combined USD test files Added `set filetype=usd` to minimal_init.lua --- README.md | 1 + lua/nvim-treesitter/parsers.lua | 8 + queries/usd/folds.scm | 3 + queries/usd/highlights.scm | 181 +++++++++++++++++++++ queries/usd/indents.scm | 26 +++ queries/usd/locals.scm | 4 + scripts/minimal_init.lua | 1 + tests/indent/usd/prim.usd | 6 + tests/indent/usd_spec.lua | 21 +++ tests/query/highlights/usd/prims.usda | 118 ++++++++++++++ tests/query/highlights/usd/properties.usda | 21 +++ tests/query/highlights/usd/subLayers.usda | 9 + 12 files changed, 399 insertions(+) create mode 100644 queries/usd/folds.scm create mode 100644 queries/usd/highlights.scm create mode 100644 queries/usd/indents.scm create mode 100644 queries/usd/locals.scm create mode 100644 tests/indent/usd/prim.usd create mode 100644 tests/indent/usd_spec.lua create mode 100644 tests/query/highlights/usd/prims.usda create mode 100644 tests/query/highlights/usd/properties.usda create mode 100644 tests/query/highlights/usd/subLayers.usda diff --git a/README.md b/README.md index 9938df2c4..3b9f577cc 100644 --- a/README.md +++ b/README.md @@ -345,6 +345,7 @@ We are looking for maintainers to add more parsers and to write query files for - [x] [twig](https://github.com/gbprod/tree-sitter-twig) (maintained by @gbprod) - [x] [typescript](https://github.com/tree-sitter/tree-sitter-typescript) (maintained by @steelsojka) - [x] [ungrammar](https://github.com/Philipp-M/tree-sitter-ungrammar) (maintained by @Philipp-M, @amaanq) +- [x] [usd](https://github.com/ColinKennedy/tree-sitter-usd) (maintained by @ColinKennedy) - [x] [uxn tal](https://github.com/amaanq/tree-sitter-uxntal) (maintained by @amaanq) - [x] [v](https://github.com/vlang/vls) (maintained by @kkharji) - [x] [vala](https://github.com/vala-lang/tree-sitter-vala) (maintained by @Prince781) diff --git a/lua/nvim-treesitter/parsers.lua b/lua/nvim-treesitter/parsers.lua index c4ed3d720..8cd86213a 100644 --- a/lua/nvim-treesitter/parsers.lua +++ b/lua/nvim-treesitter/parsers.lua @@ -1522,6 +1522,14 @@ list.ungrammar = { maintainers = { "@Philipp-M", "@amaanq" }, } +list.usd = { + install_info = { + url = "https://github.com/ColinKennedy/tree-sitter-usd", + files = { "src/parser.c" }, + }, + maintainers = { "@ColinKennedy" }, +} + list.uxntal = { install_info = { url = "https://github.com/amaanq/tree-sitter-uxntal", diff --git a/queries/usd/folds.scm b/queries/usd/folds.scm new file mode 100644 index 000000000..c4815bd07 --- /dev/null +++ b/queries/usd/folds.scm @@ -0,0 +1,3 @@ +[ + (block) +] @fold diff --git a/queries/usd/highlights.scm b/queries/usd/highlights.scm new file mode 100644 index 000000000..73968a647 --- /dev/null +++ b/queries/usd/highlights.scm @@ -0,0 +1,181 @@ +(None) @constant.builtin +(asset_path) @text.uri +(attribute_property) @property +(bool) @boolean +(comment) @comment @spell +(custom) @function.builtin +(float) @float +(integer) @number +(orderer) @function.call +(prim_path) @string.special +(relationship_type) @type +(string) @string +(uniform) @function.builtin +(variant_set_definition) @keyword + +;; Prefer namespace highlighting, if any. +;; +;; e.g. `rel fizz` - `fizz` uses `@identifier` +;; e.g. `rel foo:bar:fizz` - `foo` and `bar` use `@namespace` and `fizz` uses `@identifier` +;; +(identifier) @variable +(namespace_identifier) @namespace +(namespace_identifier + (identifier) @namespace +) + +[ + "class" + "def" + "over" +] @keyword.function + +["(" ")" "[" "]" "{" "}"] @punctuation.bracket +[":" ";" "."] @punctuation.delimiter + +[ + "=" +] @operator + +(attribute_type) @type +( + ;; Reference: https://openusd.org/release/api/sdf_page_front.html + (attribute_type) @type.builtin + (#any-of? @type.builtin + ;; Scalar types + "asset" "asset[]" + "bool" "bool[]" + "double" "double[]" + "float" "float[]" + "half" "half[]" + "int" "int[]" + "int64" "int64[]" + "string" "string[]" + "timecode" "timecode[]" + "token" "token[]" + "uchar" "uchar[]" + "uint" "uint[]" + "uint64" "uint64[]" + + ;; Dimensioned Types + "double2" "double2[]" + "double3" "double3[]" + "double4" "double4[]" + "float2" "float2[]" + "float3" "float3[]" + "float4" "float4[]" + "half2" "half2[]" + "half3" "half3[]" + "half4" "half4[]" + "int2" "int2[]" + "int3" "int3[]" + "int4" "int4[]" + "matrix2d" "matrix2d[]" + "matrix3d" "matrix3d[]" + "matrix4d" "matrix4d[]" + "quatd" "quatd[]" + "quatf" "quatf[]" + "quath" "quath[]" + + ;; Extra Types + "color3f" "color3f[]" + "normal3f" "normal3f[]" + "point3f" "point3f[]" + "texCoord2f" "texCoord2f[]" + "vector3d" "vector3d[]" + "vector3f" "vector3f[]" + "vector3h" "vector3h[]" + + "dictionary" + + ;; Deprecated Types + "EdgeIndex" "EdgeIndex[]" + "FaceIndex" "FaceIndex[]" + "Matrix4d" "Matrix4d[]" + "PointIndex" "PointIndex[]" + "PointFloat" "PointFloat[]" + "Transform" "Transform[]" + "Vec3f" "Vec3f[]" + ) +) + +( + (identifier) @keyword + (#any-of? @keyword + + ;; Reference: https://openusd.org/release/api/sdf_page_front.html + ;; LIVRPS names + "inherits" + "payload" + "references" + "specializes" + "variantSets" + "variants" + + ; assetInfo names + "assetInfo" + "identifier" + "name" + "payloadAssetDependencies" + "version" + + ;; clips names + "clips" + + "active" + "assetPaths" + "manifestAssetPath" + "primPath" + "templateAssetPath" + "templateEndTime" + "templateStartTime" + "templateStride" + "times" + + ;; customData names + "customData" + + "apiSchemaAutoApplyTo" + "apiSchemaOverridePropertyNames" + "className" + "extraPlugInfo" + "isUsdShadeContainer" + "libraryName" + "providesUsdShadeConnectableAPIBehavior" + "requiresUsdShadeEncapsulation" + "skipCodeGeneration" + + ;; Layer metadata names + "colorConfiguration" + "colorManagementSystem" + "customLayerData" + "defaultPrim" + "doc" + "endTimeCode" + "framesPerSecond" + "owner" + "startTimeCode" + "subLayers" + + ;; Prim metadata + "instanceable" + ) +) + +;; Common attribute metadata +( + (layer_offset + (identifier) @keyword + (#any-of? @keyword + + "offset" + "scale" + ) + ) +) + +;; Docstrings in USD +(metadata + (comment)* + (string) @comment.documentation +) diff --git a/queries/usd/indents.scm b/queries/usd/indents.scm new file mode 100644 index 000000000..4af153034 --- /dev/null +++ b/queries/usd/indents.scm @@ -0,0 +1,26 @@ +[ + (block) ; The {}s in `def "foo" { ... Attributes / Prims here ... }` + (dictionary) ; The {}s in `dictionary foo = { string "foo" = "bar" }` + (list) + (list_proxy) ; [@foo.usda@, @bar.usda@] + (metadata) ; ( anything = "goes-here" ) + (prim_paths) ; [, <../bar>] + (timeSamples) ; The {}s in `int value.timeSamples = { 0: 1, -10: 10, ... }` + (tuple) + (variant_set_definition) ; The {}s in `variantSet "foo" = { "vr1" { ... } "vr2" { ... } }` +] @indent.begin + +(block "}" @indent.end) +(dictionary "}" @indent.end) +(list "]" @indent.end) +(list_proxy "]" @indent.end) +(metadata ")" @indent.end) +(timeSamples "}" @indent.end) +(tuple ")" @indent.end) +(variant_set_definition "}" @indent.end) + +[ + ")" + "]" + "}" +] @indent.branch diff --git a/queries/usd/locals.scm b/queries/usd/locals.scm new file mode 100644 index 000000000..4e7e97b5f --- /dev/null +++ b/queries/usd/locals.scm @@ -0,0 +1,4 @@ +[ + (block) + (metadata) +] @scope diff --git a/scripts/minimal_init.lua b/scripts/minimal_init.lua index e47b6701a..356745b61 100644 --- a/scripts/minimal_init.lua +++ b/scripts/minimal_init.lua @@ -5,6 +5,7 @@ vim.cmd [[runtime! plugin/nvim-treesitter.lua]] vim.cmd [[au BufRead,BufNewFile *.conf set filetype=hocon]] vim.cmd [[au BufRead,BufNewFile *.gleam set filetype=gleam]] vim.cmd [[au BufRead,BufNewFile *.ncl set filetype=nickel]] +vim.cmd [[au BufRead,BufNewFile *.usd,*.usda set filetype=usd]] vim.o.swapfile = false vim.bo.swapfile = false diff --git a/tests/indent/usd/prim.usd b/tests/indent/usd/prim.usd new file mode 100644 index 000000000..4bfbff955 --- /dev/null +++ b/tests/indent/usd/prim.usd @@ -0,0 +1,6 @@ +#usda 1.0 + +def "foo" ( +) +{ +} diff --git a/tests/indent/usd_spec.lua b/tests/indent/usd_spec.lua new file mode 100644 index 000000000..c4e8198aa --- /dev/null +++ b/tests/indent/usd_spec.lua @@ -0,0 +1,21 @@ +local Runner = require("tests.indent.common").Runner + +local run = Runner:new(it, "tests/indent/usd", { + tabstop = 4, + shiftwidth = 4, + softtabstop = 4, + expandtab = true, +}) + +describe("indent USD:", function() + describe("whole file:", function() + run:whole_file(".", { + expected_failures = {}, + }) + end) + + describe("new line:", function() + run:new_line("prim.usd", { on_line = 3, text = "active = false", indent = 4 }) + run:new_line("prim.usd", { on_line = 5, text = "custom int foo = 10", indent = 4 }) + end) +end) diff --git a/tests/query/highlights/usd/prims.usda b/tests/query/highlights/usd/prims.usda new file mode 100644 index 000000000..eeac67d74 --- /dev/null +++ b/tests/query/highlights/usd/prims.usda @@ -0,0 +1,118 @@ +def Xform "cube" ( + assetInfo = { + # <- keyword + asset[] payloadAssetDependencies = [@fizz.usd@, @buzz.usd@] + # <- type + # ^ keyword + # ^ text.uri + # ^ text.uri + } +) +{ +} + +def "root" ( + add references = @foo.usda@ (offset = 1; scale = 2.0) + # <- text.uri + # ^ string.special + # ^ keyword + # ^ number + # ^ punctuation.delimiter + # ^ keyword + # ^ float +) +{ +} + +def "World" +{ + over "points" ( + clips = { + # <- keyword + dictionary default = { + # <- type + # ^ variable + double2[] times = [(101, 101), (102, 102)] + # <- type + # ^ keyword + # ^ number + } + } + ) + { + } +} + +def Xform "torch_2" ( + payload = @./torch.usda@ + kind = "model" +) +{ + // Pre-published light list + # <- comment + rel lightList = [ ] # inline comment + # ^ comment + token lightList:cacheBehavior = "consumeAndContinue" + + double3 xformOp:translate = (1, 0, 0.5) + uniform token[] xformOpOrder = ["xformOp:translate"] +} + +def "foo" ( + "some comment" + # <- comment.documentation +) +{ +} + +def "foo" ( + # inline comment + "actual in-description comment" + # <- comment.documentation +) +{ +} + +def "foo" ( + add references = @foo.usda@ + # <- function.call + append references = @foo.usda@ + # <- function.call + delete references = @foo.usda@ + # <- function.call + reorder references = [@foo.usda@] + # <- function.call + + references = [@foo.usda@] # explicit +) +{ +} + +over "Parent" ( + prepend references = [, @./ref.usda@] + # <- function.call + # ^ keyword + # ^ string.special + # ^ text.uri + # ^ string.special +) +{ +} + +def "foo" +{ + float value.timeSamples = { + # <- type + # ^ variable + # ^ property + -414: 14.4 + # <- number + # ^ float + 10: 201.0, + # <- number + # ^ float + 10.123: 201.0123, + # <- float + # ^ float + } +} diff --git a/tests/query/highlights/usd/properties.usda b/tests/query/highlights/usd/properties.usda new file mode 100644 index 000000000..67df8a623 --- /dev/null +++ b/tests/query/highlights/usd/properties.usda @@ -0,0 +1,21 @@ +dictionary foo = {} +# <- type +half[] foo = [2, 1, 2] +# <- type +string foo = "something" +# <- type +timecode time = 1.0 +# <- type +token[] purpose = ["default", "render"] +# <- type + +rel material:binding:collection:Erasers = None +# <- type +# ^ namespace +# ^ punctuation.delimiter +# ^ namespace +# ^ punctuation.delimiter +# ^ namespace +# ^ punctuation.delimiter +# ^ variable +# ^ constant.builtin diff --git a/tests/query/highlights/usd/subLayers.usda b/tests/query/highlights/usd/subLayers.usda new file mode 100644 index 000000000..b4dfa6298 --- /dev/null +++ b/tests/query/highlights/usd/subLayers.usda @@ -0,0 +1,9 @@ +#usda 1.0 +( + subLayers = [ + # <- keyword + @./model_sub.usda@ (offset = 1) + # <- text.uri + # ^ keyword + ] +)