Skip to content

Commit

Permalink
feat(treesitter): lessened performance restrictions (#1305)
Browse files Browse the repository at this point in the history
* feat(treesitter): lessened performance restrictions

This commit lifts some of the performance restrictions previously imposed on
Treesitter when incremental parsing was not yet supported. Specifically:

Treesitter will now only be disabled by default for files exceeding 7,500 lines
or 2 MiB in size. Testing on an Intel i9-9880H with an APPLE SSD AP1024N shows
noticeable lag at these thresholds.

Signed-off-by: Jint-lzxy <[email protected]>

* fixup: return correct value.

---------

Signed-off-by: Jint-lzxy <[email protected]>
Co-authored-by: ayamir <[email protected]>
  • Loading branch information
Jint-lzxy and ayamir authored Jun 24, 2024
1 parent 6c59c16 commit a6a3356
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lua/modules/configs/completion/cmp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ return function()
name = "buffer",
option = {
get_bufnrs = function()
return vim.api.nvim_list_bufs()
return vim.api.nvim_buf_line_count(0) < 7500 and vim.api.nvim_list_bufs() or {}
end,
},
},
Expand Down
7 changes: 4 additions & 3 deletions lua/modules/configs/editor/bigfile.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ return function()
}

require("modules.utils").load_plugin("bigfile", {
filesize = 1, -- size of the file in MiB
filesize = 2, -- size of the file in MiB
pattern = { "*" }, -- autocmd pattern
features = { -- features to disable
"indent_blankline",
"lsp",
"treesitter",
"syntax",
"treesitter",
"vimopts",
ftdetect,
cmp,
ftdetect,
},
})
end
15 changes: 12 additions & 3 deletions lua/modules/configs/editor/rainbow_delims.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ return function()
---@param threshold number @Use global strategy if nr of lines exceeds this value
local function init_strategy(threshold)
return function()
-- Disable on very large files
local line_count = vim.api.nvim_buf_line_count(0)
if line_count > 7500 then
return nil
end

-- Disable on parser error
local errors = 200
vim.treesitter.get_parser():for_each_tree(function(lt)
if lt:root():has_error() and errors >= 0 then
Expand All @@ -11,16 +18,17 @@ return function()
if errors < 0 then
return nil
end
return vim.fn.line("$") > threshold and require("rainbow-delimiters").strategy["global"]

return line_count > threshold and require("rainbow-delimiters").strategy["global"]
or require("rainbow-delimiters").strategy["local"]
end
end

vim.g.rainbow_delimiters = {
strategy = {
[""] = init_strategy(500),
c = init_strategy(200),
cpp = init_strategy(200),
c = init_strategy(300),
cpp = init_strategy(300),
lua = init_strategy(500),
vimdoc = init_strategy(300),
vim = init_strategy(300),
Expand All @@ -40,5 +48,6 @@ return function()
"RainbowDelimiterViolet",
},
}

require("modules.utils").load_plugin("rainbow_delimiters", nil, true)
end
12 changes: 8 additions & 4 deletions lua/modules/configs/editor/treesitter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ return vim.schedule_wrap(function()
highlight = {
enable = true,
disable = function(ft, bufnr)
if vim.tbl_contains({ "vim" }, ft) then
if
vim.tbl_contains({ "gitcommit" }, ft)
or (vim.api.nvim_buf_line_count(bufnr) > 7500 and ft ~= "vimdoc")
then
return true
end

Expand All @@ -21,6 +24,7 @@ return vim.schedule_wrap(function()
textobjects = {
select = {
enable = true,
lookahead = true,
keymaps = {
["af"] = "@function.outer",
["if"] = "@function.inner",
Expand All @@ -30,7 +34,7 @@ return vim.schedule_wrap(function()
},
move = {
enable = true,
set_jumps = true, -- whether to set jumps in the jumplist
set_jumps = true,
goto_next_start = {
["]["] = "@function.outer",
["]m"] = "@class.outer",
Expand All @@ -55,8 +59,8 @@ return vim.schedule_wrap(function()
require("nvim-treesitter.install").prefer_git = true
if use_ssh then
local parsers = require("nvim-treesitter.parsers").get_parser_configs()
for _, p in pairs(parsers) do
p.install_info.url = p.install_info.url:gsub("https://github.com/", "[email protected]:")
for _, parser in pairs(parsers) do
parser.install_info.url = parser.install_info.url:gsub("https://github.com/", "[email protected]:")
end
end
end)

0 comments on commit a6a3356

Please sign in to comment.