提交 12738e3e 编写于 作者: A Alex Dima

Clarify editor API

上级 98465c0c
......@@ -698,16 +698,8 @@ export abstract class CommonCodeEditor extends Disposable implements editorCommo
return this.cursor;
}
public executeCommand(source: string, command: editorCommon.ICommand): void {
if (!this.cursor) {
return;
}
this.cursor.trigger(source, editorCommon.Handler.ExecuteCommand, command);
}
public pushUndoStop(): boolean {
if (!this.cursor) {
// no view, no cursor
if (!this.model) {
return false;
}
if (this._configuration.editor.readOnly) {
......@@ -739,6 +731,13 @@ export abstract class CommonCodeEditor extends Disposable implements editorCommo
return true;
}
public executeCommand(source: string, command: editorCommon.ICommand): void {
if (!this.cursor) {
return;
}
this.cursor.trigger(source, editorCommon.Handler.ExecuteCommand, command);
}
public executeCommands(source: string, commands: editorCommon.ICommand[]): void {
if (!this.cursor) {
return;
......
......@@ -59,10 +59,9 @@ export class Cursor extends Disposable implements ICursors {
private readonly _configuration: editorCommon.IConfiguration;
private readonly _model: editorCommon.IModel;
private readonly _viewModelHelper: IViewModelHelper;
public context: CursorContext;
private _cursors: CursorCollection;
private _isHandling: boolean;
private _isDoingComposition: boolean;
private _columnSelectData: IColumnSelectData;
......@@ -77,31 +76,16 @@ export class Cursor extends Disposable implements ICursors {
this._configuration = configuration;
this._model = model;
this._viewModelHelper = viewModelHelper;
const createCursorContext = () => {
const config = new CursorConfiguration(
this._model.getLanguageIdentifier(),
this._model.getOneIndent(),
this._model.getOptions(),
this._configuration
);
this.context = new CursorContext(
this._model,
this._viewModelHelper,
config
);
if (this._cursors) {
this._cursors.updateContext(this.context);
}
};
createCursorContext();
this.context = new CursorContext(this._configuration, this._model, this._viewModelHelper);
this._cursors = new CursorCollection(this.context);
this._isHandling = false;
this._isDoingComposition = false;
this._columnSelectData = null;
this._handlers = {};
this._registerHandlers();
this._register(this._model.addBulkListener((events) => {
if (this._isHandling) {
return;
......@@ -115,14 +99,8 @@ export class Cursor extends Disposable implements ICursors {
if (eventType === TextModelEventType.ModelRawContentChanged2) {
hadContentChange = true;
const changeEvent = <ModelRawContentChangedEvent>event.data;
for (let j = 0, lenJ = changeEvent.changes.length; j < lenJ; j++) {
const change = changeEvent.changes[j];
if (change.changeType === RawContentChangedType.Flush) {
hadFlushEvent = true;
}
}
const rawChangeEvent = <ModelRawContentChangedEvent>event.data;
hadFlushEvent = hadFlushEvent || rawChangeEvent.containsEvent(RawContentChangedType.Flush);
}
}
......@@ -133,29 +111,29 @@ export class Cursor extends Disposable implements ICursors {
this._onModelContentChanged(hadFlushEvent);
}));
const updateCursorContext = () => {
this.context = new CursorContext(this._configuration, this._model, this._viewModelHelper);
this._cursors.updateContext(this.context);
};
this._register(this._model.onDidChangeLanguage((e) => {
createCursorContext();
updateCursorContext();
}));
this._register(LanguageConfigurationRegistry.onDidChange(() => {
// TODO@Alex: react only if certain supports changed? (and if my model's mode changed)
createCursorContext();
updateCursorContext();
}));
this._register(model.onDidChangeOptions(() => {
createCursorContext();
updateCursorContext();
}));
this._register(this._configuration.onDidChange((e) => {
if (CursorConfiguration.shouldRecreate(e)) {
createCursorContext();
updateCursorContext();
}
}));
this._handlers = {};
this._registerHandlers();
}
public dispose(): void {
this._cursors.dispose();
this._cursors = null;
super.dispose();
}
......@@ -203,8 +181,8 @@ export class Cursor extends Disposable implements ICursors {
}
if (somethingChanged) {
this.emitCursorPositionChanged(source, reason);
this.emitCursorSelectionChanged(source, reason);
this._emitCursorPositionChanged(source, reason);
this._emitCursorSelectionChanged(source, reason);
}
}
......@@ -295,16 +273,13 @@ export class Cursor extends Disposable implements ICursors {
if (hadFlushEvent) {
// a model.setValue() was called
this._cursors.dispose();
this._cursors = new CursorCollection(this.context);
this.emitCursorPositionChanged('model', CursorChangeReason.ContentFlush);
this.emitCursorSelectionChanged('model', CursorChangeReason.ContentFlush);
this._emitCursorPositionChanged('model', CursorChangeReason.ContentFlush);
this._emitCursorSelectionChanged('model', CursorChangeReason.ContentFlush);
} else {
if (!this._isHandling) {
const selectionsFromMarkers = this._cursors.readSelectionFromMarkers();
this.setStates('modelChange', CursorChangeReason.RecoverFromMarkers, CursorState.fromModelSelections(selectionsFromMarkers));
}
const selectionsFromMarkers = this._cursors.readSelectionFromMarkers();
this.setStates('modelChange', CursorChangeReason.RecoverFromMarkers, CursorState.fromModelSelections(selectionsFromMarkers));
}
}
......@@ -392,9 +367,9 @@ export class Cursor extends Disposable implements ICursors {
}
if (somethingChanged) {
this.emitCursorPositionChanged(args.eventSource, cursorPositionChangeReason);
this._emitCursorPositionChanged(args.eventSource, cursorPositionChangeReason);
this._revealRange(RevealTarget.Primary, VerticalRevealType.Simple, true);
this.emitCursorSelectionChanged(args.eventSource, cursorPositionChangeReason);
this._emitCursorSelectionChanged(args.eventSource, cursorPositionChangeReason);
}
} catch (err) {
......@@ -415,7 +390,7 @@ export class Cursor extends Disposable implements ICursors {
// -----------------------------------------------------------------------------------------------------------
// ----- emitting events
private emitCursorPositionChanged(source: string, reason: CursorChangeReason): void {
private _emitCursorPositionChanged(source: string, reason: CursorChangeReason): void {
const positions = this._cursors.getPositions();
const primaryPosition = positions[0];
const secondaryPositions = positions.slice(1);
......@@ -443,7 +418,7 @@ export class Cursor extends Disposable implements ICursors {
this._eventEmitter.emit(CursorEventType.CursorPositionChanged, e);
}
private emitCursorSelectionChanged(source: string, reason: CursorChangeReason): void {
private _emitCursorSelectionChanged(source: string, reason: CursorChangeReason): void {
const selections = this._cursors.getSelections();
const primarySelection = selections[0];
const secondarySelections = selections.slice(1);
......@@ -729,8 +704,8 @@ export class Cursor extends Disposable implements ICursors {
this._cursors.killSecondaryCursors();
return new EditOperationResult([command], {
shouldPushStackElementBefore: true,
shouldPushStackElementAfter: true
shouldPushStackElementBefore: false,
shouldPushStackElementAfter: false
});
}
......@@ -738,8 +713,8 @@ export class Cursor extends Disposable implements ICursors {
const commands = args.eventData;
return new EditOperationResult(commands, {
shouldPushStackElementBefore: true,
shouldPushStackElementAfter: true
shouldPushStackElementBefore: false,
shouldPushStackElementAfter: false
});
}
}
......
......@@ -284,10 +284,15 @@ export class CursorContext {
private readonly _viewModelHelper: IViewModelHelper;
private readonly _coordinatesConverter: ICoordinatesConverter;
constructor(model: IModel, viewModelHelper: IViewModelHelper, config: CursorConfiguration) {
constructor(configuration: IConfiguration, model: IModel, viewModelHelper: IViewModelHelper) {
this.model = model;
this.viewModel = viewModelHelper.viewModel;
this.config = config;
this.config = new CursorConfiguration(
this.model.getLanguageIdentifier(),
this.model.getOneIndent(),
this.model.getOptions(),
configuration
);;
this._viewModelHelper = viewModelHelper;
this._coordinatesConverter = viewModelHelper.coordinatesConverter;
}
......
......@@ -1886,6 +1886,7 @@ export interface ICommonCodeEditor extends IEditor {
/**
* Execute a command on the editor.
* The edits will land on the undo-redo stack, but no "undo stop" will be pushed.
* @param source The source of the call.
* @param command The command to execute
*/
......@@ -1898,6 +1899,7 @@ export interface ICommonCodeEditor extends IEditor {
/**
* Execute edits on the editor.
* The edits will land on the undo-redo stack, but no "undo stop" will be pushed.
* @param source The source of the call.
* @param edits The edits to execute.
* @param endCursoState Cursor state after the edits were applied.
......
......@@ -243,4 +243,14 @@ export class ModelRawContentChangedEvent {
this.isUndoing = isUndoing;
this.isRedoing = isRedoing;
}
public containsEvent(type: RawContentChangedType): boolean {
for (let i = 0, len = this.changes.length; i < len; i++) {
const change = this.changes[i];
if (change.changeType === type) {
return true;
}
}
return false;
}
}
......@@ -29,7 +29,9 @@ class MoveCaretAction extends EditorAction {
commands.push(new MoveCaretCommand(selections[i], this.left));
}
editor.pushUndoStop();
editor.executeCommands(this.id, commands);
editor.pushUndoStop();
}
}
......
......@@ -63,7 +63,9 @@ class TransposeLettersAction extends EditorAction {
}
if (commands.length > 0) {
editor.pushUndoStop();
editor.executeCommands(this.id, commands);
editor.pushUndoStop();
}
}
}
......@@ -35,7 +35,9 @@ abstract class CommentLineAction extends EditorAction {
commands.push(new LineCommentCommand(selections[i], opts.tabSize, this._type));
}
editor.pushUndoStop();
editor.executeCommands(this.id, commands);
editor.pushUndoStop();
}
}
......@@ -113,6 +115,8 @@ class BlockCommentAction extends EditorAction {
commands.push(new BlockCommentCommand(selections[i]));
}
editor.pushUndoStop();
editor.executeCommands(this.id, commands);
editor.pushUndoStop();
}
}
......@@ -146,7 +146,9 @@ export class DragAndDropController implements editorCommon.IEditorContribution {
this._dragSelection.getEndPosition().equals(newCursorPosition) || this._dragSelection.getStartPosition().equals(newCursorPosition)
) // we allow users to paste content beside the selection
)) {
this._editor.pushUndoStop();
this._editor.executeCommand(DragAndDropController.ID, new DragAndDropCommand(this._dragSelection, newCursorPosition, mouseEvent.event[DragAndDropController.TRIGGER_MODIFIER] || this._modiferPressed));
this._editor.pushUndoStop();
}
}
......
......@@ -468,7 +468,9 @@ export class FindModelBoundToEditorModel {
private _executeEditorCommand(source: string, command: editorCommon.ICommand): void {
try {
this._ignoreModelContentChanged = true;
this._editor.pushUndoStop();
this._editor.executeCommand(source, command);
this._editor.pushUndoStop();
} finally {
this._ignoreModelContentChanged = false;
}
......
......@@ -17,7 +17,9 @@ export class EditOperationsCommand implements editorCommon.ICommand {
if (typeof cmd._newEol === 'number') {
editor.getModel().setEOL(cmd._newEol);
}
editor.pushUndoStop();
editor.executeCommand('formatEditsCommand', cmd);
editor.pushUndoStop();
}
private _edits: TextEdit[];
......
......@@ -112,7 +112,10 @@ class InPlaceReplaceController implements IEditorContribution {
// Insert new text
var command = new InPlaceReplaceCommand(editRange, selection, result.value);
this.editor.pushUndoStop();
this.editor.executeCommand(source, command);
this.editor.pushUndoStop();
// add decoration
this.decorationIds = this.editor.deltaDecorations(this.decorationIds, [{
......
......@@ -164,7 +164,11 @@ export class IndentationToSpacesAction extends EditorAction {
}
let modelOpts = model.getOptions();
const command = new IndentationToSpacesCommand(editor.getSelection(), modelOpts.tabSize);
editor.pushUndoStop();
editor.executeCommands(this.id, [command]);
editor.pushUndoStop();
model.updateOptions({
insertSpaces: true
});
......@@ -191,7 +195,11 @@ export class IndentationToTabsAction extends EditorAction {
}
let modelOpts = model.getOptions();
const command = new IndentationToTabsCommand(editor.getSelection(), modelOpts.tabSize);
editor.pushUndoStop();
editor.executeCommands(this.id, [command]);
editor.pushUndoStop();
model.updateOptions({
insertSpaces: false
});
......
......@@ -39,7 +39,9 @@ abstract class AbstractCopyLinesAction extends EditorAction {
commands.push(new CopyLinesCommand(selections[i], this.down));
}
editor.pushUndoStop();
editor.executeCommands(this.id, commands);
editor.pushUndoStop();
}
}
......@@ -97,7 +99,9 @@ abstract class AbstractMoveLinesAction extends EditorAction {
commands.push(new MoveLinesCommand(selections[i], this.down));
}
editor.pushUndoStop();
editor.executeCommands(this.id, commands);
editor.pushUndoStop();
}
}
......@@ -151,7 +155,9 @@ abstract class AbstractSortLinesAction extends EditorAction {
var command = new SortLinesCommand(editor.getSelection(), this.descending);
editor.pushUndoStop();
editor.executeCommands(this.id, [command]);
editor.pushUndoStop();
}
}
......@@ -201,7 +207,9 @@ export class TrimTrailingWhitespaceAction extends EditorAction {
var command = new TrimTrailingWhitespaceCommand(editor.getSelection());
editor.pushUndoStop();
editor.executeCommands(this.id, [command]);
editor.pushUndoStop();
}
}
......@@ -280,7 +288,9 @@ class DeleteLinesAction extends AbstractRemoveLinesAction {
return new DeleteLinesCommand(op.startLineNumber, op.endLineNumber, op.positionColumn);
});
editor.pushUndoStop();
editor.executeCommands(this.id, commands);
editor.pushUndoStop();
}
}
......@@ -694,7 +704,9 @@ export class TransposeAction extends EditorAction {
}
}
editor.pushUndoStop();
editor.executeCommands(this.id, commands);
editor.pushUndoStop();
}
}
......@@ -725,7 +737,9 @@ export abstract class AbstractCaseAction extends EditorAction {
}
}
editor.pushUndoStop();
editor.executeCommands(this.id, commands);
editor.pushUndoStop();
}
protected abstract _modifyText(text: string): string;
......
......@@ -275,7 +275,9 @@ export abstract class DeleteWordCommand extends EditorCommand {
return new ReplaceCommand(deleteRange, '');
});
editor.pushUndoStop();
editor.executeCommands(this.id, commands);
editor.pushUndoStop();
}
protected abstract _delete(wordSeparators: WordCharacterClassifier, model: IModel, selection: Selection, whitespaceHeuristics: boolean, wordNavigationType: WordNavigationType): Range;
......
......@@ -2294,6 +2294,7 @@ declare module monaco.editor {
getAction(id: string): IEditorAction;
/**
* Execute a command on the editor.
* The edits will land on the undo-redo stack, but no "undo stop" will be pushed.
* @param source The source of the call.
* @param command The command to execute
*/
......@@ -2304,6 +2305,7 @@ declare module monaco.editor {
pushUndoStop(): boolean;
/**
* Execute edits on the editor.
* The edits will land on the undo-redo stack, but no "undo stop" will be pushed.
* @param source The source of the call.
* @param edits The edits to execute.
* @param endCursoState Cursor state after the edits were applied.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册