feat(gen_lsp.lua): sort by name, handle failure #24504

This commit is contained in:
Justin M. Keyes
2023-08-01 15:52:53 +02:00
parent da09f9b551
commit f41496ce74
5 changed files with 255 additions and 286 deletions

View File

@@ -1,10 +1,10 @@
--[[
Generates lua-ls annotations for lsp
USAGE:
nvim -l scripts/lsp_types.lua gen # this will overwrite runtime/lua/vim/lsp/types/protocol.lua
nvim -l scripts/lsp_types.lua gen --version 3.1x --build/new_lsp_types.lua
nvim -l scripts/lsp_types.lua gen --version 3.1x --out runtime/lua/vim/lsp/types/protocol.lua
nvim -l scripts/lsp_types.lua gen --version 3.1x --methods
nvim -l scripts/gen_lsp.lua gen # this will overwrite runtime/lua/vim/lsp/types/protocol.lua
nvim -l scripts/gen_lsp.lua gen --version 3.18 --build/new_lsp_types.lua
nvim -l scripts/gen_lsp.lua gen --version 3.18 --out runtime/lua/vim/lsp/types/protocol.lua
nvim -l scripts/gen_lsp.lua gen --version 3.18 --methods
--]]
local M = {}
@@ -19,47 +19,28 @@ local function tofile(fname, text)
end
end
local function sort_by_method(tbl)
local single, client, textD, workspace, others = {}, {}, {}, {}, {}
for _, item in ipairs(tbl) do
local parts = vim.split(item.method, '/', { trimempty = true })
if #parts == 1 then
single[#single + 1] = item
elseif parts[1] == 'textDocument' then
textD[#textD + 1] = item
elseif parts[1] == 'client' then
client[#client + 1] = item
elseif parts[1] == 'workspace' then
workspace[#workspace + 1] = item
else
others[#others + 1] = item
end
end
local res = {}
for _, item in ipairs({ single, client, textD, workspace, others }) do
res = vim.list_extend(res, item)
end
return res
end
local function read_json(opt)
local uri = 'https://raw.githubusercontent.com/microsoft/language-server-protocol/gh-pages/_specifications/lsp/'
.. opt.version
.. '/metaModel/metaModel.json'
local res = vim.system({ 'curl', uri, '-o', '-' }):wait()
if res.code ~= 0 then
io.write(res.stderr)
return
local res = vim.system({ 'curl', '--no-progress-meter', uri, '-o', '-' }):wait()
if res.code ~= 0 or (res.stdout or ''):len() < 999 then
print(('URL failed: %s'):format(uri))
vim.print(res)
error(res.stdout)
end
return vim.json.decode(res.stdout)
end
-- Gets the Lua symbol for a given fully-qualified LSP method name.
local function name(s)
return s:gsub('/', '_', 3)
end
local function gen_methods(protocol)
local output = {
'-- Generated by lsp_types.lua, keep at end of file.',
'-- Generated by gen_lsp.lua, keep at end of file.',
'--- LSP method names.',
'---',
'---@see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#metaModel',
@@ -67,16 +48,18 @@ local function gen_methods(protocol)
}
local indent = (' '):rep(2)
for _, item in ipairs(sort_by_method(protocol.requests)) do
table.sort(protocol.requests, function(a, b)
return name(a.method) < name(b.method)
end)
for _, item in ipairs(protocol.requests) do
if item.method then
local name = item.method:gsub('/', '_', 3)
if item.documentation then
local document = vim.split(item.documentation, '\n?\n', { trimempty = true })
for _, docstring in ipairs(document) do
output[#output + 1] = indent .. '--- ' .. docstring
end
end
output[#output + 1] = indent .. name .. " = '" .. item.method .. "',"
output[#output + 1] = ("%s%s = '%s',"):format(indent, name(item.method), item.method)
end
end
output[#output + 1] = '}'
@@ -116,10 +99,6 @@ end
function M.gen(opt)
local protocol = read_json(opt)
if not protocol then
os.exit(1)
return
end
if opt.methods then
gen_methods(protocol)
@@ -127,9 +106,9 @@ function M.gen(opt)
local output = {
'--[[',
'This file is autogenerated from scripts/lsp_types.lua',
'This file is autogenerated from scripts/gen_lsp.lua',
'Regenerate:',
[=[nvim -l scripts/lsp_types.lua gen --version 3.1x --runtime/lua/vim/lsp/types/protocol.lua]=],
[=[nvim -l scripts/gen_lsp.lua gen --version 3.18 --runtime/lua/vim/lsp/types/protocol.lua]=],
'--]]',
'',
'---@alias lsp.null nil',