Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Bug 527206 - Merge LSP support into master - formatter.js changes
Signed-off-by: Remy Suen <remy.suen@gmail.com>
  • Loading branch information
rcjsuen committed Jan 13, 2018
1 parent 3313179 commit f9d57f0
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 12 deletions.
10 changes: 6 additions & 4 deletions bundles/org.eclipse.orion.client.ui/web/orion/editorView.js
@@ -1,6 +1,6 @@
/*******************************************************************************
* @license
* Copyright (c) 2010, 2016, 2017 IBM Corporation and others.
* Copyright (c) 2010, 2018 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials are made
* available under the terms of the Eclipse Public License v1.0
* (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution
Expand Down Expand Up @@ -49,7 +49,8 @@ define([
'orion/objects',
'orion/formatter',
'orion/references',
'orion/openDeclaration'
'orion/openDeclaration',
'lsp/languageServerRegistry'
], function(
messages,
mEditor, mAnnotations, mEventTarget, mTextView, mTextModelFactory, mEditorFeatures, mHoverFactory, mContentAssist,
Expand All @@ -59,7 +60,7 @@ define([
mMarkOccurrences, mSyntaxchecker, LiveEditSession,
mProblems, mBlamer, mDiffer,
mKeyBinding, util, Deferred, mContextMenu, mMetrics, mCommonPreferences, memoryFileSysConst, objects, mFormatter, mReferences,
mOpenDecl
mOpenDecl, mLanguageServerRegistry
) {
var inMemoryFilePattern = memoryFileSysConst.MEMORY_FILE_PATTERN;
var Dispatcher = mDispatcher.Dispatcher;
Expand Down Expand Up @@ -351,6 +352,7 @@ define([
var progress = this.progress;
var contentTypeRegistry = this.contentTypeRegistry;
var editorCommands = this.editorCommands;
var languageServerRegistry = new mLanguageServerRegistry.LanguageServerRegistry(serviceRegistry, this.problemsServiceID);

var textViewFactory = function() {
var options = that.updateViewOptions(that.settings);
Expand Down Expand Up @@ -557,7 +559,7 @@ define([

this.blamer = new mBlamer.Blamer(serviceRegistry, inputManager, editor);
this.differ = new mDiffer.Differ(serviceRegistry, inputManager, editor);
this.formatter = new mFormatter.Formatter(serviceRegistry, inputManager, editor);
this.formatter = new mFormatter.Formatter(serviceRegistry, inputManager, editor, languageServerRegistry);
this.references = new mReferences.References(serviceRegistry, inputManager, editor);
this.openDecl = new mOpenDecl.OpenDeclaration(serviceRegistry, inputManager, editor);

Expand Down
76 changes: 70 additions & 6 deletions bundles/org.eclipse.orion.client.ui/web/orion/formatter.js
@@ -1,6 +1,6 @@
/*******************************************************************************
* @license
* Copyright (c) 2016, 2017 IBM Corporation and others.
* Copyright (c) 2016, 2018 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials are made
* available under the terms of the Eclipse Public License v1.0
* (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution
Expand All @@ -12,13 +12,15 @@
/*eslint-env browser, amd*/

define ([
'orion/Deferred'
], function(Deferred) {
'orion/Deferred',
'lsp/utils'
], function(Deferred, Utils) {

function Formatter(serviceRegistry, inputManager, editor) {
function Formatter(serviceRegistry, inputManager, editor, languageServerRegistry) {
this.serviceRegistry = serviceRegistry;
this.inputManager = inputManager;
this.editor = editor;
this.languageServerRegistry = languageServerRegistry;
}

Formatter.prototype = {
Expand All @@ -43,17 +45,79 @@ define ([
}
return null;
},
getLspFormatter : function() {
// check lsp extensions
var inputManagerContentType = this.inputManager.getContentType();
return this.languageServerRegistry.getServerByContentType(inputManagerContentType);
},
isVisible: function() {
return !!this.getFormatter();
if (!!this.getFormatter()) {
return true;
}
var languageServer = this.getLspFormatter();
if (languageServer) {
return languageServer.isFormatDocumentEnabled() || languageServer.isRangeFormatDocumentEnabled();
}
return false;
},

doFormat: function() {
var selection = this.editor.getSelection();
var service = this.getFormatter();
if (service) {
var selection = this.editor.getSelection();
var context = {start: selection.start, end: selection.end};
return service.format(this.editor.getEditorContext(), context);
}
// check lsp formatters
var lspFormatter = this.getLspFormatter();
if (lspFormatter) {
var textView = this.editor.getTextView();
var options = {
tabSize: textView.getOptions("tabSize"),
insertSpaces: textView.getOptions("expandTab")
};
if (selection.start !== selection.end) {
// we want to format the selected text only
var start = Utils.getPosition(this.editor, selection.start);
var end = Utils.getPosition(this.editor, selection.end);
return lspFormatter.rangeFormatting(this.inputManager.getFileMetadata().Location, start, end, options).then(
function(edits) {
if (Array.isArray(edits) && edits.length !== 0) {
this.editor.setText({
text: edits.map(function(e) {
return e.newText;
}),
selection: edits.map(function(edit) {
var range = edit.range;
return {
start: this.editor.getLineStart(range.start.line) + range.start.character,
end: this.editor.getLineStart(range.end.line) + range.end.character
};
}.bind(this)),
preserveSelection: true
});
}
}.bind(this));
}
return lspFormatter.formatDocument(this.inputManager.getFileMetadata().Location, options).then(
function(edits) {
if (Array.isArray(edits) && edits.length !== 0) {
this.editor.setText({
text: edits.map(function(e) {
return e.newText;
}),
selection: edits.map(function(edit) {
var range = edit.range;
return {
start: this.editor.getLineStart(range.start.line) + range.start.character,
end: this.editor.getLineStart(range.end.line) + range.end.character
};
}.bind(this)),
preserveSelection: true
});
}
}.bind(this));
}
return new Deferred().resolve();
}
};
Expand Down
4 changes: 2 additions & 2 deletions bundles/org.eclipse.orion.client.ui/web/orion/inputManager.js
@@ -1,6 +1,6 @@
/*******************************************************************************
* @license
* Copyright (c) 2010, 2017 IBM Corporation and others.
* Copyright (c) 2010, 2018 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials are made
* available under the terms of the Eclipse Public License v1.0
* (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution
Expand Down Expand Up @@ -784,7 +784,7 @@ define([
this._logMetrics("open"); //$NON-NLS-0$
this.dispatchEvent(evt);
this.editor = editor = evt.editor;
this._formatter = new mFormatter.Formatter(this.serviceRegistry, this, editor);
this._formatter = new mFormatter.Formatter(this.serviceRegistry, this, editor, this.languageServerRegistry);
if (!isDir) {
if (!noSetInput) {
editor.setInput(title, null, contents);
Expand Down

0 comments on commit f9d57f0

Please sign in to comment.