Skip to content

Commit

Permalink
refactor: update log messages
Browse files Browse the repository at this point in the history
  • Loading branch information
lopi-py committed Aug 19, 2024
1 parent 9f58cc2 commit efff92f
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 65 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
2 changes: 1 addition & 1 deletion lua/luau-lsp/command.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 5 additions & 6 deletions lua/luau-lsp/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ local defaults = {
end) or server.root(path, {
".git",
".luaurc",
"stylua.toml",
"selene.toml",
"selene.yml",
"stylua.toml",
})
end,
},
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lua/luau-lsp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 8 additions & 8 deletions lua/luau-lsp/roblox/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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(),
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
28 changes: 17 additions & 11 deletions lua/luau-lsp/roblox/sourcemap.lua
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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)
Expand All @@ -24,19 +24,19 @@ 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]
)
callback(found_project_files[1])
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)
Expand All @@ -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,
Expand All @@ -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

Expand All @@ -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
65 changes: 33 additions & 32 deletions lua/luau-lsp/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions lua/luau-lsp/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions plugin/luau-lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "?",
})
2 changes: 1 addition & 1 deletion spec/server_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit efff92f

Please sign in to comment.