Skip to content

Commit 7ceff61

Browse files
committed
feat: clamp text edit range to bounds
Technically the range should never be outside of the bounds of the line/buffer, but this seems like a case we should handle silently Closes #257
1 parent 5bfc1be commit 7ceff61

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

lua/blink/cmp/lib/text_edits.lua

+18
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ function text_edits.get_from_item(item)
116116
local offset_encoding = text_edits.offset_encoding_from_item(item)
117117
text_edit = text_edits.to_utf_8(text_edit, offset_encoding)
118118

119+
text_edit.range = text_edits.clamp_range_to_bounds(text_edit.range)
120+
119121
return text_edit
120122
end
121123

@@ -156,4 +158,20 @@ function text_edits.guess(item)
156158
}
157159
end
158160

161+
--- Clamps the range to the bounds of their respective lines
162+
--- @param range lsp.Range
163+
--- @return lsp.Range
164+
--- TODO: clamp start and end lines
165+
function text_edits.clamp_range_to_bounds(range)
166+
range = vim.deepcopy(range)
167+
168+
local start_line = vim.api.nvim_buf_get_lines(0, range.start.line, range.start.line + 1, false)[1]
169+
range.start.character = math.min(math.max(range.start.character, 0), #start_line)
170+
171+
local end_line = vim.api.nvim_buf_get_lines(0, range['end'].line, range['end'].line + 1, false)[1]
172+
range['end'].character = math.min(math.max(range['end'].character, 0), #end_line)
173+
174+
return range
175+
end
176+
159177
return text_edits

0 commit comments

Comments
 (0)