repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

neovim/lua/bugabinga/treesitter.lua

Raw
File outline
local puk = require 'std.puk'
local auto = require 'std.auto'
local ignored = require 'std.ignored'
local dbg = require 'std.dbg'

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

local function is_ignored(match)
	for _, v in ipairs(ignored.filetypes) do
		if v == match or match:match(v) then
			return true
		end
	end
	return false
end

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 is_ignored(match) then
			dbg.print('Ignore ' .. match .. ' for treesitter.')
			return
		end
		local ok, _ = pcall(vim.treesitter.start, data.buf)
		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)
				local task = require 'nvim-treesitter.install'.install(match, { force = false })
				task:await(function(err, success)
					if not success then
						vim.notify('Failed to install treesitter parser: ' .. match, vim.log.levels.ERROR)
						return
					end
					vim.treesitter.start(data.buf)
				end)
			end
			return
		end
		vim.opt_local.foldmethod = 'expr'
		vim.opt_local.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,
}

return {
	is_ignored = is_ignored,
}