replaceCommand.ts 4.4 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

J
Johannes Rieken 已提交
7
import { Selection } from 'vs/editor/common/core/selection';
A
Alex Dima 已提交
8
import * as editorCommon from 'vs/editor/common/editorCommon';
J
Johannes Rieken 已提交
9
import { Range } from 'vs/editor/common/core/range';
E
Erich Gamma 已提交
10

A
Alex Dima 已提交
11
export class ReplaceCommand implements editorCommon.ICommand {
E
Erich Gamma 已提交
12

A
Alex Dima 已提交
13 14 15
	private readonly _range: Range;
	private readonly _text: string;
	public readonly insertsAutoWhitespace: boolean;
E
Erich Gamma 已提交
16

A
Alex Dima 已提交
17
	constructor(range: Range, text: string, insertsAutoWhitespace: boolean = false) {
E
Erich Gamma 已提交
18 19
		this._range = range;
		this._text = text;
A
Alex Dima 已提交
20
		this.insertsAutoWhitespace = insertsAutoWhitespace;
E
Erich Gamma 已提交
21 22
	}

A
Alex Dima 已提交
23
	public getEditOperations(model: editorCommon.ITokenizedModel, builder: editorCommon.IEditOperationBuilder): void {
A
Alex Dima 已提交
24
		builder.addTrackedEditOperation(this._range, this._text);
E
Erich Gamma 已提交
25 26
	}

27
	public computeCursorState(model: editorCommon.ITokenizedModel, helper: editorCommon.ICursorStateComputerData): Selection {
A
Alex Dima 已提交
28 29
		let inverseEditOperations = helper.getInverseEditOperations();
		let srcRange = inverseEditOperations[0].range;
E
Erich Gamma 已提交
30 31 32 33 34 35 36 37 38
		return new Selection(
			srcRange.endLineNumber,
			srcRange.endColumn,
			srcRange.endLineNumber,
			srcRange.endColumn
		);
	}
}

A
Alex Dima 已提交
39 40
export class ReplaceCommandWithoutChangingPosition implements editorCommon.ICommand {

A
Alex Dima 已提交
41 42 43
	private readonly _range: Range;
	private readonly _text: string;
	public readonly insertsAutoWhitespace: boolean;
E
Erich Gamma 已提交
44

A
Alex Dima 已提交
45
	constructor(range: Range, text: string, insertsAutoWhitespace: boolean = false) {
A
Alex Dima 已提交
46 47
		this._range = range;
		this._text = text;
A
Alex Dima 已提交
48
		this.insertsAutoWhitespace = insertsAutoWhitespace;
A
Alex Dima 已提交
49 50 51 52
	}

	public getEditOperations(model: editorCommon.ITokenizedModel, builder: editorCommon.IEditOperationBuilder): void {
		builder.addTrackedEditOperation(this._range, this._text);
E
Erich Gamma 已提交
53 54
	}

55
	public computeCursorState(model: editorCommon.ITokenizedModel, helper: editorCommon.ICursorStateComputerData): Selection {
A
Alex Dima 已提交
56 57
		let inverseEditOperations = helper.getInverseEditOperations();
		let srcRange = inverseEditOperations[0].range;
E
Erich Gamma 已提交
58 59 60 61 62 63 64 65 66
		return new Selection(
			srcRange.startLineNumber,
			srcRange.startColumn,
			srcRange.startLineNumber,
			srcRange.startColumn
		);
	}
}

A
Alex Dima 已提交
67
export class ReplaceCommandWithOffsetCursorState implements editorCommon.ICommand {
E
Erich Gamma 已提交
68

A
Alex Dima 已提交
69 70 71 72 73
	private readonly _range: Range;
	private readonly _text: string;
	private readonly _columnDeltaOffset: number;
	private readonly _lineNumberDeltaOffset: number;
	public readonly insertsAutoWhitespace: boolean;
E
Erich Gamma 已提交
74

A
Alex Dima 已提交
75
	constructor(range: Range, text: string, lineNumberDeltaOffset: number, columnDeltaOffset: number, insertsAutoWhitespace: boolean = false) {
A
Alex Dima 已提交
76 77
		this._range = range;
		this._text = text;
E
Erich Gamma 已提交
78 79
		this._columnDeltaOffset = columnDeltaOffset;
		this._lineNumberDeltaOffset = lineNumberDeltaOffset;
A
Alex Dima 已提交
80
		this.insertsAutoWhitespace = insertsAutoWhitespace;
E
Erich Gamma 已提交
81 82
	}

A
Alex Dima 已提交
83 84 85 86
	public getEditOperations(model: editorCommon.ITokenizedModel, builder: editorCommon.IEditOperationBuilder): void {
		builder.addTrackedEditOperation(this._range, this._text);
	}

87
	public computeCursorState(model: editorCommon.ITokenizedModel, helper: editorCommon.ICursorStateComputerData): Selection {
A
Alex Dima 已提交
88 89
		let inverseEditOperations = helper.getInverseEditOperations();
		let srcRange = inverseEditOperations[0].range;
E
Erich Gamma 已提交
90 91 92 93 94 95 96 97 98
		return new Selection(
			srcRange.endLineNumber + this._lineNumberDeltaOffset,
			srcRange.endColumn + this._columnDeltaOffset,
			srcRange.endLineNumber + this._lineNumberDeltaOffset,
			srcRange.endColumn + this._columnDeltaOffset
		);
	}
}

A
Alex Dima 已提交
99
export class ReplaceCommandThatPreservesSelection implements editorCommon.ICommand {
E
Erich Gamma 已提交
100

A
Alex Dima 已提交
101 102
	private _range: Range;
	private _text: string;
103
	private _initialSelection: Selection;
E
Erich Gamma 已提交
104 105
	private _selectionId: string;

106
	constructor(editRange: Range, text: string, initialSelection: Selection) {
A
Alex Dima 已提交
107 108
		this._range = editRange;
		this._text = text;
E
Erich Gamma 已提交
109 110 111
		this._initialSelection = initialSelection;
	}

A
Alex Dima 已提交
112
	public getEditOperations(model: editorCommon.ITokenizedModel, builder: editorCommon.IEditOperationBuilder): void {
A
Alex Dima 已提交
113
		builder.addEditOperation(this._range, this._text);
E
Erich Gamma 已提交
114 115 116
		this._selectionId = builder.trackSelection(this._initialSelection);
	}

117
	public computeCursorState(model: editorCommon.ITokenizedModel, helper: editorCommon.ICursorStateComputerData): Selection {
E
Erich Gamma 已提交
118 119 120
		return helper.getTrackedSelection(this._selectionId);
	}
}