editorState.ts 2.3 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';

A
Alex Dima 已提交
7
import * as strings from 'vs/base/common/strings';
J
Johannes Rieken 已提交
8 9
import { Position } from 'vs/editor/common/core/position';
import { Range } from 'vs/editor/common/core/range';
10
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
E
Erich Gamma 已提交
11

A
Alex Dima 已提交
12 13 14 15 16 17 18 19
export const enum CodeEditorStateFlag {
	Value = 1,
	Selection = 2,
	Position = 4,
	Scroll = 8
}

export class EditorState {
E
Erich Gamma 已提交
20

A
Alex Dima 已提交
21
	private readonly flags: number;
E
Erich Gamma 已提交
22

A
Alex Dima 已提交
23 24 25 26 27
	private readonly position: Position;
	private readonly selection: Range;
	private readonly modelVersionId: string;
	private readonly scrollLeft: number;
	private readonly scrollTop: number;
E
Erich Gamma 已提交
28

29
	constructor(editor: ICodeEditor, flags: number) {
E
Erich Gamma 已提交
30 31
		this.flags = flags;

A
Alex Dima 已提交
32
		if ((this.flags & CodeEditorStateFlag.Value) !== 0) {
A
Alex Dima 已提交
33
			let model = editor.getModel();
A
Alex Dima 已提交
34 35 36 37 38 39 40 41 42 43 44 45
			this.modelVersionId = model ? strings.format('{0}#{1}', model.uri.toString(), model.getVersionId()) : null;
		}
		if ((this.flags & CodeEditorStateFlag.Position) !== 0) {
			this.position = editor.getPosition();
		}
		if ((this.flags & CodeEditorStateFlag.Selection) !== 0) {
			this.selection = editor.getSelection();
		}
		if ((this.flags & CodeEditorStateFlag.Scroll) !== 0) {
			this.scrollLeft = editor.getScrollLeft();
			this.scrollTop = editor.getScrollTop();
		}
E
Erich Gamma 已提交
46 47
	}

J
Johannes Rieken 已提交
48
	private _equals(other: any): boolean {
E
Erich Gamma 已提交
49

J
Johannes Rieken 已提交
50
		if (!(other instanceof EditorState)) {
E
Erich Gamma 已提交
51 52
			return false;
		}
A
Alex Dima 已提交
53
		let state = <EditorState>other;
E
Erich Gamma 已提交
54

J
Johannes Rieken 已提交
55
		if (this.modelVersionId !== state.modelVersionId) {
E
Erich Gamma 已提交
56 57
			return false;
		}
J
Johannes Rieken 已提交
58
		if (this.scrollLeft !== state.scrollLeft || this.scrollTop !== state.scrollTop) {
E
Erich Gamma 已提交
59 60
			return false;
		}
J
Johannes Rieken 已提交
61
		if (!this.position && state.position || this.position && !state.position || this.position && state.position && !this.position.equals(state.position)) {
E
Erich Gamma 已提交
62 63
			return false;
		}
J
Johannes Rieken 已提交
64
		if (!this.selection && state.selection || this.selection && !state.selection || this.selection && state.selection && !this.selection.equalsRange(state.selection)) {
E
Erich Gamma 已提交
65 66 67 68 69
			return false;
		}
		return true;
	}

70
	public validate(editor: ICodeEditor): boolean {
E
Erich Gamma 已提交
71 72 73
		return this._equals(new EditorState(editor, this.flags));
	}
}