Skip to content

Commit 9afe7d2

Browse files
committed
Trivial refactor in HistoryTracker
1 parent 732d2e9 commit 9afe7d2

File tree

1 file changed

+15
-44
lines changed

1 file changed

+15
-44
lines changed

src/history/historyTracker.ts

Lines changed: 15 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ class UndoStack {
322322
return step?.marks ?? this.initialMarks;
323323
}
324324

325-
public removeMarks(marks?: string[]): void {
325+
public removeMarks(marks?: readonly string[]): void {
326326
const step = this.getCurrentHistoryStep();
327327
if (marks === undefined) {
328328
if (step) {
@@ -345,10 +345,7 @@ class ChangeList {
345345
private index: number | undefined;
346346

347347
public addChangePosition(position: Position) {
348-
if (
349-
this.changeLocations.length > 0 &&
350-
this.changeLocations[this.changeLocations.length - 1].line === position.line
351-
) {
348+
if (this.changeLocations.at(-1)?.line === position.line) {
352349
this.changeLocations[this.changeLocations.length - 1] = position;
353350
} else {
354351
this.changeLocations.push(position);
@@ -363,13 +360,12 @@ class ChangeList {
363360
return VimError.ChangeListIsEmpty();
364361
}
365362
this.index = this.changeLocations.length - 1;
366-
return this.changeLocations[this.index];
367363
} else if (this.index < this.changeLocations.length - 1) {
368364
this.index++;
369-
return this.changeLocations[this.index];
370365
} else {
371366
return VimError.AtEndOfChangeList();
372367
}
368+
return this.changeLocations[this.index];
373369
}
374370

375371
public prevChangePosition(): Position | VimError {
@@ -378,24 +374,23 @@ class ChangeList {
378374
return VimError.ChangeListIsEmpty();
379375
}
380376
this.index = this.changeLocations.length - 1;
381-
return this.changeLocations[this.index];
382377
} else if (this.index > 0) {
383378
this.index--;
384-
return this.changeLocations[this.index];
385379
} else {
386380
return VimError.AtStartOfChangeList();
387381
}
382+
return this.changeLocations[this.index];
388383
}
389384
}
390385

391386
export class HistoryTracker {
392-
public currentContentChanges: vscode.TextDocumentContentChangeEvent[];
387+
public currentContentChanges: vscode.TextDocumentContentChangeEvent[] = [];
393388

394389
private nextStepCursorsAtStart: readonly Cursor[] | undefined;
395390

396-
private readonly undoStack: UndoStack;
391+
private readonly undoStack = new UndoStack();
397392

398-
private readonly changeList: ChangeList;
393+
private readonly changeList = new ChangeList();
399394

400395
/**
401396
* The state of the document the last time HistoryTracker.addChange() or HistoryTracker.ignoreChange() was called.
@@ -410,13 +405,10 @@ export class HistoryTracker {
410405

411406
constructor(vimState: VimState) {
412407
this.vimState = vimState;
413-
this.undoStack = new UndoStack();
414-
this.changeList = new ChangeList();
415408
this.previousDocumentState = {
416409
text: this.getDocumentText(),
417410
versionNumber: this.getDocumentVersion(),
418411
};
419-
this.currentContentChanges = [];
420412
}
421413

422414
private getDocumentText(): string {
@@ -653,7 +645,7 @@ export class HistoryTracker {
653645
/**
654646
* Removes all marks matching from either the global or local array.
655647
*/
656-
public removeMarks(markNames: string[]): void {
648+
public removeMarks(markNames: readonly string[]): void {
657649
if (markNames.length === 0) {
658650
return;
659651
}
@@ -669,18 +661,18 @@ export class HistoryTracker {
669661
* Gets all local marks. I.e., marks that are specific for the current
670662
* editor.
671663
*/
672-
public getLocalMarks(): ILocalMark[] {
673-
return [...this.undoStack.getCurrentMarkList().filter((mark) => !mark.isUppercaseMark)];
664+
public getLocalMarks(): readonly ILocalMark[] {
665+
return this.undoStack.getCurrentMarkList().filter((mark) => !mark.isUppercaseMark);
674666
}
675667

676668
/**
677669
* Gets all global marks. I.e., marks that are shared among all editors.
678670
*/
679-
public getGlobalMarks(): IMark[] {
680-
return [...HistoryStep.globalMarks];
671+
public getGlobalMarks(): readonly IMark[] {
672+
return HistoryStep.globalMarks;
681673
}
682674

683-
public getMarks(): IMark[] {
675+
public getMarks(): readonly IMark[] {
684676
return [...this.getLocalMarks(), ...HistoryStep.globalMarks];
685677
}
686678

@@ -960,36 +952,15 @@ export class HistoryTracker {
960952
* the most recent text change.
961953
*/
962954
public getLastChangeEndPosition(): Position | undefined {
963-
const currentHistoryStep = this.undoStack.getCurrentHistoryStep();
964-
if (currentHistoryStep === undefined) {
965-
return undefined;
966-
}
967-
968-
const lastChangeIndex = currentHistoryStep.changes.length;
969-
if (lastChangeIndex === 0) {
970-
return undefined;
971-
}
972-
973-
const lastChange = currentHistoryStep.changes[lastChangeIndex - 1];
974-
return lastChange.afterRange.end;
955+
return this.undoStack.getCurrentHistoryStep()?.changes.at(-1)?.afterRange.end;
975956
}
976957

977958
public getLastHistoryStartPosition(): Position | undefined {
978959
return this.undoStack.getCurrentHistoryStep()?.cursorsAtStart?.[0]?.start;
979960
}
980961

981962
private getLastChangeStartPosition(): Position | undefined {
982-
const currentHistoryStep = this.undoStack.getCurrentHistoryStep();
983-
if (currentHistoryStep === undefined) {
984-
return undefined;
985-
}
986-
987-
const changes = currentHistoryStep.changes;
988-
if (changes.length === 0) {
989-
return undefined;
990-
}
991-
992-
return changes[changes.length - 1].start;
963+
return this.undoStack.getCurrentHistoryStep()?.changes.at(-1)?.start;
993964
}
994965

995966
/**

0 commit comments

Comments
 (0)