treesitter.lua 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. local M = {}
  2. local Log = require "lvim.core.log"
  3. M.config = function()
  4. lvim.builtin.treesitter = {
  5. on_config_done = nil,
  6. ensure_installed = {}, -- one of "all", "maintained" (parsers with maintainers), or a list of languages
  7. ignore_install = {},
  8. matchup = {
  9. enable = false, -- mandatory, false will disable the whole extension
  10. -- disable = { "c", "ruby" }, -- optional, list of language that will be disabled
  11. },
  12. highlight = {
  13. enable = true, -- false will disable the whole extension
  14. additional_vim_regex_highlighting = false,
  15. disable = function(lang, buf)
  16. if vim.tbl_contains({ "latex" }, lang) then
  17. return true
  18. end
  19. local max_filesize = 1024 * 1024
  20. local ok, stats = pcall(vim.loop.fs_stat, vim.api.nvim_buf_get_name(buf))
  21. if ok and stats and stats.size > max_filesize then
  22. if lvim.builtin.illuminate.active then
  23. pcall(require("illuminate").pause_buf)
  24. end
  25. vim.schedule(function()
  26. vim.api.nvim_buf_call(buf, function()
  27. vim.cmd "setlocal noswapfile noundofile"
  28. if vim.tbl_contains({ "json" }, lang) then
  29. vim.cmd "NoMatchParen"
  30. vim.cmd "syntax off"
  31. vim.cmd "syntax clear"
  32. vim.cmd "setlocal nocursorline nolist bufhidden=unload"
  33. vim.api.nvim_create_autocmd({ "BufDelete" }, {
  34. callback = function()
  35. vim.cmd "DoMatchParen"
  36. vim.cmd "syntax on"
  37. end,
  38. buffer = buf,
  39. })
  40. end
  41. end)
  42. end)
  43. Log:info "File larger than 1MB, turned off treesitter for this buffer"
  44. return true
  45. end
  46. end,
  47. },
  48. context_commentstring = {
  49. enable = true,
  50. enable_autocmd = false,
  51. config = {
  52. -- Languages that have a single comment style
  53. typescript = "// %s",
  54. css = "/* %s */",
  55. scss = "/* %s */",
  56. html = "<!-- %s -->",
  57. svelte = "<!-- %s -->",
  58. vue = "<!-- %s -->",
  59. json = "",
  60. },
  61. },
  62. indent = { enable = true, disable = { "yaml", "python" } },
  63. autotag = { enable = false },
  64. textobjects = {
  65. swap = {
  66. enable = false,
  67. -- swap_next = textobj_swap_keymaps,
  68. },
  69. -- move = textobj_move_keymaps,
  70. select = {
  71. enable = false,
  72. -- keymaps = textobj_sel_keymaps,
  73. },
  74. },
  75. textsubjects = {
  76. enable = false,
  77. keymaps = { ["."] = "textsubjects-smart", [";"] = "textsubjects-big" },
  78. },
  79. playground = {
  80. enable = false,
  81. disable = {},
  82. updatetime = 25, -- Debounced time for highlighting nodes in the playground from source code
  83. persist_queries = false, -- Whether the query persists across vim sessions
  84. keybindings = {
  85. toggle_query_editor = "o",
  86. toggle_hl_groups = "i",
  87. toggle_injected_languages = "t",
  88. toggle_anonymous_nodes = "a",
  89. toggle_language_display = "I",
  90. focus_language = "f",
  91. unfocus_language = "F",
  92. update = "R",
  93. goto_node = "<cr>",
  94. show_help = "?",
  95. },
  96. },
  97. rainbow = {
  98. enable = false,
  99. extended_mode = true, -- Highlight also non-parentheses delimiters, boolean or table: lang -> boolean
  100. max_file_lines = 1000, -- Do not enable for files with more than 1000 lines, int
  101. },
  102. }
  103. end
  104. M.setup = function()
  105. -- avoid running in headless mode since it's harder to detect failures
  106. if #vim.api.nvim_list_uis() == 0 then
  107. Log:debug "headless mode detected, skipping running setup for treesitter"
  108. return
  109. end
  110. local status_ok, treesitter_configs = pcall(require, "nvim-treesitter.configs")
  111. if not status_ok then
  112. Log:error "Failed to load nvim-treesitter.configs"
  113. return
  114. end
  115. local opts = vim.deepcopy(lvim.builtin.treesitter)
  116. treesitter_configs.setup(opts)
  117. if lvim.builtin.treesitter.on_config_done then
  118. lvim.builtin.treesitter.on_config_done(treesitter_configs)
  119. end
  120. end
  121. return M