From 37dc9233a473dd6c3f54456ef9994d8f77c80211 Mon Sep 17 00:00:00 2001 From: Tristan Knight Date: Sat, 26 Oct 2024 15:33:10 +0100 Subject: [PATCH] fix: add shim for vim.lsp.util._str_byteindex() (#3338) Will be removed in Nvim 0.11: https://github.com/neovim/neovim/pull/30915 --- lua/telescope/builtin/__lsp.lua | 2 +- lua/telescope/utils.lua | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lua/telescope/builtin/__lsp.lua b/lua/telescope/builtin/__lsp.lua index 662a6af..af79bcb 100644 --- a/lua/telescope/builtin/__lsp.lua +++ b/lua/telescope/builtin/__lsp.lua @@ -105,7 +105,7 @@ end ---@return lsp.Location local function item_to_location(item, offset_encoding) local line = item.lnum - 1 - local character = vim.lsp.util._str_utfindex_enc(item.text, item.col, offset_encoding) - 1 + local character = utils.str_byteindex(item.text, item.col, offset_encoding or "utf-16") - 1 local uri if utils.is_uri(item.filename) then uri = item.filename diff --git a/lua/telescope/utils.lua b/lua/telescope/utils.lua index 145b7f3..d20dbbe 100644 --- a/lua/telescope/utils.lua +++ b/lua/telescope/utils.lua @@ -17,6 +17,18 @@ local utils = {} utils.iswin = vim.loop.os_uname().sysname == "Windows_NT" +---@param s string +---@param i number +---@param encoding "utf-8" | "utf-16" | "utf-32" +---@return integer +utils.str_byteindex = function(s, i, encoding) + if vim.fn.has "nvim-0.11" == 1 then + return vim.str_byteindex(s, encoding, i, false) + else + return vim.lsp.util._str_byteindex_enc(s, i, encoding) + end +end + --TODO(clason): Remove when dropping support for Nvim 0.9 utils.islist = vim.fn.has "nvim-0.10" == 1 and vim.islist or vim.tbl_islist local flatten = function(t)