aboutsummaryrefslogtreecommitdiffstats
path: root/lua/simenandre/plugins/treesitter.lua
blob: 1eb9080641459f6604fb3501f729cc87f32c47ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
local ensure_installed = {
  "astro",
  "bash",
  "c",
  "css",
  "diff",
  "gitcommit",
  "gitignore",
  "go",
  "html",
  "javascript",
  "json",
  "lua",
  "luadoc",
  "markdown",
  "markdown_inline",
  "nix",
  "query",
  "rust",
  "ssh_config",
  "toml",
  "tsx",
  "typescript",
  "vim",
  "vimdoc",
  "yaml",
  "zig",
}

local function start(buf, lang)
  if not pcall(vim.treesitter.start, buf, lang) then
    return
  end
  vim.bo[buf].indentexpr = "v:lua.require'nvim-treesitter'.indentexpr()"
end

return {
  { -- Parsers and queries; highlighting itself is native (vim.treesitter.start)
    "nvim-treesitter/nvim-treesitter",
    branch = "main",
    lazy = false,
    build = ":TSUpdate",
    config = function()
      local ts = require("nvim-treesitter")
      ts.install(ensure_installed)

      vim.api.nvim_create_autocmd("FileType", {
        group = vim.api.nvim_create_augroup("treesitter-start", { clear = true }),
        callback = function(args)
          local lang = vim.treesitter.language.get_lang(args.match)
          if not lang then
            return
          end
          if vim.list_contains(ts.get_installed(), lang) then
            start(args.buf, lang)
          elseif vim.list_contains(ts.get_available(), lang) then
            ts.install(lang):await(vim.schedule_wrap(function()
              if vim.api.nvim_buf_is_valid(args.buf) then
                start(args.buf, lang)
              end
            end))
          end
        end,
      })
    end,
  },
}