This commit is contained in:
hrsh7th
2021-08-16 14:02:09 +09:00
parent 141187561c
commit e4deb0142a
5 changed files with 41 additions and 22 deletions

View File

@@ -56,7 +56,7 @@ context.new = function(prev_context, option)
self.cursor.row = vim.api.nvim_win_get_cursor(0)[1]
self.cursor.col = vim.api.nvim_win_get_cursor(0)[2] + 1
self.cursor.line = self.cursor.row - 1
self.cursor.character = vim.str_utfindex(self.cursor_line, self.cursor.col - 1)
self.cursor.character = misc.to_utfindex(self.cursor_line, self.cursor.col)
self.cursor_before_line = string.sub(self.cursor_line, 1, self.cursor.col - 1)
self.cursor_after_line = string.sub(self.cursor_line, self.cursor.col)
return self

View File

@@ -53,7 +53,7 @@ entry.get_offset = function(self)
if misc.safe(self.completion_item.textEdit) then
local range = misc.safe(self.completion_item.textEdit.insert) or misc.safe(self.completion_item.textEdit.range)
if range then
local c = vim.str_byteindex(self.context.cursor_line, range.start.character) + 1
local c = misc.to_vimindex(self.context.cursor_line, range.start.character)
for idx = c, self.source_offset do
if not char.is_white(string.byte(self.context.cursor_line, idx)) then
offset = math.min(offset, idx)
@@ -125,8 +125,8 @@ entry.get_overwrite = function(self)
return self.cache:ensure('get_overwrite', function()
if misc.safe(self.completion_item.textEdit) then
local r = misc.safe(self.completion_item.textEdit.insert) or misc.safe(self.completion_item.textEdit.range)
local s = vim.str_byteindex(self.context.cursor_line, r.start.character) + 1
local e = vim.str_byteindex(self.context.cursor_line, r['end'].character) + 1
local s = misc.to_vimindex(self.context.cursor_line, r.start.character)
local e = misc.to_vimindex(self.context.cursor_line, r['end'].character)
local before = self.context.cursor.col - s
local after = e - self.context.cursor.col
return before, after
@@ -275,7 +275,7 @@ entry.get_insert_range = function(self)
insert_range = {
start = {
line = self.context.cursor.row - 1,
character = math.min(vim.str_utfindex(self.context.cursor_line, self:get_offset() - 1), self.source_insert_range.start.character),
character = math.min(misc.to_utfindex(self.context.cursor_line, self:get_offset()), self.source_insert_range.start.character),
},
['end'] = self.source_insert_range['end'],
}
@@ -298,7 +298,7 @@ entry.get_replace_range = function(self)
replace_range = {
start = {
line = self.source_replace_range.start.line,
character = math.min(vim.str_utfindex(self.context.cursor_line, self:get_offset() - 1), self.source_replace_range.start.character),
character = math.min(misc.to_utfindex(self.context.cursor_line, self:get_offset()), self.source_replace_range.start.character),
},
['end'] = self.source_replace_range['end'],
}

View File

@@ -133,11 +133,11 @@ source.get_default_insert_range = function(self)
return {
start = {
line = self.context.cursor.row - 1,
character = vim.str_utfindex(self.context.cursor_line, self.offset - 1),
character = misc.to_utfindex(self.context.cursor_line, self.offset),
},
['end'] = {
line = self.context.cursor.row - 1,
character = vim.str_utfindex(self.context.cursor_line, self.context.cursor.col - 1),
character = misc.to_utfindex(self.context.cursor_line, self.context.cursor.col),
},
}
end)
@@ -155,11 +155,11 @@ source.get_default_replace_range = function(self)
return {
start = {
line = self.context.cursor.row - 1,
character = vim.str_utfindex(self.context.cursor_line, self.offset - 1),
character = misc.to_utfindex(self.context.cursor_line, self.offset),
},
['end'] = {
line = self.context.cursor.row - 1,
character = vim.str_utfindex(self.context.cursor_line, e and self.offset + e - 2 or self.context.cursor.col - 1),
character = misc.to_utfindex(self.context.cursor_line, e and self.offset + e - 1 or self.context.cursor.col),
},
}
end)

View File

@@ -1,3 +1,5 @@
local misc = require "cmp.utils.misc"
---@see https://microsoft.github.io/language-server-protocol/specifications/specification-current/
---@class lsp
local lsp = {}
@@ -14,17 +16,10 @@ lsp.Position.to_vim = function(buf, position)
end
local lines = vim.api.nvim_buf_get_lines(buf, position.line, position.line + 1, false)
if #lines > 0 then
for i = position.character, 1, -1 do
local s, v = pcall(function()
return {
row = position.line + 1,
col = vim.str_byteindex(lines[1], i) + 1
}
end)
if s then
return v
end
end
return {
row = position.line + 1,
col = misc.to_vimindex(lines[1], position.character)
}
end
return {
row = position.line + 1,
@@ -44,7 +39,7 @@ lsp.Position.to_lsp = function(buf, position)
if #lines > 0 then
return {
line = position.row - 1,
character = vim.str_utfindex(lines[1], math.max(0, math.min(position.col - 1, #lines[1]))),
character = misc.to_utfindex(lines[1], position.col),
}
end
return {

View File

@@ -112,4 +112,28 @@ misc.copy = function(tbl)
return copy
end
---Safe version of vim.str_utfindex
---@param text string
---@param vimindex number
---@return number
misc.to_utfindex = function(text, vimindex)
return vim.str_utfindex(text, math.max(0, math.min(vimindex - 1, #text)))
end
---Safe version of vim.str_byteindex
---@param text string
---@param utfindex number
---@return number
misc.to_vimindex = function(text, utfindex)
for i = utfindex, 1, -1 do
local s, v = pcall(function()
return vim.str_byteindex(text, i) + 1
end)
if s then
return v
end
end
return utfindex + 1
end
return misc