Skip to content

Commit 4ee2f42

Browse files
committed
1 parent e573b30 commit 4ee2f42

13 files changed

Lines changed: 130 additions & 130 deletions

File tree

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

Lines changed: 0 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
1717
import { CommonCodeEditor } from 'vs/editor/common/commonCodeEditor';
1818
import { CommonEditorConfiguration } from 'vs/editor/common/config/commonEditorConfig';
1919
import { Range, IRange } from 'vs/editor/common/core/range';
20-
import { Selection } from 'vs/editor/common/core/selection';
2120
import * as editorCommon from 'vs/editor/common/editorCommon';
2221
import { EditorAction } from 'vs/editor/common/editorCommonExtensions';
2322
import { ICodeEditorService } from 'vs/editor/common/services/codeEditorService';
@@ -686,99 +685,3 @@ class CodeEditorWidgetFocusTracker extends Disposable {
686685
return this._hasFocus;
687686
}
688687
}
689-
690-
class OverlayWidget2 implements editorBrowser.IOverlayWidget {
691-
692-
private _id: string;
693-
private _position: editorBrowser.IOverlayWidgetPosition;
694-
private _domNode: HTMLElement;
695-
696-
constructor(id: string, position: editorBrowser.IOverlayWidgetPosition) {
697-
this._id = id;
698-
this._position = position;
699-
this._domNode = document.createElement('div');
700-
this._domNode.className = this._id.replace(/\./g, '-').replace(/[^a-z0-9\-]/, '');
701-
}
702-
703-
public getId(): string {
704-
return this._id;
705-
}
706-
707-
public getDomNode(): HTMLElement {
708-
return this._domNode;
709-
}
710-
711-
public getPosition(): editorBrowser.IOverlayWidgetPosition {
712-
return this._position;
713-
}
714-
}
715-
716-
export enum EditCursorState {
717-
EndOfLastEditOperation = 0
718-
}
719-
720-
class SingleEditOperation {
721-
722-
range: Range;
723-
text: string;
724-
forceMoveMarkers: boolean;
725-
726-
constructor(source: editorCommon.ISingleEditOperation) {
727-
this.range = new Range(source.range.startLineNumber, source.range.startColumn, source.range.endLineNumber, source.range.endColumn);
728-
this.text = source.text;
729-
this.forceMoveMarkers = source.forceMoveMarkers || false;
730-
}
731-
732-
}
733-
734-
export class CommandRunner implements editorCommon.ICommand {
735-
736-
private _ops: SingleEditOperation[];
737-
private _editCursorState: EditCursorState;
738-
739-
constructor(ops: editorCommon.ISingleEditOperation[], editCursorState: EditCursorState) {
740-
this._ops = ops.map(op => new SingleEditOperation(op));
741-
this._editCursorState = editCursorState;
742-
}
743-
744-
public getEditOperations(model: editorCommon.ITokenizedModel, builder: editorCommon.IEditOperationBuilder): void {
745-
if (this._ops.length === 0) {
746-
return;
747-
}
748-
749-
// Sort them in ascending order by range starts
750-
this._ops.sort((o1, o2) => {
751-
return Range.compareRangesUsingStarts(o1.range, o2.range);
752-
});
753-
754-
// Merge operations that touch each other
755-
let resultOps: editorCommon.ISingleEditOperation[] = [];
756-
let previousOp = this._ops[0];
757-
for (let i = 1; i < this._ops.length; i++) {
758-
if (previousOp.range.endLineNumber === this._ops[i].range.startLineNumber && previousOp.range.endColumn === this._ops[i].range.startColumn) {
759-
// These operations are one after another and can be merged
760-
previousOp.range = Range.plusRange(previousOp.range, this._ops[i].range);
761-
previousOp.text = previousOp.text + this._ops[i].text;
762-
} else {
763-
resultOps.push(previousOp);
764-
previousOp = this._ops[i];
765-
}
766-
}
767-
resultOps.push(previousOp);
768-
769-
for (let i = 0; i < resultOps.length; i++) {
770-
builder.addEditOperation(Range.lift(resultOps[i].range), resultOps[i].text);
771-
}
772-
}
773-
774-
public computeCursorState(model: editorCommon.ITokenizedModel, helper: editorCommon.ICursorStateComputerData): Selection {
775-
let inverseEditOperations = helper.getInverseEditOperations();
776-
let srcRange = inverseEditOperations[inverseEditOperations.length - 1].range;
777-
return new Selection(
778-
srcRange.endLineNumber,
779-
srcRange.endColumn,
780-
srcRange.endLineNumber,
781-
srcRange.endColumn
782-
);
783-
}
784-
}

src/vs/editor/common/commands/replaceCommand.ts

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,8 @@ export class ReplaceCommand implements editorCommon.ICommand {
1818
this._text = text;
1919
}
2020

21-
public getText(): string {
22-
return this._text;
23-
}
24-
25-
public getRange(): Range {
26-
return this._range;
27-
}
28-
29-
public setRange(newRange: Range): void {
30-
this._range = newRange;
31-
}
32-
3321
public getEditOperations(model: editorCommon.ITokenizedModel, builder: editorCommon.IEditOperationBuilder): void {
34-
builder.addEditOperation(this._range, this._text);
22+
builder.addTrackedEditOperation(this._range, this._text);
3523
}
3624

3725
public computeCursorState(model: editorCommon.ITokenizedModel, helper: editorCommon.ICursorStateComputerData): Selection {
@@ -46,10 +34,18 @@ export class ReplaceCommand implements editorCommon.ICommand {
4634
}
4735
}
4836

49-
export class ReplaceCommandWithoutChangingPosition extends ReplaceCommand {
37+
export class ReplaceCommandWithoutChangingPosition implements editorCommon.ICommand {
38+
39+
private _range: Range;
40+
private _text: string;
5041

5142
constructor(range: Range, text: string) {
52-
super(range, text);
43+
this._range = range;
44+
this._text = text;
45+
}
46+
47+
public getEditOperations(model: editorCommon.ITokenizedModel, builder: editorCommon.IEditOperationBuilder): void {
48+
builder.addTrackedEditOperation(this._range, this._text);
5349
}
5450

5551
public computeCursorState(model: editorCommon.ITokenizedModel, helper: editorCommon.ICursorStateComputerData): Selection {
@@ -64,17 +60,24 @@ export class ReplaceCommandWithoutChangingPosition extends ReplaceCommand {
6460
}
6561
}
6662

67-
export class ReplaceCommandWithOffsetCursorState extends ReplaceCommand {
63+
export class ReplaceCommandWithOffsetCursorState implements editorCommon.ICommand {
6864

65+
private _range: Range;
66+
private _text: string;
6967
private _columnDeltaOffset: number;
7068
private _lineNumberDeltaOffset: number;
7169

7270
constructor(range: Range, text: string, lineNumberDeltaOffset: number, columnDeltaOffset: number) {
73-
super(range, text);
71+
this._range = range;
72+
this._text = text;
7473
this._columnDeltaOffset = columnDeltaOffset;
7574
this._lineNumberDeltaOffset = lineNumberDeltaOffset;
7675
}
7776

77+
public getEditOperations(model: editorCommon.ITokenizedModel, builder: editorCommon.IEditOperationBuilder): void {
78+
builder.addTrackedEditOperation(this._range, this._text);
79+
}
80+
7881
public computeCursorState(model: editorCommon.ITokenizedModel, helper: editorCommon.ICursorStateComputerData): Selection {
7982
let inverseEditOperations = helper.getInverseEditOperations();
8083
let srcRange = inverseEditOperations[0].range;
@@ -87,19 +90,21 @@ export class ReplaceCommandWithOffsetCursorState extends ReplaceCommand {
8790
}
8891
}
8992

90-
export class ReplaceCommandThatPreservesSelection extends ReplaceCommand {
93+
export class ReplaceCommandThatPreservesSelection implements editorCommon.ICommand {
9194

95+
private _range: Range;
96+
private _text: string;
9297
private _initialSelection: Selection;
9398
private _selectionId: string;
9499

95100
constructor(editRange: Range, text: string, initialSelection: Selection) {
96-
super(editRange, text);
101+
this._range = editRange;
102+
this._text = text;
97103
this._initialSelection = initialSelection;
98104
}
99105

100106
public getEditOperations(model: editorCommon.ITokenizedModel, builder: editorCommon.IEditOperationBuilder): void {
101-
super.getEditOperations(model, builder);
102-
107+
builder.addEditOperation(this._range, this._text);
103108
this._selectionId = builder.trackSelection(this._initialSelection);
104109
}
105110

src/vs/editor/common/commands/shiftCommand.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ export class ShiftCommand implements ICommand {
5252
this._useLastEditRangeForCursorEndPosition = false;
5353
}
5454

55+
private _addEditOperation(builder: IEditOperationBuilder, range: Range, text: string) {
56+
if (this._useLastEditRangeForCursorEndPosition) {
57+
builder.addTrackedEditOperation(range, text);
58+
} else {
59+
builder.addEditOperation(range, text);
60+
}
61+
}
62+
5563
public getEditOperations(model: ITokenizedModel, builder: IEditOperationBuilder): void {
5664
const startLine = this._selection.startLineNumber;
5765

@@ -147,7 +155,7 @@ export class ShiftCommand implements ICommand {
147155
indents[j] = indents[j - 1] + oneIndent;
148156
}
149157

150-
builder.addEditOperation(new Range(lineNumber, 1, lineNumber, indentationEndIndex + 1), indents[desiredIndentCount]);
158+
this._addEditOperation(builder, new Range(lineNumber, 1, lineNumber, indentationEndIndex + 1), indents[desiredIndentCount]);
151159
}
152160
} else {
153161

@@ -186,9 +194,9 @@ export class ShiftCommand implements ICommand {
186194
}
187195
}
188196

189-
builder.addEditOperation(new Range(lineNumber, 1, lineNumber, indentationEndIndex + 1), '');
197+
this._addEditOperation(builder, new Range(lineNumber, 1, lineNumber, indentationEndIndex + 1), '');
190198
} else {
191-
builder.addEditOperation(new Range(lineNumber, 1, lineNumber, 1), oneIndent);
199+
this._addEditOperation(builder, new Range(lineNumber, 1, lineNumber, 1), oneIndent);
192200
}
193201
}
194202
}

src/vs/editor/common/commands/surroundSelectionCommand.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ export class SurroundSelectionCommand implements ICommand {
2020
}
2121

2222
public getEditOperations(model: ITokenizedModel, builder: IEditOperationBuilder): void {
23-
builder.addEditOperation(new Range(
23+
builder.addTrackedEditOperation(new Range(
2424
this._range.startLineNumber,
2525
this._range.startColumn,
2626
this._range.startLineNumber,
2727
this._range.startColumn
2828
), this._charBeforeSelection);
2929

30-
builder.addEditOperation(new Range(
30+
builder.addTrackedEditOperation(new Range(
3131
this._range.endLineNumber,
3232
this._range.endColumn,
3333
this._range.endLineNumber,

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,14 @@ interface IExecContext {
4242
interface ICommandData {
4343
operations: editorCommon.IIdentifiedSingleEditOperation[];
4444
hadTrackedRange: boolean;
45+
hadTrackedEditOperation: boolean;
4546
}
4647

4748
interface ICommandsData {
4849
operations: editorCommon.IIdentifiedSingleEditOperation[];
4950
hadTrackedRanges: boolean[];
5051
anyoneHadTrackedRange: boolean;
52+
anyoneHadTrackedEditOperation: boolean;
5153
}
5254

5355
export class Cursor extends Disposable implements ICursors {
@@ -479,6 +481,12 @@ export class Cursor extends Disposable implements ICursors {
479481
});
480482
};
481483

484+
var hadTrackedEditOperation = false;
485+
var addTrackedEditOperation = (selection: Range, text: string) => {
486+
hadTrackedEditOperation = true;
487+
addEditOperation(selection, text);
488+
};
489+
482490
var hadTrackedRange = false;
483491
var trackSelection = (selection: Selection, trackPreviousOnEmpty?: boolean) => {
484492
var selectionMarkerStickToPreviousCharacter: boolean,
@@ -517,6 +525,7 @@ export class Cursor extends Disposable implements ICursors {
517525

518526
var editOperationBuilder: editorCommon.IEditOperationBuilder = {
519527
addEditOperation: addEditOperation,
528+
addTrackedEditOperation: addTrackedEditOperation,
520529
trackSelection: trackSelection
521530
};
522531

@@ -527,20 +536,23 @@ export class Cursor extends Disposable implements ICursors {
527536
onUnexpectedError(e);
528537
return {
529538
operations: [],
530-
hadTrackedRange: false
539+
hadTrackedRange: false,
540+
hadTrackedEditOperation: false
531541
};
532542
}
533543

534544
return {
535545
operations: operations,
536-
hadTrackedRange: hadTrackedRange
546+
hadTrackedRange: hadTrackedRange,
547+
hadTrackedEditOperation: hadTrackedEditOperation
537548
};
538549
}
539550

540551
private _getEditOperations(ctx: IExecContext, commands: editorCommon.ICommand[], isAutoWhitespaceCommand: boolean[]): ICommandsData {
541552
var oneResult: ICommandData;
542553
var operations: editorCommon.IIdentifiedSingleEditOperation[] = [];
543554
var hadTrackedRanges: boolean[] = [];
555+
var anyoneHadTrackedEditOperation: boolean = false;
544556
var anyoneHadTrackedRange: boolean;
545557

546558
for (var i = 0; i < commands.length; i++) {
@@ -549,14 +561,16 @@ export class Cursor extends Disposable implements ICursors {
549561
operations = operations.concat(oneResult.operations);
550562
hadTrackedRanges[i] = oneResult.hadTrackedRange;
551563
anyoneHadTrackedRange = anyoneHadTrackedRange || hadTrackedRanges[i];
564+
anyoneHadTrackedEditOperation = anyoneHadTrackedEditOperation || oneResult.hadTrackedEditOperation;
552565
} else {
553566
hadTrackedRanges[i] = false;
554567
}
555568
}
556569
return {
557570
operations: operations,
558571
hadTrackedRanges: hadTrackedRanges,
559-
anyoneHadTrackedRange: anyoneHadTrackedRange
572+
anyoneHadTrackedRange: anyoneHadTrackedRange,
573+
anyoneHadTrackedEditOperation: anyoneHadTrackedEditOperation
560574
};
561575
}
562576

@@ -682,6 +696,11 @@ export class Cursor extends Disposable implements ICursors {
682696
}
683697
}
684698

699+
// TODO@Alex: find a better way to do this.
700+
// give the hint that edit operations are tracked to the model
701+
if (commandsData.anyoneHadTrackedEditOperation && filteredOperations.length > 0) {
702+
filteredOperations[0]._isTracked = true;
703+
}
685704
var selectionsAfter = this.model.pushEditOperations(selectionsBefore, filteredOperations, (inverseEditOperations: editorCommon.IIdentifiedSingleEditOperation[]): Selection[] => {
686705
var groupedInverseEditOperations: editorCommon.IIdentifiedSingleEditOperation[][] = [];
687706
for (var i = 0; i < selectionsBefore.length; i++) {

src/vs/editor/common/editorCommon.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,14 @@ export interface IEditOperationBuilder {
291291
*/
292292
addEditOperation(range: Range, text: string): void;
293293

294+
/**
295+
* Add a new edit operation (a replace operation).
296+
* The inverse edits will be accessible in `ICursorStateComputerData.getInverseEditOperations()`
297+
* @param range The range to replace (delete). May be empty to represent a simple insert.
298+
* @param text The text to replace with. May be null to represent a simple delete.
299+
*/
300+
addTrackedEditOperation(range: Range, text: string): void;
301+
294302
/**
295303
* Track `selection` when applying edit operations.
296304
* A best effort will be made to not grow/expand the selection.
@@ -384,6 +392,11 @@ export interface IIdentifiedSingleEditOperation {
384392
* that can be removed on next model edit operation if `config.trimAutoWhitespace` is true.
385393
*/
386394
isAutoWhitespaceEdit?: boolean;
395+
/**
396+
* This indicates that this operation is in a set of operations that are tracked and should not be "simplified".
397+
* @internal
398+
*/
399+
_isTracked?: boolean;
387400
}
388401

389402
/**

0 commit comments

Comments
 (0)