Skip to content

Commit

Permalink
feat: more fine-grained control for disabling highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb committed Aug 28, 2024
1 parent c6e9ca5 commit 9bd981b
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 9 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ auto_highlight = [
"toml"
]
auto_install = "prompt" # true | false
# NOTE: These are filetypes
disable = [
"lhaskell"
]

[treesitter.parser_map]
# You can add custom filetype to parser mappings.
Expand All @@ -112,6 +116,8 @@ vim.g.rocks_nvim = {
auto_highlight = { },
auto_install = "prompt",
parser_map = { },
---@type string[] | fun(lang: string, bufnr: integer):boolean
disable = { }, -- filetypes or a function
},
}
```
Expand Down
6 changes: 6 additions & 0 deletions doc/rocks-treesitter.nvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ configure this plugin.
"toml"
]
auto_install = "prompt" # true | false
# NOTE: These are filetypes
disable = [
"lhaskell"
]

[treesitter.parser_map]
# You can add custom filetype to parser mappings.
Expand All @@ -98,6 +102,8 @@ Or, you add a lua table to your `vim.g.rocks_nvim` setting:
auto_highlight = { },
auto_install = "prompt",
parser_map = { },
---@type string[] | fun(lang: string, bufnr: integer):boolean
disable = { }, -- filetypes or a function
},
}
<
Expand Down
50 changes: 42 additions & 8 deletions lua/rocks_treesitter/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,31 @@ local config = {
auto_install = "prompt",
---@type "all" | table<string, boolean>
auto_highlight = {},
---@type table<string, boolean> | fun(lang: string, bufnr: integer):boolean
disable = {},
---@type table<string, string>
parser_map = require("rocks_treesitter.ft_parser_map"),
}

local api = require("rocks.api")

---@class RocksTreesitterOpts
---@field auto_highlight? string[] | "all" Parsers to automatically enable syntax highlighting for. Note that these are parser languages, not file types. Defaults to an empty list.
---@field auto_install? boolean | "prompt" Auto-install parsers in `auto_highlight` as needed? Default: 'prompt'
---@field parser_map? table<string, string> Map from filetypes to parser languages
---
--- Parsers to automatically enable syntax highlighting for.
--- Note that these are parser languages, not file types.
--- Defaults to an empty list.
---@field auto_highlight? string[] | "all"
---
--- Auto-install parsers in `auto_highlight` as needed?
--- Default: 'prompt'
---@field auto_install? boolean | "prompt"
---
--- Map from filetypes to parser languages
---@field parser_map? table<string, string>
---
--- File types to disable highlighting for (lua + toml config)
--- or a function to conditionally disable highlighting (lua only)
---@field disable? string[] | fun(lang: string, bufnr: integer):boolean

local toml_config = api.get_rocks_toml()["treesitter"]
---@type RocksTreesitterOpts
Expand All @@ -32,13 +47,32 @@ if type(lua_config) == "table" then
opts = vim.tbl_deep_extend("force", opts, lua_config)
end

---@generic T
---@param lst? T[]
---@return table<T, boolean>
local function list_to_bool_map(lst)
return vim
.iter(lst or {})
---@generic T
---@param acc table<T, boolean>
---@param val T
:fold(
{},
function(acc, val)
acc[val] = true
return acc
end
)
end

--- Map opts to configs, preserving defaults if not set
config.auto_highlight = opts.auto_highlight == "all" and "all"
or vim.iter(opts.auto_highlight or {}):fold({}, function(acc, lang)
---@cast lang string
acc[lang] = true
return acc
end)
---@diagnostic disable-next-line: param-type-mismatch
or list_to_bool_map(opts.auto_highlight)
config.disable = type(opts.disable) == "function" and opts.disable
---@diagnostic disable-next-line: param-type-mismatch
or list_to_bool_map(opts.disable)
---@diagnostic disable-next-line: assign-type-mismatch
config.auto_install = vim.F.if_nil(opts.auto_install, config.auto_install)
config.parser_map = vim.tbl_extend("force", config.parser_map, opts.parser_map or {})

Expand Down
5 changes: 4 additions & 1 deletion lua/rocks_treesitter/highlight.lua
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,10 @@ function highlight.create_autocmd()
local bufnr = ctx.buf
local filetype = vim.bo[bufnr].filetype
local lang = get_lang(filetype)
if config.auto_highlight == "all" or config.auto_highlight[lang] then
---@type boolean | nil
local disable = type(config.disable) == "function" and config.disable(lang, bufnr)
or config.disable[filetype]
if not disable and config.auto_highlight == "all" or config.auto_highlight[lang] then
do_highlight(lang)
end
end,
Expand Down

0 comments on commit 9bd981b

Please sign in to comment.