Skip to content

Commit 366141a

Browse files
committed
Add Buffer:extmark.
1 parent 0ac39cd commit 366141a

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

lua/lean/infoview.lua

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -947,8 +947,7 @@ function Pin:__update_extmark_style(buffer, line, col)
947947
return
948948
end
949949
buffer = self.__extmark_buffer
950-
local extmark_pos =
951-
vim.api.nvim_buf_get_extmark_by_id(buffer.bufnr, self.__extmark_ns, self.__extmark, {})
950+
local extmark_pos = buffer:extmark(self.__extmark_ns, self.__extmark, {})
952951
if vim.tbl_isempty(extmark_pos) then
953952
return
954953
end
@@ -996,8 +995,7 @@ function Pin:update_position()
996995
return
997996
end
998997

999-
local extmark_pos =
1000-
vim.api.nvim_buf_get_extmark_by_id(buffer.bufnr, self.__extmark_ns, extmark, {})
998+
local extmark_pos = buffer:extmark(self.__extmark_ns, extmark, {})
1001999

10021000
local new_pos = { line = extmark_pos[1] }
10031001

lua/std/nvim/buffer.lua

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,28 @@ end
158158
---See :h nvim_buf_get_extmarks for details.
159159
---
160160
---@param ns_id? integer
161-
---@param start? integer| { [1]: integer, [2]: integer }
162-
---@param end_? integer| { [1]: integer, [2]: integer }
161+
---@param start? integer|[integer, integer]
162+
---@param end_? integer|[integer, integer]
163163
---@param opts? table
164164
function Buffer:extmarks(ns_id, start, end_, opts)
165165
return vim.api.nvim_buf_get_extmarks(self.bufnr, ns_id or -1, start or 0, end_ or -1, opts or {})
166166
end
167167

168+
---Get a specific extmark by id from the buffer.
169+
---
170+
---See :h nvim_buf_get_extmark_by_id for details.
171+
---
172+
---Note in particular that if the extmark doesn't exist, an empty list is
173+
---returned.
174+
---
175+
---@param ns_id integer
176+
---@param id integer
177+
---@param opts? table
178+
---@return [integer, integer]|{}
179+
function Buffer:extmark(ns_id, id, opts)
180+
return vim.api.nvim_buf_get_extmark_by_id(self.bufnr, ns_id, id, opts or {})
181+
end
182+
168183
---Set an extmark in the buffer.
169184
---
170185
---See :h nvim_buf_set_extmark for details.

spec/std/nvim/buffer_spec.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,23 @@ describe('Buffer', function()
320320
end)
321321
end)
322322

323+
describe('extmark', function()
324+
it('gets a specific extmark by id', function()
325+
local buffer = Buffer.create {}
326+
buffer:set_lines { 'line' }
327+
local ns = vim.api.nvim_create_namespace ''
328+
local id = buffer:set_extmark(ns, 0, 0, {})
329+
assert.are.same({ 0, 0 }, buffer:extmark(ns, id))
330+
buffer:force_delete()
331+
end)
332+
333+
it('returns empty table for a non-existent extmark', function()
334+
local buffer = Buffer.create {}
335+
local ns = vim.api.nvim_create_namespace ''
336+
assert.are.same(buffer:extmark(ns, 99999), {})
337+
end)
338+
end)
339+
323340
describe('clear_namespace', function()
324341
it('clears all extmarks in a namespace', function()
325342
local buffer = Buffer.create {}

0 commit comments

Comments
 (0)