2021-07-13 03:11:43 +02:00
|
|
|
local M = {}
|
2021-10-10 21:07:41 +02:00
|
|
|
local Log = require "lvim.core.log"
|
2021-08-22 20:03:19 +02:00
|
|
|
|
2022-11-16 11:13:41 +01:00
|
|
|
function M.config()
|
2021-07-25 03:17:11 +02:00
|
|
|
lvim.builtin.treesitter = {
|
2021-08-22 20:03:19 +02:00
|
|
|
on_config_done = nil,
|
2022-11-16 11:13:41 +01:00
|
|
|
|
|
|
|
-- A list of parser names, or "all"
|
|
|
|
ensure_installed = {},
|
|
|
|
|
|
|
|
-- List of parsers to ignore installing (for "all")
|
2021-07-13 03:11:43 +02:00
|
|
|
ignore_install = {},
|
2022-11-16 11:13:41 +01:00
|
|
|
|
|
|
|
-- A directory to install the parsers into.
|
|
|
|
-- By default parsers are installed to either the package dir, or the "site" dir.
|
|
|
|
-- If a custom path is used (not nil) it must be added to the runtimepath.
|
|
|
|
parser_install_dir = nil,
|
|
|
|
|
|
|
|
-- Install parsers synchronously (only applied to `ensure_installed`)
|
|
|
|
sync_install = false,
|
|
|
|
|
|
|
|
-- Automatically install missing parsers when entering buffer
|
|
|
|
auto_install = false,
|
|
|
|
|
2021-07-13 03:11:43 +02:00
|
|
|
matchup = {
|
|
|
|
enable = false, -- mandatory, false will disable the whole extension
|
|
|
|
-- disable = { "c", "ruby" }, -- optional, list of language that will be disabled
|
|
|
|
},
|
|
|
|
highlight = {
|
|
|
|
enable = true, -- false will disable the whole extension
|
2022-05-11 17:15:00 +02:00
|
|
|
additional_vim_regex_highlighting = false,
|
2022-11-01 10:37:50 +01:00
|
|
|
disable = function(lang, buf)
|
|
|
|
if vim.tbl_contains({ "latex" }, lang) then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2022-11-28 19:11:06 +01:00
|
|
|
local status_ok, big_file_detected = pcall(vim.api.nvim_buf_get_var, buf, "bigfile_disable_treesitter")
|
|
|
|
return status_ok and big_file_detected
|
2022-11-01 10:37:50 +01:00
|
|
|
end,
|
2021-07-13 03:11:43 +02:00
|
|
|
},
|
|
|
|
context_commentstring = {
|
2021-11-05 23:15:10 +01:00
|
|
|
enable = true,
|
2022-05-11 17:15:00 +02:00
|
|
|
enable_autocmd = false,
|
2021-11-05 23:15:10 +01:00
|
|
|
config = {
|
|
|
|
-- Languages that have a single comment style
|
|
|
|
typescript = "// %s",
|
|
|
|
css = "/* %s */",
|
|
|
|
scss = "/* %s */",
|
|
|
|
html = "<!-- %s -->",
|
|
|
|
svelte = "<!-- %s -->",
|
|
|
|
vue = "<!-- %s -->",
|
|
|
|
json = "",
|
|
|
|
},
|
2021-07-13 03:11:43 +02:00
|
|
|
},
|
2022-05-11 17:15:00 +02:00
|
|
|
indent = { enable = true, disable = { "yaml", "python" } },
|
2021-07-13 03:11:43 +02:00
|
|
|
autotag = { enable = false },
|
|
|
|
textobjects = {
|
|
|
|
swap = {
|
|
|
|
enable = false,
|
|
|
|
-- swap_next = textobj_swap_keymaps,
|
|
|
|
},
|
|
|
|
-- move = textobj_move_keymaps,
|
|
|
|
select = {
|
|
|
|
enable = false,
|
|
|
|
-- keymaps = textobj_sel_keymaps,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
textsubjects = {
|
|
|
|
enable = false,
|
|
|
|
keymaps = { ["."] = "textsubjects-smart", [";"] = "textsubjects-big" },
|
|
|
|
},
|
|
|
|
playground = {
|
|
|
|
enable = false,
|
|
|
|
disable = {},
|
|
|
|
updatetime = 25, -- Debounced time for highlighting nodes in the playground from source code
|
|
|
|
persist_queries = false, -- Whether the query persists across vim sessions
|
|
|
|
keybindings = {
|
|
|
|
toggle_query_editor = "o",
|
|
|
|
toggle_hl_groups = "i",
|
|
|
|
toggle_injected_languages = "t",
|
|
|
|
toggle_anonymous_nodes = "a",
|
|
|
|
toggle_language_display = "I",
|
|
|
|
focus_language = "f",
|
|
|
|
unfocus_language = "F",
|
|
|
|
update = "R",
|
|
|
|
goto_node = "<cr>",
|
|
|
|
show_help = "?",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
rainbow = {
|
|
|
|
enable = false,
|
|
|
|
extended_mode = true, -- Highlight also non-parentheses delimiters, boolean or table: lang -> boolean
|
|
|
|
max_file_lines = 1000, -- Do not enable for files with more than 1000 lines, int
|
|
|
|
},
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2022-11-16 11:13:41 +01:00
|
|
|
---@class bundledParsersOpts
|
|
|
|
---@field name_only boolean
|
|
|
|
---@field filter function
|
|
|
|
|
|
|
|
---Retrives a list of bundled parsers paths (any parser not found in default `install_dir`)
|
|
|
|
---@param opts bundledParsersOpts
|
|
|
|
---@return string[]
|
|
|
|
local function get_parsers(opts)
|
|
|
|
opts = opts or {}
|
|
|
|
opts.filter = opts.filter or function()
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
local bundled_parsers = vim.tbl_filter(opts.filter, vim.api.nvim_get_runtime_file("parser/*.so", true))
|
|
|
|
|
|
|
|
if opts.name_only then
|
|
|
|
bundled_parsers = vim.tbl_map(function(parser)
|
|
|
|
return vim.fn.fnamemodify(parser, ":t:r")
|
|
|
|
end, bundled_parsers)
|
|
|
|
end
|
|
|
|
|
|
|
|
return bundled_parsers
|
|
|
|
end
|
|
|
|
|
|
|
|
---Checks if parser is installed with nvim-treesitter
|
|
|
|
---@param lang string
|
|
|
|
---@return boolean
|
|
|
|
local function is_installed(lang)
|
|
|
|
local configs = require "nvim-treesitter.configs"
|
|
|
|
local result = get_parsers {
|
|
|
|
filter = function(parser)
|
|
|
|
local install_dir = configs.get_parser_install_dir()
|
|
|
|
return vim.startswith(parser, install_dir) and (vim.fn.fnamemodify(parser, ":t:r") == lang)
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
local parser_file = result and result[1] or ""
|
|
|
|
local stat = vim.loop.fs_stat(parser_file)
|
|
|
|
return stat and stat.type == "file"
|
|
|
|
end
|
|
|
|
|
|
|
|
local function ensure_updated_bundled()
|
|
|
|
local configs = require "nvim-treesitter.configs"
|
|
|
|
local bundled_parsers = get_parsers {
|
|
|
|
name_only = true,
|
|
|
|
filter = function(parser)
|
|
|
|
local install_dir = configs.get_parser_install_dir()
|
|
|
|
return not vim.startswith(parser, install_dir)
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
|
|
|
|
vim.api.nvim_create_autocmd("VimEnter", {
|
|
|
|
callback = function()
|
|
|
|
local missing = vim.tbl_filter(function(parser)
|
|
|
|
return not is_installed(parser)
|
|
|
|
end, bundled_parsers)
|
|
|
|
|
|
|
|
if #missing > 0 then
|
|
|
|
vim.cmd { cmd = "TSInstall", args = missing, bang = true }
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
function M.setup()
|
2021-12-28 09:48:42 +01:00
|
|
|
-- avoid running in headless mode since it's harder to detect failures
|
|
|
|
if #vim.api.nvim_list_uis() == 0 then
|
|
|
|
Log:debug "headless mode detected, skipping running setup for treesitter"
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2021-07-13 03:11:43 +02:00
|
|
|
local status_ok, treesitter_configs = pcall(require, "nvim-treesitter.configs")
|
|
|
|
if not status_ok then
|
2021-12-28 09:48:42 +01:00
|
|
|
Log:error "Failed to load nvim-treesitter.configs"
|
2021-07-13 03:11:43 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2021-11-03 12:30:34 +01:00
|
|
|
local opts = vim.deepcopy(lvim.builtin.treesitter)
|
|
|
|
|
|
|
|
treesitter_configs.setup(opts)
|
2021-08-22 20:03:19 +02:00
|
|
|
|
2022-11-16 11:13:41 +01:00
|
|
|
ensure_updated_bundled()
|
|
|
|
|
2021-08-22 20:03:19 +02:00
|
|
|
if lvim.builtin.treesitter.on_config_done then
|
|
|
|
lvim.builtin.treesitter.on_config_done(treesitter_configs)
|
|
|
|
end
|
2021-07-13 03:11:43 +02:00
|
|
|
end
|
|
|
|
|
2022-11-16 11:13:41 +01:00
|
|
|
M.get_parsers = get_parsers
|
|
|
|
M.is_installed = is_installed
|
|
|
|
|
2021-07-13 03:11:43 +02:00
|
|
|
return M
|