Skip to content

Commit

Permalink
Don't track selections if tracking is not needed (#42317)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdima committed Apr 25, 2018
1 parent 6091135 commit 9cbc162
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/vs/editor/common/controller/cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,12 @@ export class Cursor extends viewEvents.ViewEventEmitter implements ICursors {
const oldState = new CursorModelState(this._model, this);
let cursorChangeReason = CursorChangeReason.NotSet;

if (handlerId !== H.Undo && handlerId !== H.Redo) {
// TODO@Alex: if the undo/redo stack contains non-null selections
// it would also be OK to stop tracking selections here
this._cursors.stopTrackingSelections();
}

// ensure valid state on all cursors
this._cursors.ensureValidState();

Expand Down Expand Up @@ -524,6 +530,10 @@ export class Cursor extends viewEvents.ViewEventEmitter implements ICursors {

this._isHandling = false;

if (handlerId !== H.Undo && handlerId !== H.Redo) {
this._cursors.startTrackingSelections();
}

if (this._emitStateChangedIfNecessary(source, cursorChangeReason, oldState)) {
this._revealRange(RevealTarget.Primary, viewEvents.VerticalRevealType.Simple, true, editorCommon.ScrollType.Smooth);
}
Expand Down
14 changes: 14 additions & 0 deletions src/vs/editor/common/controller/cursorCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ export class CursorCollection {
this.killSecondaryCursors();
}

public startTrackingSelections(): void {
this.primaryCursor.startTrackingSelection(this.context);
for (let i = 0, len = this.secondaryCursors.length; i < len; i++) {
this.secondaryCursors[i].startTrackingSelection(this.context);
}
}

public stopTrackingSelections(): void {
this.primaryCursor.stopTrackingSelection(this.context);
for (let i = 0, len = this.secondaryCursors.length; i < len; i++) {
this.secondaryCursors[i].stopTrackingSelection(this.context);
}
}

public updateContext(context: CursorContext): void {
this.context = context;
}
Expand Down
26 changes: 25 additions & 1 deletion src/vs/editor/common/controller/oneCursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ export class OneCursor {
public viewState: SingleCursorState;

private _selTrackedRange: string;
private _trackSelection: boolean;

constructor(context: CursorContext) {
this.modelState = null;
this.viewState = null;

this._selTrackedRange = null;
this._trackSelection = true;

this._setState(
context,
Expand All @@ -31,6 +33,28 @@ export class OneCursor {
}

public dispose(context: CursorContext): void {
this._removeTrackedRange(context);
}

public startTrackingSelection(context: CursorContext): void {
this._trackSelection = true;
this._updateTrackedRange(context);
}

public stopTrackingSelection(context: CursorContext): void {
this._trackSelection = false;
this._removeTrackedRange(context);
}

private _updateTrackedRange(context: CursorContext): void {
if (!this._trackSelection) {
// don't track the selection
return;
}
this._selTrackedRange = context.model._setTrackedRange(this._selTrackedRange, this.modelState.selection, TrackedRangeStickiness.AlwaysGrowsWhenTypingAtEdges);
}

private _removeTrackedRange(context: CursorContext): void {
this._selTrackedRange = context.model._setTrackedRange(this._selTrackedRange, null, TrackedRangeStickiness.AlwaysGrowsWhenTypingAtEdges);
}

Expand Down Expand Up @@ -96,6 +120,6 @@ export class OneCursor {
this.modelState = modelState;
this.viewState = viewState;

this._selTrackedRange = context.model._setTrackedRange(this._selTrackedRange, this.modelState.selection, TrackedRangeStickiness.AlwaysGrowsWhenTypingAtEdges);
this._updateTrackedRange(context);
}
}

0 comments on commit 9cbc162

Please sign in to comment.