Skip to content

Instantly share code, notes, and snippets.

Created February 13, 2021 16:58
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
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
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