From 692b051b09935653befdb8f7ba8afdb640adf17b Mon Sep 17 00:00:00 2001 From: Christian Clason Date: Mon, 12 Jun 2023 09:54:30 -0600 Subject: feat!: drop modules, general refactor and cleanup --- scripts/format-queries.lua | 76 +++++++++++++++++++++++++--------------------- 1 file changed, 41 insertions(+), 35 deletions(-) (limited to 'scripts/format-queries.lua') 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!') -- cgit v1.2.3-70-g09d2