From 8d1233a1440cb93f64ff1d6181138437251e3e63 Mon Sep 17 00:00:00 2001 From: Yi Ming Date: Tue, 19 May 2026 16:32:12 +0800 Subject: [PATCH] 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()`, --- runtime/doc/lua.txt | 37 ++++-------------- runtime/doc/news.txt | 2 + runtime/lua/vim/lsp/inline_completion.lua | 2 +- runtime/lua/vim/pos.lua | 9 +++++ runtime/lua/vim/range.lua | 46 ----------------------- 5 files changed, 20 insertions(+), 76 deletions(-) diff --git a/runtime/doc/lua.txt b/runtime/doc/lua.txt index 725d013999..bdbb441dba 100644 --- a/runtime/doc/lua.txt +++ b/runtime/doc/lua.txt @@ -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|). diff --git a/runtime/doc/news.txt b/runtime/doc/news.txt index adf2a09583..880f772306 100644 --- a/runtime/doc/news.txt +++ b/runtime/doc/news.txt @@ -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 diff --git a/runtime/lua/vim/lsp/inline_completion.lua b/runtime/lua/vim/lsp/inline_completion.lua index e759562747..7dae0c717b 100644 --- a/runtime/lua/vim/lsp/inline_completion.lua +++ b/runtime/lua/vim/lsp/inline_completion.lua @@ -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], diff --git a/runtime/lua/vim/pos.lua b/runtime/lua/vim/pos.lua index a37353f736..c1142aa632 100644 --- a/runtime/lua/vim/pos.lua +++ b/runtime/lua/vim/pos.lua @@ -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) diff --git a/runtime/lua/vim/range.lua b/runtime/lua/vim/range.lua index e069df4a3a..faf5f650ad 100644 --- a/runtime/lua/vim/range.lua +++ b/runtime/lua/vim/range.lua @@ -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(_, ...)