1

Text objects

This commit is contained in:
Alex Yatskov 2025-01-19 17:04:20 -08:00
parent 06113ccb2f
commit 8b29e84959
3 changed files with 77 additions and 19 deletions

View File

@ -52,16 +52,28 @@ local function inspect()
local trace = range:hit_test(Cursor.get_current())
if trace then
dump(trace)
else
print('No range trace!')
end
else
print('No range found!')
end
end
local function object(inner)
local range = Range.find_closest()
if not range then
return
end
local trace = range:hit_test(Cursor.get_current())
if not trace then
return
end
table.remove(trace, #trace)
range:hit_search(trace, 1):select(inner)
end
return {
inspect = inspect,
object = object,
reflow = reflow,
setup = Options.setup,
}

View File

@ -33,12 +33,12 @@ function ParamCursorCell:is_after_cursor(cursor)
return self.cursor > cursor
end
function ParamCursorCell:get_start_row()
return self.cursor.row
function ParamCursorCell:get_start_cursor()
return self.cursor
end
function ParamCursorCell:get_stop_row()
return self.cursor.row
function ParamCursorCell:get_stop_cursor()
return self.cursor
end
function ParamCursorCell:is_empty()
@ -84,12 +84,12 @@ function ParamRangeCell:hit_search(trace, depth)
end
end
function ParamRangeCell:get_start_row()
return self.range.start_cursor.row
function ParamRangeCell:get_start_cursor()
return self.range.start_cursor
end
function ParamRangeCell:get_stop_row()
return self.range.stop_cursor.row
function ParamRangeCell:get_stop_cursor()
return self.range.stop_cursor
end
function ParamRangeCell:is_empty()
@ -191,15 +191,15 @@ function Param:get_next()
end
end
function Param:get_start_row()
function Param:get_start_cursor()
if #self.cells > 0 then
return self.cells[1]:get_start_row()
return self.cells[1]:get_start_cursor()
end
end
function Param:get_stop_row()
function Param:get_stop_cursor()
if #self.cells > 0 then
return self.cells[#self.cells]:get_stop_row()
return self.cells[#self.cells]:get_stop_cursor()
end
end
@ -226,7 +226,7 @@ function Param:is_contiguous()
for i = start_index, stop_index - 1 do
local cell = self.cells[i]
local next_cell = self.cells[i + 1]
if cell:get_stop_row() ~= next_cell:get_start_row() then
if cell:get_stop_cursor().row ~= next_cell:get_start_cursor().row then
return false
end
end
@ -237,9 +237,9 @@ end
function Param:is_wrapped()
local previous_param = self:get_previous()
if previous_param then
return previous_param:get_stop_row() ~= self:get_start_row()
return previous_param:get_stop_cursor().row ~= self:get_start_cursor().row
else
return self.range.start_cursor.row ~= self:get_start_row()
return self.range.start_cursor.row ~= self:get_start_cursor().row
end
end
@ -247,4 +247,38 @@ function Param:is_empty()
return self:get_start_index() == nil
end
function Param:select(inner)
if #self.cells == 0 then
return
end
local start_cursor = nil
local stop_cursor = nil
if inner then
local start_index = self:get_start_index()
local stop_index = self:get_stop_index()
start_cursor = self.cells[start_index]:get_start_cursor()
stop_cursor = self.cells[stop_index]:get_stop_cursor()
else
start_cursor = self.cells[1]:get_start_cursor()
stop_cursor = self.cells[#self.cells]:get_stop_cursor()
local previous_param = self:get_previous()
if previous_param then
start_cursor = previous_param:get_stop_cursor():get_next()
else
local next_param = self:get_next()
if next_param then
stop_cursor = next_param:get_start_cursor():get_previous()
end
end
end
start_cursor:set_current()
vim.cmd(string.format(':normal! v'))
stop_cursor:set_current()
end
return Param

View File

@ -11,6 +11,16 @@ if not vim.g.argonaut_loaded then
require('argonaut').inspect()
end
local function argonaut_object_inner()
argonaut_reload()
require('argonaut').object(true)
end
local function argonaut_object_outer()
argonaut_reload()
require('argonaut').object(false)
end
local function argonaut_reflow()
require('argonaut').reflow(false)
end
@ -20,6 +30,8 @@ if not vim.g.argonaut_loaded then
end
vim.api.nvim_create_user_command('ArgonautInspect', argonaut_inspect, {})
vim.api.nvim_create_user_command('ArgonautObjectInner', argonaut_object_inner, {})
vim.api.nvim_create_user_command('ArgonautObjectOuter', argonaut_object_outer, {})
vim.api.nvim_create_user_command('ArgonautReflow', argonaut_reflow, {})
vim.api.nvim_create_user_command('ArgonautReload', argonaut_reload, {})
vim.api.nvim_create_user_command('ArgonautToggle', argonaut_toggle, {})