我已经实现了一个 hack,可以使用 .tern-config 文件为特定项目加载文件。
它不适用于包含 * 的值,因为这需要更改服务器。
这只是一个简单的 hack,很容易破解,但目前对我有用。
这是我如何更改版本 9 构建 S20150504-1254 的代码:
在 org.eclipse.orion.client.javascript_1.0.0.v20150504-1644.jar 中
文件:/web/javascript/handlers/ternOccurrencesHandler.js
function sortProposals if 语句检查文件是否是当前打开的文件
只需检查它是否是以 /file/ 开头的值
替换:
if(_o === args.meta.location) {
与:
if(/^\/file\//.test(_o)) {
查找函数 computeProposals 并在该函数之前添加以下代码:
function getFile(url){
return new Promise(function(resolve,reject){
var xhr = new XMLHttpRequest();
xhr.open('GET',url);
xhr.addEventListener("load", function(e){
//@todo: have to check something I dont think this should be called on a 404 but it is
console.log('ok, done:',xhr.responseURL);
if(xhr.status!==200){
reject('file not found:',xhr.responseURL);
return;
}
resolve(xhr.responseText);
}, false);
xhr.addEventListener("error", function(e){
console.log('an error:',e);
reject(e);
}, false);
xhr.send();
});
}
var loadFilesInTern = (function(){
var loadedConfigs = [];
var currentConfig = '';
function loadJsFileIntoTernServer(fileName,ternserver){
return getFile(fileName)
.then(function resolve(val){
ternserver.addFile(fileName,val);
},function reject(err){
console.log('an error:',fileName);
return true;
});
}
return function(location,ternserver){
var p = new Promise(function(resolve){resolve(true);});
rootPath = location.split('/').slice(0,4).join('/');
console.log('got rootpath, trying to get tern-config from:',rootPath+'/.tern-config');
return p
.then(function(){
if(!loadedConfigs[rootPath]){
return getFile(rootPath+'/.tern-config');
}else {
return loadedConfigs[rootPath];
}
})
.then(function(config){
loadedConfigs[rootPath]=config;
if(config===currentConfig){
return;
};
currentConfig = config;
var settings = JSON.parse(config);
var promises = [];
settings.loadEagerly.forEach(function(fileName){
promises.push(loadJsFileIntoTernServer(rootPath + '/' + fileName,ternserver));
});
return Promise.all(promises);
})
.then(null,function reject(e){
console.log('an error:',e);
return true;
});
p.resolve('start');
};
}());
在计算提案之前加载配置(第一次)
function computeProposals(ternserver, args, callback) {
if(ternserver) {
loadFilesInTern(args.meta.location,ternserver)
.then(function(){
console.log('ternserver is now:',ternserver);
ternserver.request({
//... rest of the computeProposals code
});//close the then
} else {//original code from computeProposals
callback({request: 'completions', proposals:[]});
}
您可以在项目目录中创建一个 .tern-config 并添加 loadEagerly 文件:
{
"libs": [
"browser",
"ecma5",
"jquery"
],
"loadEagerly": [
"goog/base.js",
"somefile.js",
"another file.js"
],
"plugins": {
"requirejs": {
"baseURL": "./",
"paths": {}
}
}
}
此时会忽略库和插件,但会加载其他文件(请注意,这是一个简单的 hack,在某些情况下可能会中断)