p00f / nvim-ts-rainbow

Rainbow parentheses for neovim using tree-sitter. Use https://sr.ht/~p00f/nvim-ts-rainbow instead
Apache License 2.0
869 stars 67 forks source link

enable for only a single language or list of languages #30

Closed akinsho closed 3 years ago

akinsho commented 3 years ago

Hi 👋🏿 thanks for your work on this plugin.

I've just moved over from luochen1990's plugin and I'm wondering if it's possible to enable this plugin for only a few filetypes primarily since there are only a few languages I use that tend to get a little unmanageable with their parens (e.g. lisps) but for things like lua I don't really need this functionality.

p00f commented 3 years ago

~ Yeah just change enabled from true to a table of languages you want to enable it for (just like the main nvim-treesitter plugin) ~

akinsho commented 3 years ago

@p00f I just tried setting enable = {"dart"} as an example but that doesn't work lua still shows rainbow parentheses 🤔 the README says the key is enable not enabled but I tried using enabled and that just turns the plugin off completely

p00f commented 3 years ago

It is enable indeed. Also, sorry but there's only enable = true and disable = list of languages

https://github.com/nvim-treesitter/nvim-treesitter#modules

akinsho commented 3 years ago

@p00f thanks that works, shame it works in that order since to only enable it for a few languages I'd end up having to list all available languages except the one I want 😅 . Anyway thanks for the help.

ahmedelgabri commented 3 years ago

I came here for the same exact request as @akinsho, it will be great @p00f if there is a way to enable the plugin only for some langauges.

p00f commented 3 years ago

I don't want to implement this on the module side, can you open an issue on the main nvim-treesitter repo and see? If they don't want to then I'll try doing it

@ahmedelgabri

ahmedelgabri commented 3 years ago

I don't want to implement this on the module side, can you open an issue on the main nvim-treesitter repo and see? If they don't want to then I'll try doing it

@ahmedelgabri

Unless I'm missing something why is it needed to be added to treesitter? I actually don't see that useful on the treesitter side but I see it useful here, the reason is I might want treesitter support for more languages than I'd like rainbow parentheses support. Technically I want support for treesitter for everything, but rainbow parentheses support only for lisp like languages.

p00f commented 3 years ago

This is an not a standalone plugin but an nvim-treesitter module generated from the module template given by nvim-treesitter:

image

Enabling and disabling is controlled by the main nvim-treesitter plugin: https://github.com/nvim-treesitter/nvim-treesitter#modules

https://github.com/nvim-treesitter/nvim-treesitter/blob/972f70956ac8635ab2f6d7ed0b24e5cd159d7a04/doc/nvim-treesitter.txt#L58

If they implement this on their side then you can do it to any module given here

https://github.com/nvim-treesitter/nvim-treesitter/wiki/Extra-modules-and-plugins#extra-modules

note that 3 out of the 6 modules mentioned there are outside the nvim-treesitter organization

ahmedelgabri commented 3 years ago

I think I found a way around this limitation without maintaining a full table of disabled languages.

local parsers = require("nvim-treesitter.parsers")

rainbow = {
  enable = true,
  -- Enable only for lisp like languages
  disable = vim.tbl_filter(
    function(p)
      return p ~= "clojure" and p ~= "commonlisp" and p ~= "fennel" and p ~= "query"
    end,
    parsers.available_parsers()
  )
},
p00f commented 3 years ago

Neat! If that list grows larger you can use

local enabled_list = {"clojure", "fennel", "commonlisp", "query", ...}
rainbow = {
  enable = true,
  -- Enable only for lisp like languages
  disable = vim.tbl_filter(
    function(p)
        local disable = true
        for _, lang in pairs(enabled_list) do
          if p==lang then disable = false end
        end
        return disable
    end,
    parsers.available_parsers()
  )
},
illia-danko commented 2 years ago

Should be a part of the documentation imho.

oblitum commented 2 years ago

It would be nice if enable could either be true or a list of languages. The vim.tbl_filter snippet isn't hmm... ergonomic.