Skip to content

Commit fcd799f

Browse files
committed
Retrieve paths from sourcemap
1 parent 429cdb7 commit fcd799f

1 file changed

Lines changed: 58 additions & 2 deletions

File tree

src/luau-lsp-plugin.luau

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,54 @@ local function GetRelativePath(Path: string, RelativeTo: string)
4242
return Path
4343
end
4444

45+
type Sourcemap = {name: string, className: string, filePaths: {string}?, children: {Sourcemap}?}
46+
47+
local function GetSourcemap(): Sourcemap?
48+
local RootUri = lsp.workspace.getRootUri()
49+
local SourcemapUri = RootUri:joinPath("sourcemap.json")
50+
lsp.client.sendLogMessage("log", `Sourcemap at: {SourcemapUri:toString()}`)
51+
52+
local ReadSuccess, Content = pcall(lsp.fs.readFile, SourcemapUri)
53+
if not ReadSuccess then lsp.client.sendLogMessage("log", `Sourcemap could not be read: {Content}`); return nil end
54+
55+
local DecodeSuccess, Sourcemap = pcall(lsp.json.deserialize, Content)
56+
if not DecodeSuccess then lsp.client.sendLogMessage("warning", `Sourcemap could not be decoded: {Sourcemap}`); return nil end
57+
58+
return Sourcemap
59+
end
60+
61+
local function _GetLibrariesFromSourcemap(_Libraries, _Sourcemap: Sourcemap?, RobloxPath: string?)
62+
local Libraries = (_Libraries or {})
63+
local Sourcemap = (_Sourcemap or GetSourcemap())
64+
65+
if not Sourcemap then
66+
return
67+
end
68+
69+
if Sourcemap.className == "ModuleScript" and Sourcemap.filePaths then
70+
local MetaFile, SourceFile
71+
72+
for _, FilePath in Sourcemap.filePaths do
73+
if string.find(FilePath, ".*%.meta%.json$") then
74+
MetaFile = FilePath
75+
else
76+
SourceFile = FilePath
77+
end
78+
end
79+
80+
if MetaFile and SourceFile and IsLibrary(lsp.workspace.getRootUri():joinPath(MetaFile)) then
81+
Libraries[Sourcemap.name] = `{RobloxPath}/{Sourcemap.name}`
82+
end
83+
end
84+
85+
if Sourcemap.children then
86+
for _, Child in Sourcemap.children do
87+
_GetLibrariesFromSourcemap(Libraries, Child, (RobloxPath and `{RobloxPath}/{Sourcemap.name}` or "@game"))
88+
end
89+
end
90+
91+
return Libraries
92+
end
4593

4694
local function _GetLibrariesFromRoot(_Libraries, _Uri: Uri?)
4795
local Libraries = (_Libraries or {})
@@ -68,11 +116,15 @@ local function GetLibraries()
68116
return LibraryCache
69117
end
70118

71-
local Libraries = _GetLibrariesFromRoot()
119+
local Libraries = (_GetLibrariesFromSourcemap() or _GetLibrariesFromRoot())
72120

73121
LibraryCache = Libraries
74122
LibraryCacheExpiration = (os.clock() + 5)
75123

124+
lsp.client.sendLogMessage("log", `Library log start`)
125+
for i, v in Libraries do lsp.client.sendLogMessage("log", `\tLibrary log: {i} = {v}`) end
126+
lsp.client.sendLogMessage("log", `Library log end`)
127+
76128
return Libraries
77129
end
78130

@@ -85,7 +137,11 @@ return {
85137
local Parent = GetParentDirectory(Context.filePath)
86138

87139
for Index, Library in GetLibraries() do
88-
Libraries[Index] = GetRelativePath(Library, Parent)
140+
if string.find(Library, "^@game") then
141+
Libraries[Index] = Library
142+
else
143+
Libraries[Index] = GetRelativePath(Library, Parent)
144+
end
89145
end
90146

91147
for Index = 1, #Source do

0 commit comments

Comments
 (0)