|
| 1 | +local uv = vim.uv |
| 2 | +local ci_build = vim.env.CI == 'true' or vim.env.GITHUB_ACTIONS == 'true' |
| 3 | + |
| 4 | +local severity_type = { |
| 5 | + { color = 31, text = 'error' }, |
| 6 | + { color = 33, text = 'warning' }, |
| 7 | + { color = 90, text = 'info' }, |
| 8 | + { color = 36, text = 'hint' }, |
| 9 | +} |
| 10 | + |
| 11 | +local function lua_language_server_executable() |
| 12 | + local lua_language_server |
| 13 | + if vim.env.LUA_LS_SERVER then |
| 14 | + lua_language_server = vim.env.LUA_LS_SERVER |
| 15 | + if vim.fn.executable(lua_language_server) == 0 then |
| 16 | + io.write(('LUS_LS_SERVER %s not found or not executable\n'):format(lua_language_server)) |
| 17 | + os.exit(1) |
| 18 | + end |
| 19 | + else |
| 20 | + lua_language_server = vim.fn.exepath('lua-language-server') |
| 21 | + end |
| 22 | + |
| 23 | + if lua_language_server == '' or vim.fn.executable(lua_language_server) == 0 then |
| 24 | + if ci_build then |
| 25 | + lua_language_server = vim.env.GITHUB_WORKSPACE |
| 26 | + .. [[/tmp/lua_language_server/bin/lua-language-server]] |
| 27 | + io.write( |
| 28 | + ('lua-language-server not found or not executable. CI build detected, falling back to %s\n'):format( |
| 29 | + lua_language_server |
| 30 | + ) |
| 31 | + ) |
| 32 | + elseif lua_language_server == '' then |
| 33 | + lua_language_server = vim.fn.stdpath('data') |
| 34 | + .. '/mason/packages/lua-language-server/lua-language-server' |
| 35 | + io.write( |
| 36 | + ('lua-language-server not found or not executable. falling back to %s\n'):format( |
| 37 | + lua_language_server |
| 38 | + ) |
| 39 | + ) |
| 40 | + end |
| 41 | + end |
| 42 | + |
| 43 | + if vim.fn.executable(lua_language_server) == 0 then |
| 44 | + io.write(('%s not found or not executable\n'):format(lua_language_server)) |
| 45 | + os.exit(1) |
| 46 | + end |
| 47 | + return lua_language_server |
| 48 | +end |
| 49 | + |
| 50 | +local function tmpfile() |
| 51 | + local tmp_dir = (vim.env.TMPDIR or '/tmp/') .. 'lua_ls_report/' |
| 52 | + vim.fn.mkdir(tmp_dir, '-p') |
| 53 | + local template = tmp_dir .. 'lua_ls_report.XXXXXX' |
| 54 | + local _, path_or_errname, err_msg = uv.fs_mkstemp(template) |
| 55 | + if err_msg ~= nil or path_or_errname == nil then |
| 56 | + io.write(('%s\n'):format(err_msg or 'mkstemp failed')) |
| 57 | + os.exit(1) |
| 58 | + end |
| 59 | + return path_or_errname |
| 60 | +end |
| 61 | + |
| 62 | +local function check_command(lua_language_server, tmp_path) |
| 63 | + local command = { |
| 64 | + lua_language_server, |
| 65 | + '--check', |
| 66 | + '.', |
| 67 | + '--check_out_path', |
| 68 | + tmp_path, |
| 69 | + } |
| 70 | + |
| 71 | + if vim.env.LUA_LS_LOGLEVEL then |
| 72 | + table.insert(command, '--loglevel') |
| 73 | + table.insert(command, vim.env.LUA_LS_LOGLEVEL) |
| 74 | + end |
| 75 | + |
| 76 | + if vim.env.LUA_LS_LOGPATH then |
| 77 | + table.insert(command, '--logpath') |
| 78 | + table.insert(command, vim.env.LUA_LS_LOGPATH) |
| 79 | + end |
| 80 | + |
| 81 | + if vim.env.LUA_LS_CONFIGPATH then |
| 82 | + table.insert(command, '--configpath') |
| 83 | + table.insert(command, vim.env.LUA_LS_CONFIGPATH) |
| 84 | + end |
| 85 | + return command |
| 86 | +end |
| 87 | + |
| 88 | +local lua_language_server = lua_language_server_executable() |
| 89 | +local tmp_path = tmpfile() |
| 90 | +local command = check_command(lua_language_server, tmp_path) |
| 91 | +local check_command_result = vim.system(command):wait() |
| 92 | +local ok = check_command_result.code == 0 |
| 93 | +if not ok then |
| 94 | + io.write(('%s\n'):format(vim.inspect(check_command_result))) |
| 95 | + os.exit(check_command_result.code) |
| 96 | +end |
| 97 | + |
| 98 | +local input = vim.fn.readfile(tmp_path) |
| 99 | +local diagnostics = vim.json.decode(table.concat(input)) |
| 100 | +local formatted_diagnostics = {} |
| 101 | +for filepath, file_diagnostics in pairs(diagnostics) do |
| 102 | + local path = filepath:gsub('^file://', ''):gsub('/%./', '/') |
| 103 | + for _, v in ipairs(file_diagnostics) do |
| 104 | + local item = { |
| 105 | + file = path, |
| 106 | + message = v.message, |
| 107 | + severity = v.severity or 1, |
| 108 | + line = v.range.start.line + 1, |
| 109 | + col = v.range.start.character + 1, |
| 110 | + } |
| 111 | + table.insert(formatted_diagnostics, item) |
| 112 | + end |
| 113 | +end |
| 114 | + |
| 115 | +if vim.tbl_isempty(formatted_diagnostics) then |
| 116 | + io.write(('%s'):format(check_command_result.stdout)) |
| 117 | + os.exit(0) |
| 118 | +else |
| 119 | + for _, v in ipairs(formatted_diagnostics) do |
| 120 | + if ci_build then |
| 121 | + io.write( |
| 122 | + ('%s:%s:%s %s %s\n'):format( |
| 123 | + vim.fn.fnamemodify(v.file, ':.'), |
| 124 | + v.line, |
| 125 | + v.col, |
| 126 | + ('%s:'):format(severity_type[v.severity].text), |
| 127 | + v.message |
| 128 | + ) |
| 129 | + ) |
| 130 | + else |
| 131 | + io.write( |
| 132 | + ('\x1b]8;;file://%s\x1b\\%s\x1b]8;;\x1b\\:%s:%s %s %s\n'):format( |
| 133 | + v.file, |
| 134 | + vim.fn.fnamemodify(v.file, ':.'), |
| 135 | + v.line, |
| 136 | + v.col, |
| 137 | + ('\x1b[%sm%s:\x1b[0m'):format( |
| 138 | + severity_type[v.severity].color, |
| 139 | + severity_type[v.severity].text |
| 140 | + ), |
| 141 | + v.message |
| 142 | + ) |
| 143 | + ) |
| 144 | + end |
| 145 | + end |
| 146 | + io.write(('%s'):format(check_command_result.stdout)) |
| 147 | + os.exit(1) |
| 148 | +end |
0 commit comments