feat(lsp): add type annotations for lsp.util.locations_to_items (#26694)

Problem: luals reported many warnings
Solution: Add type annotations
This commit is contained in:
Mathias Fußenegger 2023-12-22 11:38:02 +01:00 committed by GitHub
parent 19aba5916a
commit db0ec84fb4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 10 deletions

View File

@ -1803,13 +1803,13 @@ locations_to_items({locations}, {offset_encoding})
|setloclist()|. |setloclist()|.
Parameters: ~ Parameters: ~
• {locations} (table) list of `Location`s or `LocationLink`s • {locations} lsp.Location[]|lsp.LocationLink[]
• {offset_encoding} (string) offset_encoding for locations • {offset_encoding} (string) offset_encoding for locations
utf-8|utf-16|utf-32 default to first client of utf-8|utf-16|utf-32 default to first client of
buffer buffer
Return: ~ Return: ~
(table) list of items vim.lsp.util.LocationItem [] list of items
lookup_section({settings}, {section}) *vim.lsp.util.lookup_section()* lookup_section({settings}, {section}) *vim.lsp.util.lookup_section()*
Helper function to return nested values in language server settings Helper function to return nested values in language server settings

View File

@ -1754,6 +1754,13 @@ local position_sort = sort_by_key(function(v)
return { v.start.line, v.start.character } return { v.start.line, v.start.character }
end) end)
---@class vim.lsp.util.LocationItem
---@field filename string
---@field lnum integer 1-indexed line number
---@field col integer 1-indexed column
---@field text string
---@field user_data lsp.Location|lsp.LocationLink
--- Returns the items with the byte position calculated correctly and in sorted --- Returns the items with the byte position calculated correctly and in sorted
--- order, for display in quickfix and location lists. --- order, for display in quickfix and location lists.
--- ---
@ -1763,10 +1770,10 @@ end)
--- The result can be passed to the {list} argument of |setqflist()| or --- The result can be passed to the {list} argument of |setqflist()| or
--- |setloclist()|. --- |setloclist()|.
--- ---
---@param locations table list of `Location`s or `LocationLink`s ---@param locations lsp.Location[]|lsp.LocationLink[]
---@param offset_encoding string offset_encoding for locations utf-8|utf-16|utf-32 ---@param offset_encoding string offset_encoding for locations utf-8|utf-16|utf-32
--- default to first client of buffer --- default to first client of buffer
---@return table list of items ---@return vim.lsp.util.LocationItem[] list of items
function M.locations_to_items(locations, offset_encoding) function M.locations_to_items(locations, offset_encoding)
if offset_encoding == nil then if offset_encoding == nil then
vim.notify_once( vim.notify_once(
@ -1777,6 +1784,7 @@ function M.locations_to_items(locations, offset_encoding)
end end
local items = {} local items = {}
---@type table<string, {start: lsp.Position, location: lsp.Location|lsp.LocationLink}[]>
local grouped = setmetatable({}, { local grouped = setmetatable({}, {
__index = function(t, k) __index = function(t, k)
local v = {} local v = {}
@ -1791,6 +1799,7 @@ function M.locations_to_items(locations, offset_encoding)
table.insert(grouped[uri], { start = range.start, location = d }) table.insert(grouped[uri], { start = range.start, location = d })
end end
---@type string[]
local keys = vim.tbl_keys(grouped) local keys = vim.tbl_keys(grouped)
table.sort(keys) table.sort(keys)
-- TODO(ashkan) I wish we could do this lazily. -- TODO(ashkan) I wish we could do this lazily.
@ -1799,16 +1808,13 @@ function M.locations_to_items(locations, offset_encoding)
table.sort(rows, position_sort) table.sort(rows, position_sort)
local filename = vim.uri_to_fname(uri) local filename = vim.uri_to_fname(uri)
-- list of row numbers local line_numbers = {}
local uri_rows = {}
for _, temp in ipairs(rows) do for _, temp in ipairs(rows) do
local pos = temp.start table.insert(line_numbers, temp.start.line)
local row = pos.line
table.insert(uri_rows, row)
end end
-- get all the lines for this uri -- get all the lines for this uri
local lines = get_lines(vim.uri_to_bufnr(uri), uri_rows) local lines = get_lines(vim.uri_to_bufnr(uri), line_numbers)
for _, temp in ipairs(rows) do for _, temp in ipairs(rows) do
local pos = temp.start local pos = temp.start