diff options
| author | Christian Clason <c.clason@uni-graz.at> | 2023-06-12 09:54:30 -0600 |
|---|---|---|
| committer | Christian Clason <c.clason@uni-graz.at> | 2025-05-12 18:43:40 +0200 |
| commit | 692b051b09935653befdb8f7ba8afdb640adf17b (patch) | |
| tree | 167162b6b129ae04f68c5735078521a72917c742 /scripts | |
| parent | feat(c-family): inherit injections (diff) | |
| download | nvim-treesitter-692b051b09935653befdb8f7ba8afdb640adf17b.tar nvim-treesitter-692b051b09935653befdb8f7ba8afdb640adf17b.tar.gz nvim-treesitter-692b051b09935653befdb8f7ba8afdb640adf17b.tar.bz2 nvim-treesitter-692b051b09935653befdb8f7ba8afdb640adf17b.tar.lz nvim-treesitter-692b051b09935653befdb8f7ba8afdb640adf17b.tar.xz nvim-treesitter-692b051b09935653befdb8f7ba8afdb640adf17b.tar.zst nvim-treesitter-692b051b09935653befdb8f7ba8afdb640adf17b.zip | |
feat!: drop modules, general refactor and cleanup
Diffstat (limited to 'scripts')
| -rwxr-xr-x | scripts/check-queries.lua | 80 | ||||
| -rwxr-xr-x | scripts/ci-install.sh | 4 | ||||
| -rwxr-xr-x | scripts/format-queries.lua | 76 | ||||
| -rw-r--r-- | scripts/minimal_init.lua | 36 | ||||
| -rwxr-xr-x | scripts/update-lockfile.sh | 16 | ||||
| -rwxr-xr-x | scripts/update-readme.lua | 90 | ||||
| -rwxr-xr-x | scripts/write-lockfile.lua | 56 |
7 files changed, 220 insertions, 138 deletions
diff --git a/scripts/check-queries.lua b/scripts/check-queries.lua index f3b45ea1a..f5d6d463b 100755 --- a/scripts/check-queries.lua +++ b/scripts/check-queries.lua @@ -1,36 +1,35 @@ #!/usr/bin/env -S nvim -l -vim.opt.rtp:prepend "./" +vim.opt.runtimepath:append('.') -- Equivalent to print(), but this will ensure consistent output regardless of -- operating system. local function io_print(text) if not text then - text = "" + text = '' end - io.write(text, "\n") + io.write(text, '\n') end local function extract_captures() - local lines = vim.fn.readfile "CONTRIBUTING.md" local captures = {} local current_query - for _, line in ipairs(lines) do - if vim.startswith(line, "### ") then - current_query = vim.fn.tolower(line:sub(5)) - elseif vim.startswith(line, "@") and current_query then + for line in io.lines('CONTRIBUTING.md') do + if vim.startswith(line, '### ') then + current_query = line:sub(5):lower() + elseif vim.startswith(line, '@') and current_query then if not captures[current_query] then captures[current_query] = {} end - table.insert(captures[current_query], vim.split(line:sub(2), " ", true)[1]) + table.insert(captures[current_query], vim.split(line:sub(2), ' ', true)[1]) end end -- Complete captures for injections. - local parsers = vim.tbl_keys(require("nvim-treesitter.parsers").list) + local parsers = vim.tbl_keys(require('nvim-treesitter.parsers').configs) for _, lang in pairs(parsers) do - table.insert(captures["injections"], lang) + table.insert(captures['injections'], lang) end return captures @@ -38,36 +37,38 @@ end local function do_check() local timings = {} - local queries = require "nvim-treesitter.query" - local parsers = #_G.arg > 0 and { unpack(_G.arg) } or require("nvim-treesitter.info").installed_parsers() - local query_types = queries.built_in_query_groups + local parsers = require('nvim-treesitter.config').installed_parsers() + local query_types = require('nvim-treesitter.health').bundled_queries local captures = extract_captures() local errors = {} - io_print "::group::Check parsers" + io_print('::group::Check parsers') for _, lang in pairs(parsers) do timings[lang] = {} for _, query_type in pairs(query_types) do local before = vim.loop.hrtime() - local ok, query = pcall(queries.get_query, lang, query_type) + local ok, query = pcall(vim.treesitter.query.get, lang, query_type) local after = vim.loop.hrtime() local duration = after - before table.insert(timings, { duration = duration, lang = lang, query_type = query_type }) - io_print("Checking " .. lang .. " " .. query_type .. string.format(" (%.02fms)", duration * 1e-6)) + io_print( + 'Checking ' .. lang .. ' ' .. query_type .. string.format(' (%.02fms)', duration * 1e-6) + ) if not ok then - local err_msg = lang .. " (" .. query_type .. "): " .. query + local err_msg = lang .. ' (' .. query_type .. '): ' .. query errors[#errors + 1] = err_msg else if query then for _, capture in ipairs(query.captures) do local is_valid = ( - vim.startswith(capture, "_") -- Helpers. - or vim.tbl_contains(captures[query_type], capture) + vim.startswith(capture, '_') -- Helpers. + or vim.list_contains(captures[query_type], capture) ) if not is_valid then - local error = string.format("(x) Invalid capture @%s in %s for %s.", capture, query_type, lang) + local error = + string.format('(x) Invalid capture @%s in %s for %s.', capture, query_type, lang) errors[#errors + 1] = error end end @@ -76,10 +77,10 @@ local function do_check() end end - io_print "::endgroup::" + io_print('::endgroup::') if #errors > 0 then - io_print "\nCheck failed!\nErrors:" + io_print('\nCheck failed!\nErrors:') for _, err in ipairs(errors) do print(err) end @@ -89,35 +90,42 @@ local function do_check() end local ok, result = pcall(do_check) -local allowed_to_fail = vim.split(vim.env.ALLOWED_INSTALLATION_FAILURES or "", ",", true) +local allowed_to_fail = vim.split(vim.env.ALLOWED_INSTALLATION_FAILURES or '', ',', true) -for k, v in pairs(require("nvim-treesitter.parsers").get_parser_configs()) do - if not require("nvim-treesitter.parsers").has_parser(k) then +for k, v in pairs(require('nvim-treesitter.parsers').configs) do + if #vim.api.nvim_get_runtime_file('parser/' .. k .. '.*', false) == 0 then -- On CI all parsers that can be installed from C files should be installed if vim.env.CI and not v.install_info.requires_generate_from_grammar - and not vim.tbl_contains(allowed_to_fail, k) + and not vim.list_contains(allowed_to_fail, k) then - io_print("Error: parser for " .. k .. " is not installed") - vim.cmd "cq" + io_print('Error: parser for ' .. k .. ' is not installed') + vim.cmd('cq') else - io_print("Warning: parser for " .. k .. " is not installed") + io_print('Warning: parser for ' .. k .. ' is not installed') end end end if ok then - io_print "::group::Timings" + io_print('::group::Timings') table.sort(result, function(a, b) return a.duration < b.duration end) for i, val in ipairs(result) do - io_print(string.format("%i. %.02fms %s %s", #result - i + 1, val.duration * 1e-6, val.lang, val.query_type)) + io_print( + string.format( + '%i. %.02fms %s %s', + #result - i + 1, + val.duration * 1e-6, + val.lang, + val.query_type + ) + ) end - io_print "::endgroup::" - io_print "Check successful!" - vim.cmd "q" + io_print('::endgroup::') + io_print('Check successful!') else - vim.cmd "cq" + vim.cmd('cq') end diff --git a/scripts/ci-install.sh b/scripts/ci-install.sh index 5ab8887de..100cfde27 100755 --- a/scripts/ci-install.sh +++ b/scripts/ci-install.sh @@ -2,6 +2,8 @@ set -e +NVIM_TAG=${NVIM_TAG-nightly} + os=$(uname -s) if [[ $os == Linux ]]; then wget https://github.com/neovim/neovim/releases/download/${NVIM_TAG}/nvim-linux-x86_64.tar.gz @@ -18,7 +20,7 @@ elif [[ $os == Darwin ]]; then mkdir -p ~/.local/share/nvim/site/pack/nvim-treesitter/start ln -s "$PWD" ~/.local/share/nvim/site/pack/nvim-treesitter/start else - curl -L https://github.com/neovim/neovim/releases/download/${NVIM_TAG}/nvim-win64.zip -o nvim-win64.zip + curl -L "https://github.com/neovim/neovim/releases/download/${NVIM_TAG}/nvim-win64.zip" -o nvim-win64.zip unzip nvim-win64 mkdir -p ~/AppData/Local/nvim/pack/nvim-treesitter/start mkdir -p ~/AppData/Local/nvim-data diff --git a/scripts/format-queries.lua b/scripts/format-queries.lua index 0839760d2..8639daf2b 100755 --- a/scripts/format-queries.lua +++ b/scripts/format-queries.lua @@ -6,16 +6,16 @@ local get_node_text = ts.get_node_text ---@type string[] local files -local arg = _G.arg[1] or "." -if arg:match ".*%.scm$" then +local arg = _G.arg[1] or '.' +if arg:match('.*%.scm$') then files = { arg } else - files = vim.fn.split(vim.fn.glob(arg .. "/**/*.scm")) + files = vim.fn.split(vim.fn.glob(arg .. '/**/*.scm')) end -ts.query.add_predicate("kind-eq?", function(match, _, _, pred) +ts.query.add_predicate('kind-eq?', function(match, _, _, pred) local cap = match[pred[2]] - local node = type(cap) == "table" and cap[1] or cap + local node = type(cap) == 'table' and cap[1] or cap if not node then return true end @@ -24,9 +24,9 @@ ts.query.add_predicate("kind-eq?", function(match, _, _, pred) return vim.tbl_contains(types, node:type()) end, true) -ts.query.add_predicate("is-start-of-line?", function(match, _, _, pred) +ts.query.add_predicate('is-start-of-line?', function(match, _, _, pred) local cap = match[pred[2]] - local node = type(cap) == "table" and cap[1] or cap + local node = type(cap) == 'table' and cap[1] or cap if not node then return true end @@ -35,7 +35,7 @@ ts.query.add_predicate("is-start-of-line?", function(match, _, _, pred) end) --- Control the indent here. Change to \t if uses tab instead -local indent_str = " " +local indent_str = ' ' local indent_width_plus_one = 3 local textwidth = 100 @@ -328,7 +328,7 @@ local function append_lines(lines, lines_to_append) for i = 1, #lines_to_append, 1 do lines[#lines] = lines[#lines] .. lines_to_append[i] if i ~= #lines_to_append then - lines[#lines + 1] = "" + lines[#lines + 1] = '' end end end @@ -347,16 +347,17 @@ local function iter(bufnr, node, lines, q, level) apply_newline = false lines[#lines + 1] = string.rep(indent_str, level) end - if q["format.ignore"][id] then - local text = vim.split(get_node_text(child, bufnr):gsub("\r\n?", "\n"), "\n", { trimempty = true }) + if q['format.ignore'][id] then + local text = + vim.split(get_node_text(child, bufnr):gsub('\r\n?', '\n'), '\n', { trimempty = true }) append_lines(lines, text) - elseif not q["format.remove"][id] then - if not q["format.cancel-prepend"][id] then - if q["format.prepend-newline"][id] then + elseif not q['format.remove'][id] then + if not q['format.cancel-prepend'][id] then + if q['format.prepend-newline'][id] then lines[#lines + 1] = string.rep(indent_str, level) - elseif q["format.prepend-space"][id] then - if not q["format.prepend-space"][id]["conditional-newline"] then - lines[#lines] = lines[#lines] .. " " + elseif q['format.prepend-space'][id] then + if not q['format.prepend-space'][id]['conditional-newline'] then + lines[#lines] = lines[#lines] .. ' ' elseif child:byte_length() + 1 + #lines[#lines] > textwidth then lines[#lines + 1] = string.rep(indent_str, level) else @@ -365,43 +366,47 @@ local function iter(bufnr, node, lines, q, level) local _, _, byte_start = child:start() local _, _, byte_end = node:end_() if - q["format.prepend-space"][id]["lookahead-newline"] + q['format.prepend-space'][id]['lookahead-newline'] and (byte_end - byte_start) + #lines[#lines] > textwidth then lines[#lines + 1] = string.rep(indent_str, level) else - lines[#lines] = lines[#lines] .. " " + lines[#lines] = lines[#lines] .. ' ' end end end end - if q["format.replace"][id] then - append_lines(lines, vim.split(q["format.replace"][id].text, "\n", { trimempty = true })) + if q['format.replace'][id] then + append_lines(lines, vim.split(q['format.replace'][id].text, '\n', { trimempty = true })) elseif child:named_child_count() == 0 -- Workaround to preserve string content - or child:type() == "string" + or child:type() == 'string' then append_lines( lines, - vim.split(string.gsub(get_node_text(child, bufnr), "\r\n?", "\n"), "\n+", { trimempty = true }) + vim.split( + string.gsub(get_node_text(child, bufnr), '\r\n?', '\n'), + '\n+', + { trimempty = true } + ) ) else iter(bufnr, child, lines, q, level) end - if q["format.indent.begin"][id] then + if q['format.indent.begin'][id] then level = level + 1 apply_newline = true - elseif q["format.indent.dedent"][id] then + elseif q['format.indent.dedent'][id] then lines[#lines] = string.sub(lines[#lines], indent_width_plus_one) end end - if q["format.cancel-append"][id] then + if q['format.cancel-append'][id] then apply_newline = false - elseif q["format.append-newline"][id] then + elseif q['format.append-newline'][id] then apply_newline = true - elseif q["format.append-space"][id] then - lines[#lines] = lines[#lines] .. " " + elseif q['format.append-space'][id] then + lines[#lines] = lines[#lines] .. ' ' end end end @@ -409,7 +414,7 @@ end ---@param bufnr integer ---@param queries string local function format(bufnr, queries) - local lines = { "" } + local lines = { '' } -- stylua: ignore local map = { ['format.ignore'] = {}, -- Ignore the node and its children @@ -424,11 +429,12 @@ local function format(bufnr, queries) ['format.replace'] = {}, -- Dedicated capture used to store results of `(#gsub!)` ['format.remove'] = {}, -- Do not add the syntax node to the result, i.e. brackets [], parens () } - local root = ts.get_parser(bufnr, "query"):parse(true)[1]:root() - local query = ts.query.parse("query", queries) + local root = ts.get_parser(bufnr, 'query'):parse(true)[1]:root() + local query = ts.query.parse('query', queries) for id, node, metadata in query:iter_captures(root, bufnr) do - if query.captures[id]:sub(1, 1) ~= "_" then - map[query.captures[id]][node:id()] = metadata and (metadata[id] and metadata[id] or metadata) or {} + if query.captures[id]:sub(1, 1) ~= '_' then + map[query.captures[id]][node:id()] = metadata and (metadata[id] and metadata[id] or metadata) + or {} end end @@ -443,4 +449,4 @@ for _, file in ipairs(files) do format(buf, format_queries) end -vim.cmd "silent wa!" +vim.cmd('silent wa!') diff --git a/scripts/minimal_init.lua b/scripts/minimal_init.lua index f7a7ee590..c826ddbf2 100644 --- a/scripts/minimal_init.lua +++ b/scripts/minimal_init.lua @@ -1,25 +1,25 @@ -vim.opt.runtimepath:append "." -vim.cmd.runtime { "plugin/plenary.vim", bang = true } -vim.cmd.runtime { "plugin/nvim-treesitter.lua", bang = true } +vim.opt.runtimepath:append('.') +vim.cmd.runtime({ 'plugin/plenary.vim', bang = true }) +vim.cmd.runtime({ 'plugin/nvim-treesitter.lua', bang = true }) +vim.cmd.runtime({ 'plugin/query_predicates.lua', bang = true }) -vim.filetype.add { +vim.filetype.add({ extension = { - conf = "hocon", - cmm = "t32", - hurl = "hurl", - ncl = "nickel", - tig = "tiger", - usd = "usd", - usda = "usd", - wgsl = "wgsl", - w = "wing", + conf = 'hocon', + cmm = 't32', + ncl = 'nickel', + tig = 'tiger', + w = 'wing', }, -} +}) vim.o.swapfile = false vim.bo.swapfile = false -require("nvim-treesitter.configs").setup { - indent = { enable = true }, - highlight = { enable = true }, -} +require('nvim-treesitter').setup() +vim.api.nvim_create_autocmd('FileType', { + callback = function(args) + pcall(vim.treesitter.start) + vim.bo[args.buffer].indentexpr = 'v:lua.require"nvim-treesitter".indentexpr()' + end, +}) diff --git a/scripts/update-lockfile.sh b/scripts/update-lockfile.sh index 22460d14b..d950b321a 100755 --- a/scripts/update-lockfile.sh +++ b/scripts/update-lockfile.sh @@ -1,18 +1,20 @@ #!/usr/bin/env bash make_ignored() { - if [[ -n $1 ]]; then + if [ -n "$1" ] + then while read -r lang; do - if [[ $lang != "$1" ]]; then - printf '%s,' "$lang" + if [ "$lang" != "$1" ] + then + printf "%s," "$lang" fi - done < <(jq -r 'keys[]' lockfile.json) + done < <(jq 'keys|@sh' -c lockfile.json) fi } -SKIP_LOCKFILE_UPDATE_FOR_LANGS="$(make_ignored "$1")" \ - nvim --headless -c 'luafile ./scripts/write-lockfile.lua' +q +TO_IGNORE=$(make_ignored $1) +SKIP_LOCKFILE_UPDATE_FOR_LANGS="$TO_IGNORE" nvim -l ./scripts/write-lockfile.lua # Pretty print cp lockfile.json /tmp/lockfile.json -jq --sort-keys > lockfile.json < /tmp/lockfile.json +cat /tmp/lockfile.json | jq --sort-keys > lockfile.json diff --git a/scripts/update-readme.lua b/scripts/update-readme.lua index 251a64af4..825d84037 100755 --- a/scripts/update-readme.lua +++ b/scripts/update-readme.lua @@ -1,59 +1,85 @@ #!/usr/bin/env -S nvim -l +vim.opt.runtimepath:append('.') ---@class Parser ---@field name string ---@field parser ParserInfo -local parsers = require("nvim-treesitter.parsers").get_parser_configs() +local parsers = require('nvim-treesitter.parsers').configs local sorted_parsers = {} - for k, v in pairs(parsers) do table.insert(sorted_parsers, { name = k, parser = v }) end - ----@param a Parser ----@param b Parser table.sort(sorted_parsers, function(a, b) return a.name < b.name end) -local generated_text = "" +local tiers = require('nvim-treesitter.parsers').tiers ----@param v Parser -for _, v in ipairs(sorted_parsers) do - local link = "[" .. (v.parser.readme_name or v.name) .. "](" .. v.parser.install_info.url .. ")" +local generated_text = [[ +Language | Tier | Queries | CLI | NPM | Maintainer +-------- |:----:|:-------:|:---:|:---:| ---------- +]] +local footnotes = '' - if v.parser.maintainers then - generated_text = generated_text - .. "- [x] " - .. link - .. " (" - .. (v.parser.experimental and "experimental, " or "") - .. "maintained by " - .. table.concat(v.parser.maintainers, ", ") - .. ")\n" - else - generated_text = generated_text .. "- [ ] " .. link .. (v.parser.experimental and " (experimental)" or "") .. "\n" +for _, v in ipairs(sorted_parsers) do + local p = v.parser + -- language + generated_text = generated_text + .. '[' + .. v.name + .. '](' + .. p.install_info.url + .. ')' + .. (p.readme_note and '[^' .. v.name .. ']' or '') + .. ' | ' + if p.readme_note then + footnotes = footnotes .. '[^' .. v.name .. ']: ' .. p.readme_note .. '\n' end -end -print(generated_text) -print "\n" + -- tier + generated_text = generated_text .. (p.tier and tiers[p.tier] or '') .. ' | ' + + -- queries + generated_text = generated_text + .. '`' + .. (vim.loop.fs_stat('runtime/queries/' .. v.name .. '/highlights.scm') and 'H' or ' ') + .. (vim.loop.fs_stat('runtime/queries/' .. v.name .. '/folds.scm') and 'F' or ' ') + .. (vim.loop.fs_stat('runtime/queries/' .. v.name .. '/indents.scm') and 'I' or ' ') + .. (vim.loop.fs_stat('runtime/queries/' .. v.name .. '/injections.scm') and 'J' or ' ') + .. '` | ' -local readme_text = table.concat(vim.fn.readfile "README.md", "\n") + -- CLI + generated_text = generated_text + .. (p.install_info.requires_generate_from_grammar and '✓' or '') + .. ' | ' + + -- NPM + generated_text = generated_text .. (p.install_info.generate_requires_npm and '✓' or '') .. ' | ' + + -- Maintainer + generated_text = generated_text + .. (p.maintainers and table.concat(p.maintainers, ', ') or '') + .. '\n' +end +generated_text = generated_text .. footnotes + +local readme = assert(io.open('SUPPORTED_LANGUAGES.md', 'r')) +local readme_text = readme:read('*a') +readme:close() local new_readme_text = string.gsub( readme_text, - "<!%-%-parserinfo%-%->.*<!%-%-parserinfo%-%->", - "<!--parserinfo-->\n" .. generated_text .. "<!--parserinfo-->" + '<!%-%-parserinfo%-%->.*<!%-%-parserinfo%-%->', + '<!--parserinfo-->\n' .. generated_text .. '<!--parserinfo-->' ) -vim.fn.writefile(vim.fn.split(new_readme_text, "\n"), "README.md") + +readme = assert(io.open('SUPPORTED_LANGUAGES.md', 'w')) +readme:write(new_readme_text) +readme:close() if string.find(readme_text, generated_text, 1, true) then - print "README.md is up-to-date!" - vim.cmd "q" + print('README.md is up-to-date\n') else - print "New README.md was written. Please commit that change! Old text was: " - print(string.sub(readme_text, string.find(readme_text, "<!%-%-parserinfo%-%->.*<!%-%-parserinfo%-%->"))) - vim.cmd "cq" + print('New README.md was written\n') end diff --git a/scripts/write-lockfile.lua b/scripts/write-lockfile.lua index 7a7606c51..a264af83c 100755 --- a/scripts/write-lockfile.lua +++ b/scripts/write-lockfile.lua @@ -1,14 +1,52 @@ #!/usr/bin/env -S nvim -l +vim.opt.runtimepath:append('.') ----@type string|any[] -local skip_langs = vim.fn.getenv "SKIP_LOCKFILE_UPDATE_FOR_LANGS" +-- Load previous lockfile +local filename = require('nvim-treesitter.utils').get_package_path('lockfile.json') +local file = assert(io.open(filename, 'r')) +local lockfile = vim.json.decode(file:read('*a')) +file:close() -if skip_langs == vim.NIL then - skip_langs = {} -else - ---@diagnostic disable-next-line: param-type-mismatch - skip_langs = vim.fn.split(skip_langs, ",") +---@type string? +local skip_lang_string = os.getenv('SKIP_LOCKFILE_UPDATE_FOR_LANGS') +local skip_langs = skip_lang_string and vim.split(skip_lang_string, ',') or {} +vim.print('Skipping languages: ', skip_langs) + +local sorted_parsers = {} +local configs = require('nvim-treesitter.parsers').configs +for k, v in pairs(configs) do + table.insert(sorted_parsers, { name = k, parser = v }) +end +table.sort(sorted_parsers, function(a, b) + return a.name < b.name +end) + +-- check for new revisions +for _, v in ipairs(sorted_parsers) do + if skip_langs and not vim.list_contains(skip_langs, v.name) then + local sha ---@type string + if v.parser.install_info.branch then + sha = vim.split( + vim.fn.systemlist( + 'git ls-remote ' + .. v.parser.install_info.url + .. ' | grep refs/heads/' + .. v.parser.install_info.branch + )[1], + '\t' + )[1] + else + sha = vim.split(vim.fn.systemlist('git ls-remote ' .. v.parser.install_info.url)[1], '\t')[1] + end + lockfile[v.name] = { revision = sha } + print(v.name .. ': ' .. sha) + else + print('Skipping ' .. v.name) + end end +vim.print(lockfile) -require("nvim-treesitter.install").write_lockfile("verbose", skip_langs) -vim.cmd "q" +-- write new lockfile +file = assert(io.open(filename, 'w')) +file:write(vim.json.encode(lockfile)) +file:close() |
