1

68 lines
1.8 KiB
Lua
Raw Normal View History

2024-12-30 20:31:50 -08:00
local Builder = require('argonaut.builder')
local Cursor = require('argonaut.cursor')
local Options = require('argonaut.options')
local Range = require('argonaut.range')
2022-12-30 15:10:22 -08:00
2025-01-19 15:35:54 -08:00
local function reflow(toggle_wrapping)
2024-12-30 20:31:50 -08:00
local range = Range.find_closest()
if not range then
return false
2024-04-24 20:02:10 -07:00
end
2024-12-30 20:31:50 -08:00
local trace = range:hit_test(Cursor.get_current())
local line_first = vim.fn.getline(range.start_cursor.row)
local line_last = vim.fn.getline(range.stop_cursor.row)
local indent_level = #line_first:match('^(%s*)')
local indent_block = '\t'
if vim.o.expandtab then
indent_level = math.floor(indent_level / vim.o.shiftwidth)
indent_block = string.rep(' ', vim.o.shiftwidth)
end
local range_prefix = line_first:sub(indent_level * #indent_block + 1, range.start_cursor.col - 1)
local range_suffix = line_last:sub(range.stop_cursor.col + 1)
local builder = Builder.new(indent_level, indent_block)
builder:write(range_prefix)
2025-01-19 15:35:54 -08:00
if toggle_wrapping then
range:write(builder, not range:is_wrapped())
else
range:write(builder, range:is_wrapped())
end
2024-12-30 20:31:50 -08:00
builder:write(range_suffix)
builder:endline()
builder:output(range.start_cursor.row, range.stop_cursor.row)
local new_range = Range.find_at_cursor(range.start_cursor)
2025-01-18 20:36:32 -08:00
assert(new_range)
2025-01-19 15:35:54 -08:00
if trace then
new_range:hit_search(trace, 1).cursor:set_current()
else
new_range.start_cursor:set_current()
end
2024-12-30 20:31:50 -08:00
return true
2022-12-30 15:10:22 -08:00
end
2025-01-18 18:24:01 -08:00
local function inspect()
local range = Range.find_closest()
if range then
2025-01-19 10:52:37 -08:00
local trace = range:hit_test(Cursor.get_current())
2025-01-19 15:35:54 -08:00
if trace then
dump(trace)
else
print('No range trace!')
end
else
print('No range found!')
2025-01-18 18:24:01 -08:00
end
end
2022-12-30 10:40:23 -08:00
return {
2025-01-18 18:24:01 -08:00
inspect = inspect,
2024-04-24 20:02:10 -07:00
reflow = reflow,
2025-01-18 20:36:32 -08:00
setup = Options.setup,
2022-12-30 10:40:23 -08:00
}