@@ -42,6 +42,9 @@ export class LineCursor extends Marker {
4242 /** Locations to try moving the cursor to after a deletion. */
4343 private potentialNodes : IFocusableNode [ ] | null = null ;
4444
45+ /** Whether or not navigation loops around when reaching the end. */
46+ private navigationLoops = true ;
47+
4548 /**
4649 * @param workspace The workspace this cursor belongs to.
4750 */
@@ -64,7 +67,7 @@ export class LineCursor extends Marker {
6467 const newNode = this . getNextNode (
6568 curNode ,
6669 this . getValidationFunction ( NavigationDirection . NEXT ) ,
67- true ,
70+ this . getNavigationLoops ( ) ,
6871 ) ;
6972
7073 if ( newNode ) {
@@ -89,7 +92,7 @@ export class LineCursor extends Marker {
8992 const newNode = this . getNextNode (
9093 curNode ,
9194 this . getValidationFunction ( NavigationDirection . IN ) ,
92- true ,
95+ this . getNavigationLoops ( ) ,
9396 ) ;
9497
9598 if ( newNode ) {
@@ -112,7 +115,7 @@ export class LineCursor extends Marker {
112115 const newNode = this . getPreviousNode (
113116 curNode ,
114117 this . getValidationFunction ( NavigationDirection . PREVIOUS ) ,
115- true ,
118+ this . getNavigationLoops ( ) ,
116119 ) ;
117120
118121 if ( newNode ) {
@@ -137,7 +140,7 @@ export class LineCursor extends Marker {
137140 const newNode = this . getPreviousNode (
138141 curNode ,
139142 this . getValidationFunction ( NavigationDirection . OUT ) ,
140- true ,
143+ this . getNavigationLoops ( ) ,
141144 ) ;
142145
143146 if ( newNode ) {
@@ -158,12 +161,12 @@ export class LineCursor extends Marker {
158161 const inNode = this . getNextNode (
159162 curNode ,
160163 this . getValidationFunction ( NavigationDirection . IN ) ,
161- true ,
164+ this . getNavigationLoops ( ) ,
162165 ) ;
163166 const nextNode = this . getNextNode (
164167 curNode ,
165168 this . getValidationFunction ( NavigationDirection . NEXT ) ,
166- true ,
169+ this . getNavigationLoops ( ) ,
167170 ) ;
168171
169172 return inNode === nextNode ;
@@ -219,11 +222,22 @@ export class LineCursor extends Marker {
219222 getNextNode (
220223 node : IFocusableNode | null ,
221224 isValid : ( p1 : IFocusableNode | null ) => boolean ,
225+ // TODO: Consider deprecating and removing this argument.
222226 loop : boolean ,
223227 ) : IFocusableNode | null {
224- if ( ! node || ( ! loop && this . getLastNode ( ) === node ) ) return null ;
228+ const originalLoop = this . getNavigationLoops ( ) ;
229+ this . setNavigationLoops ( loop ) ;
230+
231+ let result : IFocusableNode | null ;
232+ if ( ! node || ( ! loop && this . getLastNode ( ) === node ) ) {
233+ result = null ;
234+ } else {
235+ result = this . getNextNodeImpl ( node , isValid ) ;
236+ }
225237
226- return this . getNextNodeImpl ( node , isValid ) ;
238+ this . setNavigationLoops ( originalLoop ) ;
239+
240+ return result ;
227241 }
228242
229243 /**
@@ -273,11 +287,22 @@ export class LineCursor extends Marker {
273287 getPreviousNode (
274288 node : IFocusableNode | null ,
275289 isValid : ( p1 : IFocusableNode | null ) => boolean ,
290+ // TODO: Consider deprecating and removing this argument.
276291 loop : boolean ,
277292 ) : IFocusableNode | null {
278- if ( ! node || ( ! loop && this . getFirstNode ( ) === node ) ) return null ;
293+ const originalLoop = this . getNavigationLoops ( ) ;
294+ this . setNavigationLoops ( loop ) ;
295+
296+ let result : IFocusableNode | null ;
297+ if ( ! node || ( ! loop && this . getFirstNode ( ) === node ) ) {
298+ result = null ;
299+ } else {
300+ result = this . getPreviousNodeImpl ( node , isValid ) ;
301+ }
279302
280- return this . getPreviousNodeImpl ( node , isValid ) ;
303+ this . setNavigationLoops ( originalLoop ) ;
304+
305+ return result ;
281306 }
282307
283308 /**
@@ -538,6 +563,24 @@ export class LineCursor extends Marker {
538563 const first = this . getFirstNode ( ) ;
539564 return this . getPreviousNode ( first , ( ) => true , true ) ;
540565 }
566+
567+ /**
568+ * Sets whether or not navigation should loop around when reaching the end
569+ * of the workspace.
570+ *
571+ * @param loops True if navigation should loop around, otherwise false.
572+ */
573+ setNavigationLoops ( loops : boolean ) {
574+ this . navigationLoops = loops ;
575+ }
576+
577+ /**
578+ * Returns whether or not navigation loops around when reaching the end of
579+ * the workspace.
580+ */
581+ getNavigationLoops ( ) : boolean {
582+ return this . navigationLoops ;
583+ }
541584}
542585
543586registry . register ( registry . Type . CURSOR , registry . DEFAULT , LineCursor ) ;
0 commit comments