tree-sitter-d2


Nametree-sitter-d2 JSON
Version 0.0.1 PyPI version JSON
download
home_pageNone
SummaryD2 grammar for tree-sitter
upload_time2025-02-02 10:11:05
maintainerNone
docs_urlNone
authorNone
requires_python>=3.9
licenseMIT
keywords incremental parsing tree-sitter d2
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # tree-sitter-d2

[![Build](https://github.com/ravsii/tree-sitter-d2/actions/workflows/build.yml/badge.svg?branch=main)](https://github.com/ravsii/tree-sitter-d2/actions/workflows/build.yml)
[![Test](https://github.com/ravsii/tree-sitter-d2/actions/workflows/test.yml/badge.svg?branch=main)](https://github.com/ravsii/tree-sitter-d2/actions/workflows/test.yml)

## Table of Contents

<!--toc:start-->

- [tree-sitter-d2](#tree-sitter-d2)
  - [Table of Contents](#table-of-contents)
  - [Description](#description)
  - [Installation](#installation)
    - [Neovim](#neovim)
    - [Helix](#helix)
    - [Other editors](#other-editors)
  - [Showcase](#showcase)
  - [Contribution](#contribution)
  - [Comparison](#comparison)
    - [Better consistency overall](#better-consistency-overall)
    - [Better handling of foreign languages](#better-handling-of-foreign-languages)
    <!--toc:end-->

## 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.6.8**.
- **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.)

[highlight]: https://neovim.io/doc/user/treesitter.html#_treesitter-queries
[d2lang]: https://d2lang.com/
[github]: https://github.com/terrastruct/d2

---

_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]

```lua
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,
  },
})
```

2. Do `:TSInstall d2`
3. 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`

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

Example using default [nvim-treesitter] dir with [lazy.nvim]

```text
~/.local/share/nvim/lazy/nvim-treesitter/queries/d2
├── highlights.scm
├── injections.scm
└── locals.scm
```

4. Check if `:TSModuleInfo` has `highlight` option enabled.

[lazy.nvim]: https://github.com/folke/lazy.nvim

### Helix

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

[guide]: https://docs.helix-editor.com/guides/adding_languages.html

A quick example for installing `d2` would be:

1. Add new language to `languages.toml`

```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"

```

2. `hx --grammar fetch` && `hx --grammar build`
3. Add your queries to `~/.config/helix/runtime/queries/d2`

```text
~/.config/helix/runtime/queries/d2
├── highlights.scm
├── injections.scm
└── locals.scm
```

### 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.

[nvim-treesitter]: https://github.com/nvim-treesitter/nvim-treesitter

## Showcase

`Tokyo Night Storm` theme with `CaskaydiaCove` font.

_Props to Neovim's Tokyo Night theme for supporting pretty much every
[highlighting group]._

[highlighting group]: <https://neovim.io/doc/user/treesitter.html#_treesitter-queries:~:text=the%20exact%20definition)%3A-,%40variable,-various%20variable%20names>

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

## Contribution

See [CONTRIBUTION.md](/CONTRIBUTING.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

[Helix]: https://helix-editor.com/
[tree-sitter-d2]: https://github.com/pleshevskiy/tree-sitter-d2

### Better consistency overall

![cmp1](./pics/cmp1.png)
![cmp2](./pics/cmp2.png)

### Better handling of foreign languages

![cmpru1](./pics/cmp_ru1.png)
![cmpru2](./pics/cmp_ru2.png)

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "tree-sitter-d2",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": "incremental, parsing, tree-sitter, d2",
    "author": null,
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/90/ef/14f2f7e1728c2bd29a7c3bf4c278ecb3fc7a728d88b224e4be9ca702f728/tree_sitter_d2-0.0.1.tar.gz",
    "platform": null,
    "description": "# tree-sitter-d2\n\n[![Build](https://github.com/ravsii/tree-sitter-d2/actions/workflows/build.yml/badge.svg?branch=main)](https://github.com/ravsii/tree-sitter-d2/actions/workflows/build.yml)\n[![Test](https://github.com/ravsii/tree-sitter-d2/actions/workflows/test.yml/badge.svg?branch=main)](https://github.com/ravsii/tree-sitter-d2/actions/workflows/test.yml)\n\n## Table of Contents\n\n<!--toc:start-->\n\n- [tree-sitter-d2](#tree-sitter-d2)\n  - [Table of Contents](#table-of-contents)\n  - [Description](#description)\n  - [Installation](#installation)\n    - [Neovim](#neovim)\n    - [Helix](#helix)\n    - [Other editors](#other-editors)\n  - [Showcase](#showcase)\n  - [Contribution](#contribution)\n  - [Comparison](#comparison)\n    - [Better consistency overall](#better-consistency-overall)\n    - [Better handling of foreign languages](#better-handling-of-foreign-languages)\n    <!--toc:end-->\n\n## Description\n\nTree Sitter grammar for [d2lang] \\([github]\\). It's not yet finished, but usable for\nevery-day cases with support for latest features like globs, filters and\nvariables.\n\nThe goal is to provide better experience for existing keywords, code blocks\ninjections, folds, etc.\n\n- **Better parsing of modern structures** - The latest supported version of d2\n  is **v0.6.8**.\n- **Built with [nvim-treesitter] in mind** - We respect and follow its\n  [highlight] groups.\n- **Keywords** - _All_ keywords from the documentation are treated as such.\n  (Open an issue if any are missing.)\n- **Tests** - We aim to cover all edge cases and examples from the\n  documentation, resulting in more robust parsing. Currently, we have more\n  than 100 tests. (This will be reduced later when the grammar becomes stable.)\n\n[highlight]: https://neovim.io/doc/user/treesitter.html#_treesitter-queries\n[d2lang]: https://d2lang.com/\n[github]: https://github.com/terrastruct/d2\n\n---\n\n_I've never done anything remotely close to parsing grammar, so there could be\nsome bad patterns and simply bad code. Feel free to open issues._\n\n## Installation\n\n### Neovim\n\n1. Add this to your config, after installing [nvim-treesitter]\n\n```lua\nlocal parser_config = require(\"nvim-treesitter.parsers\").get_parser_configs()\nparser_config.d2 = {\n  install_info = {\n    url = \"https://github.com/ravsii/tree-sitter-d2\",\n    files = { \"src/parser.c\" },\n    branch = \"main\"\n  },\n  filetype = \"d2\",\n}\n\n-- we also need to tell neovim to use \"d2\" filetype on \"*.d2\" files, as well as\n-- token comment.\n-- ftplugin/autocmd is also an option.\nvim.filetype.add({\n  extension = {\n    d2 = function()\n      return \"d2\", function(bufnr)\n        vim.bo[bufnr].commentstring = \"# %s\"\n      end\n    end,\n  },\n})\n```\n\n2. Do `:TSInstall d2`\n3. Copy queries to one of your `rtp` paths. Usually `~/.config/nvim/` will do\n   the job. You can check it using `:echo &rtp` command.\n\nExample using `~/.config/nvim`\n\n```text\n~/.config/nvim/queries/d2\n\u251c\u2500\u2500 highlights.scm\n\u251c\u2500\u2500 injections.scm\n\u2514\u2500\u2500 locals.scm\n```\n\nExample using default [nvim-treesitter] dir with [lazy.nvim]\n\n```text\n~/.local/share/nvim/lazy/nvim-treesitter/queries/d2\n\u251c\u2500\u2500 highlights.scm\n\u251c\u2500\u2500 injections.scm\n\u2514\u2500\u2500 locals.scm\n```\n\n4. Check if `:TSModuleInfo` has `highlight` option enabled.\n\n[lazy.nvim]: https://github.com/folke/lazy.nvim\n\n### Helix\n\nFortunately Helix has it's own [guide] on how to add new languages and\ngrammars.\n\n[guide]: https://docs.helix-editor.com/guides/adding_languages.html\n\nA quick example for installing `d2` would be:\n\n1. Add new language to `languages.toml`\n\n```toml\n[[language]]\nname = \"d2\"\nscope = \"source.git\"\nfile-types = [\"d2\"]\ncomment-token = \"#\"\nroots = [\"\"]\nindent = { tab-width = 2, unit = \"  \" }\n\n[[grammar]]\nsource = { git = \"https://github.com/ravsii/tree-sitter-d2\", rev = \"main\" }\nname = \"d2\"\n\n```\n\n2. `hx --grammar fetch` && `hx --grammar build`\n3. Add your queries to `~/.config/helix/runtime/queries/d2`\n\n```text\n~/.config/helix/runtime/queries/d2\n\u251c\u2500\u2500 highlights.scm\n\u251c\u2500\u2500 injections.scm\n\u2514\u2500\u2500 locals.scm\n```\n\n### Other editors\n\nIf you're using some other editor that's using tree-sitter, please open an\nissue. Would be great if you could also provide instructions for other how to\ninstall it.\n\n[nvim-treesitter]: https://github.com/nvim-treesitter/nvim-treesitter\n\n## Showcase\n\n`Tokyo Night Storm` theme with `CaskaydiaCove` font.\n\n_Props to Neovim's Tokyo Night theme for supporting pretty much every\n[highlighting group]._\n\n[highlighting group]: <https://neovim.io/doc/user/treesitter.html#_treesitter-queries:~:text=the%20exact%20definition)%3A-,%40variable,-various%20variable%20names>\n\n<https://github.com/user-attachments/assets/a9bd20d0-eb6c-43c2-8e58-7d211d350025>\n\n## Contribution\n\nSee [CONTRIBUTION.md](/CONTRIBUTING.md)\n\n## Comparison\n\n**This section is outdated and not maintained**\n\nActually, there's another [tree-sitter-d2] by pleshevskiy. My project's initial\ngoal was to fix issues his grammar had, but later I realized that his grammar\nis not maintained and lacks many features `d2` has added since then.\n\nHere's a list of comparisons as of `v0.2` version of this project, both using\n`Tokyonight Storm` theme.\n\nPlease note that:\n\n- On the left is our project using Neovim, on the right is pleshevskiy's\n  grammar using [Helix] (I was unable to install pleshevskiy's grammar in Neovim)\n- Some example are my random homework, thus being in Russian, please don't try\n  make sense of them. Thanks! :D\n\n[Helix]: https://helix-editor.com/\n[tree-sitter-d2]: https://github.com/pleshevskiy/tree-sitter-d2\n\n### Better consistency overall\n\n![cmp1](./pics/cmp1.png)\n![cmp2](./pics/cmp2.png)\n\n### Better handling of foreign languages\n\n![cmpru1](./pics/cmp_ru1.png)\n![cmpru2](./pics/cmp_ru2.png)\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "D2 grammar for tree-sitter",
    "version": "0.0.1",
    "project_urls": {
        "Homepage": "https://github.com/tree-sitter/tree-sitter-d2"
    },
    "split_keywords": [
        "incremental",
        " parsing",
        " tree-sitter",
        " d2"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "1eff834b45aaf6279d2aef6a9e0b5c2d68fc68df53b8542a49a7b5112ab9d8e4",
                "md5": "e4d9d3b7d82d31c66d775cf3bf73f6f3",
                "sha256": "0e85131f4d37ee089ff32b0bfdf03ff07a57a1cdbf948da4e4e81526d06153ae"
            },
            "downloads": -1,
            "filename": "tree_sitter_d2-0.0.1-cp39-abi3-macosx_10_9_x86_64.whl",
            "has_sig": false,
            "md5_digest": "e4d9d3b7d82d31c66d775cf3bf73f6f3",
            "packagetype": "bdist_wheel",
            "python_version": "cp39",
            "requires_python": ">=3.9",
            "size": 30961,
            "upload_time": "2025-02-02T10:10:55",
            "upload_time_iso_8601": "2025-02-02T10:10:55.063254Z",
            "url": "https://files.pythonhosted.org/packages/1e/ff/834b45aaf6279d2aef6a9e0b5c2d68fc68df53b8542a49a7b5112ab9d8e4/tree_sitter_d2-0.0.1-cp39-abi3-macosx_10_9_x86_64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "181ecae27a2b9124a2af05206100ee52a739b369c5e424cec6e5e173f02e0347",
                "md5": "0884356612db103350f106ed9a33c2e4",
                "sha256": "3982ed754222aeb3f792d8feb4b53b5b08ef44ddc58f551bed09cf317ef64749"
            },
            "downloads": -1,
            "filename": "tree_sitter_d2-0.0.1-cp39-abi3-macosx_11_0_arm64.whl",
            "has_sig": false,
            "md5_digest": "0884356612db103350f106ed9a33c2e4",
            "packagetype": "bdist_wheel",
            "python_version": "cp39",
            "requires_python": ">=3.9",
            "size": 31098,
            "upload_time": "2025-02-02T10:10:56",
            "upload_time_iso_8601": "2025-02-02T10:10:56.936834Z",
            "url": "https://files.pythonhosted.org/packages/18/1e/cae27a2b9124a2af05206100ee52a739b369c5e424cec6e5e173f02e0347/tree_sitter_d2-0.0.1-cp39-abi3-macosx_11_0_arm64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "fa968ca605aaab40a4c8ec3d1179e06c00b9453c2795bcf6a84a6559f2e34c6a",
                "md5": "2d419b57311dcee706d1862a29879777",
                "sha256": "88446bb42cb92a2649bc81c723a1e233a0ff3d3946caed3607b4f4b495f53d59"
            },
            "downloads": -1,
            "filename": "tree_sitter_d2-0.0.1-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl",
            "has_sig": false,
            "md5_digest": "2d419b57311dcee706d1862a29879777",
            "packagetype": "bdist_wheel",
            "python_version": "cp39",
            "requires_python": ">=3.9",
            "size": 53041,
            "upload_time": "2025-02-02T10:10:58",
            "upload_time_iso_8601": "2025-02-02T10:10:58.511455Z",
            "url": "https://files.pythonhosted.org/packages/fa/96/8ca605aaab40a4c8ec3d1179e06c00b9453c2795bcf6a84a6559f2e34c6a/tree_sitter_d2-0.0.1-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "69dc99c32290ec60a175e510e8a6da98a336c4801bf38ec13c490853e0c1a075",
                "md5": "75056d0e5e7b29a9197d8dd2b133240c",
                "sha256": "fec2590064f6c44944e71081f39ebb63646f7002dd828b2c47591a14f4b34caf"
            },
            "downloads": -1,
            "filename": "tree_sitter_d2-0.0.1-cp39-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
            "has_sig": false,
            "md5_digest": "75056d0e5e7b29a9197d8dd2b133240c",
            "packagetype": "bdist_wheel",
            "python_version": "cp39",
            "requires_python": ">=3.9",
            "size": 54361,
            "upload_time": "2025-02-02T10:10:59",
            "upload_time_iso_8601": "2025-02-02T10:10:59.526958Z",
            "url": "https://files.pythonhosted.org/packages/69/dc/99c32290ec60a175e510e8a6da98a336c4801bf38ec13c490853e0c1a075/tree_sitter_d2-0.0.1-cp39-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "66348b7212f5e1af09779f1dd8ac3898daec3162dcd9fa50cdf8abcc9c635928",
                "md5": "d517a3acfbbd4c9b462fed2c7e3b52d4",
                "sha256": "13ee3c4d0d882f9d2ebed48f020f43aac64d1380e6fbce46e4a083ae583cba1d"
            },
            "downloads": -1,
            "filename": "tree_sitter_d2-0.0.1-cp39-abi3-musllinux_1_2_x86_64.whl",
            "has_sig": false,
            "md5_digest": "d517a3acfbbd4c9b462fed2c7e3b52d4",
            "packagetype": "bdist_wheel",
            "python_version": "cp39",
            "requires_python": ">=3.9",
            "size": 52013,
            "upload_time": "2025-02-02T10:11:00",
            "upload_time_iso_8601": "2025-02-02T10:11:00.917362Z",
            "url": "https://files.pythonhosted.org/packages/66/34/8b7212f5e1af09779f1dd8ac3898daec3162dcd9fa50cdf8abcc9c635928/tree_sitter_d2-0.0.1-cp39-abi3-musllinux_1_2_x86_64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "1c653465b27488da211b9711c91412b80185bb8d938f9473093c1d0be417cfdb",
                "md5": "32ceb2e2e9c73afe4a2e8e663a3b00f6",
                "sha256": "8ebe8f4113606d4fc03d8fcd8876e95770190555792d88b9c083e4f1abe6ed5b"
            },
            "downloads": -1,
            "filename": "tree_sitter_d2-0.0.1-cp39-abi3-win_amd64.whl",
            "has_sig": false,
            "md5_digest": "32ceb2e2e9c73afe4a2e8e663a3b00f6",
            "packagetype": "bdist_wheel",
            "python_version": "cp39",
            "requires_python": ">=3.9",
            "size": 30723,
            "upload_time": "2025-02-02T10:11:02",
            "upload_time_iso_8601": "2025-02-02T10:11:02.498567Z",
            "url": "https://files.pythonhosted.org/packages/1c/65/3465b27488da211b9711c91412b80185bb8d938f9473093c1d0be417cfdb/tree_sitter_d2-0.0.1-cp39-abi3-win_amd64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "082947e5f9ec98c34c738ae095a7222b75af2e9ca3089ef582a46ef99f84cf98",
                "md5": "622be57afcfcc9ddebc1cbfdc7fd6b20",
                "sha256": "7e0f20378fdd1d2d58f3ea8c5d1e0aa86fb4379757f0ef4eb31a4b1a1099eaec"
            },
            "downloads": -1,
            "filename": "tree_sitter_d2-0.0.1-cp39-abi3-win_arm64.whl",
            "has_sig": false,
            "md5_digest": "622be57afcfcc9ddebc1cbfdc7fd6b20",
            "packagetype": "bdist_wheel",
            "python_version": "cp39",
            "requires_python": ">=3.9",
            "size": 29247,
            "upload_time": "2025-02-02T10:11:04",
            "upload_time_iso_8601": "2025-02-02T10:11:04.012649Z",
            "url": "https://files.pythonhosted.org/packages/08/29/47e5f9ec98c34c738ae095a7222b75af2e9ca3089ef582a46ef99f84cf98/tree_sitter_d2-0.0.1-cp39-abi3-win_arm64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "90ef14f2f7e1728c2bd29a7c3bf4c278ecb3fc7a728d88b224e4be9ca702f728",
                "md5": "c7c701a997e71406220e9e08b852d438",
                "sha256": "60488ddb8f913d4acc7a0140ea62583c5bd526047875de7b3a518c52f766bdc9"
            },
            "downloads": -1,
            "filename": "tree_sitter_d2-0.0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "c7c701a997e71406220e9e08b852d438",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 39104,
            "upload_time": "2025-02-02T10:11:05",
            "upload_time_iso_8601": "2025-02-02T10:11:05.314808Z",
            "url": "https://files.pythonhosted.org/packages/90/ef/14f2f7e1728c2bd29a7c3bf4c278ecb3fc7a728d88b224e4be9ca702f728/tree_sitter_d2-0.0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-02-02 10:11:05",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "tree-sitter",
    "github_project": "tree-sitter-d2",
    "github_not_found": true,
    "lcname": "tree-sitter-d2"
}
        
Elapsed time: 0.47130s