aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/buffer.c
diff options
context:
space:
mode:
authorTristan Knight <admin@snappeh.com>2024-10-23 14:33:57 +0100
committerGitHub <noreply@github.com>2024-10-23 06:33:57 -0700
commit230b0c7f021a57647a658edce27fe115343f083f (patch)
tree49bcf13151da2bc140408ce2fb173b782614a0ca /src/nvim/buffer.c
parent3a86b60032bd659c2b12e984abb40cee93568558 (diff)
downloadrneovim-230b0c7f021a57647a658edce27fe115343f083f.tar.gz
rneovim-230b0c7f021a57647a658edce27fe115343f083f.tar.bz2
rneovim-230b0c7f021a57647a658edce27fe115343f083f.zip
feat(stdlib): overload vim.str_byteindex, vim.str_utfindex #30735
PROBLEM: There are several limitations to vim.str_byteindex, vim.str_utfindex: 1. They throw given out-of-range indexes. An invalid (often user/lsp-provided) index doesn't feel exceptional and should be handled by the caller. `:help dev-error-patterns` suggests that `retval, errmsg` is the preferred way to handle this kind of failure. 2. They cannot accept an encoding. So LSP needs wrapper functions. #25272 3. The current signatures are not extensible. * Calling: The function currently uses a fairly opaque boolean value to indicate to identify the encoding. * Returns: The fact it can throw requires wrapping in pcall. 4. The current name doesn't follow suggestions in `:h dev-naming` and I think `get` would be suitable. SOLUTION: - Because these are performance-sensitive, don't introduce `opts`. - Introduce an "overload" that accepts `encoding:string` and `strict_indexing:bool` params. ```lua local col = vim.str_utfindex(line, encoding, [index, [no_out_of_range]]) ``` Support the old versions by dispatching on the type of argument 2, and deprecate that form. ```lua vim.str_utfindex(line) -- (utf-32 length, utf-16 length), deprecated vim.str_utfindex(line, index) -- (utf-32 index, utf-16 index), deprecated vim.str_utfindex(line, 'utf-16') -- utf-16 length vim.str_utfindex(line, 'utf-16', index) -- utf-16 index vim.str_utfindex(line, 'utf-16', math.huge) -- error: index out of range vim.str_utfindex(line, 'utf-16', math.huge, false) -- utf-16 length ```
Diffstat (limited to 'src/nvim/buffer.c')
0 files changed, 0 insertions, 0 deletions