treesitter.lua 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. local M = {}
  2. local Log = require "lvim.core.log"
  3. function M.config()
  4. lvim.builtin.treesitter = {
  5. on_config_done = nil,
  6. -- A list of parser names, or "all"
  7. ensure_installed = {},
  8. -- List of parsers to ignore installing (for "all")
  9. ignore_install = {},
  10. -- A directory to install the parsers into.
  11. -- By default parsers are installed to either the package dir, or the "site" dir.
  12. -- If a custom path is used (not nil) it must be added to the runtimepath.
  13. parser_install_dir = nil,
  14. -- Install parsers synchronously (only applied to `ensure_installed`)
  15. sync_install = false,
  16. -- Automatically install missing parsers when entering buffer
  17. auto_install = false,
  18. matchup = {
  19. enable = false, -- mandatory, false will disable the whole extension
  20. -- disable = { "c", "ruby" }, -- optional, list of language that will be disabled
  21. },
  22. highlight = {
  23. enable = true, -- false will disable the whole extension
  24. additional_vim_regex_highlighting = false,
  25. disable = function(lang, buf)
  26. if vim.tbl_contains({ "latex" }, lang) then
  27. return true
  28. end
  29. local max_filesize = 1024 * 1024
  30. local ok, stats = pcall(vim.loop.fs_stat, vim.api.nvim_buf_get_name(buf))
  31. if ok and stats and stats.size > max_filesize then
  32. if lvim.builtin.illuminate.active then
  33. pcall(require("illuminate").pause_buf)
  34. end
  35. vim.schedule(function()
  36. vim.api.nvim_buf_call(buf, function()
  37. vim.cmd "setlocal noswapfile noundofile"
  38. if vim.tbl_contains({ "json" }, lang) then
  39. vim.cmd "NoMatchParen"
  40. vim.cmd "syntax off"
  41. vim.cmd "syntax clear"
  42. vim.cmd "setlocal nocursorline nolist bufhidden=unload"
  43. vim.api.nvim_create_autocmd({ "BufDelete" }, {
  44. callback = function()
  45. vim.cmd "DoMatchParen"
  46. vim.cmd "syntax on"
  47. end,
  48. buffer = buf,
  49. })
  50. end
  51. end)
  52. end)
  53. Log:info "File larger than 1MB, turned off treesitter for this buffer"
  54. return true
  55. end
  56. end,
  57. },
  58. context_commentstring = {
  59. enable = true,
  60. enable_autocmd = false,
  61. config = {
  62. -- Languages that have a single comment style
  63. typescript = "// %s",
  64. css = "/* %s */",
  65. scss = "/* %s */",
  66. html = "<!-- %s -->",
  67. svelte = "<!-- %s -->",
  68. vue = "<!-- %s -->",
  69. json = "",
  70. },
  71. },
  72. indent = { enable = true, disable = { "yaml", "python" } },
  73. autotag = { enable = false },
  74. textobjects = {
  75. swap = {
  76. enable = false,
  77. -- swap_next = textobj_swap_keymaps,
  78. },
  79. -- move = textobj_move_keymaps,
  80. select = {
  81. enable = false,
  82. -- keymaps = textobj_sel_keymaps,
  83. },
  84. },
  85. textsubjects = {
  86. enable = false,
  87. keymaps = { ["."] = "textsubjects-smart", [";"] = "textsubjects-big" },
  88. },
  89. playground = {
  90. enable = false,
  91. disable = {},
  92. updatetime = 25, -- Debounced time for highlighting nodes in the playground from source code
  93. persist_queries = false, -- Whether the query persists across vim sessions
  94. keybindings = {
  95. toggle_query_editor = "o",
  96. toggle_hl_groups = "i",
  97. toggle_injected_languages = "t",
  98. toggle_anonymous_nodes = "a",
  99. toggle_language_display = "I",
  100. focus_language = "f",
  101. unfocus_language = "F",
  102. update = "R",
  103. goto_node = "<cr>",
  104. show_help = "?",
  105. },
  106. },
  107. rainbow = {
  108. enable = false,
  109. extended_mode = true, -- Highlight also non-parentheses delimiters, boolean or table: lang -> boolean
  110. max_file_lines = 1000, -- Do not enable for files with more than 1000 lines, int
  111. },
  112. }
  113. end
  114. ---@class bundledParsersOpts
  115. ---@field name_only boolean
  116. ---@field filter function
  117. ---Retrives a list of bundled parsers paths (any parser not found in default `install_dir`)
  118. ---@param opts bundledParsersOpts
  119. ---@return string[]
  120. local function get_parsers(opts)
  121. opts = opts or {}
  122. opts.filter = opts.filter or function()
  123. return true
  124. end
  125. local bundled_parsers = vim.tbl_filter(opts.filter, vim.api.nvim_get_runtime_file("parser/*.so", true))
  126. if opts.name_only then
  127. bundled_parsers = vim.tbl_map(function(parser)
  128. return vim.fn.fnamemodify(parser, ":t:r")
  129. end, bundled_parsers)
  130. end
  131. return bundled_parsers
  132. end
  133. ---Checks if parser is installed with nvim-treesitter
  134. ---@param lang string
  135. ---@return boolean
  136. local function is_installed(lang)
  137. local configs = require "nvim-treesitter.configs"
  138. local result = get_parsers {
  139. filter = function(parser)
  140. local install_dir = configs.get_parser_install_dir()
  141. return vim.startswith(parser, install_dir) and (vim.fn.fnamemodify(parser, ":t:r") == lang)
  142. end,
  143. }
  144. local parser_file = result and result[1] or ""
  145. local stat = vim.loop.fs_stat(parser_file)
  146. return stat and stat.type == "file"
  147. end
  148. local function ensure_updated_bundled()
  149. local configs = require "nvim-treesitter.configs"
  150. local bundled_parsers = get_parsers {
  151. name_only = true,
  152. filter = function(parser)
  153. local install_dir = configs.get_parser_install_dir()
  154. return not vim.startswith(parser, install_dir)
  155. end,
  156. }
  157. vim.api.nvim_create_autocmd("VimEnter", {
  158. callback = function()
  159. local missing = vim.tbl_filter(function(parser)
  160. return not is_installed(parser)
  161. end, bundled_parsers)
  162. if #missing > 0 then
  163. vim.cmd { cmd = "TSInstall", args = missing, bang = true }
  164. end
  165. end,
  166. })
  167. end
  168. function M.setup()
  169. -- avoid running in headless mode since it's harder to detect failures
  170. if #vim.api.nvim_list_uis() == 0 then
  171. Log:debug "headless mode detected, skipping running setup for treesitter"
  172. return
  173. end
  174. local status_ok, treesitter_configs = pcall(require, "nvim-treesitter.configs")
  175. if not status_ok then
  176. Log:error "Failed to load nvim-treesitter.configs"
  177. return
  178. end
  179. local opts = vim.deepcopy(lvim.builtin.treesitter)
  180. treesitter_configs.setup(opts)
  181. ensure_updated_bundled()
  182. if lvim.builtin.treesitter.on_config_done then
  183. lvim.builtin.treesitter.on_config_done(treesitter_configs)
  184. end
  185. end
  186. M.get_parsers = get_parsers
  187. M.is_installed = is_installed
  188. return M