fix(formatter): simplify code, remove odd line break

This commit is contained in:
Riley Bruins 2024-10-25 13:34:22 -07:00 committed by Christian Clason
parent 0a9108a7e4
commit 674100c1fc
6 changed files with 31 additions and 58 deletions

View file

@ -302,8 +302,7 @@
"var_to_str" "weakref" "wrap" "wrapf" "wrapi" "var_to_str" "weakref" "wrap" "wrapf" "wrapi"
; from modules/gdscript/doc_classes/@GDScript.xml ; from modules/gdscript/doc_classes/@GDScript.xml
"Color8" "assert" "char" "convert" "dict_to_inst" "get_stack" "inst_to_dict" "is_instance_of" "Color8" "assert" "char" "convert" "dict_to_inst" "get_stack" "inst_to_dict" "is_instance_of"
"len" "load" "preload" "print_debug" "print_stack" "range" "type_exists") "len" "load" "preload" "print_debug" "print_stack" "range" "type_exists"))
)
; Builtin Constants ; Builtin Constants
((identifier) @constant.builtin ((identifier) @constant.builtin

View file

@ -29,8 +29,7 @@
"mod" "mul" "max" "min" "len" "addf" "add1f" "subf" "divf" "mulf" "maxf" "minf" "floor" "ceil" "mod" "mul" "max" "min" "len" "addf" "add1f" "subf" "divf" "mulf" "maxf" "minf" "floor" "ceil"
"round" "getHostByName" "base" "dir" "clean" "ext" "isAbs" "kindOf" "kindIs" "typeOf" "typeIs" "round" "getHostByName" "base" "dir" "clean" "ext" "isAbs" "kindOf" "kindIs" "typeOf" "typeIs"
"typeIsLike" "deepequal" "semver" "semverCompare" "urlParse" "urlJoin" "urlquery" "lookup" "typeIsLike" "deepequal" "semver" "semverCompare" "urlParse" "urlJoin" "urlquery" "lookup"
"include") "include"))
)
; {{ .Values.test }} ; {{ .Values.test }}
(selector_expression (selector_expression

View file

@ -107,5 +107,4 @@
"reduce_max" "reduce_min" "rotate" "round" "rsqrt" "rsqrt_fast" "saturating_add" "reduce_max" "reduce_min" "rotate" "round" "rsqrt" "rsqrt_fast" "saturating_add"
"saturating_div" "saturating_mul" "saturating_sub" "seed_rng" "select" "shift" "shuffle" "saturating_div" "saturating_mul" "saturating_sub" "seed_rng" "select" "shift" "shuffle"
"signbits" "sign_extend" "sin" "sincos" "soa_to_aos2" "soa_to_aos3" "soa_to_aos4" "sqrt" "signbits" "sign_extend" "sin" "sincos" "soa_to_aos2" "soa_to_aos3" "soa_to_aos4" "sqrt"
"streaming_load" "streaming_load_uniform" "streaming_store" "tan" "trunc") "streaming_load" "streaming_load_uniform" "streaming_store" "tan" "trunc"))
)

View file

@ -129,8 +129,7 @@
"__currentSystem" "__hashFile" "__path" "__unsafeDiscardOutputDependency" "__currentTime" "__currentSystem" "__hashFile" "__path" "__unsafeDiscardOutputDependency" "__currentTime"
"__hashString" "__pathExists" "__unsafeDiscardStringContext" "__deepSeq" "__head" "__readDir" "__hashString" "__pathExists" "__unsafeDiscardStringContext" "__deepSeq" "__head" "__readDir"
"__unsafeGetAttrPos" "__div" "__intersectAttrs" "__readFile" "__zipAttrsWith" "__elem" "__unsafeGetAttrPos" "__div" "__intersectAttrs" "__readFile" "__zipAttrsWith" "__elem"
"__isAttrs" "__replaceStrings" "__elemAt" "__isBool" "__seq" "__fetchurl" "__isFloat" "__sort") "__isAttrs" "__replaceStrings" "__elemAt" "__isBool" "__seq" "__fetchurl" "__isFloat" "__sort"))
)
; constants ; constants
(variable_expression (variable_expression

View file

@ -239,8 +239,7 @@
"System.Management.Automation.PSScriptCmdlet" "Management.Automation.ErrorRecord" "System.Management.Automation.PSScriptCmdlet" "Management.Automation.ErrorRecord"
"System.Management.Automation.ErrorRecord" "Management.Automation.PSCredential" "System.Management.Automation.ErrorRecord" "Management.Automation.PSCredential"
"System.Management.Automation.PSCredential" "Management.Automation.PSMethod" "System.Management.Automation.PSCredential" "Management.Automation.PSMethod"
"System.Management.Automation.PSMethod") "System.Management.Automation.PSMethod"))
)
; Function definitions ; Function definitions
;--------------------- ;---------------------

View file

@ -36,6 +36,7 @@ end)
--- Control the indent here. Change to \t if uses tab instead --- 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 local textwidth = 100
-- Query to control the formatter -- Query to control the formatter
@ -201,9 +202,6 @@ local format_queries = [[
;; All captures should be separated with a space ;; All captures should be separated with a space
(capture) @format.prepend-space (capture) @format.prepend-space
;; Workaround to just use the string's content
(anonymous_node (string) @format.keep)
; ( (_) ) handler ; ( (_) ) handler
(grouping (grouping
"(" "("
@ -283,8 +281,6 @@ local format_queries = [[
. (_) @format.prepend-space) . (_) @format.prepend-space)
(#set! lookahead-newline) (#set! lookahead-newline)
(#set! conditional-newline)) (#set! conditional-newline))
;; Workaround to keep the string's content
(string) @format.keep
;; Comment related handlers ;; Comment related handlers
(comment) @format.append-newline (comment) @format.append-newline
@ -325,25 +321,21 @@ end
---@param bufnr integer ---@param bufnr integer
---@param node TSNode ---@param node TSNode
---@param lines string[] ---@param lines string[]
---@param q table<string, TSMetadata> ---@param q table<string, vim.treesitter.query.TSMetadata>
---@param level integer ---@param level integer
local function iter(bufnr, node, lines, q, level) local function iter(bufnr, node, lines, q, level)
--- Sometimes 2 queries apply append twice. This is to prevent the case from happening --- Sometimes 2 queries apply append twice. This is to prevent the case from happening
local apply_newline = false local apply_newline = false
for child, _ in node:iter_children() do for child, _ in node:iter_children() do
local id = child:id() local id = child:id()
repeat if apply_newline then
if apply_newline then apply_newline = false
apply_newline = false lines[#lines + 1] = string.rep(indent_str, level)
lines[#lines + 1] = string.rep(indent_str, level) end
end if q["format.ignore"][id] then
if q["format.ignore"][id] then local text = vim.split(get_node_text(child, bufnr):gsub("\r\n?", "\n"), "\n", { trimempty = true })
local text = vim.split(get_node_text(child, bufnr):gsub("\r\n?", "\n"), "\n", { trimempty = true }) append_lines(lines, text)
append_lines(lines, text) elseif not q["format.remove"][id] then
break
elseif q["format.remove"][id] then
break
end
if not q["format.cancel-prepend"][id] then if not q["format.cancel-prepend"][id] then
if q["format.prepend-newline"][id] then if q["format.prepend-newline"][id] then
lines[#lines + 1] = string.rep(indent_str, level) lines[#lines + 1] = string.rep(indent_str, level)
@ -359,7 +351,7 @@ local function iter(bufnr, node, lines, q, level)
local _, _, byte_end = node:end_() local _, _, byte_end = node:end_()
if if
q["format.prepend-space"][id]["lookahead-newline"] q["format.prepend-space"][id]["lookahead-newline"]
and textwidth - (byte_end - byte_start) - #lines[#lines] < 0 and (byte_end - byte_start) + #lines[#lines] > textwidth
then then
lines[#lines + 1] = string.rep(indent_str, level) lines[#lines + 1] = string.rep(indent_str, level)
else else
@ -370,7 +362,11 @@ local function iter(bufnr, node, lines, q, level)
end end
if q["format.replace"][id] then if q["format.replace"][id] then
append_lines(lines, vim.split(q["format.replace"][id].text, "\n", { trimempty = true })) append_lines(lines, vim.split(q["format.replace"][id].text, "\n", { trimempty = true }))
elseif child:named_child_count() == 0 or q["format.keep"][id] then elseif
child:named_child_count() == 0
-- Workaround to preserve string content
or child:type() == "string"
then
append_lines( append_lines(
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 })
@ -381,33 +377,17 @@ local function iter(bufnr, node, lines, q, level)
if q["format.indent.begin"][id] then if q["format.indent.begin"][id] then
level = level + 1 level = level + 1
apply_newline = true apply_newline = true
break elseif q["format.indent.dedent"][id] then
lines[#lines] = string.sub(lines[#lines], indent_width_plus_one)
end end
if q["format.indent.dedent"][id] then end
if string.match(lines[#lines], "^%s*" .. get_node_text(child, bufnr)) then if q["format.cancel-append"][id] then
lines[#lines] = string.sub(lines[#lines], 1 + #string.rep(indent_str, 1)) apply_newline = false
end elseif q["format.append-newline"][id] then
end apply_newline = true
if q["format.indent.end"][id] then elseif q["format.append-space"][id] then
level = math.max(level - 1, 0) lines[#lines] = lines[#lines] .. " "
if string.match(lines[#lines], "^%s*" .. get_node_text(child, bufnr)) then end
lines[#lines] = string.sub(lines[#lines], 1 + #string.rep(indent_str, 1))
end
break
end
until true
repeat
if q["format.cancel-append"][id] then
apply_newline = false
end
if not q["format.cancel-append"][id] then
if q["format.append-newline"][id] then
apply_newline = true
elseif q["format.append-space"][id] then
lines[#lines] = lines[#lines] .. " "
end
end
until true
end end
end end
@ -419,7 +399,6 @@ local function format(bufnr, queries)
local map = { local map = {
['format.ignore'] = {}, -- Ignore the node and its children ['format.ignore'] = {}, -- Ignore the node and its children
['format.indent.begin'] = {}, -- +1 shiftwidth for all nodes after this ['format.indent.begin'] = {}, -- +1 shiftwidth for all nodes after this
['format.indent.end'] = {}, -- -1 shiftwidth for all nodes after this
['format.indent.dedent'] = {}, -- -1 shiftwidth for this line only ['format.indent.dedent'] = {}, -- -1 shiftwidth for this line only
['format.prepend-space'] = {}, -- Prepend a space before inserting the node ['format.prepend-space'] = {}, -- Prepend a space before inserting the node
['format.prepend-newline'] = {}, -- Prepend a \n before inserting the node ['format.prepend-newline'] = {}, -- Prepend a \n before inserting the node
@ -427,7 +406,6 @@ local function format(bufnr, queries)
['format.append-newline'] = {}, -- Append a newline after inserting the node ['format.append-newline'] = {}, -- Append a newline after inserting the node
['format.cancel-append'] = {}, -- Cancel any `@format.append-*` applied to the node ['format.cancel-append'] = {}, -- Cancel any `@format.append-*` applied to the node
['format.cancel-prepend'] = {}, -- Cancel any `@format.prepend-*` applied to the node ['format.cancel-prepend'] = {}, -- Cancel any `@format.prepend-*` applied to the node
['format.keep'] = {}, -- String content is not exposed as a syntax node. This is a workaround for it
['format.replace'] = {}, -- Dedicated capture used to store results of `(#gsub!)` ['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 () ['format.remove'] = {}, -- Do not add the syntax node to the result, i.e. brackets [], parens ()
} }