-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: improve health checks and logger
- Loading branch information
Showing
11 changed files
with
126 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,68 @@ | ||
local Job = require "plenary.job" | ||
local config = require "luau-lsp.config" | ||
|
||
local M = {} | ||
|
||
function M.check() | ||
vim.health.start "luau-lsp" | ||
---@param opts { name: string, cmd: string[], version?: string, required: boolean? } | ||
local function check_executable(opts) | ||
local ok, job = pcall(Job.new, Job, { | ||
command = opts.cmd[1], | ||
args = vim.list_slice(opts.cmd, 2), | ||
}) | ||
|
||
if not ok then | ||
local report = opts.required == false and vim.health.warn or vim.health.error | ||
report(string.format("%s: not available", opts.name)) | ||
return | ||
end | ||
|
||
if vim.fn.executable "luau-lsp" == 1 then | ||
local version = require("luau-lsp.server").version() | ||
if vim.version.ge(version, "1.32.0") then | ||
vim.health.ok(string.format("luau-lsp: `%s`", tostring(version))) | ||
if opts.version then | ||
local version = table.concat(job:sync(), "\n") | ||
if vim.version.ge(version, opts.version) then | ||
vim.health.ok(string.format("%s: `%s`", opts.name, tostring(version))) | ||
else | ||
vim.health.error( | ||
string.format("luau-lsp: required version is `1.32.0`, found `%s`", tostring(version)) | ||
string.format( | ||
"%s: required version is `%s`, found `%s`", | ||
opts.name, | ||
opts.version:gsub("a", "b"), | ||
version | ||
) | ||
) | ||
end | ||
else | ||
vim.health.error "luau-lsp: not available" | ||
end | ||
end | ||
|
||
function M.check() | ||
vim.health.start "luau-lsp" | ||
|
||
check_executable { | ||
name = "luau-lsp", | ||
cmd = { config.get().server.cmd[1], "--version" }, | ||
version = "1.32.0", | ||
} | ||
|
||
local autocmds = vim.api.nvim_get_autocmds { | ||
group = "lspconfig", | ||
event = "FileType", | ||
pattern = "luau", | ||
} | ||
|
||
if #autocmds == 0 then | ||
vim.health.ok "No conflicts with `nvim-lspconfig`" | ||
else | ||
vim.health.error "`lspconfig.luau_lsp.setup` was called, it may cause conflicts" | ||
end | ||
|
||
vim.health.start "Rojo (required for sourcemap generation)" | ||
vim.health.start "Rojo (required for automatic sourcemap generation)" | ||
|
||
if vim.fn.executable "rojo" == 1 then | ||
local version = require("luau-lsp.roblox.sourcemap").version() | ||
if vim.version.ge(version, "7.3.0") then | ||
vim.health.ok(string.format("rojo: `%s`", tostring(version))) | ||
else | ||
vim.health.error( | ||
string.format("rojo: required version is `7.3.0`, found `%s`", tostring(version)) | ||
) | ||
end | ||
else | ||
vim.health.warn "rojo: not available" | ||
end | ||
check_executable { | ||
name = "rojo", | ||
cmd = { config.get().sourcemap.rojo_path, "--version" }, | ||
version = "7.3.0", | ||
required = config.get().platform.type == "roblox" | ||
and config.get().sourcemap.enabled | ||
and config.get().sourcemap.autogenerate, | ||
} | ||
end | ||
|
||
return M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,58 @@ | ||
local Path = require "plenary.path" | ||
local util = require "luau-lsp.util" | ||
|
||
local PLUGIN_NAME = "luau-lsp.nvim" | ||
local LOG_FILE = Path:new(util.storage_file "luau-lsp.log") | ||
local M = {} | ||
|
||
local levels_lookup = {} | ||
for k, v in pairs(vim.log.levels) do | ||
levels_lookup[v] = k | ||
end | ||
M.level = vim.log.levels.WARN | ||
M.filename = util.storage_file "luau-lsp.log" | ||
|
||
---@type file*? | ||
local logfile | ||
|
||
---@return boolean | ||
local function supports_title() | ||
local success = pcall(require, "notify") | ||
return success | ||
return package.loaded["notify"] ~= nil | ||
end | ||
|
||
---@param message string | ||
---@param level string | ||
local function write(message, level) | ||
if not logfile then | ||
logfile = io.open(M.filename, "a+") | ||
end | ||
assert(logfile) | ||
logfile:write(string.format("%s[%s] %s", os.date "%H:%M:%S", level, message)) | ||
logfile:write "\n" | ||
logfile:flush() | ||
end | ||
|
||
---@param message string | ||
---@param level number | ||
local notify = vim.schedule_wrap(function(message, level) | ||
if supports_title() then | ||
vim.notify(message, level, { title = "luau-lsp.nvim" }) | ||
else | ||
vim.notify(string.format("[%s] %s", "luau-lsp.nvim", message), level) | ||
end | ||
end) | ||
|
||
---@param level string | ||
---@param levelnr number | ||
---@return fun(message: string, ...: any) | ||
local function create_logger(level) | ||
return vim.schedule_wrap(function(message, ...) | ||
local timestr = vim.fn.strftime "%H:%M:%S" | ||
local function create_logger(level, levelnr) | ||
return function(message, ...) | ||
message = string.format(message, ...) | ||
|
||
LOG_FILE:write(string.format("%s[%s]: %s\n", timestr, levels_lookup[level], message), "a") | ||
|
||
if level >= vim.log.levels.WARN then | ||
if not supports_title() then | ||
message = string.format("[%s]: %s", PLUGIN_NAME, message) | ||
end | ||
write(message, level) | ||
|
||
vim.notify(message, level, { title = PLUGIN_NAME }) | ||
if levelnr >= M.level then | ||
notify(message, levelnr) | ||
end | ||
end) | ||
end | ||
end | ||
|
||
local M = {} | ||
|
||
M.log_file = tostring(LOG_FILE) | ||
M.debug = create_logger(vim.log.levels.DEBUG) | ||
M.info = create_logger(vim.log.levels.INFO) | ||
M.warn = create_logger(vim.log.levels.WARN) | ||
M.error = create_logger(vim.log.levels.ERROR) | ||
M.debug = create_logger("DEBUG", vim.log.levels.DEBUG) | ||
M.info = create_logger("INFO", vim.log.levels.INFO) | ||
M.warn = create_logger("WARN", vim.log.levels.WARN) | ||
M.error = create_logger("ERROR", vim.log.levels.ERROR) | ||
|
||
return M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.