Skip to main content

D2 grammar for tree-sitter

Project description

tree-sitter-d2

Build Test

Table of Contents

Description

Tree Sitter grammar for d2lang (github). It's not yet finished, but usable for everyday cases with support for latest features like globs, filters and variables.

The goal is to provide a better experience for existing keywords, code blocks injections, folds, etc.

  • Better parsing of modern structures - The latest supported version of d2 is v0.7.0.
  • Built with nvim-treesitter in mind - We respect and follow its highlight groups.
  • Keywords - All keywords from the documentation are treated as such. (Open an issue if any are missing.)
  • Tests - We aim to cover all edge cases and examples from the documentation, resulting in more robust parsing. Currently, we have more than 100 tests. (This will be reduced later when the grammar becomes stable.)

I've never done anything remotely close to parsing grammar, so there could be some bad patterns and simply bad code. Feel free to open issues.

Installation

Neovim

via lazy.nvim

{
  "ravsii/tree-sitter-d2",
  dependencies = { "nvim-treesitter/nvim-treesitter" },
  build = "make nvim-install",
},

And then :TSInstall d2. After reloading Neovim everything should be working.

Helix

Fortunately Helix has its own guide on how to add new languages and grammars.

A quick example for installing d2 would be:

  1. Add a new language to languages.toml
[[language]]
name = "d2"
scope = "source.git"
file-types = ["d2"]
comment-token = "#"
roots = [""]
indent = { tab-width = 2, unit = "  " }

[[grammar]]
source = { git = "https://github.com/ravsii/tree-sitter-d2", rev = "main" }
name = "d2"
  1. hx --grammar fetch && hx --grammar build
  2. Add your queries to ~/.config/helix/runtime/queries/d2
~/.config/helix/runtime/queries/d2
├── highlights.scm
├── injections.scm
└── locals.scm

[!WARNING]
We found out that Helix is using tree-sitter v0.22.x which makes it incompatible with this grammar, as it was built using tree-sitter v0.25.x.

If the highlighting doesn't work AND :log-open has this error:

Incompatible language version 15. Expected minimum 13, maximum 14

then try using these options

[[grammar]]
source = { git = "https://github.com/ravsii/tree-sitter-d2", rev = "main-helix" }
name = "d2"

For more info see: https://github.com/ravsii/tree-sitter-d2/issues/102

Other editors

If you're using some other editor that uses tree-sitter, please open an issue. Would be great if you could also provide instructions for others how to install it.

Showcase

Tokyo Night Storm theme with CaskaydiaCove font.

Props to Neovim's Tokyo Night theme for supporting pretty much every highlighting group.

https://github.com/user-attachments/assets/a9bd20d0-eb6c-43c2-8e58-7d211d350025

Contribution

See CONTRIBUTION.md

Comparison

[!NOTE] This section is left "as is" from the very early development. It's outdated and not maintained, but it should give you a good perspective on the differences between these two grammars.

Actually, there's another tree-sitter-d2 by pleshevskiy. My project's initial goal was to fix issues his grammar had, but later I realized that his grammar is not maintained and lacks many features d2 has added since then.

Here's a list of comparisons as of v0.2 version of this project, both using Tokyonight Storm theme.

Please note that:

  • On the left is our project using Neovim, on the right is pleshevskiy's grammar using Helix (I was unable to install pleshevskiy's grammar in Neovim)
  • Some examples are my random homework, thus being in Russian, please don't try to make sense of them. Thanks! :D

Better consistency overall

cmp1 cmp2

Better handling of foreign languages

cmpru1 cmpru2

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tree_sitter_d2-0.7.2.tar.gz (33.9 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

tree_sitter_d2-0.7.2-cp39-abi3-win_arm64.whl (27.4 kB view details)

Uploaded CPython 3.9+Windows ARM64

tree_sitter_d2-0.7.2-cp39-abi3-win_amd64.whl (28.4 kB view details)

Uploaded CPython 3.9+Windows x86-64

tree_sitter_d2-0.7.2-cp39-abi3-musllinux_1_2_x86_64.whl (50.3 kB view details)

Uploaded CPython 3.9+musllinux: musl 1.2+ x86-64

tree_sitter_d2-0.7.2-cp39-abi3-musllinux_1_2_aarch64.whl (49.0 kB view details)

Uploaded CPython 3.9+musllinux: musl 1.2+ ARM64

tree_sitter_d2-0.7.2-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (50.3 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64manylinux: glibc 2.28+ ARM64

tree_sitter_d2-0.7.2-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (51.4 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.28+ x86-64manylinux: glibc 2.5+ x86-64

tree_sitter_d2-0.7.2-cp39-abi3-macosx_11_0_arm64.whl (28.5 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

tree_sitter_d2-0.7.2-cp39-abi3-macosx_10_9_x86_64.whl (28.5 kB view details)

Uploaded CPython 3.9+macOS 10.9+ x86-64

File details

Details for the file tree_sitter_d2-0.7.2.tar.gz.

File metadata

  • Download URL: tree_sitter_d2-0.7.2.tar.gz
  • Upload date:
  • Size: 33.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tree_sitter_d2-0.7.2.tar.gz
Algorithm Hash digest
SHA256 6330479065e55bea8dc12291260d4d6cb4f01c6379c6cf6a5b53bfb1558a3426
MD5 06c17bc079ccf4e7ac45a2f79e5b6a93
BLAKE2b-256 0fc9cab3185367c986df772ee440081cd11567fd64138a9cb1b9e8c4caee4a54

See more details on using hashes here.

File details

Details for the file tree_sitter_d2-0.7.2-cp39-abi3-win_arm64.whl.

File metadata

File hashes

Hashes for tree_sitter_d2-0.7.2-cp39-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 9d2beb6355b53ae621e17e0f34118436cb5670a303fded9dc177dcc52555133d
MD5 cad0984f5641e85e3e555a04566ebe20
BLAKE2b-256 597e4850e8ba85b40d9c825c0987315ba49a7327a0c02552b39b597e1c970353

See more details on using hashes here.

File details

Details for the file tree_sitter_d2-0.7.2-cp39-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for tree_sitter_d2-0.7.2-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 0645bc540a5d8a7e5e8ef01a0c991737904b0ecf44939115f7fd6a24107a77cf
MD5 c74621b3bee67876c93d3694c46397da
BLAKE2b-256 57e08dfa7756489043733fbdb31170eb0456f5c4bf3bb0d9e2cacfa1a95e7266

See more details on using hashes here.

File details

Details for the file tree_sitter_d2-0.7.2-cp39-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for tree_sitter_d2-0.7.2-cp39-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 83221c765d529e649d28f3834a9cdec660f609366f37ead725a55dbd61f8fd3c
MD5 b00b7eb908bfa13c1d673f1086078668
BLAKE2b-256 b70224f90d1396275ccc77a8196405bb927c73e3c98eff5f71b42baa238c7aa0

See more details on using hashes here.

File details

Details for the file tree_sitter_d2-0.7.2-cp39-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for tree_sitter_d2-0.7.2-cp39-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 a5b7cfb57d87bca314ab4bc9ce0024458b9622e7f085e6145f9a2d48fb2920fa
MD5 443e63792e670cb0d8778d7dac8861d9
BLAKE2b-256 67a490601fdf9e9c57168ff019a9a4d82e213472c8a9ae5ec1fd66d9175a9fed

See more details on using hashes here.

File details

Details for the file tree_sitter_d2-0.7.2-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for tree_sitter_d2-0.7.2-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 112d0cc24d68d8ba587bf8b6558959b4c855d7c379137428f1348e29a8ac69fb
MD5 fce7d156b4ba3cdb46f2491ff4614799
BLAKE2b-256 5182d7a2789861f4b454ebb01b50820e80185e51a8475e0a4b7c7cda809984ff

See more details on using hashes here.

File details

Details for the file tree_sitter_d2-0.7.2-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for tree_sitter_d2-0.7.2-cp39-abi3-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 bbe07f8195976340547a54f6280d89ab6158cd7b4b84bff47ccdee5f709d91f4
MD5 d1fba22953cd1ded406e6fa87170b9a4
BLAKE2b-256 3049a7ff02a3dc1bb843c06917041e78821e74bce9bb4a7fbc494e34b8cb99c3

See more details on using hashes here.

File details

Details for the file tree_sitter_d2-0.7.2-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tree_sitter_d2-0.7.2-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bd70799263ad55d82dc3abc61a458246115a80b6a1b9b7fc0daa036d696f031e
MD5 874385d9b44ff6eb75ebc8054019ed29
BLAKE2b-256 dbcc202f9d535b9d01f080c33f6fc90ca6849a01fcc282731e41d6d452d748a2

See more details on using hashes here.

File details

Details for the file tree_sitter_d2-0.7.2-cp39-abi3-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for tree_sitter_d2-0.7.2-cp39-abi3-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 9fbb134314bd25388f1b7f996277e0af5fa6d016677f60e13f8a5bdc14dc8775
MD5 08796fc91b58e193902710b12d22402d
BLAKE2b-256 6cce2abef9c9526ecb1dd3603e44c55ebd3a656abe8cd90f265f8aa44cc5d4f6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page