import { TextEditor } from "vscode" ; export class TextEditorHelperReturnType { public pos : number ; public mark : string ; public constructor ( _pos , _mark ) { this . pos = _pos ; this . mark = _mark ; } } export class TextEditorHelper { public static NoneTextUtilReturnTypeValue : TextEditorHelperReturnType = new TextEditorHelperReturnType ( - 1 , "" ) ; public static indexOf ( editor , startPos , marks ) { if ( ! editor || ! editor . document ) { return TextEditorHelper . NoneTextUtilReturnTypeValue ; } let text = editor . document . getText ( ) ; var closestPosOfMarks = - 1 ; var isAnyMarkFound = false ; var closestMarkOfMarks = "" ; marks . forEach ( mark => { let startPosOfMark = text . indexOf ( mark , startPos ) ; if ( startPosOfMark < 0 ) { return ; } if ( ! isAnyMarkFound || startPosOfMark < closestPosOfMarks ) { isAnyMarkFound = true ; closestPosOfMarks = startPosOfMark ; closestMarkOfMarks = mark ; return ; } } ) ; return new TextEditorHelperReturnType ( closestPosOfMarks , closestMarkOfMarks ) ; } public static lastIndexOf ( editor , startPos , marks ) { if ( ! editor || ! editor . document ) { return TextEditorHelper . NoneTextUtilReturnTypeValue ; } let text = editor . document . getText ( ) ; var closestPosOfMarks = - 1 ; var closestMarkOfMarks = "" ; var isAnyMarkFound = false ; marks . forEach ( mark => { let startPosOfMark = text . lastIndexOf ( mark , startPos ) ; if ( startPosOfMark < 0 ) { return ; } if ( ! isAnyMarkFound || startPosOfMark > closestPosOfMarks ) { isAnyMarkFound = true ; closestPosOfMarks = startPosOfMark ; closestMarkOfMarks = mark ; return ; } } ) ; return new TextEditorHelperReturnType ( closestPosOfMarks , closestMarkOfMarks ) ; } public static regexIndexOf ( editor , startPos , regex ) { if ( ! editor || ! editor . document ) { return TextEditorHelper . NoneTextUtilReturnTypeValue ; } let text = editor . document . getText ( ) ; regex = ( regex . global ) ? regex : new RegExp ( regex . source , "g" + ( regex . ignoreCase ? "i" : "" ) + ( regex . multiline ? "m" : "" ) ) ; startPos = startPos || 0 ; var stringToWorkWith = text . substring ( startPos ) ; var closestPosOfMarks = - 1 ; var closestMarkOfMarks = "" ; var result = null ; if ( ( result = regex . exec ( stringToWorkWith ) ) != null ) { closestMarkOfMarks = result [ 0 ] ; closestPosOfMarks = result . index + startPos ; } return new TextEditorHelperReturnType ( closestPosOfMarks , closestMarkOfMarks ) ; } public static regexLastIndexOf ( editor , startPos , regex ) { if ( ! editor || ! editor . document ) { return TextEditorHelper . NoneTextUtilReturnTypeValue ; } let text = editor . document . getText ( ) ; regex = ( regex . global ) ? regex : new RegExp ( regex . source , "g" + ( regex . ignoreCase ? "i" : "" ) + ( regex . multiline ? "m" : "" ) ) ; if ( typeof ( startPos ) == "undefined" ) { startPos = text . length ; } else if ( startPos < 0 ) { startPos = 0 ; } var stringToWorkWith = text . substring ( 0 , startPos + 1 ) ; var closestPosOfMarks = - 1 ; var nextStop = 0 ; var result = null ; var closestMarkOfMarks = "" ; while ( ( result = regex . exec ( stringToWorkWith ) ) != null ) { closestPosOfMarks = result . index ; closestMarkOfMarks = result [ 0 ] ; regex . lastIndex = ++ nextStop ; } return new TextEditorHelperReturnType ( closestPosOfMarks , closestMarkOfMarks ) ; } public static format ( str ) { var args = arguments ; var pattern = new RegExp ( "%([1-" + arguments . length + "])" , "g" ) ; return String ( str ) . replace ( pattern , function ( _match , index ) { return args [ index ] ; } ) ; } ; param { String } currVer param { String } promoteVer return { Boolean } public static versionCompare ( curVer , promoteVer ) { curVer = curVer || "0.0.0" ; promoteVer = promoteVer || "0.0.0" ; curVer = curVer . trim ( ) ; promoteVer = promoteVer . trim ( ) ; if ( curVer == promoteVer ) { return 0 ; } let curVerArray = curVer . split ( "." ) ; let promoteVerArr = promoteVer . split ( "." ) ; let len = Math . max ( curVerArray . length , promoteVerArr . length ) ; for ( let i = 0 ; i < len ; i ++ ) { let curSubVer = curVerArray [ i ] . trim ( ) ; let promoteVer = promoteVerArr [ i ] . trim ( ) ; let curSubVerNo = ~ ~ curSubVer ; let promoteVerNo = ~ ~ promoteVer ; if ( curSubVerNo < promoteVerNo ) { return - 1 ; } else if ( curSubVerNo > promoteVerNo ) { return 1 ; } } return 0 ; } }