From 8b29e8495910655b0f3724fbabb987fa51c4928b Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 19 Jan 2025 17:04:20 -0800 Subject: [PATCH] Text objects --- lua/argonaut/init.lua | 20 ++++++++++--- lua/argonaut/param.lua | 64 ++++++++++++++++++++++++++++++++---------- plugin/argonaut.lua | 12 ++++++++ 3 files changed, 77 insertions(+), 19 deletions(-) diff --git a/lua/argonaut/init.lua b/lua/argonaut/init.lua index b364027..1eda016 100644 --- a/lua/argonaut/init.lua +++ b/lua/argonaut/init.lua @@ -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, } diff --git a/lua/argonaut/param.lua b/lua/argonaut/param.lua index b9882c4..16b05a9 100644 --- a/lua/argonaut/param.lua +++ b/lua/argonaut/param.lua @@ -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 diff --git a/plugin/argonaut.lua b/plugin/argonaut.lua index aba4822..e1d1be2 100644 --- a/plugin/argonaut.lua +++ b/plugin/argonaut.lua @@ -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, {})