Xinqi Bao's Git

fix(tmux): improve prompts and key handling
[dotfiles.git] / .config / nvim / lua / plugin-config / nvim-treesitter.lua
1 local treesitter = require('nvim-treesitter')
2
3 treesitter.setup {
4 install_dir = vim.fn.stdpath('data') .. '/site',
5 }
6
7 treesitter.install {
8 'bash',
9 'c',
10 'cmake',
11 'cpp',
12 'json',
13 'lua',
14 'markdown',
15 'markdown_inline',
16 'python',
17 'query',
18 'toml',
19 'vim',
20 'vimdoc',
21 'yaml',
22 }
23
24 local function foldlevel(lnum, exclude_start)
25 if lnum < 1 or lnum > vim.api.nvim_buf_line_count(0) then
26 return 0
27 end
28 local value = vim.treesitter.foldexpr(lnum)
29 local level = tonumber(value:match('%d+')) or 0
30 -- A '>N' starts a new fold even when adjacent folds have the same level.
31 return math.max(0, level - (exclude_start and value:sub(1, 1) == '>' and 1 or 0))
32 end
33
34 function _G.DotfilesFoldExpr()
35 local line = vim.v.lnum
36 -- Keep only levels spanning the previous, current, and next lines.
37 -- Default foldminlines keeps interiors of fewer than two lines visible.
38 return math.min(foldlevel(line - 1), foldlevel(line, true), foldlevel(line + 1, true))
39 end
40
41 local function enable(buf)
42 vim.wo[0][0].foldexpr = '0'
43 if not pcall(vim.treesitter.start, buf) then
44 return
45 end
46
47 vim.wo[0][0].foldexpr = 'v:lua.DotfilesFoldExpr()'
48 vim.wo[0][0].foldmethod = 'expr'
49 end
50
51 vim.api.nvim_create_autocmd('FileType', {
52 callback = function(args) enable(args.buf) end,
53 })
54
55 -- Startup filetype detection can precede this config being sourced.
56 if vim.bo.filetype ~= '' then
57 enable(0)
58 end