Skip to content

Commit

Permalink
refactor: move luaurc logic to another file
Browse files Browse the repository at this point in the history
  • Loading branch information
lopi-py committed Nov 10, 2024
1 parent a553fea commit 5780255
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 40 deletions.
39 changes: 2 additions & 37 deletions lua/luau-lsp/init.lua
Original file line number Diff line number Diff line change
@@ -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<string, string>?
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
Expand Down
1 change: 0 additions & 1 deletion lua/luau-lsp/log.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 45 additions & 0 deletions lua/luau-lsp/luaurc.lua
Original file line number Diff line number Diff line change
@@ -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<string, string>?
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
14 changes: 12 additions & 2 deletions spec/luaurc_spec.lua
Original file line number Diff line number Diff line change
@@ -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({
Expand Down

0 comments on commit 5780255

Please sign in to comment.