mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-01 19:17:02 -04:00
fix(formatter): simplify code, remove odd line break
This commit is contained in:
parent
0a9108a7e4
commit
674100c1fc
6 changed files with 31 additions and 58 deletions
|
|
@ -36,6 +36,7 @@ end)
|
|||
|
||||
--- Control the indent here. Change to \t if uses tab instead
|
||||
local indent_str = " "
|
||||
local indent_width_plus_one = 3
|
||||
local textwidth = 100
|
||||
|
||||
-- Query to control the formatter
|
||||
|
|
@ -201,9 +202,6 @@ local format_queries = [[
|
|||
;; All captures should be separated with a space
|
||||
(capture) @format.prepend-space
|
||||
|
||||
;; Workaround to just use the string's content
|
||||
(anonymous_node (string) @format.keep)
|
||||
|
||||
; ( (_) ) handler
|
||||
(grouping
|
||||
"("
|
||||
|
|
@ -283,8 +281,6 @@ local format_queries = [[
|
|||
. (_) @format.prepend-space)
|
||||
(#set! lookahead-newline)
|
||||
(#set! conditional-newline))
|
||||
;; Workaround to keep the string's content
|
||||
(string) @format.keep
|
||||
|
||||
;; Comment related handlers
|
||||
(comment) @format.append-newline
|
||||
|
|
@ -325,25 +321,21 @@ end
|
|||
---@param bufnr integer
|
||||
---@param node TSNode
|
||||
---@param lines string[]
|
||||
---@param q table<string, TSMetadata>
|
||||
---@param q table<string, vim.treesitter.query.TSMetadata>
|
||||
---@param level integer
|
||||
local function iter(bufnr, node, lines, q, level)
|
||||
--- Sometimes 2 queries apply append twice. This is to prevent the case from happening
|
||||
local apply_newline = false
|
||||
for child, _ in node:iter_children() do
|
||||
local id = child:id()
|
||||
repeat
|
||||
if apply_newline then
|
||||
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 })
|
||||
append_lines(lines, text)
|
||||
break
|
||||
elseif q["format.remove"][id] then
|
||||
break
|
||||
end
|
||||
if apply_newline then
|
||||
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 })
|
||||
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
|
||||
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_()
|
||||
if
|
||||
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
|
||||
lines[#lines + 1] = string.rep(indent_str, level)
|
||||
else
|
||||
|
|
@ -370,7 +362,11 @@ local function iter(bufnr, node, lines, q, level)
|
|||
end
|
||||
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 or q["format.keep"][id] then
|
||||
elseif
|
||||
child:named_child_count() == 0
|
||||
-- Workaround to preserve string content
|
||||
or child:type() == "string"
|
||||
then
|
||||
append_lines(
|
||||
lines,
|
||||
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
|
||||
level = level + 1
|
||||
apply_newline = true
|
||||
break
|
||||
elseif q["format.indent.dedent"][id] then
|
||||
lines[#lines] = string.sub(lines[#lines], indent_width_plus_one)
|
||||
end
|
||||
if q["format.indent.dedent"][id] then
|
||||
if string.match(lines[#lines], "^%s*" .. get_node_text(child, bufnr)) then
|
||||
lines[#lines] = string.sub(lines[#lines], 1 + #string.rep(indent_str, 1))
|
||||
end
|
||||
end
|
||||
if q["format.indent.end"][id] then
|
||||
level = math.max(level - 1, 0)
|
||||
if string.match(lines[#lines], "^%s*" .. get_node_text(child, bufnr)) then
|
||||
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
|
||||
if q["format.cancel-append"][id] then
|
||||
apply_newline = false
|
||||
elseif q["format.append-newline"][id] then
|
||||
apply_newline = true
|
||||
elseif q["format.append-space"][id] then
|
||||
lines[#lines] = lines[#lines] .. " "
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -419,7 +399,6 @@ local function format(bufnr, queries)
|
|||
local map = {
|
||||
['format.ignore'] = {}, -- Ignore the node and its children
|
||||
['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.prepend-space'] = {}, -- Prepend a space 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.cancel-append'] = {}, -- Cancel any `@format.append-*` 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.remove'] = {}, -- Do not add the syntax node to the result, i.e. brackets [], parens ()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue