未验证 提交 b4631d39 编写于 作者: A Alex Dima

Emit ReadOnlyEditAttemptEvent through view model

上级 81e71a4a
...@@ -1473,10 +1473,6 @@ export class CodeEditorWidget extends Disposable implements editorBrowser.ICodeE ...@@ -1473,10 +1473,6 @@ export class CodeEditorWidget extends Disposable implements editorBrowser.ICodeE
// Someone might destroy the model from under the editor, so prevent any exceptions by setting a null model // Someone might destroy the model from under the editor, so prevent any exceptions by setting a null model
listenersToRemove.push(model.onWillDispose(() => this.setModel(null))); listenersToRemove.push(model.onWillDispose(() => this.setModel(null)));
listenersToRemove.push(viewModel.cursor.onDidAttemptReadOnlyEdit(() => {
this._onDidAttemptReadOnlyEdit.fire(undefined);
}));
listenersToRemove.push(viewModel.onEvent((e) => { listenersToRemove.push(viewModel.onEvent((e) => {
switch (e.kind) { switch (e.kind) {
case OutgoingViewModelEventKind.ContentSizeChanged: case OutgoingViewModelEventKind.ContentSizeChanged:
...@@ -1491,6 +1487,9 @@ export class CodeEditorWidget extends Disposable implements editorBrowser.ICodeE ...@@ -1491,6 +1487,9 @@ export class CodeEditorWidget extends Disposable implements editorBrowser.ICodeE
case OutgoingViewModelEventKind.ViewZonesChanged: case OutgoingViewModelEventKind.ViewZonesChanged:
this._onDidChangeViewZones.fire(); this._onDidChangeViewZones.fire();
break; break;
case OutgoingViewModelEventKind.ReadOnlyEditAttempt:
this._onDidAttemptReadOnlyEdit.fire();
break;
case OutgoingViewModelEventKind.CursorStateChanged: { case OutgoingViewModelEventKind.CursorStateChanged: {
if (e.reachedMaxCursorCount) { if (e.reachedMaxCursorCount) {
this._notificationService.warn(nls.localize('cursors.maximum', "The number of cursors has been limited to {0}.", Cursor.MAX_CURSOR_COUNT)); this._notificationService.warn(nls.localize('cursors.maximum', "The number of cursors has been limited to {0}.", Cursor.MAX_CURSOR_COUNT));
......
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
*--------------------------------------------------------------------------------------------*/ *--------------------------------------------------------------------------------------------*/
import { onUnexpectedError } from 'vs/base/common/errors'; import { onUnexpectedError } from 'vs/base/common/errors';
import { Emitter, Event } from 'vs/base/common/event';
import * as strings from 'vs/base/common/strings'; import * as strings from 'vs/base/common/strings';
import { CursorCollection } from 'vs/editor/common/controller/cursorCollection'; import { CursorCollection } from 'vs/editor/common/controller/cursorCollection';
import { CursorColumns, CursorConfiguration, CursorContext, CursorState, EditOperationResult, EditOperationType, IColumnSelectData, PartialCursorState, ICursorSimpleModel } from 'vs/editor/common/controller/cursorCommon'; import { CursorColumns, CursorConfiguration, CursorContext, CursorState, EditOperationResult, EditOperationType, IColumnSelectData, PartialCursorState, ICursorSimpleModel } from 'vs/editor/common/controller/cursorCommon';
...@@ -124,9 +123,6 @@ export class Cursor extends Disposable { ...@@ -124,9 +123,6 @@ export class Cursor extends Disposable {
public static readonly MAX_CURSOR_COUNT = 10000; public static readonly MAX_CURSOR_COUNT = 10000;
private readonly _onDidAttemptReadOnlyEdit: Emitter<void> = this._register(new Emitter<void>());
public readonly onDidAttemptReadOnlyEdit: Event<void> = this._onDidAttemptReadOnlyEdit.event;
private readonly _model: ITextModel; private readonly _model: ITextModel;
private _knownModelVersionId: number; private _knownModelVersionId: number;
private readonly _viewModel: ICursorSimpleModel; private readonly _viewModel: ICursorSimpleModel;
...@@ -598,7 +594,6 @@ export class Cursor extends Disposable { ...@@ -598,7 +594,6 @@ export class Cursor extends Disposable {
private _executeEdit(callback: () => void, eventsCollector: ViewModelEventsCollector, source: string | null | undefined, cursorChangeReason: CursorChangeReason = CursorChangeReason.NotSet): void { private _executeEdit(callback: () => void, eventsCollector: ViewModelEventsCollector, source: string | null | undefined, cursorChangeReason: CursorChangeReason = CursorChangeReason.NotSet): void {
if (this.context.cursorConfig.readOnly) { if (this.context.cursorConfig.readOnly) {
// we cannot edit when read only... // we cannot edit when read only...
this._onDidAttemptReadOnlyEdit.fire(undefined);
return; return;
} }
...@@ -621,15 +616,17 @@ export class Cursor extends Disposable { ...@@ -621,15 +616,17 @@ export class Cursor extends Disposable {
} }
} }
public setIsDoingComposition(isDoingComposition: boolean): void {
this._isDoingComposition = isDoingComposition;
}
public startComposition(eventsCollector: ViewModelEventsCollector): void { public startComposition(eventsCollector: ViewModelEventsCollector): void {
this._isDoingComposition = true;
this._selectionsWhenCompositionStarted = this.getSelections().slice(0); this._selectionsWhenCompositionStarted = this.getSelections().slice(0);
} }
public endComposition(eventsCollector: ViewModelEventsCollector, source?: string | null | undefined): void { public endComposition(eventsCollector: ViewModelEventsCollector, source?: string | null | undefined): void {
this._isDoingComposition = false;
this._executeEdit(() => { this._executeEdit(() => {
if (!this._isDoingComposition && source === 'keyboard') { if (source === 'keyboard') {
// composition finishes, let's check if we need to auto complete if necessary. // composition finishes, let's check if we need to auto complete if necessary.
const autoClosedCharacters = AutoClosedAction.getAllAutoClosedCharacters(this._autoClosedActions); const autoClosedCharacters = AutoClosedAction.getAllAutoClosedCharacters(this._autoClosedActions);
this._executeEditOperation(TypeOperations.compositionEndWithInterceptors(this._prevEditOperationType, this.context.cursorConfig, this._model, this._selectionsWhenCompositionStarted, this.getSelections(), autoClosedCharacters)); this._executeEditOperation(TypeOperations.compositionEndWithInterceptors(this._prevEditOperationType, this.context.cursorConfig, this._model, this._selectionsWhenCompositionStarted, this.getSelections(), autoClosedCharacters));
......
...@@ -176,6 +176,7 @@ export const enum OutgoingViewModelEventKind { ...@@ -176,6 +176,7 @@ export const enum OutgoingViewModelEventKind {
FocusChanged, FocusChanged,
ScrollChanged, ScrollChanged,
ViewZonesChanged, ViewZonesChanged,
ReadOnlyEditAttempt,
CursorStateChanged, CursorStateChanged,
} }
...@@ -366,10 +367,27 @@ export class CursorStateChangedEvent { ...@@ -366,10 +367,27 @@ export class CursorStateChangedEvent {
} }
} }
export class ReadOnlyEditAttemptEvent {
public readonly kind = OutgoingViewModelEventKind.ReadOnlyEditAttempt;
constructor() {
}
public isNoOp(): boolean {
return false;
}
public merge(other: OutgoingViewModelEvent): ReadOnlyEditAttemptEvent {
return this;
}
}
export type OutgoingViewModelEvent = ( export type OutgoingViewModelEvent = (
ContentSizeChangedEvent ContentSizeChangedEvent
| FocusChangedEvent | FocusChangedEvent
| ScrollChangedEvent | ScrollChangedEvent
| ViewZonesChangedEvent | ViewZonesChangedEvent
| ReadOnlyEditAttemptEvent
| CursorStateChangedEvent | CursorStateChangedEvent
); );
...@@ -30,7 +30,7 @@ import { Cursor } from 'vs/editor/common/controller/cursor'; ...@@ -30,7 +30,7 @@ import { Cursor } from 'vs/editor/common/controller/cursor';
import { PartialCursorState, CursorState, IColumnSelectData, EditOperationType, CursorConfiguration } from 'vs/editor/common/controller/cursorCommon'; import { PartialCursorState, CursorState, IColumnSelectData, EditOperationType, CursorConfiguration } from 'vs/editor/common/controller/cursorCommon';
import { CursorChangeReason } from 'vs/editor/common/controller/cursorEvents'; import { CursorChangeReason } from 'vs/editor/common/controller/cursorEvents';
import { IWhitespaceChangeAccessor } from 'vs/editor/common/viewLayout/linesLayout'; import { IWhitespaceChangeAccessor } from 'vs/editor/common/viewLayout/linesLayout';
import { ViewModelEventDispatcher, OutgoingViewModelEvent, FocusChangedEvent, ScrollChangedEvent, ViewZonesChangedEvent, ViewModelEventsCollector } from 'vs/editor/common/viewModel/viewModelEventDispatcher'; import { ViewModelEventDispatcher, OutgoingViewModelEvent, FocusChangedEvent, ScrollChangedEvent, ViewZonesChangedEvent, ViewModelEventsCollector, ReadOnlyEditAttemptEvent } from 'vs/editor/common/viewModel/viewModelEventDispatcher';
import { ViewEventHandler } from 'vs/editor/common/viewModel/viewEventHandler'; import { ViewEventHandler } from 'vs/editor/common/viewModel/viewEventHandler';
const USE_IDENTITY_LINES_COLLECTION = true; const USE_IDENTITY_LINES_COLLECTION = true;
...@@ -52,7 +52,7 @@ export class ViewModel extends Disposable implements IViewModel { ...@@ -52,7 +52,7 @@ export class ViewModel extends Disposable implements IViewModel {
private readonly lines: IViewModelLinesCollection; private readonly lines: IViewModelLinesCollection;
public readonly coordinatesConverter: ICoordinatesConverter; public readonly coordinatesConverter: ICoordinatesConverter;
public readonly viewLayout: ViewLayout; public readonly viewLayout: ViewLayout;
public readonly cursor: Cursor; private readonly cursor: Cursor;
private readonly decorations: ViewModelDecorations; private readonly decorations: ViewModelDecorations;
constructor( constructor(
...@@ -914,32 +914,42 @@ export class ViewModel extends Disposable implements IViewModel { ...@@ -914,32 +914,42 @@ export class ViewModel extends Disposable implements IViewModel {
this._withViewEventsCollector(eventsCollector => this.cursor.restoreState(eventsCollector, states)); this._withViewEventsCollector(eventsCollector => this.cursor.restoreState(eventsCollector, states));
} }
private _executeCursorEdit(callback: (eventsCollector: ViewModelEventsCollector) => void): void {
if (this.cursor.context.cursorConfig.readOnly) {
// we cannot edit when read only...
this._eventDispatcher.emitOutgoingEvent(new ReadOnlyEditAttemptEvent());
return;
}
this._withViewEventsCollector(callback);
}
public executeEdits(source: string | null | undefined, edits: IIdentifiedSingleEditOperation[], cursorStateComputer: ICursorStateComputer): void { public executeEdits(source: string | null | undefined, edits: IIdentifiedSingleEditOperation[], cursorStateComputer: ICursorStateComputer): void {
this._withViewEventsCollector(eventsCollector => this.cursor.executeEdits(eventsCollector, source, edits, cursorStateComputer)); this._executeCursorEdit(eventsCollector => this.cursor.executeEdits(eventsCollector, source, edits, cursorStateComputer));
} }
public startComposition(): void { public startComposition(): void {
this._withViewEventsCollector(eventsCollector => this.cursor.startComposition(eventsCollector)); this.cursor.setIsDoingComposition(true);
this._executeCursorEdit(eventsCollector => this.cursor.startComposition(eventsCollector));
} }
public endComposition(source?: string | null | undefined): void { public endComposition(source?: string | null | undefined): void {
this._withViewEventsCollector(eventsCollector => this.cursor.endComposition(eventsCollector, source)); this.cursor.setIsDoingComposition(false);
this._executeCursorEdit(eventsCollector => this.cursor.endComposition(eventsCollector, source));
} }
public type(text: string, source?: string | null | undefined): void { public type(text: string, source?: string | null | undefined): void {
this._withViewEventsCollector(eventsCollector => this.cursor.type(eventsCollector, text, source)); this._executeCursorEdit(eventsCollector => this.cursor.type(eventsCollector, text, source));
} }
public replacePreviousChar(text: string, replaceCharCnt: number, source?: string | null | undefined): void { public replacePreviousChar(text: string, replaceCharCnt: number, source?: string | null | undefined): void {
this._withViewEventsCollector(eventsCollector => this.cursor.replacePreviousChar(eventsCollector, text, replaceCharCnt, source)); this._executeCursorEdit(eventsCollector => this.cursor.replacePreviousChar(eventsCollector, text, replaceCharCnt, source));
} }
public paste(text: string, pasteOnNewLine: boolean, multicursorText?: string[] | null | undefined, source?: string | null | undefined): void { public paste(text: string, pasteOnNewLine: boolean, multicursorText?: string[] | null | undefined, source?: string | null | undefined): void {
this._withViewEventsCollector(eventsCollector => this.cursor.paste(eventsCollector, text, pasteOnNewLine, multicursorText, source)); this._executeCursorEdit(eventsCollector => this.cursor.paste(eventsCollector, text, pasteOnNewLine, multicursorText, source));
} }
public cut(source?: string | null | undefined): void { public cut(source?: string | null | undefined): void {
this._withViewEventsCollector(eventsCollector => this.cursor.cut(eventsCollector, source)); this._executeCursorEdit(eventsCollector => this.cursor.cut(eventsCollector, source));
} }
public executeCommand(command: ICommand, source?: string | null | undefined): void { public executeCommand(command: ICommand, source?: string | null | undefined): void {
this._withViewEventsCollector(eventsCollector => this.cursor.executeCommand(eventsCollector, command, source)); this._executeCursorEdit(eventsCollector => this.cursor.executeCommand(eventsCollector, command, source));
} }
public executeCommands(commands: ICommand[], source?: string | null | undefined): void { public executeCommands(commands: ICommand[], source?: string | null | undefined): void {
this._withViewEventsCollector(eventsCollector => this.cursor.executeCommands(eventsCollector, commands, source)); this._executeCursorEdit(eventsCollector => this.cursor.executeCommands(eventsCollector, commands, source));
} }
public revealPrimaryCursor(source: string | null | undefined, revealHorizontal: boolean): void { public revealPrimaryCursor(source: string | null | undefined, revealHorizontal: boolean): void {
this._withViewEventsCollector(eventsCollector => this.cursor.revealPrimary(eventsCollector, source, revealHorizontal, ScrollType.Smooth)); this._withViewEventsCollector(eventsCollector => this.cursor.revealPrimary(eventsCollector, source, revealHorizontal, ScrollType.Smooth));
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册