Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Bug 528093 - Provide pluggable 'Declaration' command
  • Loading branch information
mrennie committed Dec 5, 2017
1 parent fbd17ad commit c0b4c98
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 32 deletions.
@@ -1,6 +1,6 @@
/*******************************************************************************
* @license
* Copyright (c) 2014, 2015 IBM Corporation and others.
* Copyright (c) 2014, 2017 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 @@ -48,13 +48,15 @@ define([
}

Objects.mixin(OpenDeclarationCommand.prototype, {
/* override */
execute: function(editorContext, options) {
var that = this;

/**
* @callback
*/
findDeclaration: function findDeclaration(editorContext, options) {
var deferred = new Deferred();
editorContext.getText().then(function(text) {
return that._findDecl(editorContext, options, text, deferred);
}, deferred.reject);
return this._findDecl(editorContext, options, text, deferred);
}.bind(this), deferred.reject);
return deferred;
},

Expand Down Expand Up @@ -95,13 +97,14 @@ define([
if(response.declaration.guess) {
return deferred.reject({Severity: 'Warning', Message: Messages['noDeclFound']}); //$NON-NLS-1$
}
var opts = Object.create(null);
opts.start = response.declaration.start;
opts.end = response.declaration.end;
var value = Object.create(null);
value.start = response.declaration.start;
value.end = response.declaration.end;
if(this.openMode !== null && typeof this.openMode !== 'undefined') {
opts.mode = this.openMode;
value.mode = this.openMode;
}
return deferred.resolve(editorContext.openEditor(response.declaration.file, opts));
value.file = response.declaration.file;
return deferred.resolve(value);
} else if (response.declaration.origin) {
if(response.declaration.guess) {
return deferred.reject({Severity: 'Warning', Message: Messages['noDeclFound']}); //$NON-NLS-1$
Expand Down
Expand Up @@ -692,24 +692,13 @@ define([
}
);

provider.registerServiceProvider("orion.edit.command", //$NON-NLS-1$
new OpenDeclCommand.OpenDeclarationCommand(ternWorker, "replace"), //$NON-NLS-1$
{
name: javascriptMessages["openDeclName"],
tooltip: javascriptMessages['openDeclTooltip'],
id: "open.js.decl", //$NON-NLS-1$
key: [114, false, false, false, false],
contentType: ['application/javascript', 'text/html'] //$NON-NLS-1$ //$NON-NLS-2$
provider.registerServiceProvider("orion.edit.open.declaration",
new OpenDeclCommand.OpenDeclarationCommand(ternWorker, "replace"), {
contentType: ["application/javascript", "text/html"],
id: "orion.open.declaration.js"
}
);

provider.registerServiceProvider("orion.edit.command.category", //$NON-NLS-1$
{},
{
id: "js.references", //$NON-NLS-1$
name: javascriptMessages['referencesMenuName'],
tooltip: javascriptMessages['referencesMenuTooltip']
});
var refscommand = new RefsCommand(ternWorker,
astManager,
scriptresolver,
Expand All @@ -718,7 +707,6 @@ define([

provider.registerServiceProvider("orion.edit.references",
refscommand, {
name: javascriptMessages['projectRefsTooltip'],
contentType: ["application/javascript", "text/html"],
id: "orion.references.js"
}
Expand Down
Expand Up @@ -118,7 +118,10 @@ define({
"FormatTooltip":"Format editor contents",
"References" : "References",
"ReferencesTooltip":"Find references in project",
"FindReferences": "Find references",
"FindReferences": "Finding references",
"OpenDeclaration": "Open Declaration",
"OpenDeclarationTooltip": "Open the declaration for the current selection",
"FindDeclaration": "Finding declaration",
"Cancel":"Cancel",
"Yes":"Yes",
"No":"No",
Expand Down
50 changes: 48 additions & 2 deletions bundles/org.eclipse.orion.client.ui/web/orion/editorCommands.js
@@ -1,6 +1,6 @@
/*******************************************************************************
* @license
* Copyright (c) 2011, 2015 IBM Corporation and others.
* Copyright (c) 2011, 2017 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 @@ -152,6 +152,8 @@ define([
this.blamer = options.blamer;
this.formatter = options.formatter;
this.references = options.references;
this.openDecl = options.openDecl;

var that = this;
this.listener = {
onServiceAdded: function(event) {
Expand Down Expand Up @@ -185,6 +187,8 @@ define([
this._createOpenRecentCommand();
this._createSwitchWorkspaceCommand();
this._createReferencesCommand();
this._createOpenDeclCommand();

return this._createEditCommands();
},
//TODO: We need a better way invoke side bar action
Expand All @@ -211,6 +215,7 @@ define([
this.blamer = target.blamer;
this.formatter = target.formatter;
this.references = target.references;
this.openDecl = target.openDecl;
this.textSearcher = target.textSearcher;

if (this._recreateEditCommands) {
Expand Down Expand Up @@ -265,6 +270,7 @@ define([
commandRegistry.registerCommandContribution(this.toolbarId , "orion.edit.blame", 2, "orion.menuBarToolsGroup", false, new mKeyBinding.KeyBinding('b', true, true), new mCommandRegistry.URLBinding("blame", "blame"), this); //$NON-NLS-4$ //$NON-NLS-3$ //$NON-NLS-2$ //$NON-NLS-1$ //$NON-NLS-5$
commandRegistry.registerCommandContribution(this.toolbarId , "orion.edit.diff", 3, "orion.menuBarToolsGroup", false, new mKeyBinding.KeyBinding('d', true, true), new mCommandRegistry.URLBinding("diff", "diff"), this); //$NON-NLS-4$ //$NON-NLS-3$ //$NON-NLS-2$ //$NON-NLS-1$ //$NON-NLS-5$
commandRegistry.registerCommandContribution(this.toolbarId , "orion.edit.references", 5, "orion.menuBarToolsGroup", false, new mKeyBinding.KeyBinding('y', true, true), null, this);
commandRegistry.registerCommandContribution(this.toolbarId , "orion.edit.open.declaration", 5, "orion.menuBarToolsGroup", false, new mKeyBinding.KeyBinding(114, false, false), null, this);

// 'Delimiters' cascade
var index = 0;
Expand Down Expand Up @@ -322,7 +328,8 @@ define([
commandRegistry.registerCommandContribution(this.editorContextMenuId, "orion.edit.reloadWithEncoding", 1000, "orion.editorContextMenuGroup/orion.editorContextMenuToolsGroup"); //$NON-NLS-1$ //$NON-NLS-2$
commandRegistry.registerCommandContribution(this.editorContextMenuId , "orion.edit.blame", 1, "orion.editorContextMenuGroup/orion.editorContextMenuToolsGroup", false); //$NON-NLS-1$ //$NON-NLS-2$
commandRegistry.registerCommandContribution(this.editorContextMenuId , "orion.edit.diff", 2, "orion.editorContextMenuGroup/orion.editorContextMenuToolsGroup", false); //$NON-NLS-1$ //$NON-NLS-2$
commandRegistry.registerCommandContribution(this.editorContextMenuId , "orion.edit.references", 4, "orion.editorContextMenuGroup/orion.editorContextMenuToolsGroup", false);
commandRegistry.registerCommandContribution(this.editorContextMenuId , "orion.edit.references", 5, "orion.editorContextMenuGroup/orion.editorContextMenuToolsGroup", false);
commandRegistry.registerCommandContribution(this.editorContextMenuId , "orion.edit.open.declaration", 4, "orion.editorContextMenuGroup/orion.editorContextMenuToolsGroup", false);

// 'Delimiters' cascade
commandRegistry.addCommandGroup(this.editorContextMenuId, "orion.editorContextMenuDelimitersGroup", 999, messages["Convert Line Delimiters"], "orion.editorContextMenuGroup/orion.editorContextMenuToolsGroup"); //$NON-NLS-2$ //$NON-NLS-1$ //$NON-NLS-0$
Expand Down Expand Up @@ -1020,6 +1027,45 @@ define([
});
this.commandService.addCommand(refsCommand);
},

/**
* @name _createReferencesCommand
* @description Creates the retargettable 'References' command
* @function
* @private
* @since 17.0
*/
_createOpenDeclCommand: function _createOpenDeclCommand(){
var declCommand = new mCommands.Command({
name: messages.OpenDeclaration,
tooltip: messages.OpenDeclarationTooltip,
id: "orion.edit.open.declaration",
parameters: new mCommandRegistry.ParametersDescription([new mCommandRegistry.CommandParameter('declaration', 'boolean')], {clientCollect: true}),
visibleWhen: /** @callback */ function(items, data) {
var inputManager = data.handler.inputManager || this.inputManager;
if (inputManager && inputManager.getReadOnly()) {
return false;
}
var editor = data.handler.editor || this.editor;
var decl = data.handler.openDecl || this.openDecl;
return editor && editor.installed && decl && decl.isVisible();
}.bind(this),
callback: /* @callback */ function(data) {
var statusService = this.serviceRegistry.getService("orion.page.message");
var progress = this.serviceRegistry.getService("orion.page.progress"); //$NON-NLS-0$
var msg = i18nUtil.formatMessage(messages.running, messages.FindDeclaration);
var serviceCall = this.openDecl.findDeclaration(data);
if (statusService) {
statusService.createProgressMonitor(serviceCall, msg);
}
progress.showWhile(serviceCall, msg).then(function(result) {
return this.editor.getEditorContext().openEditor(result.file, result);
}.bind(this));
this.editor.focus();
}.bind(this)
});
this.commandService.addCommand(declCommand);
},

_createDiffCommand: function(){
var that = this;
Expand Down
7 changes: 5 additions & 2 deletions bundles/org.eclipse.orion.client.ui/web/orion/editorView.js
Expand Up @@ -48,7 +48,8 @@ define([
'embeddedEditor/helper/memoryFileSysConst',
'orion/objects',
'orion/formatter',
'orion/references'
'orion/references',
'orion/openDeclaration'
], function(
messages,
mEditor, mAnnotations, mEventTarget, mTextView, mTextModelFactory, mEditorFeatures, mHoverFactory, mContentAssist,
Expand All @@ -57,7 +58,8 @@ define([
mDispatcher, EditorContext, Highlight,
mMarkOccurrences, mSyntaxchecker, LiveEditSession,
mProblems, mBlamer, mDiffer,
mKeyBinding, util, Deferred, mContextMenu, mMetrics, mCommonPreferences, memoryFileSysConst, objects, mFormatter, mReferences
mKeyBinding, util, Deferred, mContextMenu, mMetrics, mCommonPreferences, memoryFileSysConst, objects, mFormatter, mReferences,
mOpenDecl
) {
var inMemoryFilePattern = memoryFileSysConst.MEMORY_FILE_PATTERN;
var Dispatcher = mDispatcher.Dispatcher;
Expand Down Expand Up @@ -557,6 +559,7 @@ define([
this.differ = new mDiffer.Differ(serviceRegistry, inputManager, editor);
this.formatter = new mFormatter.Formatter(serviceRegistry, inputManager, editor);
this.references = new mReferences.References(serviceRegistry, inputManager, editor);
this.openDecl = new mOpenDecl.OpenDeclaration(serviceRegistry, inputManager, editor);

this.problemService = new mProblems.ProblemService(serviceRegistry, this.problemsServiceID);
var markerService = serviceRegistry.getService(this.problemsServiceID);
Expand Down
86 changes: 86 additions & 0 deletions bundles/org.eclipse.orion.client.ui/web/orion/openDeclaration.js
@@ -0,0 +1,86 @@
/*******************************************************************************
* @license
* Copyright (c) 2017 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
* License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html).
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
/*eslint-env browser, amd*/
/*eslint-disable no-implicit-coercion */
define([], function() {

/**
* @name OpenDeclaration
* @description Creates a new instance of the OpenDeclaration command
* @constructor
* @param {ServiceRegistry} serviceRegistry The backing service registry
* @param {InputManager} inputManager The backing input manager to get content type information from
* @param {EditorView} editor The editor view to work against
* @since 17.0
*/
function OpenDeclaration(serviceRegistry, inputManager, editor) {
this.serviceRegistry = serviceRegistry;
this.inputManager = inputManager;
this.editor = editor;
}

function _getDelegate(data) {
var inputManagerContentType = data.inputManager.getContentType();
var refs = data.serviceRegistry.getServiceReferences("orion.edit.open.declaration");
for (var i = 0; i < refs.length; i++) {
var serviceReference = refs[i];
var contentTypes = serviceReference.getProperty("contentType");
if (inputManagerContentType && inputManagerContentType.id) {
var inputContentType = inputManagerContentType.id;
if (Array.isArray(contentTypes)) {
for (var j = 0, max = contentTypes.length; j < max; j++) {
if (contentTypes[j] === inputContentType) {
return data.serviceRegistry.getService(serviceReference);
}
}
} else if (inputContentType === contentTypes) {
return data.serviceRegistry.getService(serviceReference);
}
}
}
return null;
}

OpenDeclaration.prototype = {
/**
* @name isVisible
* @description Returns if this command should be visible or not
* @function
* @returns {boolean} 'true' if there is a delegate for the backing content type, 'false' otherwise
*/
isVisible: function isVisible() {
return !!_getDelegate(this);
},
/**
* @name findDeclaration
* @description The delegate function that calls the service contribution
* @function
* @returns {?} The declaration location object
*/
findDeclaration: function findDeclaration() {
var service = _getDelegate(this);
if (service) {
var selection = this.editor.getSelection();
var context = {
start: selection.start,
end: selection.end,
offset: selection.start,
input: this.inputManager.getInput()
};
return service.findDeclaration(this.editor.getEditorContext(), context);
}
}
};
return {
OpenDeclaration: OpenDeclaration
};
});

0 comments on commit c0b4c98

Please sign in to comment.