Skip to content

About

slim parser for tree-sitter

Resources

Stars

4 stars

Watchers

1 watching

Forks

Repository files navigation

Tree-sitter slim parse

Slim parser for tree-sitter.

warning, this code is at it's alpha stage.

When this project began, no Slim parser was available. While an alternative now exists, I chose to continue because my approach differs.

Goals

This project aims to:

  • Support all slim features except custom syntax configurations.
  • Follow tree-sitter guidelines.
  • Enable static analysis, Ruby linting, and support for other embedded languages through injections.

Performance will improve once the syntax tree stabilizes. That said, the plugin is functional and lag-free on modern computers.

Installation

Neovim

Without plugin

Starting Neovim 0.12 and if you don't use nvim-treesitter plugin, install the grammar and highlight with the following command:

tree-sitter generate && \
tree-sitter build -o slim.so && \
make install-nvim

This will copy relevant files in ~/.config/nvim/parser and ~/.config/nvim/queries.

Execute make uninstall-nvim to uninstall.

You might need to install highlights from nvim-treesitter (or another source) for injected languages. To make it easier, use the provided script:

bash download-highlighters.sh

This will automatically download highlighters for all injected languages.

With nvim-treesitter

Install it with :TSUpdate slim and for any further injected language.

Telescope

To get highlighting in Telescope previewer, use the following settings:

-- Monkey patch plenary to detect slim files
require('plenary.filetype').add_table { extension = { slim = 'slim' } }

-- Extend the previewer to detect and assign the right filetype to slim files
require('telescope').setup {
  defaults = {
    buffer_previewer_maker = function(filepath, bufnr, opts)
      opts = opts or {}
      if opts.use_ft_detect == nil then
        local ft = pfiletype.detect(filepath)
        if ft == 'slim' then
          vim.bo[bufnr].filetype = 'slim'
        end
        opts.use_ft_detect = false
      end
      previewers.buffer_previewer_maker(filepath, bufnr, opts)
    end
  }
}

Running the following command to build an install the parser:

tree-sitter generate && \
tree-sitter build -o slim.so && \
cp slim.so ~/.local/share/nvim/lazy/nvim-treesitter/parser/ && \
mkdir -p ~/.local/share/nvim/lazy/nvim-treesitter/queries/slim && \
cp queries/* ~/.local/share/nvim/lazy/nvim-treesitter/queries/slim/

Helix

Add the following sections to ~/.config/helix/languages.toml

[[language]]
name = "slim"
scope = "source.slim"
file-types = [ "slim" ]
comment-tokens = [ "/", "/!" ]

[[grammar]]
name = "slim"
source = { git = "https://gitlab.com/theoreichel/tree-sitter-slim", rev = "REV" }

Update REV with the last commit hash SHA.

Copy queries found in the queries folder of this repo to ~/.config/helix/runtime/queries/.

The run helix --gramar fetch and you should be good to go. Use helix --health to check the languages capabilities.

Syntax tree

The syntax tree may evolve until the library stabilizes. Share your needs or feedback regarding a stable syntax tree.

Queries

All queries are provided: folds, highlights, indent, injections and locals.

Tests

Tests are based on the slim documentation, not its parse code. Test files match documentation chapters, with additional files for extensive custom tests and unsupported syntax.

Syntax highlight

Syntax highlighting follows tree-sitter's recommendations. Since Slim isn't a traditional language with variables or functions, some liberties were taken.

Unsupported features

The unsupported.txt test file lists unsupported features:

Won't be fixed:

Parser, Scanner and Performances

The custom scanner is based on the tree-sitter python scanner, modified for slim. Future refactoring will improve performance and align with tree-sitter recommendations.

Example: Parsing 12k lines under 35ms on a Ryzen 7040

> wc -cl views/**/*.slim
...
 12262 531233 total
> time tree-sitter parse --timeout 1000 views/**/*.slim -sq
Total parses: 301; successful parses: 301; failed parses: 0; success percentage: 100.00%; average speed: 20701 bytes/ms
________________________________________________________
Executed in   32.25 millis    fish           external
   usr time   28.60 millis  393.00 micros   28.21 millis
   sys time    3.46 millis  369.00 micros    3.09 millis

Developing/Contributing

The main repository is on GitLab. Issues and PRs on GitHub will be ignored.

Feedback and merge requests are welcome! To test the parser with a slim codebase, clone this repo and run:

tree-sitter generate

Then example, test all slim file in a rails project:

tree-sitter parse --timeout 1000 views/**/*.slim -sq

While developing with neovim, use this handy command:

nvim your_file.slim +"InspectTree"

Inspiration

Alternatives

About

slim parser for tree-sitter

Resources

Stars

4 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages