@@ -286,7 +286,7 @@ local function get_line_byte_from_position(bufnr, position, offset_encoding)
286
286
-- When on the first character, we can ignore the difference between byte and
287
287
-- character
288
288
if col > 0 then
289
-
local line = get_line(bufnr, position.line)
289
+
local line = get_line(bufnr, position.line) or ''
290
290
local ok, result
291
291
ok, result = pcall(_str_byteindex_enc, line, col, offset_encoding)
292
292
if ok then
@@ -402,25 +402,6 @@ function M.apply_text_edits(text_edits, bufnr, offset_encoding)
402
402
end
403
403
end)
404
404
405
-
-- Some LSP servers may return +1 range of the buffer content but nvim_buf_set_text can't accept it so we should fix it here.
406
-
local has_eol_text_edit = false
407
-
local max = vim.api.nvim_buf_line_count(bufnr)
408
-
local len = _str_utfindex_enc(vim.api.nvim_buf_get_lines(bufnr, -2, -1, false)[1] or '', nil, offset_encoding)
409
-
text_edits = vim.tbl_map(function(text_edit)
410
-
if max <= text_edit.range.start.line then
411
-
text_edit.range.start.line = max - 1
412
-
text_edit.range.start.character = len
413
-
text_edit.newText = '\n' .. text_edit.newText
414
-
has_eol_text_edit = true
415
-
end
416
-
if max <= text_edit.range['end'].line then
417
-
text_edit.range['end'].line = max - 1
418
-
text_edit.range['end'].character = len
419
-
has_eol_text_edit = true
420
-
end
421
-
return text_edit
422
-
end, text_edits)
423
-
424
405
-- Some LSP servers are depending on the VSCode behavior.
425
406
-- The VSCode will re-locate the cursor position after applying TextEdit so we also do it.
426
407
local is_current_buf = vim.api.nvim_get_current_buf() == bufnr
@@ -440,16 +421,35 @@ function M.apply_text_edits(text_edits, bufnr, offset_encoding)
440
421
441
422
-- Apply text edits.
442
423
local is_cursor_fixed = false
424
+
local has_eol_text_edit = false
443
425
for _, text_edit in ipairs(text_edits) do
426
+
-- Convert from LSP style ranges to Neovim style ranges.
444
427
local e = {
445
428
start_row = text_edit.range.start.line,
446
-
start_col = get_line_byte_from_position(bufnr, text_edit.range.start),
429
+
start_col = get_line_byte_from_position(bufnr, text_edit.range.start, offset_encoding),
447
430
end_row = text_edit.range['end'].line,
448
-
end_col = get_line_byte_from_position(bufnr, text_edit.range['end']),
431
+
end_col = get_line_byte_from_position(bufnr, text_edit.range['end'], offset_encoding),
449
432
text = vim.split(text_edit.newText, '\n', true),
450
433
}
434
+
435
+
-- Some LSP servers may return +1 range of the buffer content but nvim_buf_set_text can't accept it so we should fix it here.
436
+
local max = vim.api.nvim_buf_line_count(bufnr)
437
+
if max <= e.start_row or max <= e.end_row then
438
+
local len = #(get_line(bufnr, max - 1) or '')
439
+
if max <= e.start_row then
440
+
e.start_row = max - 1
441
+
e.start_col = len
442
+
table.insert(e.text, 1, '')
443
+
end
444
+
if max <= e.end_row then
445
+
e.end_row = max - 1
446
+
e.end_col = len
447
+
end
448
+
has_eol_text_edit = true
449
+
end
451
450
vim.api.nvim_buf_set_text(bufnr, e.start_row, e.start_col, e.end_row, e.end_col, e.text)
452
451
452
+
-- Fix cursor position.
453
453
local row_count = (e.end_row - e.start_row) + 1
454
454
if e.end_row < cursor.row then
455
455
cursor.row = cursor.row + (#e.text - row_count)
@@ -464,10 +464,13 @@ function M.apply_text_edits(text_edits, bufnr, offset_encoding)
464
464
end
465
465
end
466
466
467
+
local max = vim.api.nvim_buf_line_count(bufnr)
468
+
469
+
-- Apply fixed cursor position.
467
470
if is_cursor_fixed then
468
471
local is_valid_cursor = true
469
-
is_valid_cursor = is_valid_cursor and cursor.row < vim.api.nvim_buf_line_count(bufnr)
470
-
is_valid_cursor = is_valid_cursor and cursor.col <= #(vim.api.nvim_buf_get_lines(bufnr, cursor.row, cursor.row + 1, false)[1] or '')
472
+
is_valid_cursor = is_valid_cursor and cursor.row < max
473
+
is_valid_cursor = is_valid_cursor and cursor.col <= #(get_line(bufnr, max - 1) or '')
471
474
if is_valid_cursor then
472
475
vim.api.nvim_win_set_cursor(0, { cursor.row + 1, cursor.col })
473
476
end
@@ -476,7 +479,7 @@ function M.apply_text_edits(text_edits, bufnr, offset_encoding)
476
479
-- Remove final line if needed
477
480
local fix_eol = has_eol_text_edit
478
481
fix_eol = fix_eol and api.nvim_buf_get_option(bufnr, 'fixeol')
479
-
fix_eol = fix_eol and (vim.api.nvim_buf_get_lines(bufnr, -2, -1, false)[1] or '') == ''
482
+
fix_eol = fix_eol and get_line(bufnr, max - 1) == ''
480
483
if fix_eol then
481
484
vim.api.nvim_buf_set_lines(bufnr, -2, -1, false, {})
482
485
end
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