|
| 1 | +local severity_map = { |
| 2 | + '\27[31merror:\27[0m', |
| 3 | + '\27[33mwarning:\27[0m', |
| 4 | + '\27[90minfo:\27[0m', |
| 5 | + '\27[36mhint:\27[0m', |
| 6 | +} |
| 7 | + |
| 8 | +local lua_language_server = vim.fn.stdpath('data') |
| 9 | + .. '/mason/packages/lua-language-server/lua-language-server' |
| 10 | + |
| 11 | +if vim.env.LUA_LS_SERVER then |
| 12 | + lua_language_server = vim.env.LUA_LS_SERVER |
| 13 | +elseif vim.env.GITHUB_ACTIONS == 'true' then |
| 14 | + lua_language_server = [[/home/linuxbrew/.linuxbrew/bin/lua-language-server]] |
| 15 | +end |
| 16 | + |
| 17 | +local tmp_dir = (vim.env.TMPDIR or '/tmp/') .. 'lua_ls_report/' |
| 18 | +vim.fn.mkdir(tmp_dir, '-p') |
| 19 | +local template = tmp_dir .. 'lua_ls_report.XXXXXX' |
| 20 | +local _, path_or_errname, err_msg = vim.uv.fs_mkstemp(template) |
| 21 | +if err_msg ~= nil or path_or_errname == nil then |
| 22 | + vim.print(err_msg or 'mkstemp failed') |
| 23 | + os.exit(1) |
| 24 | +end |
| 25 | +local tmp_path = path_or_errname |
| 26 | + |
| 27 | +local check_command = { |
| 28 | + lua_language_server, |
| 29 | + '--check', |
| 30 | + '.', |
| 31 | + '--check_out_path', |
| 32 | + tmp_path, |
| 33 | +} |
| 34 | + |
| 35 | +if vim.env.LUA_LS_LOGLEVEL then |
| 36 | + table.insert(check_command, '--loglevel') |
| 37 | + table.insert(check_command, vim.env.LUA_LS_LOGLEVEL) |
| 38 | +end |
| 39 | + |
| 40 | +if vim.env.LUA_LS_LOGPATH then |
| 41 | + table.insert(check_command, '--logpath') |
| 42 | + table.insert(check_command, vim.env.LUA_LS_LOGPATH) |
| 43 | +end |
| 44 | + |
| 45 | +if vim.env.LUA_LS_CONFIGPATH then |
| 46 | + table.insert(check_command, '--configpath') |
| 47 | + table.insert(check_command, vim.env.LUA_LS_CONFIGPATH) |
| 48 | +end |
| 49 | + |
| 50 | +local check_command_result = vim.system(check_command):wait() |
| 51 | +local ok = check_command_result.code == 0 |
| 52 | +if not ok then |
| 53 | + vim.print(check_command_result) |
| 54 | + os.exit(check_command_result.code) |
| 55 | +end |
| 56 | + |
| 57 | +local input = vim.fn.readfile(tmp_path) |
| 58 | +local diagnostics = vim.json.decode(table.concat(input)) |
| 59 | +local formatted_diagnostics = {} |
| 60 | +for filepath, file_diagnostics in pairs(diagnostics) do |
| 61 | + vim.validate('diagnostics', file_diagnostics, vim.islist, 'a list of diagnostics') |
| 62 | + local path = filepath:gsub('^file://', ''):gsub('/%./', '/') |
| 63 | + for _, v in ipairs(file_diagnostics) do |
| 64 | + local item = { |
| 65 | + file = path, |
| 66 | + message = v.message, |
| 67 | + severity = severity_map[v.severity or 1], |
| 68 | + line = v.range.start.line + 1, |
| 69 | + col = v.range.start.character + 1, |
| 70 | + } |
| 71 | + table.insert(formatted_diagnostics, item) |
| 72 | + end |
| 73 | +end |
| 74 | + |
| 75 | +if vim.tbl_isempty(formatted_diagnostics) then |
| 76 | + io.write('\27[32m') |
| 77 | + io.write(check_command_result.stdout) |
| 78 | + io.write('\27[0m') |
| 79 | + os.exit(0) |
| 80 | +else |
| 81 | + io.write('\27[31m') |
| 82 | + io.write(check_command_result.stdout) |
| 83 | + io.write('\27[0m\n') |
| 84 | + for _, v in ipairs(formatted_diagnostics) do |
| 85 | + io.write( |
| 86 | + ('\x1b]8;;file://%s\x1b\\%s\x1b]8;;\x1b\\:%s:%s: %s %s\n'):format( |
| 87 | + v.file, |
| 88 | + vim.fn.fnamemodify(v.file, ':.'), |
| 89 | + v.line, |
| 90 | + v.col, |
| 91 | + v.severity, |
| 92 | + v.message |
| 93 | + ) |
| 94 | + ) |
| 95 | + end |
| 96 | + os.exit(1) |
| 97 | +end |
0 commit comments