-
Notifications
You must be signed in to change notification settings - Fork 0
/
esprima-helper.js
71 lines (56 loc) · 2.1 KB
/
esprima-helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
var fs = require('fs');
var path = require('path');
var helper = require('./helper.js');
var EsprimaJavaScriptContentAssistProvider = require('./esprima/esprimaJsContentAssist.js').EsprimaJavaScriptContentAssistProvider;
// recorded file summaries,
// key: full path of a file
// value: summary generated by EsprimaJavaScriptContentAssistProvider.computeSummary
var fileSummaries = {};
var EsprimaHelper = function(fileName) {
this.fileName = fileName;
instance = this;
this.retrieveSummary = function(fileName) {
// get the summary of `file_name`, if it is not exists, then generate it if possible
fileName = helper.findFile(fileName + '.js');
if (fileName)
return instance.getOrCreateSummary(fileName);
else
return null;
},
this.indexers = {
retrieveGlobalSummaries : function(){},
retrieveSummary : this.retrieveSummary,
};
this.esprima = new EsprimaJavaScriptContentAssistProvider(this.indexers);
}
EsprimaHelper.prototype = {
// get or create a summary for the file
// `file_name` is a path relative to current editting file, such as `./test.js`
getOrCreateSummary: function(fileName) {
var summary = fileSummaries[fileName];
if (!summary) {
// generate summary for the file
if (!fs.existsSync(fileName)) {
// console.log(this.fileName + ' require unexists file: ' + fileName);
return null;
}
var fileContent = fs.readFileSync(fileName, 'utf-8');
summary = this.saveFileSummary(fileName, fileContent,false);
}
return summary;
},
saveFileSummary: function(fileName, fileContent, forceSave) {
// if the summary is exists, then return
if (fileSummaries[fileName] && !forceSave)
return;
var esprimaHelper = new EsprimaHelper(fileName);
var summary = esprimaHelper.esprima.computeSummary(fileContent, fileName);
fileSummaries[fileName] = summary;
return summary;
},
computeCompletions: function(buf, offset, prefix) {
return this.esprima.computeCompletions(buf, offset, prefix);
}
};
EsprimaHelper.prototype.constructor = EsprimaHelper;
module.exports = EsprimaHelper;