From efff92f018a7c866c75c5c1ae276e0e6ba88f354 Mon Sep 17 00:00:00 2001 From: lopy <70210066+lopi-py@users.noreply.github.com> Date: Mon, 19 Aug 2024 12:35:58 -0500 Subject: [PATCH] refactor: update log messages --- CHANGELOG.md | 2 +- lua/luau-lsp/command.lua | 2 +- lua/luau-lsp/config.lua | 11 +++--- lua/luau-lsp/init.lua | 2 +- lua/luau-lsp/roblox/init.lua | 16 ++++---- lua/luau-lsp/roblox/sourcemap.lua | 28 +++++++------ lua/luau-lsp/server.lua | 65 ++++++++++++++++--------------- lua/luau-lsp/util.lua | 12 ++++++ plugin/luau-lsp.lua | 8 ++-- spec/server_spec.lua | 2 +- 10 files changed, 83 insertions(+), 65 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 896bdc5..d0868b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ ### Fixed -- Error loading the server when root directory not found +- Error loading the server when root directory is not found - Definition files that depend on Roblox types will now load properly - Merge internal modified capabilities with the default client capabilities if not specified in the server config - Sourcemap generation and studio server will only start if the configured platform is `roblox` diff --git a/lua/luau-lsp/command.lua b/lua/luau-lsp/command.lua index 5b4780c..eb3189d 100644 --- a/lua/luau-lsp/command.lua +++ b/lua/luau-lsp/command.lua @@ -83,7 +83,7 @@ function M.execute(cmdline) if commands[command] then commands[command].execute(args) else - log.error("Invalid command `%s`", command) + log.error("Invalid command '%s'", command) end end diff --git a/lua/luau-lsp/config.lua b/lua/luau-lsp/config.lua index 5601b2e..2739773 100644 --- a/lua/luau-lsp/config.lua +++ b/lua/luau-lsp/config.lua @@ -52,9 +52,8 @@ local defaults = { end) or server.root(path, { ".git", ".luaurc", - "stylua.toml", "selene.toml", - "selene.yml", + "stylua.toml", }) end, }, @@ -66,25 +65,25 @@ local options = defaults local function validate(opts) if vim.tbl_get(opts, "platform", "type") then if not compat.list_contains(PLATFORMS, opts.platform.type) then - log.error("Invalid option `platform.type` value: " .. opts.platform.type) + log.error("Invalid option 'platform.type' value: " .. opts.platform.type) end end if vim.tbl_get(opts, "types", "roblox_security_level") then if not compat.list_contains(SECURITY_LEVELS, opts.types.roblox_security_level) then log.error( - "Invalid option `types.roblox_security_level` value: " .. opts.types.roblox_security_level + "Invalid option 'types.roblox_security_level' value: " .. opts.types.roblox_security_level ) end end if vim.tbl_get(opts, "types", "roblox") ~= nil then - log.warn "`types.roblox` is deprecated, use `platform.type` instead" + log.warn "'types.roblox' is deprecated, use 'platform.type' instead" end local function check_server_setting(name) if vim.tbl_get(opts, "server", "settings", "luau-lsp", name) ~= nil then - log.error("`%s` should not be passed as server setting", name) + log.error("'%s' should not be passed as server setting", name) end end diff --git a/lua/luau-lsp/init.lua b/lua/luau-lsp/init.lua index ff89672..342f136 100644 --- a/lua/luau-lsp/init.lua +++ b/lua/luau-lsp/init.lua @@ -29,7 +29,7 @@ function M.aliases(paths) local success, contents = pcall(json.decode, luaurc:read "a") if not success then - log.error("Could not read `.luaurc`: %s", contents) + log.error("Failed to read '.luaurc': %s", contents) return end diff --git a/lua/luau-lsp/roblox/init.lua b/lua/luau-lsp/roblox/init.lua index a425601..7f28a18 100644 --- a/lua/luau-lsp/roblox/init.lua +++ b/lua/luau-lsp/roblox/init.lua @@ -4,7 +4,7 @@ local curl = require "plenary.curl" local log = require "luau-lsp.log" local util = require "luau-lsp.util" -local API_DOCS = +local API_DOCS_URL = "https://raw.githubusercontent.com/MaximumADHD/Roblox-Client-Tracker/roblox/api-docs/en-us.json" local function global_types_url() @@ -29,9 +29,9 @@ local download_api = async.wrap(function(callback) callback(true) end, 2) - local on_error = util.on_count(function() + local on_error = util.once(function() callback(false) - end, 2) + end) curl.get(global_types_url(), { output = global_types_file(), @@ -40,7 +40,7 @@ local download_api = async.wrap(function(callback) compressed = false, }) - curl.get(API_DOCS, { + curl.get(API_DOCS_URL, { output = api_docs_file(), callback = on_success, on_error = on_error, @@ -59,9 +59,9 @@ function M.prepare(cmd) if not download_api() then if util.is_file(global_types_file()) and util.is_file(api_docs_file()) then - log.error "Could not download roblox types, using local files" + log.warn "Failed to download Roblox API, using local files" else - log.error "Could not download roblox types, no local files found" + log.error "Failed to download Roblox API, local files not found" return end end @@ -70,7 +70,7 @@ function M.prepare(cmd) table.insert(cmd, "--docs=" .. api_docs_file()) end -M.start = vim.schedule_wrap(function() +function M.start() if config.get().platform.type ~= "roblox" then return end @@ -82,6 +82,6 @@ M.start = vim.schedule_wrap(function() if config.get().plugin.enabled then require("luau-lsp.roblox.studio").start() end -end) +end return M diff --git a/lua/luau-lsp/roblox/sourcemap.lua b/lua/luau-lsp/roblox/sourcemap.lua index 2b82edf..bef0630 100644 --- a/lua/luau-lsp/roblox/sourcemap.lua +++ b/lua/luau-lsp/roblox/sourcemap.lua @@ -1,5 +1,4 @@ local Job = require "plenary.job" -local async = require "plenary.async" local config = require "luau-lsp.config" local log = require "luau-lsp.log" local util = require "luau-lsp.util" @@ -15,7 +14,8 @@ function M.get_rojo_project_files() return vim.tbl_filter(util.is_file, project_files) end -local get_rojo_project_file = async.wrap(function(callback) +---@param callback fun(project_file: string?) +local function get_rojo_project_file(callback) local project_file = config.get().sourcemap.rojo_project_file if util.is_file(project_file) then callback(project_file) @@ -24,11 +24,11 @@ local get_rojo_project_file = async.wrap(function(callback) local found_project_files = M.get_rojo_project_files() if #found_project_files == 0 then - log.error("Unable to find project file `%s`", project_file) + log.error("Unable to find project file '%s'", project_file) callback() elseif #found_project_files == 1 then log.info( - "Unable to find project file `%s`. We found `%s`", + "Unable to find project file '%s'. We found '%s'", project_file, found_project_files[1] ) @@ -36,7 +36,7 @@ local get_rojo_project_file = async.wrap(function(callback) else vim.ui.select(found_project_files, { prompt = "Select project file" }, callback) end -end, 1) +end ---@param project_file? string local function start_sourcemap_generation(project_file) @@ -63,7 +63,7 @@ local function start_sourcemap_generation(project_file) on_exit = function(self, code) if code ~= 0 then local err = table.concat(self:stderr_result(), "\n") - log.error("Failed to update sourcemap for `%s`: %s", project_file, err) + log.error("Failed to update sourcemap for '%s': %s", project_file, err) end job = nil end, @@ -74,7 +74,7 @@ local function start_sourcemap_generation(project_file) return end - log.info("Starting sourcemap generation for `%s`", project_file) + log.info("Starting sourcemap generation for '%s'", project_file) job:start() end @@ -84,14 +84,20 @@ local function stop_sourcemap_generation() end end -M.start = async.void(function(project_file) +---@param project_file? string +function M.start(project_file) if project_file and not util.is_file(project_file) then - log.error("Invalid project file `%s`", project_file) + log.error("Invalid project file '%s'", project_file) return end stop_sourcemap_generation() - start_sourcemap_generation(project_file or get_rojo_project_file()) -end) + + if project_file then + start_sourcemap_generation(project_file) + else + get_rojo_project_file(start_sourcemap_generation) + end +end return M diff --git a/lua/luau-lsp/server.lua b/lua/luau-lsp/server.lua index 922a26f..188f128 100644 --- a/lua/luau-lsp/server.lua +++ b/lua/luau-lsp/server.lua @@ -5,21 +5,31 @@ local curl = require "plenary.curl" local log = require "luau-lsp.log" local util = require "luau-lsp.util" -local CURRENT_FFLAGS = +local CURRENT_FFLAGS_URL = "https://clientsettingscdn.roblox.com/v1/settings/application?applicationName=PCDesktopClient" +---@type integer[] local pending_buffers = {} local fetch_fflags = async.wrap(function(callback) + local function on_error(message) + log.error("Failed to fetch current Luau FFlags: %s", message) + callback {} + end + curl.get { - url = CURRENT_FFLAGS, + url = CURRENT_FFLAGS_URL, accept = "application/json", callback = function(result) - callback(vim.json.decode(result.body).applicationSettings) + local ok, content = pcall(vim.json.decode, result.body) + if ok then + callback(content.applicationSettings) + else + on_error(content) + end end, - on_error = function() - log.error "Could not fetch the latest Luau FFlags" - callback {} + on_error = function(result) + on_error(result.stderr) end, compressed = false, } @@ -62,7 +72,7 @@ local function get_cmd() table.insert(cmd, "--definitions=" .. definition_file) else log.warn( - "Definitions file at `%s` does not exist, types will not be provided from this file", + "Definitions file at '%s' does not exist, types will not be provided from this file", definition_file ) end @@ -73,7 +83,7 @@ local function get_cmd() if util.is_file(documentation_file) then table.insert(cmd, "--docs=" .. documentation_file) else - log.warn("Documentations file at `%s` does not exist", documentation_file) + log.warn("Documentations file at '%s' does not exist", documentation_file) end end @@ -119,8 +129,16 @@ local function force_push_diagnostics(opts) end end ----@async ----@return number? +---@param client_id number +local function attach_pending_buffers(client_id) + for _, bufnr in ipairs(pending_buffers) do + if vim.api.nvim_buf_is_valid(bufnr) then + vim.lsp.buf_attach_client(bufnr, client_id) + end + end + pending_buffers = {} +end + local function start_language_server() local bufnr = vim.api.nvim_get_current_buf() local bufname = vim.api.nvim_buf_get_name(bufnr) @@ -142,20 +160,10 @@ local function start_language_server() return end - require("luau-lsp.roblox").start() - - return client_id -end - ----@param client_id number -local function attach_pending_buffers(client_id) - for _, bufnr in ipairs(pending_buffers) do - if vim.api.nvim_buf_is_valid(bufnr) then - vim.lsp.buf_attach_client(bufnr, client_id) - end - end - - pending_buffers = {} + vim.schedule(function() + require("luau-lsp.roblox").start() + attach_pending_buffers(client_id) + end) end ---@param bufnr number @@ -184,14 +192,7 @@ function M.start(bufnr) end if #pending_buffers == 0 then - async.run( - start_language_server, - vim.schedule_wrap(function(client_id) - if client_id then - attach_pending_buffers(client_id) - end - end) - ) + async.run(start_language_server) end table.insert(pending_buffers, bufnr) diff --git a/lua/luau-lsp/util.lua b/lua/luau-lsp/util.lua index 9331953..0193d2e 100644 --- a/lua/luau-lsp/util.lua +++ b/lua/luau-lsp/util.lua @@ -32,6 +32,18 @@ function M.on_count(callback, n) end end +---@param fn function +---@return function +function M.once(fn) + local called = false + return function(...) + if not called then + called = true + fn(...) + end + end +end + ---@param bufnr number? ---@return vim.lsp.Client? function M.get_client(bufnr) diff --git a/plugin/luau-lsp.lua b/plugin/luau-lsp.lua index ac18dcb..0731bd1 100644 --- a/plugin/luau-lsp.lua +++ b/plugin/luau-lsp.lua @@ -26,19 +26,19 @@ end, { }) vim.api.nvim_create_user_command("LuauLog", function() - require("luau-lsp.log").warn "`LuauLog` is deprecated, use `LuauLsp log` instead" + require("luau-lsp.log").warn "'LuauLog' is deprecated, use 'LuauLsp log' instead" end, {}) vim.api.nvim_create_user_command("LuauBytecode", function() - require("luau-lsp.log").warn "`LuauBytecode` is deprecated, use `LuauLsp bytecode` instead" + require("luau-lsp.log").warn "'LuauBytecode' is deprecated, use 'LuauLsp bytecode' instead" end, {}) vim.api.nvim_create_user_command("LuauCompilerRemarks", function() - require("luau-lsp.log").warn "`LuauCompilerRemarks` is deprecated, use `LuauLsp compiler_remarks` instead" + require("luau-lsp.log").warn "'LuauCompilerRemarks' is deprecated, use 'LuauLsp compiler_remarks' instead" end, {}) vim.api.nvim_create_user_command("LuauRegenerateSourcemap", function() - require("luau-lsp.log").warn "`LuauRegenerateSourcemap` is deprecated, use `LuauLsp regenerate_sourcemap` instead" + require("luau-lsp.log").warn "'LuauRegenerateSourcemap' is deprecated, use 'LuauLsp regenerate_sourcemap' instead" end, { nargs = "?", }) diff --git a/spec/server_spec.lua b/spec/server_spec.lua index 7ce3167..18a225f 100644 --- a/spec/server_spec.lua +++ b/spec/server_spec.lua @@ -97,7 +97,7 @@ describe("luau-lsp server", function() assert .stub(notify).was - .called_with("[luau-lsp.nvim] Unable to find project file `default.project.json`", vim.log.levels.ERROR) + .called_with("[luau-lsp.nvim] Unable to find project file 'default.project.json'", vim.log.levels.ERROR) assert.match("globalTypes.PluginSecurity.d.luau", client.config.cmd[3]) assert.match("api%-docs.json", client.config.cmd[4]) end)