diff options
| author | Christian Clason <c.clason@uni-graz.at> | 2022-04-16 17:49:17 +0200 |
|---|---|---|
| committer | Christian Clason <christian.clason@uni-due.de> | 2022-04-30 16:08:01 +0200 |
| commit | 07eb437bb1c4c0a0298553dd509294f82362d6bc (patch) | |
| tree | 14dace08a1b7b3a1cb335fe8fc30497820c9e02f | |
| parent | chore!: remove ensure_installed='maintained' (diff) | |
| download | nvim-treesitter-07eb437bb1c4c0a0298553dd509294f82362d6bc.tar nvim-treesitter-07eb437bb1c4c0a0298553dd509294f82362d6bc.tar.gz nvim-treesitter-07eb437bb1c4c0a0298553dd509294f82362d6bc.tar.bz2 nvim-treesitter-07eb437bb1c4c0a0298553dd509294f82362d6bc.tar.lz nvim-treesitter-07eb437bb1c4c0a0298553dd509294f82362d6bc.tar.xz nvim-treesitter-07eb437bb1c4c0a0298553dd509294f82362d6bc.tar.zst nvim-treesitter-07eb437bb1c4c0a0298553dd509294f82362d6bc.zip | |
refactor!: update to Neovim 0.7 APIs
* set highlight groups via nvim_set_hl
* define autocommands via nvim_create_autocmd
* port plugin/nvim-treesitter.vim to Lua
* port healthcheck to Lua
| -rw-r--r-- | autoload/health/nvim_treesitter.vim | 3 | ||||
| -rw-r--r-- | lua/nvim-treesitter/configs.lua | 11 | ||||
| -rw-r--r-- | lua/nvim-treesitter/health.lua | 35 | ||||
| -rw-r--r-- | lua/nvim-treesitter/indent.lua | 5 | ||||
| -rw-r--r-- | lua/nvim-treesitter/info.lua | 33 | ||||
| -rw-r--r-- | plugin/nvim-treesitter.lua | 121 | ||||
| -rw-r--r-- | plugin/nvim-treesitter.vim | 106 | ||||
| -rw-r--r-- | scripts/minimal_init.lua | 2 |
8 files changed, 168 insertions, 148 deletions
diff --git a/autoload/health/nvim_treesitter.vim b/autoload/health/nvim_treesitter.vim deleted file mode 100644 index 53e187cda..000000000 --- a/autoload/health/nvim_treesitter.vim +++ /dev/null @@ -1,3 +0,0 @@ -function! health#nvim_treesitter#check() - lua require 'nvim-treesitter.health'.check() -endfunction diff --git a/lua/nvim-treesitter/configs.lua b/lua/nvim-treesitter/configs.lua index cc7413560..2d64098a5 100644 --- a/lua/nvim-treesitter/configs.lua +++ b/lua/nvim-treesitter/configs.lua @@ -100,8 +100,13 @@ local function enable_mod_conf_autocmd(mod) return end - local cmd = string.format("lua require'nvim-treesitter.configs'.reattach_module('%s')", mod) - api.nvim_command(string.format("autocmd NvimTreesitter FileType * %s", cmd)) + api.nvim_create_autocmd("FileType", { + group = "NvimTreesitter", + callback = function() + require("nvim-treesitter.configs").reattach_module(mod) + end, + desc = "Reattach module", + }) config_mod.loaded = true end @@ -150,7 +155,7 @@ local function disable_mod_conf_autocmd(mod) end -- TODO(kyazdani): detach the correct autocmd... doesn't work when using %s, cmd. -- This will remove all autocomands! - api.nvim_command "autocmd! NvimTreesitter FileType *" + api.nvim_clear_autocmds { event = "FileType", group = "NvimTreesitter" } config_mod.loaded = false end diff --git a/lua/nvim-treesitter/health.lua b/lua/nvim-treesitter/health.lua index aa2b35b88..3f5564100 100644 --- a/lua/nvim-treesitter/health.lua +++ b/lua/nvim-treesitter/health.lua @@ -7,29 +7,26 @@ local shell = require "nvim-treesitter.shell_command_selectors" local install = require "nvim-treesitter.install" local utils = require "nvim-treesitter.utils" -local health_start = vim.fn["health#report_start"] -local health_ok = vim.fn["health#report_ok"] -local health_error = vim.fn["health#report_error"] -local health_warn = vim.fn["health#report_warn"] +local health = require "health" local M = {} local NVIM_TREESITTER_MINIMUM_ABI = 13 local function install_health() - health_start "Installation" + health.report_start "Installation" if fn.has "nvim-0.7" == 0 then - health_error "Nvim-treesitter requires Neovim 0.7.0+" + health.report_error "Nvim-treesitter requires Neovim 0.7.0+" end if fn.executable "tree-sitter" == 0 then - health_warn( + health.report_warn( "`tree-sitter` executable not found (parser generator, only needed for :TSInstallFromGrammar," .. " not required for :TSInstall)" ) else - health_ok( + health.report_ok( "`tree-sitter` found " .. (utils.ts_cli_version() or "(unknown version)") .. " (parser generator, only needed for :TSInstallFromGrammar)" @@ -37,7 +34,7 @@ local function install_health() end if fn.executable "node" == 0 then - health_warn( + health.report_warn( "`node` executable not found (only needed for :TSInstallFromGrammar," .. " not required for :TSInstall)" ) else @@ -45,21 +42,21 @@ local function install_health() local result = handle:read "*a" handle:close() local version = vim.split(result, "\n")[1] - health_ok("`node` found " .. version .. " (only needed for :TSInstallFromGrammar)") + health.report_ok("`node` found " .. version .. " (only needed for :TSInstallFromGrammar)") end if fn.executable "git" == 0 then - health_error("`git` executable not found.", { + health.report_error("`git` executable not found.", { "Install it with your package manager.", "Check that your `$PATH` is set correctly.", }) else - health_ok "`git` executable found." + health.report_ok "`git` executable found." end local cc = shell.select_executable(install.compilers) if not cc then - health_error("`cc` executable not found.", { + health.report_error("`cc` executable not found.", { "Check that any of " .. vim.inspect(install.compilers) .. " is in your $PATH" @@ -67,7 +64,7 @@ local function install_health() }) else local version = vim.fn.systemlist(cc .. (cc == "cl" and "" or " --version"))[1] - health_ok( + health.report_ok( "`" .. cc .. "` executable found. Selected from " @@ -77,7 +74,7 @@ local function install_health() end if vim.treesitter.language_version then if vim.treesitter.language_version >= NVIM_TREESITTER_MINIMUM_ABI then - health_ok( + health.report_ok( "Neovim was compiled with tree-sitter runtime ABI version " .. vim.treesitter.language_version .. " (required >=" @@ -85,7 +82,7 @@ local function install_health() .. "). Parsers must be compatible with runtime ABI." ) else - health_error( + health.report_error( "Neovim was compiled with tree-sitter runtime ABI version " .. vim.treesitter.language_version .. ".\n" @@ -141,9 +138,9 @@ function M.check() x) errors found in the query, try to run :TSUpdate {lang}]] table.insert(parser_installation, legend) -- Finally call the report function - health_start(table.concat(parser_installation, "\n")) + health.report_start(table.concat(parser_installation, "\n")) if #error_collection > 0 then - health_start "The following errors have been detected:" + health.report_start "The following errors have been detected:" for _, p in ipairs(error_collection) do local lang, type, err = unpack(p) local lines = {} @@ -164,7 +161,7 @@ function M.check() end end end - health_error(table.concat(lines, "\n")) + health.report_error(table.concat(lines, "\n")) end end end diff --git a/lua/nvim-treesitter/indent.lua b/lua/nvim-treesitter/indent.lua index de165904e..d5a729cad 100644 --- a/lua/nvim-treesitter/indent.lua +++ b/lua/nvim-treesitter/indent.lua @@ -195,7 +195,10 @@ local indent_funcs = {} function M.attach(bufnr) indent_funcs[bufnr] = vim.bo.indentexpr vim.bo.indentexpr = "nvim_treesitter#indent()" - vim.api.nvim_command("au Filetype " .. vim.bo.filetype .. " setlocal indentexpr=nvim_treesitter#indent()") + vim.api.nvim_create_autocmd("Filetype", { + pattern = vim.bo.filetype, + command = "setlocal indentexpr=nvim_treesitter#indent()", + }) end function M.detach(bufnr) diff --git a/lua/nvim-treesitter/info.lua b/lua/nvim-treesitter/info.lua index ed5a90b8d..bad0b4e60 100644 --- a/lua/nvim-treesitter/info.lua +++ b/lua/nvim-treesitter/info.lua @@ -115,21 +115,24 @@ local function print_info_modules(parserlist, module) api.nvim_buf_set_option(curbuf, "modified", false) api.nvim_buf_set_option(curbuf, "buftype", "nofile") - api.nvim_exec( - [[ - syntax match TSModuleInfoGood /✓/ - syntax match TSModuleInfoBad /✗/ - syntax match TSModuleInfoHeader /^>>.*$/ contains=TSModuleInfoNamespace - syntax match TSModuleInfoNamespace /^>> \w*/ contained - syntax match TSModuleInfoParser /^[^> ]*\ze / - highlight default TSModuleInfoGood guifg=LightGreen gui=bold - highlight default TSModuleInfoBad guifg=Crimson - highlight default link TSModuleInfoHeader Type - highlight default link TSModuleInfoNamespace Statement - highlight default link TSModuleInfoParser Identifier - ]], - false - ) + vim.cmd [[ + syntax match TSModuleInfoGood /✓/ + syntax match TSModuleInfoBad /✗/ + syntax match TSModuleInfoHeader /^>>.*$/ contains=TSModuleInfoNamespace + syntax match TSModuleInfoNamespace /^>> \w*/ contained + syntax match TSModuleInfoParser /^[^> ]*\ze / + ]] + + local highlights = { + TSModuleInfoGood = { fg = "LightGreen", bold = true, default = true }, + TSModuleInfoBad = { fg = "Crimson", default = true }, + TSModuleInfoHeader = { link = "Type", default = true }, + TSModuleInfoNamespace = { link = "Statement", default = true }, + TSModuleInfoParser = { link = "Identifier", default = true }, + } + for k, v in pairs(highlights) do + api.nvim_set_hl(0, k, v) + end end local function module_info(module) diff --git a/plugin/nvim-treesitter.lua b/plugin/nvim-treesitter.lua new file mode 100644 index 000000000..5109c80f2 --- /dev/null +++ b/plugin/nvim-treesitter.lua @@ -0,0 +1,121 @@ +-- Last Change: 2022 Apr 16 + +if vim.g.loaded_nvim_treesitter then + return +end +vim.g.loaded_nvim_treesitter = true + +-- setup modules +require("nvim-treesitter").setup() + +local api = vim.api + +-- define autocommands +local augroup = api.nvim_create_augroup("NvimTreesitter", {}) + +api.nvim_create_autocmd("Filetype", { + pattern = "query", + group = augroup, + callback = function() + api.nvim_clear_autocmds { + group = augroup, + event = "BufWritePost", + } + api.nvim_create_autocmd("BufWritePost", { + group = augroup, + buffer = 0, + callback = function(opts) + require("nvim-treesitter.query").invalidate_query_file(opts.file) + end, + desc = "Invalidate query file", + }) + end, + desc = "Reload query", +}) + +-- define highlights +local highlights = { + TSNone = { default = true }, + TSPunctDelimiter = { link = "Delimiter", default = true }, + TSPunctBracket = { link = "Delimiter", default = true }, + TSPunctSpecial = { link = "Delimiter", default = true }, + + TSConstant = { link = "Constant", default = true }, + TSConstBuiltin = { link = "Special", default = true }, + TSConstMacro = { link = "Define", default = true }, + TSString = { link = "String", default = true }, + TSStringRegex = { link = "String", default = true }, + TSStringEscape = { link = "SpecialChar", default = true }, + TSStringSpecial = { link = "SpecialChar", default = true }, + TSCharacter = { link = "Character", default = true }, + TSCharacterSpecial = { link = "SpecialChar", default = true }, + TSNumber = { link = "Number", default = true }, + TSBoolean = { link = "Boolean", default = true }, + TSFloat = { link = "Float", default = true }, + + TSFunction = { link = "Function", default = true }, + TSFuncBuiltin = { link = "Special", default = true }, + TSFuncMacro = { link = "Macro", default = true }, + TSParameter = { link = "Identifier", default = true }, + TSParameterReference = { link = "TSParameter", default = true }, + TSMethod = { link = "Function", default = true }, + TSField = { link = "Identifier", default = true }, + TSProperty = { link = "Identifier", default = true }, + TSConstructor = { link = "Special", default = true }, + TSAnnotation = { link = "PreProc", default = true }, + TSAttribute = { link = "PreProc", default = true }, + TSNamespace = { link = "Include", default = true }, + TSSymbol = { link = "Identifier", default = true }, + + TSConditional = { link = "Conditional", default = true }, + TSRepeat = { link = "Repeat", default = true }, + TSLabel = { link = "Label", default = true }, + TSOperator = { link = "Operator", default = true }, + TSKeyword = { link = "Keyword", default = true }, + TSKeywordFunction = { link = "Keyword", default = true }, + TSKeywordOperator = { link = "TSOperator", default = true }, + TSKeywordReturn = { link = "TSKeyword", default = true }, + TSException = { link = "Exception", default = true }, + TSDebug = { link = "Debug", default = true }, + TSDefine = { link = "Define", default = true }, + TSPreProc = { link = "PreProc", default = true }, + TSStorageClass = { link = "StorageClass", default = true }, + + TSTodo = { link = "Todo", default = true }, + + TSType = { link = "Type", default = true }, + TSTypeBuiltin = { link = "Type", default = true }, + TSTypeQualifier = { link = "Type", default = true }, + TSTypeDefinition = { link = "Typedef", default = true }, + + TSInclude = { link = "Include", default = true }, + + TSVariableBuiltin = { link = "Special", default = true }, + + TSText = { link = "TSNone", default = true }, + TSStrong = { bold = true, default = true }, + TSEmphasis = { italic = true, default = true }, + TSUnderline = { underline = true }, + TSStrike = { strikethrough = true }, + + TSMath = { link = "Special", default = true }, + TSTextReference = { link = "Constant", default = true }, + TSEnvironment = { link = "Macro", default = true }, + TSEnvironmentName = { link = "Type", default = true }, + TSTitle = { link = "Title", default = true }, + TSLiteral = { link = "String", default = true }, + TSURI = { link = "Underlined", default = true }, + + TSComment = { link = "Comment", default = true }, + TSNote = { link = "SpecialComment", default = true }, + TSWarning = { link = "Todo", default = true }, + TSDanger = { link = "WarningMsg", default = true }, + + TSTag = { link = "Label", default = true }, + TSTagDelimiter = { link = "Delimiter", default = true }, + TSTagAttribute = { link = "TSProperty", default = true }, +} + +for k, v in pairs(highlights) do + api.nvim_set_hl(0, k, v) +end diff --git a/plugin/nvim-treesitter.vim b/plugin/nvim-treesitter.vim deleted file mode 100644 index c30bcaca5..000000000 --- a/plugin/nvim-treesitter.vim +++ /dev/null @@ -1,106 +0,0 @@ -" Last Change: 2020 Aug 13 - -if exists('g:loaded_nvim_treesitter') - finish -endif - -augroup NvimTreesitter - " on every query file write we want to set an autocommand that will reload the cache - autocmd FileType query - \ autocmd! NvimTreesitter BufWritePost <buffer> call v:lua.require('nvim-treesitter.query').invalidate_query_file(expand('%:p')) -augroup END - -let g:loaded_nvim_treesitter = 1 - -lua require'nvim-treesitter'.setup() - -function s:has_attr(attr, mode) - let norm_color = synIDattr(hlID('Normal'), a:attr, a:mode) - return strlen(norm_color) > 0 -endfunction - -" if the ctermfg or guifg is not known by nvim then using the -" fg or foreground highlighting value will cause an E419 error -" so we check to see if either highlight has been set if not default to NONE -let cterm_normal = s:has_attr('fg', 'cterm') ? 'fg' : 'NONE' -let gui_normal = s:has_attr('fg', 'gui') ? 'foreground' : 'NONE' - -execute 'highlight default TSNone term=NONE cterm=NONE gui=NONE guifg='.gui_normal.' ctermfg='.cterm_normal - -highlight default link TSPunctDelimiter Delimiter -highlight default link TSPunctBracket Delimiter -highlight default link TSPunctSpecial Delimiter - -highlight default link TSConstant Constant -highlight default link TSConstBuiltin Special -highlight default link TSConstMacro Define -highlight default link TSString String -highlight default link TSStringRegex String -highlight default link TSStringEscape SpecialChar -highlight default link TSStringSpecial SpecialChar -highlight default link TSCharacter Character -highlight default link TSCharacterSpecial SpecialChar -highlight default link TSNumber Number -highlight default link TSBoolean Boolean -highlight default link TSFloat Float - -highlight default link TSFunction Function -highlight default link TSFuncBuiltin Special -highlight default link TSFuncMacro Macro -highlight default link TSParameter Identifier -highlight default link TSParameterReference TSParameter -highlight default link TSMethod Function -highlight default link TSField Identifier -highlight default link TSProperty Identifier -highlight default link TSConstructor Special -highlight default link TSAnnotation PreProc -highlight default link TSAttribute PreProc -highlight default link TSNamespace Include -highlight default link TSSymbol Identifier - -highlight default link TSConditional Conditional -highlight default link TSRepeat Repeat -highlight default link TSLabel Label -highlight default link TSOperator Operator -highlight default link TSKeyword Keyword -highlight default link TSKeywordFunction Keyword -highlight default link TSKeywordOperator TSOperator -highlight default link TSKeywordReturn TSKeyword -highlight default link TSException Exception -highlight default link TSDebug Debug -highlight default link TSDefine Define -highlight default link TSPreProc PreProc -highlight default link TSStorageClass StorageClass - -highlight default link TSTodo Todo - -highlight default link TSType Type -highlight default link TSTypeBuiltin Type -highlight default link TSTypeQualifier Type -highlight default link TSTypeDefinition Typedef - -highlight default link TSInclude Include - -highlight default link TSVariableBuiltin Special - -highlight default link TSText TSNone -highlight default TSStrong term=bold cterm=bold gui=bold -highlight default TSEmphasis term=italic cterm=italic gui=italic -highlight default TSUnderline term=underline cterm=underline gui=underline -highlight default TSStrike term=strikethrough cterm=strikethrough gui=strikethrough -highlight default link TSMath Special -highlight default link TSTextReference Constant -highlight default link TSEnvironment Macro -highlight default link TSEnvironmentName Type -highlight default link TSTitle Title -highlight default link TSLiteral String -highlight default link TSURI Underlined - -highlight default link TSComment Comment -highlight default link TSNote SpecialComment -highlight default link TSWarning Todo -highlight default link TSDanger WarningMsg - -highlight default link TSTag Label -highlight default link TSTagDelimiter Delimiter -highlight default link TSTagAttribute TSProperty diff --git a/scripts/minimal_init.lua b/scripts/minimal_init.lua index 8a6a8506a..546dd0a6c 100644 --- a/scripts/minimal_init.lua +++ b/scripts/minimal_init.lua @@ -1,6 +1,6 @@ vim.cmd [[set runtimepath+=.]] vim.cmd [[runtime! plugin/plenary.vim]] -vim.cmd [[runtime! plugin/nvim-treesitter.vim]] +vim.cmd [[runtime! plugin/nvim-treesitter.lua]] vim.cmd [[au BufRead,BufNewFile *.conf set filetype=hocon]] vim.cmd [[au BufRead,BufNewFile *.gleam set filetype=gleam]] |
