treesitter.lua 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 = { "comment", "markdown_inline", "regex" },
  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 = true,
  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. function M.setup()
  90. -- avoid running in headless mode since it's harder to detect failures
  91. if #vim.api.nvim_list_uis() == 0 then
  92. Log:debug "headless mode detected, skipping running setup for treesitter"
  93. return
  94. end
  95. local ts_status_ok, treesitter_configs = pcall(require, "nvim-treesitter.configs")
  96. if not ts_status_ok then
  97. Log:error "Failed to load nvim-treesitter.configs"
  98. return
  99. end
  100. local status_ok, ts_context_commentstring = pcall(require, "ts_context_commentstring")
  101. if not status_ok then
  102. Log:error "Failed to load ts_context_commentstring"
  103. return
  104. end
  105. local opts = vim.deepcopy(lvim.builtin.treesitter)
  106. -- handle deprecated API, https://github.com/JoosepAlviste/nvim-ts-context-commentstring/issues/82
  107. ts_context_commentstring.setup(opts.context_commentstring)
  108. opts.context_commentstring = nil
  109. treesitter_configs.setup(opts)
  110. if lvim.builtin.treesitter.on_config_done then
  111. lvim.builtin.treesitter.on_config_done(treesitter_configs)
  112. end
  113. -- handle deprecated API, https://github.com/windwp/nvim-autopairs/pull/324
  114. local ts_utils = require "nvim-treesitter.ts_utils"
  115. ts_utils.is_in_node_range = vim.treesitter.is_in_node_range
  116. ts_utils.get_node_range = vim.treesitter.get_node_range
  117. end
  118. return M