Skip to content

Commit 402c641

Browse files
committed
Reduce cursor API surface
1 parent c71daba commit 402c641

2 files changed

Lines changed: 17 additions & 14 deletions

File tree

src/vs/editor/browser/widget/codeEditorWidget.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,15 +1482,15 @@ export class CodeEditorWidget extends Disposable implements editorBrowser.ICodeE
14821482
// Someone might destroy the model from under the editor, so prevent any exceptions by setting a null model
14831483
listenersToRemove.push(model.onWillDispose(() => this.setModel(null)));
14841484

1485-
listenersToRemove.push(viewModel.cursor.onDidReachMaxCursorCount(() => {
1486-
this._notificationService.warn(nls.localize('cursors.maximum', "The number of cursors has been limited to {0}.", Cursor.MAX_CURSOR_COUNT));
1487-
}));
1488-
14891485
listenersToRemove.push(viewModel.cursor.onDidAttemptReadOnlyEdit(() => {
14901486
this._onDidAttemptReadOnlyEdit.fire(undefined);
14911487
}));
14921488

14931489
listenersToRemove.push(viewModel.cursor.onDidChange((e: CursorStateChangedEvent) => {
1490+
if (e.reachedMaxCursorCount) {
1491+
this._notificationService.warn(nls.localize('cursors.maximum', "The number of cursors has been limited to {0}.", Cursor.MAX_CURSOR_COUNT));
1492+
}
1493+
14941494
const positions: Position[] = [];
14951495
for (let i = 0, len = e.selections.length; i < len; i++) {
14961496
positions[i] = e.selections[i].getPosition();

src/vs/editor/common/controller/cursor.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,19 @@ export class CursorStateChangedEvent {
4848
* Reason.
4949
*/
5050
readonly reason: CursorChangeReason;
51+
/**
52+
* The number of cursors was limited because it has reached the maximum cursor count.
53+
*/
54+
readonly reachedMaxCursorCount: boolean;
5155

52-
constructor(selections: Selection[], modelVersionId: number, oldSelections: Selection[] | null, oldModelVersionId: number, source: string, reason: CursorChangeReason) {
56+
constructor(selections: Selection[], modelVersionId: number, oldSelections: Selection[] | null, oldModelVersionId: number, source: string, reason: CursorChangeReason, reachedMaxCursorCount: boolean) {
5357
this.selections = selections;
5458
this.modelVersionId = modelVersionId;
5559
this.oldSelections = oldSelections;
5660
this.oldModelVersionId = oldModelVersionId;
5761
this.source = source;
5862
this.reason = reason;
63+
this.reachedMaxCursorCount = reachedMaxCursorCount;
5964
}
6065
}
6166

@@ -161,9 +166,6 @@ export class Cursor extends viewEvents.ViewEventEmitter implements ICursors {
161166

162167
public static readonly MAX_CURSOR_COUNT = 10000;
163168

164-
private readonly _onDidReachMaxCursorCount: Emitter<void> = this._register(new Emitter<void>());
165-
public readonly onDidReachMaxCursorCount: Event<void> = this._onDidReachMaxCursorCount.event;
166-
167169
private readonly _onDidAttemptReadOnlyEdit: Emitter<void> = this._register(new Emitter<void>());
168170
public readonly onDidAttemptReadOnlyEdit: Event<void> = this._onDidAttemptReadOnlyEdit.event;
169171

@@ -282,9 +284,10 @@ export class Cursor extends viewEvents.ViewEventEmitter implements ICursors {
282284
}
283285

284286
public setStates(source: string | null | undefined, reason: CursorChangeReason, states: PartialCursorState[] | null): boolean {
287+
let reachedMaxCursorCount = false;
285288
if (states !== null && states.length > Cursor.MAX_CURSOR_COUNT) {
286289
states = states.slice(0, Cursor.MAX_CURSOR_COUNT);
287-
this._onDidReachMaxCursorCount.fire(undefined);
290+
reachedMaxCursorCount = true;
288291
}
289292

290293
const oldState = new CursorModelState(this._model, this);
@@ -295,7 +298,7 @@ export class Cursor extends viewEvents.ViewEventEmitter implements ICursors {
295298

296299
this._validateAutoClosedActions();
297300

298-
return this._emitStateChangedIfNecessary(source, reason, oldState);
301+
return this._emitStateChangedIfNecessary(source, reason, oldState, reachedMaxCursorCount);
299302
}
300303

301304
public setColumnSelectData(columnSelectData: IColumnSelectData): void {
@@ -390,7 +393,7 @@ export class Cursor extends viewEvents.ViewEventEmitter implements ICursors {
390393
this._cursors.dispose();
391394
this._cursors = new CursorCollection(this.context);
392395
this._validateAutoClosedActions();
393-
this._emitStateChangedIfNecessary('model', CursorChangeReason.ContentFlush, null);
396+
this._emitStateChangedIfNecessary('model', CursorChangeReason.ContentFlush, null, false);
394397
} else {
395398
if (this._hasFocus && e.resultingSelection && e.resultingSelection.length > 0) {
396399
const cursorState = CursorState.fromModelSelections(e.resultingSelection);
@@ -524,7 +527,7 @@ export class Cursor extends viewEvents.ViewEventEmitter implements ICursors {
524527
// -----------------------------------------------------------------------------------------------------------
525528
// ----- emitting events
526529

527-
private _emitStateChangedIfNecessary(source: string | null | undefined, reason: CursorChangeReason, oldState: CursorModelState | null): boolean {
530+
private _emitStateChangedIfNecessary(source: string | null | undefined, reason: CursorChangeReason, oldState: CursorModelState | null, reachedMaxCursorCount: boolean): boolean {
528531
const newState = new CursorModelState(this._model, this);
529532
if (newState.equals(oldState)) {
530533
return false;
@@ -543,7 +546,7 @@ export class Cursor extends viewEvents.ViewEventEmitter implements ICursors {
543546
) {
544547
const oldSelections = oldState ? oldState.cursorState.map(s => s.modelState.selection) : null;
545548
const oldModelVersionId = oldState ? oldState.modelVersionId : 0;
546-
this._onDidChange.fire(new CursorStateChangedEvent(selections, newState.modelVersionId, oldSelections, oldModelVersionId, source || 'keyboard', reason));
549+
this._onDidChange.fire(new CursorStateChangedEvent(selections, newState.modelVersionId, oldSelections, oldModelVersionId, source || 'keyboard', reason, reachedMaxCursorCount));
547550
}
548551

549552
return true;
@@ -683,7 +686,7 @@ export class Cursor extends viewEvents.ViewEventEmitter implements ICursors {
683686
this._isHandling = false;
684687
this._cursors.startTrackingSelections();
685688
this._validateAutoClosedActions();
686-
if (this._emitStateChangedIfNecessary(source, cursorChangeReason, oldState)) {
689+
if (this._emitStateChangedIfNecessary(source, cursorChangeReason, oldState, false)) {
687690
this._revealRange(source, RevealTarget.Primary, viewEvents.VerticalRevealType.Simple, true, editorCommon.ScrollType.Smooth);
688691
}
689692
}

0 commit comments

Comments
 (0)