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 every-day cases with support for latest features like globs, filters and variables.

The goal is to provide 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",
  branch = "v0.7.1",
},

[!TIP] We recommend using branch set to the latest release tag instead of main, as we cannot guarantee that there won't be breaking changes, because that's fully on d2 team. But if there are, we simply follow the latest release.

Helix

Fortunately Helix has it's own guide on how to add new languages and grammars.

A quick example for installing d2 would be:

  1. Add 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's using tree-sitter, please open an issue. Would be great if you could also provide instructions for other 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

This section is outdated and not maintained

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 example are my random homework, thus being in Russian, please don't try 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.1.tar.gz (32.8 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.1-cp39-abi3-win_arm64.whl (26.7 kB view details)

Uploaded CPython 3.9+Windows ARM64

tree_sitter_d2-0.7.1-cp39-abi3-win_amd64.whl (28.0 kB view details)

Uploaded CPython 3.9+Windows x86-64

tree_sitter_d2-0.7.1-cp39-abi3-musllinux_1_2_x86_64.whl (49.5 kB view details)

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

tree_sitter_d2-0.7.1-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (48.3 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64

tree_sitter_d2-0.7.1-cp39-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (49.5 kB view details)

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

tree_sitter_d2-0.7.1-cp39-abi3-macosx_11_0_arm64.whl (28.1 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

tree_sitter_d2-0.7.1-cp39-abi3-macosx_10_9_x86_64.whl (27.8 kB view details)

Uploaded CPython 3.9+macOS 10.9+ x86-64

File details

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

File metadata

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

File hashes

Hashes for tree_sitter_d2-0.7.1.tar.gz
Algorithm Hash digest
SHA256 aaafe103b491d64d340ee892ec8c9fc42e4cf2dca5574564cdb6acc084aad2cf
MD5 f2b33ac381c77d17e2916d1085f0fa0e
BLAKE2b-256 ea047e32d274534f935ae8c42f8a5137e45fa2909983f6d5684e779f1a83f3c2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_d2-0.7.1-cp39-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 8f98d4c9a3472abead396005be99e746750348dbc93a00f29b1df3a377164810
MD5 993c2c17a0df9cdfcd2f65e64d2a10d1
BLAKE2b-256 169038a2deb83a8aace2eb77bef28b9e829c485c259f96a91824c5c722a436c2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_d2-0.7.1-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 661aafa51c3404543e3e4171c900812598b4daabeb22e4d473f366ffe641a489
MD5 b10a67225d800faebdf2535513f54d9e
BLAKE2b-256 c0f366bd48dec71bc7a17469a1c7d8e589fd8bc312a69029f0a8f8007a2d9d39

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_d2-0.7.1-cp39-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 08f57953be26443666afc9b060f43e01df80ce3229d05fc5ff3528da69706c57
MD5 dabd8d4382bfec20e30be4db1a9235f3
BLAKE2b-256 a153f0f231ecdf4d29d1fdf2b0b2ed76261e5e3b854be9668aae48447857890f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_d2-0.7.1-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 80935e4b61e1ce9093ca81f01fb30987fc21e6a265c1235c3a10aad40078856c
MD5 912b92ba803e0bca405699b997053e66
BLAKE2b-256 af992f79e18cd3d846b364ae4da5d2bfb1d499e9647807ec56a6d3ff7511886a

See more details on using hashes here.

File details

Details for the file tree_sitter_d2-0.7.1-cp39-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for tree_sitter_d2-0.7.1-cp39-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b6b926a667074363ffcb36cf7a6db6d93e7ff77b43ffdd35059f275f9c4ebc74
MD5 2ebb13df8c55e06ee51920e4f4d5d52a
BLAKE2b-256 7136f307bb5684262d33b791997588145205695f4b9ca7c4f744802a562ed0c9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_d2-0.7.1-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c5de4ba97355d48a3de71622e6c2c7d67edf104b7dc807d18b7021e31765053a
MD5 918fc194a9e65521625440d6ec20d730
BLAKE2b-256 c32cec68c2c06e859c8f557f09dbbefb062dcd081795030d1d6f0c0da24c0db0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_d2-0.7.1-cp39-abi3-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 bab20a1a14331bba7760c3ade71e8b0648842f77f0d2c01f3c1b7dbee9892ac3
MD5 1e8c2b4c848da69c218a1a20edddc67a
BLAKE2b-256 c01bf0f22496fb479702f60da7b39b8db4d51fe3606a050ab197f818b7c883b9

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