treesitter.lua 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 status_ok, big_file_detected = pcall(vim.api.nvim_buf_get_var, buf, "bigfile_disable_treesitter")
  30. return status_ok and big_file_detected
  31. end,
  32. },
  33. context_commentstring = {
  34. enable = true,
  35. enable_autocmd = false,
  36. config = {
  37. -- Languages that have a single comment style
  38. typescript = "// %s",
  39. css = "/* %s */",
  40. scss = "/* %s */",
  41. html = "<!-- %s -->",
  42. svelte = "<!-- %s -->",
  43. vue = "<!-- %s -->",
  44. json = "",
  45. },
  46. },
  47. indent = { enable = true, disable = { "yaml", "python" } },
  48. autotag = { enable = false },
  49. textobjects = {
  50. swap = {
  51. enable = false,
  52. -- swap_next = textobj_swap_keymaps,
  53. },
  54. -- move = textobj_move_keymaps,
  55. select = {
  56. enable = false,
  57. -- keymaps = textobj_sel_keymaps,
  58. },
  59. },
  60. textsubjects = {
  61. enable = false,
  62. keymaps = { ["."] = "textsubjects-smart", [";"] = "textsubjects-big" },
  63. },
  64. playground = {
  65. enable = false,
  66. disable = {},
  67. updatetime = 25, -- Debounced time for highlighting nodes in the playground from source code
  68. persist_queries = false, -- Whether the query persists across vim sessions
  69. keybindings = {
  70. toggle_query_editor = "o",
  71. toggle_hl_groups = "i",
  72. toggle_injected_languages = "t",
  73. toggle_anonymous_nodes = "a",
  74. toggle_language_display = "I",
  75. focus_language = "f",
  76. unfocus_language = "F",
  77. update = "R",
  78. goto_node = "<cr>",
  79. show_help = "?",
  80. },
  81. },
  82. rainbow = {
  83. enable = false,
  84. extended_mode = true, -- Highlight also non-parentheses delimiters, boolean or table: lang -> boolean
  85. max_file_lines = 1000, -- Do not enable for files with more than 1000 lines, int
  86. },
  87. }
  88. end
  89. ---@class bundledParsersOpts
  90. ---@field name_only boolean
  91. ---@field filter function
  92. ---Retrives a list of bundled parsers paths (any parser not found in default `install_dir`)
  93. ---@param opts bundledParsersOpts
  94. ---@return string[]
  95. local function get_parsers(opts)
  96. opts = opts or {}
  97. opts.filter = opts.filter or function()
  98. return true
  99. end
  100. local bundled_parsers = vim.tbl_filter(opts.filter, vim.api.nvim_get_runtime_file("parser/*.so", true))
  101. if opts.name_only then
  102. bundled_parsers = vim.tbl_map(function(parser)
  103. return vim.fn.fnamemodify(parser, ":t:r")
  104. end, bundled_parsers)
  105. end
  106. return bundled_parsers
  107. end
  108. ---Checks if parser is installed with nvim-treesitter
  109. ---@param lang string
  110. ---@return boolean
  111. local function is_installed(lang)
  112. local configs = require "nvim-treesitter.configs"
  113. local result = get_parsers {
  114. filter = function(parser)
  115. local install_dir = configs.get_parser_install_dir()
  116. return vim.startswith(parser, install_dir) and (vim.fn.fnamemodify(parser, ":t:r") == lang)
  117. end,
  118. }
  119. local parser_file = result and result[1] or ""
  120. local stat = vim.loop.fs_stat(parser_file)
  121. return stat and stat.type == "file"
  122. end
  123. local function ensure_updated_bundled()
  124. local configs = require "nvim-treesitter.configs"
  125. local bundled_parsers = get_parsers {
  126. name_only = true,
  127. filter = function(parser)
  128. local install_dir = configs.get_parser_install_dir()
  129. return not vim.startswith(parser, install_dir)
  130. end,
  131. }
  132. vim.api.nvim_create_autocmd("VimEnter", {
  133. callback = function()
  134. local missing = vim.tbl_filter(function(parser)
  135. return not is_installed(parser)
  136. end, bundled_parsers)
  137. if #missing > 0 then
  138. vim.cmd { cmd = "TSInstall", args = missing, bang = true }
  139. end
  140. end,
  141. })
  142. end
  143. function M.setup()
  144. -- avoid running in headless mode since it's harder to detect failures
  145. if #vim.api.nvim_list_uis() == 0 then
  146. Log:debug "headless mode detected, skipping running setup for treesitter"
  147. return
  148. end
  149. local status_ok, treesitter_configs = pcall(require, "nvim-treesitter.configs")
  150. if not status_ok then
  151. Log:error "Failed to load nvim-treesitter.configs"
  152. return
  153. end
  154. local opts = vim.deepcopy(lvim.builtin.treesitter)
  155. treesitter_configs.setup(opts)
  156. ensure_updated_bundled()
  157. if lvim.builtin.treesitter.on_config_done then
  158. lvim.builtin.treesitter.on_config_done(treesitter_configs)
  159. end
  160. end
  161. M.get_parsers = get_parsers
  162. M.is_installed = is_installed
  163. return M