Skip to content

Commit 6402d08

Browse files
committed
Cleanup
1 parent 14c8d71 commit 6402d08

1 file changed

Lines changed: 33 additions & 22 deletions

File tree

src/luau-lsp-plugin.luau

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,48 @@ local function IsLibrary(Uri: Uri): boolean
1212
return true
1313
end
1414

15+
local function NormalizePath(Path: string)
16+
return string.gsub(Path, "[/\\]", "/")
17+
end
18+
19+
local function GetParentDirectory(Path: string)
20+
return string.gsub(Path, "[/\\][^/\\]-$", "")
21+
end
22+
23+
local function GetRelativePath(Path: string, RelativeTo: string)
24+
Path = NormalizePath(Path)
25+
RelativeTo = NormalizePath(RelativeTo)
26+
27+
if string.find(Path, "^/?%a:") then
28+
Path = string.sub(Path, 2)
29+
end
30+
31+
for _, Directory in ipairs(string.split(RelativeTo, "/")) do
32+
if string.find(Path, `{Directory}/`, 1, true) == 1 then
33+
Path = string.sub(Path, #Directory + 2)
34+
else
35+
Path = `../{Path}`
36+
end
37+
end
38+
39+
Path = `./{Path}`
40+
Path = string.gsub(Path, "%./%.%.", "..")
41+
42+
return Path
43+
end
44+
1545
local function GetLibraries(RelativeTo: string, _Libraries, _Uri: Uri?)
1646
local Libraries = (_Libraries or {})
1747
local Uri = (_Uri or lsp.workspace.getRootUri())
1848

19-
RelativeTo = string.gsub(RelativeTo, "[/\\]", "/")
49+
RelativeTo = NormalizePath(RelativeTo)
2050

2151
for _, Entry in lsp.fs.listDirectory(Uri) do
2252
local Name = string.sub(Entry:toString(), #Uri:toString() + 2)
2353
local Library = string.match(Name, "(.*)%.meta%.json$")
2454

2555
if Library and IsLibrary(Entry) then
26-
local FixedPath = Entry.path
27-
28-
FixedPath = string.gsub(FixedPath, "[/\\]", "/")
29-
30-
if string.find(FixedPath, "^/?%a:") then
31-
FixedPath = string.sub(FixedPath, 2)
32-
end
33-
34-
for _, Directory in ipairs(string.split(RelativeTo, "/")) do
35-
if string.find(FixedPath, `{Directory}/`, 1, true) == 1 then
36-
FixedPath = string.sub(FixedPath, #Directory + 2)
37-
else
38-
FixedPath = `../{FixedPath}`
39-
end
40-
end
41-
42-
FixedPath = `./{FixedPath}`
43-
FixedPath = string.gsub(FixedPath, "%./%.%.", "..")
44-
45-
Libraries[Library] = (string.sub(FixedPath, 1, -#Name - 1) .. Library)
56+
Libraries[Library] = (string.sub(GetRelativePath(Entry.path, RelativeTo), 1, -#Name - 1) .. Library)
4657
else
4758
pcall(GetLibraries, RelativeTo, Libraries, Entry)
4859
end
@@ -56,7 +67,7 @@ return {
5667
local Changes = {}
5768
local Line, LineStart = 1, 1
5869

59-
local Libraries = GetLibraries((string.gsub(Context.filePath, "[/\\][^/\\]-$", "")))
70+
local Libraries = GetLibraries(GetParentDirectory(Context.filePath))
6071

6172
for Index = 1, #Source do
6273
if string.sub(Source, Index, Index) == "\n" then

0 commit comments

Comments
 (0)