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

import {Selection} from 'vs/editor/common/core/selection';
A
Alex Dima 已提交
8
import * as editorCommon from 'vs/editor/common/editorCommon';
E
Erich Gamma 已提交
9

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

A
Alex Dima 已提交
12
	private _range: editorCommon.IEditorRange;
E
Erich Gamma 已提交
13 14
	private _text: string;

A
Alex Dima 已提交
15
	constructor(range: editorCommon.IEditorRange, text: string) {
E
Erich Gamma 已提交
16 17 18 19 20 21 22 23
		this._range = range;
		this._text = text;
	}

	public getText():string {
		return this._text;
	}

A
Alex Dima 已提交
24
	public getRange():editorCommon.IEditorRange {
E
Erich Gamma 已提交
25 26 27
		return this._range;
	}

A
Alex Dima 已提交
28
	public setRange(newRange:editorCommon.IEditorRange): void {
E
Erich Gamma 已提交
29 30 31
		this._range = newRange;
	}

A
Alex Dima 已提交
32
	public getEditOperations(model: editorCommon.ITokenizedModel, builder: editorCommon.IEditOperationBuilder): void {
E
Erich Gamma 已提交
33 34 35
		builder.addEditOperation(this._range, this._text);
	}

A
Alex Dima 已提交
36
	public computeCursorState(model: editorCommon.ITokenizedModel, helper: editorCommon.ICursorStateComputerData): editorCommon.IEditorSelection {
E
Erich Gamma 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49
		var inverseEditOperations = helper.getInverseEditOperations();
		var srcRange = inverseEditOperations[0].range;
		return new Selection(
			srcRange.endLineNumber,
			srcRange.endColumn,
			srcRange.endLineNumber,
			srcRange.endColumn
		);
	}
}

export class ReplaceCommandWithoutChangingPosition extends ReplaceCommand {

A
Alex Dima 已提交
50
	constructor(range: editorCommon.IEditorRange, text: string) {
E
Erich Gamma 已提交
51 52 53
		super(range, text);
	}

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

export class ReplaceCommandWithOffsetCursorState extends ReplaceCommand {

	private _columnDeltaOffset: number;
	private _lineNumberDeltaOffset: number;

A
Alex Dima 已提交
71
	constructor(range: editorCommon.IEditorRange, text: string, lineNumberDeltaOffset: number, columnDeltaOffset: number) {
E
Erich Gamma 已提交
72 73 74 75 76
		super(range, text);
		this._columnDeltaOffset = columnDeltaOffset;
		this._lineNumberDeltaOffset = lineNumberDeltaOffset;
	}

A
Alex Dima 已提交
77
	public computeCursorState(model: editorCommon.ITokenizedModel, helper: editorCommon.ICursorStateComputerData): editorCommon.IEditorSelection {
E
Erich Gamma 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90
		var inverseEditOperations = helper.getInverseEditOperations();
		var srcRange = inverseEditOperations[0].range;
		return new Selection(
			srcRange.endLineNumber + this._lineNumberDeltaOffset,
			srcRange.endColumn + this._columnDeltaOffset,
			srcRange.endLineNumber + this._lineNumberDeltaOffset,
			srcRange.endColumn + this._columnDeltaOffset
		);
	}
}

export class ReplaceCommandThatPreservesSelection extends ReplaceCommand {

A
Alex Dima 已提交
91
	private _initialSelection: editorCommon.IEditorSelection;
E
Erich Gamma 已提交
92 93
	private _selectionId: string;

A
Alex Dima 已提交
94
	constructor(editRange: editorCommon.IEditorRange, text: string, initialSelection: editorCommon.IEditorSelection) {
E
Erich Gamma 已提交
95 96 97 98
		super(editRange, text);
		this._initialSelection = initialSelection;
	}

A
Alex Dima 已提交
99
	public getEditOperations(model: editorCommon.ITokenizedModel, builder: editorCommon.IEditOperationBuilder): void {
E
Erich Gamma 已提交
100 101 102 103 104
		super.getEditOperations(model, builder);

		this._selectionId = builder.trackSelection(this._initialSelection);
	}

A
Alex Dima 已提交
105
	public computeCursorState(model: editorCommon.ITokenizedModel, helper: editorCommon.ICursorStateComputerData): editorCommon.IEditorSelection {
E
Erich Gamma 已提交
106 107 108
		return helper.getTrackedSelection(this._selectionId);
	}
}