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
|
/*******************************************************************************
* 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(['xtext/services/XtextService', 'jquery'], function(XtextService, jQuery) {
/**
* Service class for formatting text.
*/
function FormattingService(serviceUrl, resourceId, updateService) {
this.initialize(serviceUrl, 'format', resourceId, updateService);
};
FormattingService.prototype = new XtextService();
FormattingService.prototype._initServerData = function(serverData, editorContext, params) {
var selection = params.selection ? params.selection : editorContext.getSelection();
if (selection.end > selection.start) {
serverData.selectionStart = selection.start;
serverData.selectionEnd = selection.end;
}
return {
httpMethod: 'POST'
};
};
FormattingService.prototype._processResult = function(result, editorContext) {
// The text update may be asynchronous, so we have to compute the new text ourselves
var newText;
if (result.replaceRegion) {
var fullText = editorContext.getText();
var start = result.replaceRegion.offset;
var end = result.replaceRegion.offset + result.replaceRegion.length;
editorContext.setText(result.formattedText, start, end);
newText = fullText.substring(0, start) + result.formattedText + fullText.substring(end);
} else {
editorContext.setText(result.formattedText);
newText = result.formattedText;
}
var listeners = editorContext.updateServerState(newText, result.stateId);
for (var i = 0; i < listeners.length; i++) {
listeners[i]({});
}
};
return FormattingService;
});
|