repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

neovim/lua/bugabinga/treesitter.lua

Raw
local puk = require 'std.puk'
local auto = require 'std.auto'
local ignored = require 'std.ignored'
local table = require 'std.table'

require 'bugabinga.health'.add_dependency
{ name_of_executable = 'zig' }
		{ name = 'TreeSitter CLI', name_of_executable = 'tree-sitter' }
		{ name_of_executable = 'tar' }
		{ name_of_executable = 'curl' }

local treesitter = puk 'nvim-treesitter'
treesitter.setup {}

auto 'look_for_treesitter_parser' {
	description = 'Try loading a treesitter parser, if available.',
	events = 'FileType',
	pattern = '*',
	command = function(data)
		local match = data.match
		if match == 'text' then return end
		if table.contains(ignored.filetypes, match) then
			vim.notify('Ignore ' .. match .. ' for treesitter.', vim.log.levels.TRACE)
			return
		end
		local ok, err = pcall(vim.treesitter.start)
		if not ok then
			local available = require 'nvim-treesitter.parsers'
			local potential_parser = available[match]
			if potential_parser then
				vim.notify('Found potential treesitter parser for current filetype. Trying to install it. ' .. match)
				vim.cmd.TSInstall(match)
				require 'nvim-treesitter.install'.install(match, { force = false }, function()
					vim.treesitter.start()
					-- vim.cmd.edit()
				end)
			elseif err then
				vim.notify(err, vim.log.levels.ERROR)
			end
			return
		end
		vim.bo.indentexpr = "v:lua.require'nvim-treesitter'.indentexpr()"
		vim.wo.foldexpr = 'v:lua.vim.treesitter.foldexpr()'
	end,
}
-- add asciidoc parser
auto 'install_asciidoc_parser' {
	description = "Install asciidoc parser into nvim-treesitter",
	events = 'User',
	pattern = 'TSUpdate',
	command = function()
		require('nvim-treesitter.parsers').asciidoc = {
			install_info = {
				url = 'https://github.com/cathaysia/tree-sitter-asciidoc',
				revision = "5b6ccea9953b939959f447f7bfea699ae6968a99",
				branch = 'master', -- only needed if different from default branch
				location = 'tree-sitter-asciidoc',
				generate = false,
				generate_from_json = false,
				queries = 'queries/asciidoc', -- also install queries from given directory
			},
		}
	end,
}
-- add d2 parser
vim.treesitter.language.register("d2", "d2")
auto 'install_d2_parser' {
	description = "Install d2 parser into nvim-treesitter",
	events = 'User',
	pattern = 'TSUpdate',
	command = function()
		require('nvim-treesitter.parsers').d2 = {
			install_info = {
				url = 'https://github.com/ravsii/tree-sitter-d2',
				revision = '519f92699691d101d1aa91f69956383de7b28ae6',
				branch = 'main', -- only needed if different from default branch
				generate = true,
				generate_from_json = true,
				queries = 'queries', -- also install queries from given directory
			},
		}
	end,
}