diff --git a/lua/argonaut/init.lua b/lua/argonaut/init.lua index ce21998..b364027 100644 --- a/lua/argonaut/init.lua +++ b/lua/argonaut/init.lua @@ -3,14 +3,13 @@ local Cursor = require('argonaut.cursor') local Options = require('argonaut.options') local Range = require('argonaut.range') -local function reflow() +local function reflow(toggle_wrapping) local range = Range.find_closest() if not range then return false end local trace = range:hit_test(Cursor.get_current()) - assert(trace) local line_first = vim.fn.getline(range.start_cursor.row) local line_last = vim.fn.getline(range.stop_cursor.row) @@ -27,14 +26,22 @@ local function reflow() local builder = Builder.new(indent_level, indent_block) builder:write(range_prefix) - range:write(builder, not range:is_wrapped()) + if toggle_wrapping then + range:write(builder, not range:is_wrapped()) + else + range:write(builder, range:is_wrapped()) + end 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) assert(new_range) - new_range:hit_search(trace, 1).cursor:set_current() + if trace then + new_range:hit_search(trace, 1).cursor:set_current() + else + new_range.start_cursor:set_current() + end return true end @@ -43,8 +50,13 @@ local function inspect() local range = Range.find_closest() if range then local trace = range:hit_test(Cursor.get_current()) - assert(trace) - dump(trace) + if trace then + dump(trace) + else + print('No range trace!') + end + else + print('No range found!') end end @@ -52,5 +64,4 @@ return { inspect = inspect, reflow = reflow, setup = Options.setup, - query = Options.query, } diff --git a/lua/argonaut/range.lua b/lua/argonaut/range.lua index ef54631..11f2b96 100644 --- a/lua/argonaut/range.lua +++ b/lua/argonaut/range.lua @@ -54,8 +54,9 @@ local function find_closest() end local function find_at_cursor(start_cursor, parent_range) - local pairing = Pairing.from_brace(start_cursor:get_value()) - if pairing then + local value = start_cursor:get_value() + local pairing = Pairing.from_brace(value) + if pairing and value == pairing.open then local cursor_current = Cursor:get_current() start_cursor:set_current() @@ -75,6 +76,10 @@ local function find_at_cursor(start_cursor, parent_range) end function Range.new(start_cursor, stop_cursor, pairing) + assert(start_cursor < stop_cursor) + assert(start_cursor:get_value() == pairing.open) + assert(stop_cursor:get_value() == pairing.close) + local range = { start_cursor = start_cursor, stop_cursor = stop_cursor, @@ -133,38 +138,40 @@ function Range:parse() end function Range:hit_test(cursor) - if self:contains_cursor(cursor) then - local frame = { - type = 'range', - pairing = self.pairing.open .. self.pairing.close, - param_count = #self.params, - param_index = 0, - } + if not self:contains_cursor(cursor) or #self.params == 0 then + return + end - if cursor == self.start_cursor and self.params[1]:is_after_cursor(cursor) then - cursor = cursor:get_next() - elseif cursor == self.stop_cursor and self.params[#self.params]:is_before_cursor(cursor) then - cursor = cursor:get_previous() - end + local frame = { + type = 'range', + pairing = self.pairing.open .. self.pairing.close, + param_count = #self.params, + param_index = 0, + } - for i = 2, #self.params do - local param = self.params[i] - local previous_param = self.params[i - 1] - if previous_param:is_before_cursor(cursor) and param:is_after_cursor(cursor) then - if not previous_param:hit_test(cursor) and not param:hit_test(cursor) then - cursor = cursor:get_previous() - break - end + if cursor == self.start_cursor and self.params[1]:is_after_cursor(cursor) then + cursor = cursor:get_next() + elseif cursor == self.stop_cursor and self.params[#self.params]:is_before_cursor(cursor) then + cursor = cursor:get_previous() + end + + for i = 2, #self.params do + local param = self.params[i] + local previous_param = self.params[i - 1] + if previous_param:is_before_cursor(cursor) and param:is_after_cursor(cursor) then + if not previous_param:hit_test(cursor) and not param:hit_test(cursor) then + cursor = cursor:get_previous() + break end end + end - for i, param in pairs(self.params) do - local trace = param:hit_test(cursor) - if trace then - frame.param_index = i - table.insert(trace, 1, frame) - return trace - end + for i, param in pairs(self.params) do + local trace = param:hit_test(cursor) + if trace then + frame.param_index = i + table.insert(trace, 1, frame) + return trace end end end diff --git a/plugin/argonaut.lua b/plugin/argonaut.lua index 54550d3..aba4822 100644 --- a/plugin/argonaut.lua +++ b/plugin/argonaut.lua @@ -7,25 +7,22 @@ if not vim.g.argonaut_loaded then end end - local function argonaut_auto_reload() - if require('argonaut').query('auto_reload') then - argonaut_reload() - end - end - - local function argonaut_reflow() - argonaut_auto_reload() - require('argonaut').reflow() - end - local function argonaut_inspect() - argonaut_auto_reload() require('argonaut').inspect() end - vim.api.nvim_create_user_command('ArgonautReload', argonaut_reload, {}) - vim.api.nvim_create_user_command('ArgonautReflow', argonaut_reflow, {}) + local function argonaut_reflow() + require('argonaut').reflow(false) + end + + local function argonaut_toggle() + require('argonaut').reflow(true) + end + vim.api.nvim_create_user_command('ArgonautInspect', argonaut_inspect, {}) + 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, {}) vim.g.argonaut_loaded = true end