-
Notifications
You must be signed in to change notification settings - Fork 63
/
tern_wrapper.js
232 lines (204 loc) · 5.88 KB
/
tern_wrapper.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/env node
var fs = require('fs');
var glob = require('glob');
var minimatch = require('minimatch');
var path = require('path');
var readline = require('readline');
var resolveFrom = require('resolve-from');
var tern = require('tern');
var DIST_DIR = path.resolve(__dirname, path.join('node_modules', 'tern'));
var PROJECT_FILE_NAME = '.tern-project';
var PROJECT_DIR = (function() {
var dir = process.cwd();
while (true) {
try {
if (fs.statSync(path.resolve(dir, PROJECT_FILE_NAME)).isFile()) {
return dir;
}
} catch(e) {}
var shorter = path.dirname(dir);
if (shorter == dir) {
return process.cwd();
}
dir = shorter;
}
})();
function genConfig() {
var defaultConfig = {
libs: [],
loadEagerly: false,
plugins: {doc_comment: true},
ecmaScript: true,
ecmaVersion: 6,
dependencyBudget: tern.defaultOptions.dependencyBudget
};
function merge(base, value) {
if (!base) return value;
if (!value) return base;
var result = {};
for (var prop in base) result[prop] = base[prop];
for (var prop in value) result[prop] = value[prop];
return result;
}
function readConfig(fileName) {
var data = readJSON(fileName);
for (var option in defaultConfig) {
if (!data.hasOwnProperty(option))
data[option] = defaultConfig[option];
else if (option == 'plugins')
data[option] = merge(defaultConfig[option], data[option]);
}
return data;
}
var home = process.env.HOME || process.env.USERPROFILE;
if (home && fs.existsSync(path.resolve(home, '.tern-config'))) {
defaultConfig = readConfig(path.resolve(home, '.tern-config'));
}
var projectFile = path.resolve(PROJECT_DIR, PROJECT_FILE_NAME);
if (fs.existsSync(projectFile)) {
return readConfig(projectFile);
}
return defaultConfig;
}
function readJSON(fileName) {
var file = fs.readFileSync(fileName, 'utf8');
try {
return JSON.parse(file);
} catch (e) {
console.error('Bad JSON in ' + fileName + ': ' + e.message);
process.exit(1);
}
}
function findFile(file, projectDir, fallbackDir) {
var local = path.resolve(projectDir, file);
if (fs.existsSync(local)) return local;
var shared = path.resolve(fallbackDir, file);
if (fs.existsSync(shared)) return shared;
}
function findDefs(projectDir, config) {
var defs = [], src = config.libs.slice();
if (config.ecmaScript && src.indexOf('ecmascript') == -1) {
src.unshift('ecmascript');
}
for (var i = 0; i < src.length; ++i) {
var file = src[i];
if (!/\.json$/.test(file)) file = file + '.json';
var found = findFile(file, projectDir, path.resolve(DIST_DIR, 'defs'))
|| resolveFrom(projectDir, 'tern-' + src[i]);
if (!found) {
try {
found = require.resolve('tern-' + src[i]);
} catch (e) {
process.stderr.write('Failed to find library ' + src[i] + '.\n');
continue;
}
}
if (found) defs.push(readJSON(found));
}
return defs;
}
function loadPlugins(projectDir, config) {
var plugins = config.plugins, options = {};
for (var plugin in plugins) {
var val = plugins[plugin];
if (!val) continue;
var found = findFile(plugin + '.js', projectDir, path.resolve(DIST_DIR, 'plugin'))
|| resolveFrom(projectDir, 'tern-' + plugin);
if (!found) {
try {
found = require.resolve('tern-' + plugin);
} catch (e) {
process.stderr.write('Failed to find plugin ' + plugin + '.\n');
continue;
}
}
var mod = require(found);
if (mod.hasOwnProperty('initialize')) mod.initialize(DIST_DIR);
options[path.basename(plugin)] = val;
}
return options;
}
function startServer(dir, config) {
var defs = findDefs(dir, config);
var plugins = loadPlugins(dir, config);
var server = new tern.Server({
getFile: function(name, c) {
if (config.dontLoad && config.dontLoad.some(function(pat) { return minimatch(name, pat); })) {
c(null, '');
} else {
fs.readFile(path.resolve(dir, name), 'utf8', c);
}
},
normalizeFilename: function(name) {
var pt = path.resolve(dir, name);
try {
pt = fs.realPathSync(path.resolve(dir, name), true);
} catch(e) {}
return path.relative(dir, pt);
},
async: true,
defs: defs,
plugins: plugins,
projectDir: dir,
ecmaVersion: config.ecmaVersion,
dependencyBudget: config.dependencyBudget,
});
if (config.loadEagerly) config.loadEagerly.forEach(function(pat) {
glob.sync(pat, { cwd: dir }).forEach(function(file) {
server.addFile(file);
});
});
return server;
}
(function() {
var server = startServer(PROJECT_DIR, genConfig());
function complete(filename, line, col, data) {
var query = {
type: 'completions',
types: true,
guess: false,
docs: true,
file: '#0',
filter: true,
expandWordForward: false,
inLiteral: false,
end: {line: line, ch: col},
caseInsensitive: true
};
var file = {
type: 'full',
name: filename,
text: data,
};
var doc = {query: query, files: [file]};
server.request(doc, function(err, res) {
var info = [];
for (var i = 0; i < res.completions.length; i++) {
var completion = res.completions[i];
var comp = {word: completion.name, menu: completion.type};
if (completion.guess) {
comp.menu += ' ' + completion.guess;
}
if (completion.doc) {
comp.info = completion.doc;
}
info.push(comp);
}
console.log(JSON.stringify(info));
});
}
readline.createInterface({
input: process.stdin,
output: null,
historySize: 0,
terminal: false
})
.on('line', function(line) {
try {
var input = JSON.parse(line);
complete(input.filename, input.line, input.col, input.content);
} catch (e) {
console.log(JSON.stringify([]));
}
});
})();