Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Bug 496655 - [tern] Allow plugins not specified in ternDefaults.js to…
… be loaded
  • Loading branch information
mrennie committed May 10, 2017
1 parent 33cba04 commit f174862
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 45 deletions.
Expand Up @@ -393,6 +393,15 @@ define([
};
var fileClient = serviceRegistry.getService("orion.core.file.client"); //$NON-NLS-1$
if (typeof request.args.file === 'object') {
if(request.args.file.tourl) {
var f = request.args.file.file;
response.args.file = request.args.file;
response.args.file.url = f;
if(/^[/]?file/.test(f)) {
response.args.file.url = new URL(f, self.location.origin).href;
}
return ternWorker.postMessage(response);
}
var _l = request.args.file.logical;
response.args.logical = _l;
if (request.args.file.env === 'node') {
Expand All @@ -417,7 +426,7 @@ define([
}
}
} else {
_normalRead(response, request.args.file, fileClient);
_normalRead(response, request.args.file, fileClient);
}
}
/**
Expand Down
Expand Up @@ -103,7 +103,6 @@ define([
plugins: plugins,
defs: defs,
defNames: defNames,
pluginsDir: 'tern/plugin/',
serverOptions: _serverOptions
};
});
Expand Up @@ -74,12 +74,10 @@ function(Tern, defaultOptions, Deferred, Objects, Serialize, Messages, i18nUtil)
var options = defaultOptions.serverOptions();
options.getFile = _getFile;

var pluginsDir = defaultOptions.pluginsDir;
var defNames = [], plugins, projectLoc;
if (jsonOptions) {
projectLoc = jsonOptions.projectLoc;
plugins = jsonOptions.plugins;
pluginsDir = jsonOptions.pluginsDir;
if(jsonOptions.libs) {
mergeArray(defNames, jsonOptions.libs);
}
Expand Down Expand Up @@ -163,7 +161,7 @@ function(Tern, defaultOptions, Deferred, Objects, Serialize, Messages, i18nUtil)
fallback(err);
}
}
Deferred.all(loadPlugins(options.plugins, pluginsDir)).then(/* @callback */ function(plugins) {
Deferred.all(loadPlugins(options.plugins, projectLoc)).then(function() {
Deferred.all(loadDefs(defNames, projectLoc)).then(function(json) {
options.defs = json;
startAndMessage(options);
Expand Down Expand Up @@ -695,48 +693,45 @@ function(Tern, defaultOptions, Deferred, Objects, Serialize, Messages, i18nUtil)
/**
* @description Loads the plugins listed in the given plugins object
* @param {Object} plugins The object of plugins
* @param {String} pluginsDir The base directory to load plugins from, if not defined, the default of 'tern/plugin/' is assumed
* @param {String} projectDir The base directory of the project
* @returns {Promise} The promise to resolve all of the plugin loads
* @since 11.0
*/
function loadPlugins(plugins, pluginsDir) {
function loadPlugins(plugins, projectDir) {
var promises = [];
//TODO disable for now, as it does not work
// if(plugins) {
// Object.keys(plugins).forEach(function(key) {
// if(defaultOptions.plugins.required[key] || defaultOptions.plugins.optional[key]) {
// //default plugins are statically loaded
// return;
// }
// var plugin = plugins[key];
// if(!plugin || typeof plugin !== 'object') {
// return;
// }
// var loc = plugin.location;
// if(typeof loc !== 'string') {
// if(typeof pluginsDir === 'string') {
// loc = pluginsDir + key;
// } else {
// //assume it is in /tern/plugin/
// loc = 'tern/plugin/' + key; //$NON-NLS-1$
// }
// }
// var deferred = new Deferred();
// try {
// promises.push(deferred);
// requirejs([loc], function(_) {
// deferred.resolve(_);
// },
// function(err) {
// deferred.reject(err);
// });
// }
// catch(err) {
// post(err);
// deferred.reject(err);
// }
// });
// }
if(plugins) {
Object.keys(plugins).forEach(function(key) {
if(defaultOptions.plugins.required[key] || defaultOptions.plugins.optional[key]) {
//default plugins are statically loaded
return;
}
var plugin = plugins[key];
if(!plugin || typeof plugin !== 'object') {
return;
}
if(typeof plugin.location === 'string' && typeof key === 'string') {
var deferred = new Deferred(),
loc = /^.tern-plugins/.test(plugin.location) ? projectDir + plugin.location : plugin.location;
promises.push(deferred);
_getFile({file: loc, tourl: true}, function(err, meta) {
var config = {};
loc = !err && meta && meta.file && meta.file.url ? meta.file.url : plugin.location;
config[key] = loc;
requirejs.config({
paths: config
});
requirejs([loc], /* @callback */ function(p) {
return deferred.resolve(key); //doesn't matter what we resolve, just that the plugin loaded into require
}, function(err) {
delete plugins[key];
promises.pop();
post("Tern plugin '"+key+"' from '"+loc+"'failed to load.\n\nReason:\n"+ Serialize.serializeError(err));
return deferred.resolve(err);
});
});
}
});
}
return promises;
}

Expand All @@ -763,8 +758,6 @@ function(Tern, defaultOptions, Deferred, Objects, Serialize, Messages, i18nUtil)
if(idx > -1) {
//a default def, get it
_defs.push(new Deferred().resolve(defaultOptions.defs[idx]));
} else {
//TODO do we want to support loading defs from arbitrary locations?
}
}
});
Expand Down

0 comments on commit f174862

Please sign in to comment.