/******************************************************************************* * 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; });