import { SourceMap } from "./sourcemap" ; import { Path } from "./path" ; export interface Breakpoint extends LuaDebug . Breakpoint { sourceFile ? : string ; sourceLine ? : number ; sourceMap ? : SourceMap ; } export namespace Breakpoint { export interface BreakpointSet { [ line ] : Breakpoint [ ] | undefined ; } const current = { } ; let count = 0 ; export function get ( file , line ) { file = Path . format ( file ) ; for ( const [ breakpointLine , lineBreakpoints ] of pairs ( current ) ) { for ( const breakpoint of lineBreakpoints ) { if ( breakpoint . sourceMap ) { if ( breakpoint . sourceLine === line && breakpoint . sourceFile === file ) { return breakpoint ; } } else if ( breakpointLine === line && breakpoint . file === file ) { return breakpoint ; } } } } export function getLookup ( ) { return current ; } export function getAll ( ) { const breakpointList = [ ] ; for ( const [ _ , lineBreakpoints ] of pairs ( current ) ) { for ( const breakpoint of lineBreakpoints ) { table . insert ( breakpointList , breakpoint ) ; } } return breakpointList ; } export function add ( file , line , condition ? ) { file = Path . format ( file ) ; let sourceFile ; let sourceLine ; const [ scriptFile , sourceMap ] = SourceMap . find ( file ) ; if ( scriptFile && sourceMap ) { for ( const [ scriptLine , mapping ] of pairs ( sourceMap . mappings ) ) { if ( mapping . sourceLine === line ) { sourceFile = file ; file = scriptFile ; sourceLine = line ; line = scriptLine ; break ; } } } let lineBreakpoints = current [ line ] ; if ( ! lineBreakpoints ) { lineBreakpoints = [ ] ; current [ line ] = lineBreakpoints ; } table . insert ( lineBreakpoints , { file , line , enabled : true , condition , sourceFile , sourceLine , sourceMap } ) ; ++ count ; } function removeBreakpoint ( breakpointLine , lineBreakpoints , i ) { table . remove ( lineBreakpoints , i ) ; if ( lineBreakpoints . length === 0 ) { current [ breakpointLine ] = undefined ; } -- count ; } export function remove ( file , line ) { file = Path . format ( file ) ; for ( const [ breakpointLine , lineBreakpoints ] of pairs ( current ) ) { for ( const [ i , breakpoint ] of ipairs ( lineBreakpoints ) ) { if ( breakpoint . sourceMap ) { if ( breakpoint . sourceLine === line && breakpoint . sourceFile === file ) { removeBreakpoint ( breakpointLine , lineBreakpoints , i ) ; return ; } } else if ( breakpointLine === line && breakpoint . file === file ) { removeBreakpoint ( breakpointLine , lineBreakpoints , i ) ; return ; } } } } export function clear ( ) { for ( const [ line ] of pairs ( current ) ) { current [ line ] = undefined ; } count = 0 ; } export function getCount ( ) { return count ; } }