import * as Utils from "../../Utils" ; import { Range } from "../../VimStyle" ; import { AbstractTextObjectSelection } from "./AbstractTextObjectSelection" ; const INNER = 0 ; const OUTER = 1 ; /**
 * ci[ ci( ci{ ci<
 * ca[ ca( ca{ ca<
 */ export class BrancketSelection extends AbstractTextObjectSelection { public LeftBrancket : string ; public RightBrancket : string ; public InnerOuter : number ; constructor ( ) { super ( ) ; } public CalculateRange ( editor , vim , start ) { let left = this . SearchLeftBrancket ( editor , vim , start ) ; let right = this . SearchRightBrancket ( editor , vim , start ) ; if ( left == null || right == null ) { return null ; } if ( this . InnerOuter === INNER ) { left . Char ++ ; } else { right . Char ++ ; } let result = new Range ( ) ; result . start = left ; result . end = right ; return result ; } public SearchRightBrancket ( editor , vim , start ) { let p = start . Copy ( ) ; let line = editor . ReadLineAtCurrentPosition ( ) ; let lastLine = editor . GetLastLineNum ( ) ; let count = this . Count ; while ( count > 0 ) { p . Char ++ ; if ( p . Char >= line . length ) { p . Line ++ ; if ( p . Line > lastLine ) { return null ; } line = editor . ReadLine ( p . Line ) ; if ( line . length === 0 ) { continue ; } p . Char = 0 ; } let c = line [ p . Char ] ; if ( c === this . LeftBrancket ) { count ++ ; } if ( c === this . RightBrancket ) { count -- ; } if ( count === 0 ) { break ; } } return p ; } private SearchLeftBrancket ( editor , vim , start ) { let p = start . Copy ( ) ; let line = editor . ReadLineAtCurrentPosition ( ) ; let lastLine = editor . GetLastLineNum ( ) ; let count = this . Count ; while ( this . Count > 0 ) { p . Char -- ; if ( p . Char < 0 ) { p . Line -- ; if ( p . Line < 0 ) { return null ; } line = editor . ReadLine ( p . Line ) ; if ( line . length === 0 ) { continue ; } p . Char = line . length - 1 ; } let c = line [ p . Char ] ; if ( c === this . LeftBrancket ) { count -- ; } if ( c === this . RightBrancket ) { count ++ ; } if ( count === 0 ) { break ; } } return p ; } } export function AddInnerUnclosedParenthesisSelection ( num , action ) { let m = new BrancketSelection ( ) ; m . LeftBrancket = "(" ; m . RightBrancket = ")" ; m . InnerOuter = INNER ; m . Count = num > 0 ? num : 1 ; let a = < IRequireMotionAction > action ; a . Selection = m ; } export function AddOuterUnclosedParenthesisSelection ( num , action ) { let m = new BrancketSelection ( ) ; m . LeftBrancket = "(" ; m . RightBrancket = ")" ; m . InnerOuter = OUTER ; m . Count = num > 0 ? num : 1 ; let a = < IRequireMotionAction > action ; a . Selection = m ; } export function AddInnerLessThanSignSelection ( num , action ) { let m = new BrancketSelection ( ) ; m . LeftBrancket = "<" ; m . RightBrancket = ">" ; m . InnerOuter = INNER ; m . Count = num > 0 ? num : 1 ; let a = < IRequireMotionAction > action ; a . Selection = m ; } export function AddOuterLessThanSignSelection ( num , action ) { let m = new BrancketSelection ( ) ; m . LeftBrancket = "<" ; m . RightBrancket = ">" ; m . InnerOuter = OUTER ; m . Count = num > 0 ? num : 1 ; let a = < IRequireMotionAction > action ; a . Selection = m ; } export function AddInnerSquareBlancketSelection ( num , action ) { let m = new BrancketSelection ( ) ; m . LeftBrancket = "[" ; m . RightBrancket = "]" ; m . InnerOuter = INNER ; m . Count = num > 0 ? num : 1 ; let a = < IRequireMotionAction > action ; a . Selection = m ; } export function AddOuterSquareBlancketSelection ( num , action ) { let m = new BrancketSelection ( ) ; m . LeftBrancket = "[" ; m . RightBrancket = "]" ; m . InnerOuter = OUTER ; m . Count = num > 0 ? num : 1 ; let a = < IRequireMotionAction > action ; a . Selection = m ; } export function AddInnerCurlyBrancketSelection ( num , action ) { let m = new BrancketSelection ( ) ; m . LeftBrancket = "{" ; m . RightBrancket = "}" ; m . InnerOuter = INNER ; m . Count = num > 0 ? num : 1 ; let a = < IRequireMotionAction > action ; a . Selection = m ; } export function AddOuterCurlyBrancketSelection ( num , action ) { let m = new BrancketSelection ( ) ; m . LeftBrancket = "{" ; m . RightBrancket = "}" ; m . InnerOuter = OUTER ; m . Count = num > 0 ? num : 1 ; let a = < IRequireMotionAction > action ; a . Selection = m ; }