277
277
---@private
278
278
--- Position is a https://microsoft.github.io/language-server-protocol/specifications/specification-current/#position
279
279
--- Returns a zero-indexed column, since set_lines() does the conversion to
280
-
---@param offset_encoding string utf-8|utf-16|utf-32|nil defaults to utf-16
280
+
---@param offset_encoding string utf-8|utf-16|utf-32
281
281
--- 1-indexed
282
282
local function get_line_byte_from_position(bufnr, position, offset_encoding)
283
283
-- LSP's line and characters are 0-indexed
@@ -360,15 +360,14 @@ end
360
360
--- Applies a list of text edits to a buffer.
361
361
---@param text_edits table list of `TextEdit` objects
362
362
---@param bufnr number Buffer id
363
-
---@param offset_encoding string utf-8|utf-16|utf-32|nil defaults to encoding of first client of `bufnr`
363
+
---@param offset_encoding string utf-8|utf-16|utf-32 defaults to encoding of first client of `bufnr`
364
364
---@see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textEdit
365
365
function M.apply_text_edits(text_edits, bufnr, offset_encoding)
366
366
validate {
367
367
text_edits = { text_edits, 't', false };
368
368
bufnr = { bufnr, 'number', false };
369
-
offset_encoding = { offset_encoding, 'string', true };
369
+
offset_encoding = { offset_encoding, 'string', false };
370
370
}
371
-
offset_encoding = offset_encoding or M._get_offset_encoding(bufnr)
372
371
if not next(text_edits) then return end
373
372
if not api.nvim_buf_is_loaded(bufnr) then
374
373
vim.fn.bufload(bufnr)
517
516
---@param text_document_edit table: a `TextDocumentEdit` object
518
517
---@param index number: Optional index of the edit, if from a list of edits (or nil, if not from a list)
519
518
---@see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocumentEdit
520
-
function M.apply_text_document_edit(text_document_edit, index)
519
+
function M.apply_text_document_edit(text_document_edit, index, offset_encoding)
521
520
local text_document = text_document_edit.textDocument
522
521
local bufnr = vim.uri_to_bufnr(text_document.uri)
522
+
if offset_encoding == nil then
523
+
vim.notify_once("apply_text_document_edit must be called with valid offset encoding", vim.log.levels.WARN)
524
+
end
523
525
524
526
-- For lists of text document edits,
525
527
-- do not check the version after the first edit.
@@ -538,7 +540,7 @@ function M.apply_text_document_edit(text_document_edit, index)
538
540
return
539
541
end
540
542
541
-
M.apply_text_edits(text_document_edit.edits, bufnr)
543
+
M.apply_text_edits(text_document_edit.edits, bufnr, offset_encoding)
542
544
end
543
545
544
546
--- Parses snippets in a completion entry.
737
739
---
738
740
---@param workspace_edit (table) `WorkspaceEdit`
739
741
--see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#workspace_applyEdit
740
-
function M.apply_workspace_edit(workspace_edit)
742
+
function M.apply_workspace_edit(workspace_edit, offset_encoding)
743
+
if offset_encoding == nil then
744
+
vim.notify_once("apply_workspace_edit must be called with valid offset encoding", vim.log.levels.WARN)
745
+
end
741
746
if workspace_edit.documentChanges then
742
747
for idx, change in ipairs(workspace_edit.documentChanges) do
743
748
if change.kind == "rename" then
@@ -753,7 +758,7 @@ function M.apply_workspace_edit(workspace_edit)
753
758
elseif change.kind then
754
759
error(string.format("Unsupported change: %q", vim.inspect(change)))
755
760
else
756
-
M.apply_text_document_edit(change, idx)
761
+
M.apply_text_document_edit(change, idx, offset_encoding)
757
762
end
758
763
end
759
764
return
@@ -984,12 +989,16 @@ end
984
989
985
990
--- Jumps to a location.
986
991
---
987
-
---@param location (`Location`|`LocationLink`)
992
+
---@param location table (`Location`|`LocationLink`)
993
+
---@param offset_encoding string utf-8|utf-16|utf-32 (required)
988
994
---@returns `true` if the jump succeeded
989
-
function M.jump_to_location(location)
995
+
function M.jump_to_location(location, offset_encoding)
990
996
-- location may be Location or LocationLink
991
997
local uri = location.uri or location.targetUri
992
998
if uri == nil then return end
999
+
if offset_encoding == nil then
1000
+
vim.notify_once("jump_to_location must be called with valid offset encoding", vim.log.levels.WARN)
1001
+
end
993
1002
local bufnr = vim.uri_to_bufnr(uri)
994
1003
-- Save position in jumplist
995
1004
vim.cmd "normal! m'"
@@ -1004,7 +1013,7 @@ function M.jump_to_location(location)
1004
1013
api.nvim_buf_set_option(bufnr, 'buflisted', true)
1005
1014
local range = location.range or location.targetSelectionRange
1006
1015
local row = range.start.line
1007
-
local col = get_line_byte_from_position(bufnr, range.start)
1016
+
local col = get_line_byte_from_position(bufnr, range.start, offset_encoding)
1008
1017
api.nvim_win_set_cursor(0, {row + 1, col})
1009
1018
-- Open folds under the cursor
1010
1019
vim.cmd("normal! zv")
@@ -1513,11 +1522,13 @@ do --[[ References ]]
1513
1522
---
1514
1523
---@param bufnr number Buffer id
1515
1524
---@param references table List of `DocumentHighlight` objects to highlight
1516
-
---@param offset_encoding string One of "utf-8", "utf-16", "utf-32", or nil. Defaults to `offset_encoding` of first client of `bufnr`
1525
+
---@param offset_encoding string One of "utf-8", "utf-16", "utf-32".
1517
1526
---@see https://microsoft.github.io/language-server-protocol/specifications/specification-3-17/#documentHighlight
1518
1527
function M.buf_highlight_references(bufnr, references, offset_encoding)
1519
-
validate { bufnr = {bufnr, 'n', true} }
1520
-
offset_encoding = offset_encoding or M._get_offset_encoding(bufnr)
1528
+
validate {
1529
+
bufnr = {bufnr, 'n', true},
1530
+
offset_encoding = { offset_encoding, 'string', false };
1531
+
}
1521
1532
for _, reference in ipairs(references) do
1522
1533
local start_line, start_char = reference["range"]["start"]["line"], reference["range"]["start"]["character"]
1523
1534
local end_line, end_char = reference["range"]["end"]["line"], reference["range"]["end"]["character"]
@@ -1550,9 +1561,14 @@ end)
1550
1561
--- The result can be passed to the {list} argument of |setqflist()| or
1551
1562
--- |setloclist()|.
1552
1563
---
1553
-
---@param locations (table) list of `Location`s or `LocationLink`s
1564
+
---@param locations table list of `Location`s or `LocationLink`s
1565
+
---@param offset_encoding string offset_encoding for locations utf-8|utf-16|utf-32
1554
1566
---@returns (table) list of items
1555
-
function M.locations_to_items(locations)
1567
+
function M.locations_to_items(locations, offset_encoding)
1568
+
if offset_encoding == nil then
1569
+
vim.notify_once("locations_to_items must be called with valid offset encoding", vim.log.levels.WARN)
1570
+
end
1571
+
1556
1572
local items = {}
1557
1573
local grouped = setmetatable({}, {
1558
1574
__index = function(t, k)
@@ -1592,7 +1608,7 @@ function M.locations_to_items(locations)
1592
1608
local pos = temp.start
1593
1609
local row = pos.line
1594
1610
local line = lines[row] or ""
1595
-
local col = pos.character
1611
+
local col = M._str_byteindex_enc(line, pos.character, offset_encoding)
1596
1612
table.insert(items, {
1597
1613
filename = filename,
1598
1614
lnum = row + 1,
@@ -1776,7 +1792,13 @@ function M._get_offset_encoding(bufnr)
1776
1792
local offset_encoding
1777
1793
1778
1794
for _, client in pairs(vim.lsp.buf_get_clients(bufnr)) do
1779
-
local this_offset_encoding = client.offset_encoding or "utf-16"
1795
+
if client.offset_encoding == nil then
1796
+
vim.notify_once(
1797
+
string.format("Client (id: %s) offset_encoding is nil. Do not unset offset_encoding.", client.id),
1798
+
vim.log.levels.ERROR
1799
+
)
1800
+
end
1801
+
local this_offset_encoding = client.offset_encoding
1780
1802
if not offset_encoding then
1781
1803
offset_encoding = this_offset_encoding
1782
1804
elseif offset_encoding ~= this_offset_encoding then
@@ -1905,7 +1927,9 @@ end
1905
1927
---@returns (number, number) `offset_encoding` index of the character in line {row} column {col} in buffer {buf}
1906
1928
function M.character_offset(buf, row, col, offset_encoding)
1907
1929
local line = get_line(buf, row)
1908
-
offset_encoding = offset_encoding or M._get_offset_encoding(buf)
1930
+
if offset_encoding == nil then
1931
+
vim.notify_once("character_offset must be called with valid offset encoding", vim.log.levels.WARN)
1932
+
end
1909
1933
-- If the col is past the EOL, use the line length.
1910
1934
if col > #line then
1911
1935
return _str_utfindex_enc(line, nil, offset_encoding)
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4