feat(format-scripts): linewrap predicates

"format-ignore".kick()
This commit is contained in:
再生花 2024-02-23 17:42:01 +09:00 committed by GitHub
parent a29058fe8b
commit 31641d72a4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
57 changed files with 1622 additions and 1537 deletions

View file

@ -34,6 +34,7 @@ end)
--- Control the indent here. Change to \t if uses tab instead
local indent_str = " "
local textwidth = 100
-- Query to control the formatter
local format_queries = [[
@ -174,8 +175,8 @@ local format_queries = [[
[
"_"
name: (identifier)
]
(_) @format.cancel-append
(_)
] @format.cancel-append
.
")"
(#not-has-type? @format.cancel-append comment))
@ -254,9 +255,26 @@ local format_queries = [[
.
(capture))
; Separate this query to avoid capture duplication
(predicate
"(" @format.indent.begin @format.cancel-append)
(predicate
(parameters
(_) @format.prepend-space))
(comment) @format.prepend-newline
.
(_) @format.cancel-prepend)
(#is-start-of-line? @format.prepend-newline))
(predicate
(parameters
(_) @format.prepend-space)
(#set! conditional-newline))
(predicate
(parameters
.
(capture)
. (_) @format.prepend-space)
(#set! lookahead-newline)
(#set! conditional-newline))
;; Workaround to keep the string's content
(string) @format.keep
@ -322,7 +340,24 @@ local function iter(bufnr, node, lines, q, level)
if q["format.prepend-newline"][id] then
lines[#lines + 1] = string.rep(indent_str, level)
elseif q["format.prepend-space"][id] then
lines[#lines] = lines[#lines] .. " "
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
-- Do a rough guess of the actual byte length. If it's larger than `columns` then add a newline first
-- column - byte_end + byte_start
local _, _, byte_start = child:start()
local _, _, byte_end = node:end_()
if
q["format.prepend-space"][id]["lookahead-newline"]
and textwidth - (byte_end - byte_start) - #lines[#lines] < 0
then
lines[#lines + 1] = string.rep(indent_str, level)
else
lines[#lines] = lines[#lines] .. " "
end
end
end
end
if q["format.replace"][id] then