diff --git a/lua/luau-lsp/init.lua b/lua/luau-lsp/init.lua index edd3fc4..8994882 100644 --- a/lua/luau-lsp/init.lua +++ b/lua/luau-lsp/init.lua @@ -1,43 +1,8 @@ -local json = require "luau-lsp.json" -local log = require "luau-lsp.log" - ----@param paths string[] ----@return file*? -local function find_luaurc(paths) - local luaurc = io.open(".luaurc", "r") - if luaurc then - return luaurc - end - - for _, path in ipairs(paths) do - luaurc = io.open(path .. "/.luaurc", "r") - if luaurc then - return luaurc - end - end -end - local M = {} ----@param paths? string[] ---@return table? -function M.aliases(paths) - local luaurc = find_luaurc(paths or { "src", "lib" }) - if not luaurc then - return - end - - local ok, content = pcall(json.decode, luaurc:read "a") - if not ok then - log.error("Failed to read '.luaurc': %s", content) - return - end - - local aliases = vim.empty_dict() - for alias, value in pairs(content.aliases or {}) do - aliases["@" .. alias] = value - end - return aliases +function M.aliases() + return require("luau-lsp.luaurc").aliases { "lib", "src" } end ---@param opts luau-lsp.Config diff --git a/lua/luau-lsp/log.lua b/lua/luau-lsp/log.lua index 5c5dd56..3b4b7c7 100644 --- a/lua/luau-lsp/log.lua +++ b/lua/luau-lsp/log.lua @@ -40,7 +40,6 @@ end) local function create_logger(level, levelnr) return function(message, ...) message = string.format(message, ...) - write(message, level) if levelnr >= M.level then diff --git a/lua/luau-lsp/luaurc.lua b/lua/luau-lsp/luaurc.lua new file mode 100644 index 0000000..e9585b2 --- /dev/null +++ b/lua/luau-lsp/luaurc.lua @@ -0,0 +1,45 @@ +local json = require "luau-lsp.json" +local log = require "luau-lsp.log" +local util = require "luau-lsp.util" + +local M = {} + +---@param dirs string[] +---@return string? +function M.find_luaurc(dirs) + if util.is_file ".luaurc" then + return ".luaurc" + end + + for _, dir in ipairs(dirs) do + if util.is_file(vim.fs.joinpath(dir, ".luaurc")) then + return vim.fs.joinpath(dir, ".luaurc") + end + end +end + +---@param dirs? string[] +---@return table? +function M.aliases(dirs) + local path = M.find_luaurc(dirs or {}) + if not path then + return + end + + local luaurc = io.open(path, "r") + assert(luaurc) + + local ok, content = pcall(json.decode, luaurc:read "a") + if not ok then + log.error("Failed to read '.luaurc': %s", content) + return + end + + local aliases = vim.empty_dict() + for alias, value in pairs(content.aliases or {}) do + aliases["@" .. alias] = value + end + return aliases +end + +return M diff --git a/spec/luaurc_spec.lua b/spec/luaurc_spec.lua index 814eb7d..fa63213 100644 --- a/spec/luaurc_spec.lua +++ b/spec/luaurc_spec.lua @@ -1,9 +1,19 @@ describe(".luaurc", function() - it("should read .luaurc aliases", function() - local aliases = require("luau-lsp").aliases { + it("should find a .luaurc file", function() + local path = require("luau-lsp.luaurc").find_luaurc { + "spec/roblox-project", "spec/roblox-project/lib", "spec/roblox-project/src", + } + + assert.same("spec/roblox-project/.luaurc", path) + end) + + it("should read .luaurc aliases", function() + local aliases = require("luau-lsp.luaurc").aliases { "spec/roblox-project", + "spec/roblox-project/lib", + "spec/roblox-project/src", } assert.same({