import { GoAction } from "../action/GoAction" ; import * as Utils from "../Utils" ; import { Position } from "../VimStyle" ; import { AbstractMotion } from "./AbstractMotion" ; /**
 * [{ [( ]} ])
 */ export class BackToBrancketMotion extends AbstractMotion { public LeftBrancket : string ; public RightBrancket : string ; public TargetBrancket : string ; constructor ( ) { super ( ) ; } public CalculateEnd ( editor , vim , start ) { let p = start . Copy ( ) ; let line = editor . ReadLineAtCurrentPosition ( ) ; let lastLine = editor . GetLastLineNum ( ) ; let lastBrancket = null ; while ( this . Count > 0 ) { p . Char -- ; if ( p . Char < 0 ) { p . Line -- ; if ( p . Line < 0 ) { p = new Position ( 0 , 0 ) ; break ; } line = editor . ReadLine ( p . Line ) ; if ( line . length === 0 ) { continue ; } p . Char = line . length - 1 ; } let c = line [ p . Char ] ; if ( c === this . LeftBrancket ) { lastBrancket = p . Copy ( ) ; this . Count -- ; } if ( c === this . RightBrancket ) { this . Count ++ ; } if ( this . Count === 0 && c === this . TargetBrancket ) { break ; } } if ( lastBrancket == null ) { return start . Copy ( ) ; } return lastBrancket ; } } export class ToBrancketMotion extends AbstractMotion { public LeftBrancket : string ; public RightBrancket : string ; public TargetBrancket : string ; constructor ( ) { super ( ) ; } public CalculateEnd ( editor , vim , start ) { let p = start . Copy ( ) ; let line = editor . ReadLineAtCurrentPosition ( ) ; let lastLine = editor . GetLastLineNum ( ) ; let lastBrancket = null ; while ( this . Count > 0 ) { p . Char ++ ; if ( p . Char >= line . length ) { p . Line ++ ; if ( p . Line > lastLine ) { break ; } line = editor . ReadLine ( p . Line ) ; if ( line . length === 0 ) { continue ; } p . Char = 0 ; } let c = line [ p . Char ] ; if ( c === this . LeftBrancket ) { this . Count ++ ; } if ( c === this . RightBrancket ) { lastBrancket = p . Copy ( ) ; this . Count -- ; } if ( this . Count === 0 && c === this . TargetBrancket ) { break ; } } if ( lastBrancket == null ) { return start . Copy ( ) ; } return lastBrancket ; } } export function GoBackToUnclosedLeftParenthesis ( num ) { let a = new GoAction ( ) ; let m = new BackToBrancketMotion ( ) ; m . LeftBrancket = "(" ; m . RightBrancket = ")" ; m . TargetBrancket = "(" ; m . Count = num > 0 ? num : 1 ; a . Motion = m ; return a ; } export function AddBackToUnclosedLeftParenthesisMotion ( num , action ) { let m = new BackToBrancketMotion ( ) ; m . LeftBrancket = "(" ; m . RightBrancket = ")" ; m . TargetBrancket = "(" ; m . Count = num > 0 ? num : 1 ; let a = < IRequireMotionAction > action ; a . Motion = m ; } export function GoBackToUnclosedLeftCurlyBracket ( num ) { let a = new GoAction ( ) ; let m = new BackToBrancketMotion ( ) ; m . LeftBrancket = "{" ; m . RightBrancket = "}" ; m . TargetBrancket = "{" ; m . Count = num > 0 ? num : 1 ; a . Motion = m ; return a ; } export function AddBackToUnclosedLeftCurlyBracketMotion ( num , action ) { let m = new BackToBrancketMotion ( ) ; m . LeftBrancket = "{" ; m . RightBrancket = "}" ; m . TargetBrancket = "{" ; m . Count = num > 0 ? num : 1 ; let a = < IRequireMotionAction > this . action ; a . Motion = m ; } export function GoToUnclosedRightParenthesis ( num ) { let a = new GoAction ( ) ; let m = new ToBrancketMotion ( ) ; m . LeftBrancket = "(" ; m . RightBrancket = ")" ; m . TargetBrancket = ")" ; m . Count = num > 0 ? num : 1 ; a . Motion = m ; return a ; } export function AddToUnclosedRightParenthesisMotion ( num , action ) { let m = new ToBrancketMotion ( ) ; m . LeftBrancket = "(" ; m . RightBrancket = ")" ; m . TargetBrancket = ")" ; m . Count = num > 0 ? num : 1 ; let a = < IRequireMotionAction > action ; a . Motion = m ; } export function GoToUnclosedRightCurlyBracket ( num ) { let a = new GoAction ( ) ; let m = new ToBrancketMotion ( ) ; m . LeftBrancket = "{" ; m . RightBrancket = "}" ; m . TargetBrancket = "}" ; m . Count = num > 0 ? num : 1 ; a . Motion = m ; return a ; } export function AddToUnclosedRightCurlyBracketMotion ( num , action ) { let m = new ToBrancketMotion ( ) ; m . LeftBrancket = "{" ; m . RightBrancket = "}" ; m . TargetBrancket = "}" ; m . Count = num > 0 ? num : 1 ; let a = < IRequireMotionAction > action ; a . Motion = m ; }