import { IWorkerModel , VAIndent , VAToken } from "./common" ; import { KeywordMatcher } from "./matcher" ; function getIndent ( text , tabSize ) { let indent = 0 ; let length = text . search ( /\S/ ) ; for ( let i = 0 ; i < length ; i ++ ) { if ( text . charAt ( i ) == "\t" ) { indent = indent + tabSize - ( indent % tabSize ) ; } else indent ++ ; } return indent + 1 ; } function getToken ( text ) { if ( /^\s*$/ . test ( text ) ) return VAToken . Empty ; if ( /^[\s]*@/ . test ( text ) ) return VAToken . Instruction ; if ( /^[\s]*\|/ . test ( text ) ) return VAToken . Parameter ; if ( /^[\s]*[#|//]/ . test ( text ) ) return VAToken . Comment ; if ( /^[\s]*"""/ . test ( text ) ) return VAToken . Multiline ; if ( /^[\s]*\*/ . test ( text ) ) return VAToken . Asterisk ; return VAToken . Operator ; } export function getModelTokens ( matcher , model , tabSize ) { const tokens = [ ] ; let multiline = false ; const lineCount = model . getLineCount ( ) ; for ( let lineNumber = 1 ; lineNumber <= lineCount ; lineNumber ++ ) { let text = model . getLineContent ( lineNumber ) ; let token = getToken ( text ) ; if ( multiline ) { if ( token == VAToken . Multiline ) multiline = false ; token = VAToken . Multiline ; } else { if ( token == VAToken . Multiline ) multiline = true ; } if ( token != VAToken . Operator && token != VAToken . Asterisk ) { tokens . push ( { token : token , indent : 0 } ) ; } else { let ident = 0 ; if ( matcher . isSection ( text ) ) token = VAToken . Section ; else ident = getIndent ( text , tabSize ) ; tokens . push ( { token : token , indent : ident } ) ; } } return tokens ; } export class WorkerModel implements IWorkerModel { private tokens : Array < VAIndent > = [ ] ; private content : string [ ] ; private versionId : number ; constructor ( matcher , msg ) { this . content = msg . content ; this . versionId = msg . versionId ; this . tokens = getModelTokens ( matcher , this , msg . tabSize ) ; } getLineContent ( lineNumber ) { return this . content [ lineNumber - 1 ] ; } getLineCount ( ) { return this . content . length ; } getLineToken ( lineNumber ) { return this . tokens [ lineNumber - 1 ] ; } }