feat(pos,range)!: remove range.cursor() and range:to_cursor(),

Problem:
- A window can only have one cursor, ranges selected by the cursor are typically
  obtained by marks like ">" and "<", instead of calling get_cursor() twice.
- `vim.Range` is described as end-exclusive,
  but the current `range.cursor()`/`range:to_cursor()` are end-inclusive.
- Conversion between `vim.Range` and mark-indexed range can be done by
  `range.mark()`/`range:to_mark()`

Solution:
Remove `range.cursor()` and `range:to_cursor()`,
This commit is contained in:
Yi Ming
2026-05-19 16:32:12 +08:00
parent f3bb21e71d
commit 8d1233a144
5 changed files with 20 additions and 76 deletions

View File

@@ -4560,6 +4560,14 @@ offset({buf}, {offset}) *vim.pos.offset()*
to_cursor({pos}) *vim.pos.to_cursor()*
Converts |vim.Pos| to cursor position (see |api-indexing|).
Example: >lua
local pos = vim.pos(0, 3, 5)
-- Convert to cursor position, you can call it in a method style.
local cursor_pos = { pos:to_cursor() }
vim.api.nvim_win_set_cursor(0, cursor_pos)
<
Parameters: ~
• {pos} (`vim.Pos`) See |vim.Pos|.
@@ -4658,22 +4666,6 @@ Provides operations to compare, calculate, and convert ranges represented by
• {start_row} (`integer`) 0-based byte index.
cursor({buf}, {start_pos}, {end_pos}) *vim.range.cursor()*
Creates a new |vim.Range| from mark-like range (see |api-indexing|).
Example: >lua
local start = vim.api.nvim_win_get_cursor(0)
-- move the cursor
local end_ = vim.api.nvim_win_get_cursor(0)
local range = vim.range.cursor(0, start, end_)
<
Parameters: ~
• {buf} (`integer`)
• {start_pos} (`[integer, integer]`)
• {end_pos} (`[integer, integer]`)
*vim.range.extmark()*
extmark({buf}, {start_row}, {start_col}, {end_row}, {end_col})
Creates a new |vim.Range| from extmark range (see |api-indexing|).
@@ -4757,19 +4749,6 @@ mark({buf}, {start_row}, {start_col}, {end_row}, {end_col})
• {end_row} (`integer`)
• {end_col} (`integer`)
to_cursor({range}) *vim.range.to_cursor()*
Converts |vim.Range| to mark-like range (see |api-indexing|).
Example: >lua
local range = vim.range(0, 3, 5, 4, 0)
-- Convert to cursor range, you can call it in a method style.
local cursor_range = range:to_cursor()
<
Parameters: ~
• {range} (`vim.Range`) See |vim.Range|.
to_extmark({range}) *vim.range.to_extmark()*
Converts |vim.Range| to extmark range (see |api-indexing|).

View File

@@ -33,6 +33,8 @@ LSP
LUA
• vim.pos, vim.range always require the `buf` parameter.
• range.cursor() and range.to_cursor() are removed.
Use range.mark() and range.to_mark() instead.
DIAGNOSTICS

View File

@@ -348,7 +348,7 @@ function Completor:accept(item)
api.nvim_buf_set_text(self.bufnr, start_row, start_col, end_row, end_col, lines)
local win = api.nvim_get_current_win()
win = api.nvim_win_get_buf(win) == self.bufnr and win or vim.fn.bufwinid(self.bufnr)
local row, col = item.range:to_cursor()
local row, col = item.range:to_mark()
api.nvim_win_set_cursor(win, {
row + #lines - 1,
(#lines == 1 and col or 0) + #lines[#lines],

View File

@@ -278,6 +278,15 @@ function M.lsp(buf, pos, position_encoding)
end
--- Converts |vim.Pos| to cursor position (see |api-indexing|).
---
--- Example:
--- ```lua
--- local pos = vim.pos(0, 3, 5)
---
--- -- Convert to cursor position, you can call it in a method style.
--- local cursor_pos = { pos:to_cursor() }
--- vim.api.nvim_win_set_cursor(0, cursor_pos)
--- ```
---@param pos vim.Pos
---@return integer, integer
function M.to_cursor(pos)

View File

@@ -438,52 +438,6 @@ function M.extmark(buf, start_row, start_col, end_row, end_col)
return M.new(start, end_)
end
--- Converts |vim.Range| to mark-like range (see |api-indexing|).
---
--- Example:
--- ```lua
--- local range = vim.range(0, 3, 5, 4, 0)
---
--- -- Convert to cursor range, you can call it in a method style.
--- local cursor_range = range:to_cursor()
--- ```
---@param range vim.Range
function M.to_cursor(range)
validate('range', range, 'table')
local srow, scol = vim.pos(range.buf, range[1], range[2]):to_cursor()
local erow, ecol = vim.pos(range.buf, range[3], range[4]):to_cursor()
return srow, scol, erow, ecol
end
--- Creates a new |vim.Range| from mark-like range (see |api-indexing|).
---
--- Example:
--- ```lua
--- local start = vim.api.nvim_win_get_cursor(0)
--- -- move the cursor
--- local end_ = vim.api.nvim_win_get_cursor(0)
---
--- local range = vim.range.cursor(0, start, end_)
--- ```
---@param buf integer
---@param start_pos [integer, integer]
---@param end_pos [integer, integer]
function M.cursor(buf, start_pos, end_pos)
validate('buf', buf, 'number')
validate('range', start_pos, 'table')
validate('range', end_pos, 'table')
if buf == 0 then
buf = api.nvim_get_current_buf()
end
local start = vim.pos.cursor(buf, start_pos)
local end_ = vim.pos.cursor(buf, end_pos)
return M.new(start, end_)
end
-- Overload `Range.new` to allow calling this module as a function.
setmetatable(M, {
__call = function(_, ...)