Skip to content

Instantly share code, notes, and snippets.

@svermeulen
Created February 13, 2021 16:58
Show Gist options
  • Save svermeulen/49a3a1c5c79c32915b50108abf8843ba to your computer and use it in GitHub Desktop.
Save svermeulen/49a3a1c5c79c32915b50108abf8843ba to your computer and use it in GitHub Desktop.
How I map moon script line numbers to lua
-- TODO - instead of calculating this every time we should cache the map to file
class MoonScriptLineNumberMap
new: =>
@_fileMapMap = {}
@_jobManager = LazyResolve('JobManager')
_createFileMap: (moonPath) =>
lines = @_jobManager\executeAndWaitGetAllOutputLines("moonc -X '#{moonPath}'")
fileMap = {}
first = true
lastLuaLineNumber = nil
lastMoonLineNumber = nil
for line in *lines
if first
first = false
continue
pos, luaLineNumberStr, luaContents, moonscriptLineNumberStr, moonScriptContents = line\match('^(%d+)%s+(%d+):%[ (.+) %] >> (%d+):%[ (.+) %]')
luaLineNumber = tonumber(luaLineNumberStr)
moonscriptLineNumber = tonumber(moonscriptLineNumberStr)
-- We assume here that when the lua line number skips lines, those lines are associated with the previous moon script line
if lastLuaLineNumber != nil
for i=lastLuaLineNumber+1,luaLineNumber-1
fileMap[i] = lastMoonLineNumber
fileMap[luaLineNumber] = moonscriptLineNumber
lastLuaLineNumber = luaLineNumber
lastMoonLineNumber = moonscriptLineNumber
return fileMap
mapLineNumber: (moonPath, luaLineNumber) =>
fileMap = @_fileMapMap[moonPath]
if not fileMap
fileMap = @\_createFileMap(moonPath)
@_fileMapMap[moonPath] = fileMap
lineNo = fileMap[luaLineNumber]
if not lineNo
sv.log.debug("Unable to map lua line '#{luaLineNumber}' for file '#{moonPath}'")
return 0
return lineNo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment