aboutsummaryrefslogtreecommitdiffstats
path: root/language-web/src/main/js/xtext/CodeMirrorEditorContext.js
blob: b829c680e0129aa89b4bf021cb1bc0103082b6de (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*******************************************************************************
 * Copyright (c) 2015 itemis AG (http://www.itemis.eu) and others.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 *
 * SPDX-License-Identifier: EPL-2.0
 *******************************************************************************/

define([], function() {
	
	/**
	 * An editor context mediates between the Xtext services and the CodeMirror editor framework.
	 */
	function CodeMirrorEditorContext(editor) {
		this._editor = editor;
		this._serverState = {};
		this._serverStateListeners = [];
		this._dirty = false;
		this._dirtyStateListeners = [];
	};

	CodeMirrorEditorContext.prototype = {
		
		getServerState: function() {
			return this._serverState;
		},
		
		updateServerState: function(currentText, currentStateId) {
			this._serverState.text = currentText;
			this._serverState.stateId = currentStateId;
			return this._serverStateListeners;
		},
		
		addServerStateListener: function(listener) {
			this._serverStateListeners.push(listener);
		},
		
		getCaretOffset: function() {
			var editor = this._editor;
			return editor.indexFromPos(editor.getCursor());
		},
		
		getLineStart: function(lineNumber) {
			var editor = this._editor;
			return editor.indexFromPos({line: lineNumber, ch: 0});
		},
		
		getSelection: function() {
			var editor = this._editor;
        	return {
        		start: editor.indexFromPos(editor.getCursor('from')),
        		end: editor.indexFromPos(editor.getCursor('to'))
        	};
		},
		
		getText: function(start, end) {
			var editor = this._editor;
			if (start && end) {
				return editor.getRange(editor.posFromIndex(start), editor.posFromIndex(end));
			} else {
				return editor.getValue();
			}
		},
		
		isDirty: function() {
			return !this._clean;
		},
		
		setDirty: function(dirty) {
			if (dirty != this._dirty) {
				for (var i = 0; i < this._dirtyStateListeners.length; i++) {
					this._dirtyStateListeners[i](dirty);
				}
			}
			this._dirty = dirty;
		},
		
		addDirtyStateListener: function(listener) {
			this._dirtyStateListeners.push(listener);
		},
		
		clearUndoStack: function() {
			this._editor.clearHistory();
		},
		
		setCaretOffset: function(offset) {
			var editor = this._editor;
			editor.setCursor(editor.posFromIndex(offset));
		},
		
		setSelection: function(selection) {
			var editor = this._editor;
			editor.setSelection(editor.posFromIndex(selection.start), editor.posFromIndex(selection.end));
		},
		
		setText: function(text, start, end) {
			var editor = this._editor;
			if (!start)
				start = 0;
			if (!end)
				end = editor.getValue().length;
			var cursor = editor.getCursor();
			editor.replaceRange(text, editor.posFromIndex(start), editor.posFromIndex(end));
			editor.setCursor(cursor);
		}
		
	};
	
	return CodeMirrorEditorContext;
});