rename Range.from_txtobj => from_motion
Some checks failed
NeoVim tests / plenary-tests (push) Failing after 7s

This commit is contained in:
Jonathan Apodaca 2025-04-11 21:37:28 -06:00
parent 0ee6caa7ba
commit 87930bf3af
7 changed files with 33 additions and 33 deletions

View File

@ -253,11 +253,11 @@ Range.from_line(bufnr, 1)
-- Text Objects (any text object valid in your configuration is supported): -- Text Objects (any text object valid in your configuration is supported):
-- get the word the cursor is on: -- get the word the cursor is on:
Range.from_txtobj('iw') Range.from_motion('iw')
-- get the WORD the cursor is on: -- get the WORD the cursor is on:
Range.from_txtobj('iW') Range.from_motion('iW')
-- get the "..." the cursor is within: -- get the "..." the cursor is within:
Range.from_txtobj('a"') Range.from_motion('a"')
-- Get the currently visually selected text: -- Get the currently visually selected text:
-- NOTE: this does NOT work within certain contexts; more specialized utilities are more appropriate in certain circumstances -- NOTE: this does NOT work within certain contexts; more specialized utilities are more appropriate in certain circumstances

View File

@ -139,7 +139,7 @@ function M.setup()
local from_c = vim.fn.nr2char(from_cn) local from_c = vim.fn.nr2char(from_cn)
local from = surrounds[from_c] or { left = from_c, right = from_c } local from = surrounds[from_c] or { left = from_c, right = from_c }
local function get_fresh_arange() 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 arange == nil then return end
if from_c == 'q' then if from_c == 'q' then
from.left = arange.start:char() from.left = arange.start:char()
@ -166,7 +166,7 @@ function M.setup()
if from_c == 't' then if from_c == 't' then
-- For tags, we want to replace the inner text, not the tag: -- 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 if arange == nil or irange == nil then return end
local lrange = Range.new(arange.start, irange.start:must_next(-1)) local lrange = Range.new(arange.start, irange.start:must_next(-1))
@ -204,8 +204,8 @@ function M.setup()
CACHED_DELETE_FROM = txt_obj CACHED_DELETE_FROM = txt_obj
local buf = Buffer.current() local buf = Buffer.current()
local irange = Range.from_txtobj('i' .. txt_obj) local irange = Range.from_motion('i' .. txt_obj)
local arange = Range.from_txtobj('a' .. txt_obj) local arange = Range.from_motion('a' .. txt_obj)
if arange == nil or irange == nil then return end if arange == nil or irange == nil then return end
local starting_cursor_pos = arange.start:clone() 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: -- Dedenting moves the cursor, so we need to set the cursor to a consistent starting spot:
arange.start:save_to_pos '.' arange.start:save_to_pos '.'
-- Dedenting also changed the inner text, so re-acquire it: -- Dedenting also changed the inner text, so re-acquire it:
arange = Range.from_txtobj('a' .. txt_obj) arange = Range.from_motion('a' .. txt_obj)
irange = Range.from_txtobj('i' .. txt_obj) irange = Range.from_motion('i' .. txt_obj)
if arange == nil or irange == nil then return end -- should never be true if arange == nil or irange == nil then return end -- should never be true
arange:replace(irange:lines()) arange:replace(irange:lines())
-- `arange:replace(..)` updates its own `stop` position, so we will use -- `arange:replace(..)` updates its own `stop` position, so we will use

View File

@ -23,7 +23,7 @@ function M.setup()
---Selects the next quote object (searches forward) ---Selects the next quote object (searches forward)
--- @param q string --- @param q string
local function define_quote_obj(q) 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('a' .. q, function() return select_around() end)
txtobj.define('i' .. q, function() txtobj.define('i' .. q, function()
@ -48,7 +48,7 @@ function M.setup()
if not curr then return end if not curr then return end
-- Reset visual selection to current context: -- Reset visual selection to current context:
curr:save_to_pos '.' curr:save_to_pos '.'
return Range.from_txtobj('a' .. q) return Range.from_motion('a' .. q)
end end
txtobj.define('al' .. q, function() return select_around() end) txtobj.define('al' .. q, function() return select_around() end)

View File

@ -1,5 +1,5 @@
local Range = require 'u.range' local Range = require 'u.range'
local Renderer = require 'u.renderer'.Renderer local Renderer = require('u.renderer').Renderer
--- @class u.Buffer --- @class u.Buffer
--- @field bufnr number --- @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 } --- @param opts? { contains_cursor?: boolean; pos?: u.Pos }
function Buffer:txtobj(txt_obj, opts) function Buffer:txtobj(txt_obj, opts)
opts = vim.tbl_extend('force', opts or {}, { bufnr = self.bufnr }) 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 end
--- @param event string|string[] --- @param event string|string[]

View File

@ -95,7 +95,7 @@ end
--- @param text_obj string --- @param text_obj string
--- @param opts? { bufnr?: number; contains_cursor?: boolean; pos?: u.Pos, user_defined?: boolean } --- @param opts? { bufnr?: number; contains_cursor?: boolean; pos?: u.Pos, user_defined?: boolean }
--- @return u.Range|nil --- @return u.Range|nil
function Range.from_txtobj(text_obj, opts) function Range.from_motion(text_obj, opts)
opts = opts or {} opts = opts or {}
if opts.bufnr == nil then opts.bufnr = vim.api.nvim_get_current_buf() end 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 if opts.contains_cursor == nil then opts.contains_cursor = false end
@ -214,18 +214,18 @@ end
--- ---
function Range.find_nearest_brackets() function Range.find_nearest_brackets()
return Range.smallest { return Range.smallest {
Range.from_txtobj('a<', { contains_cursor = true }), Range.from_motion('a<', { contains_cursor = true }),
Range.from_txtobj('a[', { contains_cursor = true }), Range.from_motion('a[', { contains_cursor = true }),
Range.from_txtobj('a(', { contains_cursor = true }), Range.from_motion('a(', { contains_cursor = true }),
Range.from_txtobj('a{', { contains_cursor = true }), Range.from_motion('a{', { contains_cursor = true }),
} }
end end
function Range.find_nearest_quotes() function Range.find_nearest_quotes()
return Range.smallest { return Range.smallest {
Range.from_txtobj([[a']], { contains_cursor = true }), Range.from_motion([[a']], { contains_cursor = true }),
Range.from_txtobj([[a"]], { contains_cursor = true }), Range.from_motion([[a"]], { contains_cursor = true }),
Range.from_txtobj([[a`]], { contains_cursor = true }), Range.from_motion([[a`]], { contains_cursor = true }),
} }
end end

View File

@ -2,7 +2,7 @@ local Range = require 'u.range'
local M = {} local M = {}
local ESC = vim.api.nvim_replace_termcodes("<Esc>", true, false, true) local ESC = vim.api.nvim_replace_termcodes('<Esc>', true, false, true)
--- @param key_seq string --- @param key_seq string
--- @param fn fun(key_seq: string):u.Range|nil --- @param fn fun(key_seq: string):u.Range|nil

View File

@ -187,53 +187,53 @@ describe('Range', function()
it('text object: word', function() it('text object: word', function()
withbuf({ 'the quick brown fox' }, function() withbuf({ 'the quick brown fox' }, function()
vim.fn.setpos('.', { 0, 1, 5, 0 }) 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 }) 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)
end) end)
it('text object: quote', function() it('text object: quote', function()
withbuf({ [[the "quick" brown fox]] }, function() withbuf({ [[the "quick" brown fox]] }, function()
vim.fn.setpos('.', { 0, 1, 5, 0 }) 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 }) 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)
withbuf({ [[the 'quick' brown fox]] }, function() withbuf({ [[the 'quick' brown fox]] }, function()
vim.fn.setpos('.', { 0, 1, 5, 0 }) 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 }) 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)
withbuf({ [[the `quick` brown fox]] }, function() withbuf({ [[the `quick` brown fox]] }, function()
vim.fn.setpos('.', { 0, 1, 5, 0 }) 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 }) 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)
end) end)
it('text object: block', function() it('text object: block', function()
withbuf({ 'this is a {', 'block', '} here' }, function() withbuf({ 'this is a {', 'block', '} here' }, function()
vim.fn.setpos('.', { 0, 2, 1, 0 }) 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 }) 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)
end) end)
it('text object: restores cursor position', function() it('text object: restores cursor position', function()
withbuf({ 'this is a {block} here' }, function() withbuf({ 'this is a {block} here' }, function()
vim.fn.setpos('.', { 0, 1, 13, 0 }) 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 }) assert.are.same(vim.api.nvim_win_get_cursor(0), { 1, 12 })
end) end)
end) end)