diff options
author | Justin M. Keyes <justinkz@gmail.com> | 2023-08-01 15:52:53 +0200 |
---|---|---|
committer | Justin M. Keyes <justinkz@gmail.com> | 2023-08-01 16:13:22 +0200 |
commit | f41496ce74fb30c18bb9a03027a172800b269643 (patch) | |
tree | 8209dd6ed61325486329e5bb787a691b35863874 /scripts | |
parent | da09f9b551badfb3fd363589009168560ae607f6 (diff) | |
download | rneovim-f41496ce74fb30c18bb9a03027a172800b269643.tar.gz rneovim-f41496ce74fb30c18bb9a03027a172800b269643.tar.bz2 rneovim-f41496ce74fb30c18bb9a03027a172800b269643.zip |
feat(gen_lsp.lua): sort by name, handle failure #24504
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/gen_lsp.lua (renamed from scripts/lsp_types.lua) | 65 |
1 files changed, 22 insertions, 43 deletions
diff --git a/scripts/lsp_types.lua b/scripts/gen_lsp.lua index 61ee6ad309..ef9d6c41df 100644 --- a/scripts/lsp_types.lua +++ b/scripts/gen_lsp.lua @@ -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', |