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

  1. Add this to your config, after installing nvim-treesitter
local parser_config = require("nvim-treesitter.parsers").get_parser_configs()
parser_config.d2 = {
  install_info = {
    url = "https://github.com/ravsii/tree-sitter-d2",
    files = { "src/parser.c" },
    branch = "main"
  },
  filetype = "d2",
}

-- we also need to tell neovim to use "d2" filetype on "*.d2" files, as well as
-- token comment.
-- ftplugin/autocmd is also an option.
vim.filetype.add({
  extension = {
    d2 = function()
      return "d2", function(bufnr)
        vim.bo[bufnr].commentstring = "# %s"
      end
    end,
  },
})
  1. Do :TSInstall d2
  2. Copy queries to one of your rtp paths. Usually ~/.config/nvim/ will do the job. You can check it using :echo &rtp command.

Example using ~/.config/nvim

~/.config/nvim/queries/d2
├── highlights.scm
├── injections.scm
└── locals.scm

Example using default nvim-treesitter dir with lazy.nvim

~/.local/share/nvim/lazy/nvim-treesitter/queries/d2
├── highlights.scm
├── injections.scm
└── locals.scm
  1. Check if :TSModuleInfo has highlight option enabled.

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.0.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.0-cp39-abi3-win_arm64.whl (26.9 kB view details)

Uploaded CPython 3.9+Windows ARM64

tree_sitter_d2-0.7.0-cp39-abi3-win_amd64.whl (28.2 kB view details)

Uploaded CPython 3.9+Windows x86-64

tree_sitter_d2-0.7.0-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.0-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.0-cp39-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (49.4 kB view details)

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

tree_sitter_d2-0.7.0-cp39-abi3-macosx_11_0_arm64.whl (28.2 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

tree_sitter_d2-0.7.0-cp39-abi3-macosx_10_9_x86_64.whl (28.0 kB view details)

Uploaded CPython 3.9+macOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: tree_sitter_d2-0.7.0.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.0.tar.gz
Algorithm Hash digest
SHA256 6af5b732b7c445e5151d8e0b19b2acb814784080525d3e50e13d2ec95fc6a3bc
MD5 a612c5e3209b2716df7d7b5909703383
BLAKE2b-256 9d9fc09470985fb0f52419d71d6907dbeab4420a62ce1f9cb690ef006145d3dc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_d2-0.7.0-cp39-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 b49c20204cc612596968ebb21a36f7af57e1e5960f4cd2482e036e6ad097997a
MD5 a88c7724c175103d22069a7c1b729c39
BLAKE2b-256 71ba1c389e8875ad96a3ec1cdfc6c60252b9d21d4d656f75b85fa8106fbb694d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_d2-0.7.0-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 ac520f555291ea09f292dc5c3411849db5647d0087688fc04ec7b51972a34aec
MD5 aa6bec33f0dc80155b92c39b7a97383e
BLAKE2b-256 717a6abe01c6cd66de3d1fab544b53a05bdcf51aca14c84c82829bcdadfbf42a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_d2-0.7.0-cp39-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 cb4c75d56b0fc8aedf8846825f6120ddf7cddff9cafded5d61b5d351d4a67916
MD5 a76b2febb6593c0ca695624324950438
BLAKE2b-256 9d7ecb9f04db78cb24cd0fc69d3e8afdf6e5ac9c48bf8f4f974791cc50682f74

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_d2-0.7.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 30237ba84e3fa7c5f5df172e766401538c2eee1a3b97e17de3b26610998e8200
MD5 7302a8c1f390052c502e1591875131f0
BLAKE2b-256 deec6fd756bdb14ed511e4bae1969f3fa706b9f2590271de2be656f0b16cb347

See more details on using hashes here.

File details

Details for the file tree_sitter_d2-0.7.0-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.0-cp39-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5d58c5c988b310d8bb015bf0e9301ab152755f4cd96f2677b927ba3b25425193
MD5 e20b40d9028ab70efb098d0a2ddfafb2
BLAKE2b-256 797da500a2a9abc59f97ecb173457b83cff88420583d485079957df633093996

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_d2-0.7.0-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 cc21ef74efa96af231c3b1d93bb1a720c42891fb6d8542984bd0b6d74f8d877d
MD5 d75add360b8ae7ef437a7dc7a4717bf0
BLAKE2b-256 b854a1c9f3d0492ad32536fe8000af8df874ff53b312b583c23aeadf85be0f1d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_d2-0.7.0-cp39-abi3-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 0f4673b84a5f04a73ed4583cc1319e41c4ce87a81bfcf8c9d70a51a1113f072f
MD5 f56f21dd047d463391500f1fe71be7d3
BLAKE2b-256 b1a0753c106356da038db60cb13c83093d715e628b8d0497c868a178d4714336

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