-- Treesitter highlighting. With the grammars the installer compiled (ide/parsers.lock, when a -- C compiler was there) the queries of nvim-treesitter are used with them. Without, Neovim's own -- bundled grammars and queries are (C, Lua, Markdown, Vimscript, help, queries) and every other -- language is coloured by Neovim's regular syntax files. Nothing is ever installed from here. local M = {} M.compiled = false --- A grammar, and the file types it colours beside the one of its own name. M.filetypes = { bash = { "sh" }, tsx = { "typescriptreact" }, javascript = { "javascriptreact" }, vimdoc = { "help" }, json = { "jsonc" }, html = { "xhtml" }, } local function parsers_dir() local dir = require("llembas_ide").prefix .. "/site/pack/llembas/start/llembas-parsers/parser" return vim.fn.isdirectory(dir) == 1 and dir or nil end function M.setup() if parsers_dir() then -- The plugin's own start-up (its module system and :TSInstall commands) stays off: only its -- queries and the predicates they use are wanted. vim.g.loaded_nvim_treesitter = true local ok = pcall(vim.cmd.packadd, "nvim-treesitter") if ok and pcall(require, "nvim-treesitter.query_predicates") then M.compiled = true -- File types whose name is not their grammar's. nvim-treesitter names these when it starts -- up; its start-up is off here, so they are named here, for the grammars that are compiled. local has = {} for _, lang in ipairs(M.languages()) do has[lang] = true end for lang, fts in pairs(M.filetypes) do if has[lang] then pcall(vim.treesitter.language.register, lang, fts) end end else table.insert(require("llembas_ide").problems, "treesitter: grammars are compiled but nvim-treesitter's queries are missing") end end vim.api.nvim_create_autocmd("FileType", { group = vim.api.nvim_create_augroup("llembas_ide_treesitter", { clear = true }), callback = function(ev) M.start(ev.buf) end, }) end --- Start highlighting for a buffer when its language has a grammar and a query; quietly not otherwise. function M.start(buf) local ft = vim.bo[buf].filetype if ft == "" or vim.bo[buf].buftype == "terminal" then return false end local lang = vim.treesitter.language.get_lang(ft) or ft local ok, added = pcall(vim.treesitter.language.add, lang) if not ok or added == false then return false end local has_query, query = pcall(vim.treesitter.query.get, lang, "highlights") if not has_query or not query then return false end return (pcall(vim.treesitter.start, buf, lang)) end --- The languages with a compiled grammar. function M.languages() local out = {} for _, f in ipairs(vim.api.nvim_get_runtime_file("parser/*.so", true)) do out[vim.fn.fnamemodify(f, ":t:r")] = true end local list = vim.tbl_keys(out) table.sort(list) return list end return M