diff --git a/README.md b/README.md index 7629340..130119d 100644 --- a/README.md +++ b/README.md @@ -253,11 +253,11 @@ Range.from_line(bufnr, 1) -- Text Objects (any text object valid in your configuration is supported): -- get the word the cursor is on: -Range.from_txtobj('iw') +Range.from_motion('iw') -- get the WORD the cursor is on: -Range.from_txtobj('iW') +Range.from_motion('iW') -- get the "..." the cursor is within: -Range.from_txtobj('a"') +Range.from_motion('a"') -- Get the currently visually selected text: -- NOTE: this does NOT work within certain contexts; more specialized utilities are more appropriate in certain circumstances diff --git a/examples/surround.lua b/examples/surround.lua index e2be312..a9e9905 100644 --- a/examples/surround.lua +++ b/examples/surround.lua @@ -139,7 +139,7 @@ function M.setup() local from_c = vim.fn.nr2char(from_cn) local from = surrounds[from_c] or { left = from_c, right = from_c } local function get_fresh_arange() - local arange = Range.from_txtobj('a' .. from_c, { user_defined = true }) + local arange = Range.from_motion('a' .. from_c, { user_defined = true }) if arange == nil then return end if from_c == 'q' then from.left = arange.start:char() @@ -166,7 +166,7 @@ function M.setup() if from_c == 't' then -- For tags, we want to replace the inner text, not the tag: - local irange = Range.from_txtobj('i' .. from_c, { user_defined = true }) + local irange = Range.from_motion('i' .. from_c, { user_defined = true }) if arange == nil or irange == nil then return end local lrange = Range.new(arange.start, irange.start:must_next(-1)) @@ -204,8 +204,8 @@ function M.setup() CACHED_DELETE_FROM = txt_obj local buf = Buffer.current() - local irange = Range.from_txtobj('i' .. txt_obj) - local arange = Range.from_txtobj('a' .. txt_obj) + local irange = Range.from_motion('i' .. txt_obj) + local arange = Range.from_motion('a' .. txt_obj) if arange == nil or irange == nil then return end local starting_cursor_pos = arange.start:clone() @@ -217,8 +217,8 @@ function M.setup() -- Dedenting moves the cursor, so we need to set the cursor to a consistent starting spot: arange.start:save_to_pos '.' -- Dedenting also changed the inner text, so re-acquire it: - arange = Range.from_txtobj('a' .. txt_obj) - irange = Range.from_txtobj('i' .. txt_obj) + arange = Range.from_motion('a' .. txt_obj) + irange = Range.from_motion('i' .. txt_obj) if arange == nil or irange == nil then return end -- should never be true arange:replace(irange:lines()) -- `arange:replace(..)` updates its own `stop` position, so we will use diff --git a/examples/text-objects.lua b/examples/text-objects.lua index b89690c..af10caa 100644 --- a/examples/text-objects.lua +++ b/examples/text-objects.lua @@ -23,7 +23,7 @@ function M.setup() ---Selects the next quote object (searches forward) --- @param q string local function define_quote_obj(q) - local function select_around() return Range.from_txtobj('a' .. q) end + local function select_around() return Range.from_motion('a' .. q) end txtobj.define('a' .. q, function() return select_around() end) txtobj.define('i' .. q, function() @@ -48,7 +48,7 @@ function M.setup() if not curr then return end -- Reset visual selection to current context: curr:save_to_pos '.' - return Range.from_txtobj('a' .. q) + return Range.from_motion('a' .. q) end txtobj.define('al' .. q, function() return select_around() end) diff --git a/lua/u/buffer.lua b/lua/u/buffer.lua index d86b9b6..9a2c2c0 100644 --- a/lua/u/buffer.lua +++ b/lua/u/buffer.lua @@ -1,5 +1,5 @@ local Range = require 'u.range' -local Renderer = require 'u.renderer'.Renderer +local Renderer = require('u.renderer').Renderer --- @class u.Buffer --- @field bufnr number @@ -71,7 +71,7 @@ function Buffer:lines(start, stop) return Range.from_lines(self.bufnr, start, st --- @param opts? { contains_cursor?: boolean; pos?: u.Pos } function Buffer:txtobj(txt_obj, opts) opts = vim.tbl_extend('force', opts or {}, { bufnr = self.bufnr }) - return Range.from_txtobj(txt_obj, opts) + return Range.from_motion(txt_obj, opts) end --- @param event string|string[] diff --git a/lua/u/range.lua b/lua/u/range.lua index 76b3742..99f54a4 100644 --- a/lua/u/range.lua +++ b/lua/u/range.lua @@ -95,7 +95,7 @@ end --- @param text_obj string --- @param opts? { bufnr?: number; contains_cursor?: boolean; pos?: u.Pos, user_defined?: boolean } --- @return u.Range|nil -function Range.from_txtobj(text_obj, opts) +function Range.from_motion(text_obj, opts) opts = opts or {} if opts.bufnr == nil then opts.bufnr = vim.api.nvim_get_current_buf() end if opts.contains_cursor == nil then opts.contains_cursor = false end @@ -214,18 +214,18 @@ end --- function Range.find_nearest_brackets() return Range.smallest { - Range.from_txtobj('a<', { contains_cursor = true }), - Range.from_txtobj('a[', { contains_cursor = true }), - Range.from_txtobj('a(', { contains_cursor = true }), - Range.from_txtobj('a{', { contains_cursor = true }), + Range.from_motion('a<', { contains_cursor = true }), + Range.from_motion('a[', { contains_cursor = true }), + Range.from_motion('a(', { contains_cursor = true }), + Range.from_motion('a{', { contains_cursor = true }), } end function Range.find_nearest_quotes() return Range.smallest { - Range.from_txtobj([[a']], { contains_cursor = true }), - Range.from_txtobj([[a"]], { contains_cursor = true }), - Range.from_txtobj([[a`]], { contains_cursor = true }), + Range.from_motion([[a']], { contains_cursor = true }), + Range.from_motion([[a"]], { contains_cursor = true }), + Range.from_motion([[a`]], { contains_cursor = true }), } end diff --git a/lua/u/txtobj.lua b/lua/u/txtobj.lua index 78c16f8..dc399a2 100644 --- a/lua/u/txtobj.lua +++ b/lua/u/txtobj.lua @@ -2,7 +2,7 @@ local Range = require 'u.range' local M = {} -local ESC = vim.api.nvim_replace_termcodes("", true, false, true) +local ESC = vim.api.nvim_replace_termcodes('', true, false, true) --- @param key_seq string --- @param fn fun(key_seq: string):u.Range|nil diff --git a/spec/range_spec.lua b/spec/range_spec.lua index 9187df9..ec3d3d8 100644 --- a/spec/range_spec.lua +++ b/spec/range_spec.lua @@ -187,53 +187,53 @@ describe('Range', function() it('text object: word', function() withbuf({ 'the quick brown fox' }, function() vim.fn.setpos('.', { 0, 1, 5, 0 }) - assert.are.same('quick ', Range.from_txtobj('aw'):text()) + assert.are.same('quick ', Range.from_motion('aw'):text()) vim.fn.setpos('.', { 0, 1, 5, 0 }) - assert.are.same('quick', Range.from_txtobj('iw'):text()) + assert.are.same('quick', Range.from_motion('iw'):text()) end) end) it('text object: quote', function() withbuf({ [[the "quick" brown fox]] }, function() vim.fn.setpos('.', { 0, 1, 5, 0 }) - assert.are.same('"quick"', Range.from_txtobj('a"'):text()) + assert.are.same('"quick"', Range.from_motion('a"'):text()) vim.fn.setpos('.', { 0, 1, 6, 0 }) - assert.are.same('quick', Range.from_txtobj('i"'):text()) + assert.are.same('quick', Range.from_motion('i"'):text()) end) withbuf({ [[the 'quick' brown fox]] }, function() vim.fn.setpos('.', { 0, 1, 5, 0 }) - assert.are.same("'quick'", Range.from_txtobj([[a']]):text()) + assert.are.same("'quick'", Range.from_motion([[a']]):text()) vim.fn.setpos('.', { 0, 1, 6, 0 }) - assert.are.same('quick', Range.from_txtobj([[i']]):text()) + assert.are.same('quick', Range.from_motion([[i']]):text()) end) withbuf({ [[the `quick` brown fox]] }, function() vim.fn.setpos('.', { 0, 1, 5, 0 }) - assert.are.same('`quick`', Range.from_txtobj([[a`]]):text()) + assert.are.same('`quick`', Range.from_motion([[a`]]):text()) vim.fn.setpos('.', { 0, 1, 6, 0 }) - assert.are.same('quick', Range.from_txtobj([[i`]]):text()) + assert.are.same('quick', Range.from_motion([[i`]]):text()) end) end) it('text object: block', function() withbuf({ 'this is a {', 'block', '} here' }, function() vim.fn.setpos('.', { 0, 2, 1, 0 }) - assert.are.same('{\nblock\n}', Range.from_txtobj('a{'):text()) + assert.are.same('{\nblock\n}', Range.from_motion('a{'):text()) vim.fn.setpos('.', { 0, 2, 1, 0 }) - assert.are.same('block', Range.from_txtobj('i{'):text()) + assert.are.same('block', Range.from_motion('i{'):text()) end) end) it('text object: restores cursor position', function() withbuf({ 'this is a {block} here' }, function() vim.fn.setpos('.', { 0, 1, 13, 0 }) - assert.are.same('{block}', Range.from_txtobj('a{'):text()) + assert.are.same('{block}', Range.from_motion('a{'):text()) assert.are.same(vim.api.nvim_win_get_cursor(0), { 1, 12 }) end) end)