repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

neovim/lua/bugabinga/treesitter.lua

Raw
local map = require 'bugabinga.std.keymap'
local auto = require 'bugabinga.std.auto'
local want = require 'bugabinga.std.want'

want {
	'nvim-treesitter.install',
	'nvim-treesitter.configs',
	'nvim-treesitter.parsers',
	'hlargs',
	'neogen',
} (function(install, configs, parsers, hlargs, neogen)
	install.compilers = { 'zig', 'clang', 'gcc', 'cl', 'cc', vim.fn.getenv 'CC' }
	install.prefer_git = true

	configs.setup {
		-- A list of parser names, or "all"
		ensure_installed = 'all',
		-- Install parsers asynchronously (only applied to `ensure_installed`)
		-- The synchronous install method seems to rely on a posix shell, which i
		-- do not use.
		sync_install = false,
		-- Automatically install missing parsers when entering buffer
		auto_install = true,

		highlight = {
			-- `false` will disable the whole extension
			enable = true,
			disable = {},

			-- Setting this to true will run `:h syntax` and tree-sitter at the same time.
			-- Set this to `true` if you depend on 'syntax' being enabled (like for indentation).
			-- Using this option may slow down your editor, and you may see some duplicate highlights.
			-- Instead of true it can also be a list of languages
			additional_vim_regex_highlighting = false,
		},
		incremental_selection = {
			enable = true,
			keymaps = {
				init_selection = 'gnn',
				node_incremental = 'grn',
				scope_incremental = 'grc',
				node_decremental = 'grm',
			},
		},
		-- indent on = with TS
		indent = {
			enable = true,
		},
		-- These features are mostly useful when there is treesitter parser, but no
		-- LSP.
		-- FIXME: harmonize the keybinds with LSP equivalents
		refactor = 'all',
		textsubjects = {
			enable = true,
			prev_selection = '<Down>', -- (Optional) keymap to select the previous selection
			keymaps = {
				['<Up>'] = 'textsubjects-smart',
				['<Right>'] = 'textsubjects-container-outer',
				['<Left>'] = 'textsubjects-container-inner',
			},
		},
		textobjects = {
			select = {
				enable = true,
				-- Automatically jump forward to textobj, similar to targets.vim
				lookahead = true,
				keymaps = {
					-- You can use the capture groups defined in textobjects.scm
					['af'] = '@function.outer',
					['if'] = '@function.inner',
					['ac'] = '@class.outer',
					['ic'] = '@class.inner',
				},
			},
			swap = {
				enable = true,
				swap_next = {
					['<leader>a'] = '@parameter.inner',
				},
				swap_previous = {
					['<leader>A'] = '@parameter.inner',
				},
			},
			move = {
				enable = true,
				set_jumps = true, -- whether to set jumps in the jumplist
				goto_next_start = {
					[']m'] = '@function.outer',
					[']]'] = '@class.outer',
				},
				goto_next_end = {
					[']M'] = '@function.outer',
					[']['] = '@class.outer',
				},
				goto_previous_start = {
					['[m'] = '@function.outer',
					['[['] = '@class.outer',
				},
				goto_previous_end = {
					['[M'] = '@function.outer',
					['[]'] = '@class.outer',
				},
			},
			lsp_interop = {
				enable = true,
				border = 'rounded',
				peek_definition_code = {
					['<leader>df'] = '@function.outer',
					['<leader>dc'] = '@class.outer',
				},
			},
		},
		playground = {
			enable = true,
			disable = {},
			updatetime = 25, -- Debounced time for highlighting nodes in the playground from source code
			persist_queries = false, -- Whether the query persists across vim sessions
			keybindings = {
				toggle_query_editor = 'o',
				toggle_hl_groups = 'i',
				toggle_injected_languages = 't',
				toggle_anonymous_nodes = 'a',
				toggle_language_display = 'I',
				focus_language = 'f',
				unfocus_language = 'F',
				update = 'R',
				goto_node = '<cr>',
				show_help = '?',
			},
		},
		context_commentstring = {
			enable = true,
		},
		-- autoclose and rename tags
		autotag = {
			enable = true,
		},
	}

	parsers.get_parser_configs().just = {
		install_info = {
			url = 'https://github.com/IndianBoy42/tree-sitter-just', -- local path or git repo
			files = { 'src/parser.c', 'src/scanner.cc' },
			branch = 'main',
		},
		maintainers = { '@IndianBoy42' },
	}

	-- highlight function arguments
	-- TODO: define color
	hlargs.setup()

	-- generate doc comment annotations
	-- TODO: setup keybind
	neogen.setup{
		snippet_engine = 'luasnip',
	}

	map {
		description = 'Generate Doc Comment for Function',
		category = map.CATEGORY.EDITING,
		mode = map.MODE.NORMAL,
		keys = map.KEY.LEADER .. map.KEY.N .. map.KEY.F,
		command = function() neogen.generate{ type = 'func'} end,
	}
	map {
		description = 'Generate Doc Comment for File',
		category = map.CATEGORY.EDITING,
		mode = map.MODE.NORMAL,
		keys = map.KEY.LEADER .. map.KEY.N .. map.KEY.F .. map.KEY.F,
		command = function() neogen.generate{ type = 'file'} end,
	}
	map {
		description = 'Generate Doc Comment for Class',
		category = map.CATEGORY.EDITING,
		mode = map.MODE.NORMAL,
		keys = map.KEY.LEADER .. map.KEY.N .. map.KEY.C,
		command = function() neogen.generate{ type = 'class'} end,
	}

	-- FIXME: this aucmd needs to be restricted to buffers where treesitter is loaded
	-- Treesitter highlights tend to highlight syntax errors immediately.
	-- But that i find distracting while typing.
	-- Disable the error highlight while typing, but keep it in normal mode.
	auto 'disable_error_highlight_while_typing' {
		{
			description = 'Disable the @error highlight while in Insert mode',
			events = 'InsertEnter',
			pattern = '*',
			command = function()
				want { 'nvim-treesitter.highlight' } (function(highlight)
					pcall(highlight.stop)
					pcall(highlight.set_custom_captures, { error = 'None' })
					pcall(highlight.start)
				end)
			end,
		},
		{
			description = 'Enable the @error highlight while in Normal mode',
			events = 'InsertLeave',
			pattern = '*',
			command = function()
				want { 'nvim-treesitter.highlight' } (function(highlight)
					pcall(highlight.stop)
					pcall(highlight.set_custom_captures, { error = 'TSError' })
					pcall(highlight.start)
				end)
			end,
		},
	}
end)