Split line command polished

This commit is contained in:
Ivar Fatland
2023-07-05 22:28:34 +02:00
parent b780f4e21d
commit 940edef36e
+36 -13
View File
@@ -308,20 +308,38 @@ do
TAB_WHITESPACE = TAB_WHITESPACE .. ' ' TAB_WHITESPACE = TAB_WHITESPACE .. ' '
end end
local BRACKET_COMPLEMENTS = { local get_closing_bracket = function(char)
['{'] ='}', if char == '(' then return ')' end
['['] = ']', if char == '[' then return ']' end
['('] = ')' if char == '{' then return '}' end
} error("Not an opening bracket")
end
local OPENING_BRACKET_PATTERN = "[%(%[{]" ---@param char string
---@return boolean
local is_opening_bracket = function(char)
return char == '(' or char == '[' or char == '{'
end
split_line = function() split_line = function()
local line = vim.api.nvim_get_current_line() local line = vim.api.nvim_get_current_line()
local _, col = unpack(vim.api.nvim_win_get_cursor(0))
col = col + 1 -- Doing this to make it 1-indexed
---@type integer? ---@type integer?
local first_bracket_i = string.find(line, OPENING_BRACKET_PATTERN) local first_bracket_i = nil
assert(first_bracket_i, 'No opening brackets found on given line') for i = col, #line do
local char = line:sub(i, i)
if is_opening_bracket(char) then
first_bracket_i = i
break
end
end
if not first_bracket_i then
print('No opening brackets found after cursor on this line.')
return
end
---@type integer[] ---@type integer[]
local comma_indexes = {} -- Populate this array local comma_indexes = {} -- Populate this array
@@ -349,12 +367,10 @@ do
end end
-- string handling complete -- string handling complete
if char:match(OPENING_BRACKET_PATTERN) then if is_opening_bracket( char ) then
table.insert( table.insert(
closing_bracket_stack, closing_bracket_stack,
BRACKET_COMPLEMENTS[ get_closing_bracket( char )
line:sub(i, i)
]
) )
end end
@@ -374,7 +390,14 @@ do
end end
end end
assert(last_bracket_i, "The opening bracket was not closed") if not last_bracket_i then
print("The opening bracket was not closed")
return
end
if #comma_indexes == 0 then
print('No comma separated items in the given parenthesis scope')
return
end
---@type string ---@type string
local leading_whitespace = string.match(line, "^%s*") local leading_whitespace = string.match(line, "^%s*")