forked from MiguelCastillo/Brackets-Ternific
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTernProvider.js
328 lines (264 loc) · 9.81 KB
/
TernProvider.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
* Copyright (c) 2013 Miguel Castillo.
*
* Licensed under MIT
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
define(function (require, exports, module) {
"use strict";
var TernDemo = require("TernDemo"),
fileLoader = require("FileLoader");
/**
* @constructor
*
* TernProvider is a set of interfaces that facilitate the interaction
* with tern.
*/
function TernProvider(options) {
var _self = this;
_self.ready = $.Deferred();
_self.docs = [];
_self.currentDocument = null;
_self.onReady = _self.ready.promise().done;
}
TernProvider.prototype.clear = function() {
var _self = this;
_self.docs = [];
if (_self._server) {
_self._server.clear();
}
};
TernProvider.prototype.query = function (query) {
throw "Must implement";
};
TernProvider.prototype.findDocByProperty = function (_propName, data) {
var index = 0, length = this.docs.length;
for (index = 0; index < length; index++) {
if (this.docs[index][_propName] === data) {
return this.docs[index];
}
}
};
TernProvider.prototype.findDocByName = function (name) {
return this.findDocByProperty("name", name);
};
TernProvider.prototype.findDocByInstance = function (doc) {
return this.findDocByProperty("doc", doc);
};
TernProvider.prototype.findDocByCM = function (cm) {
return this.findDocByProperty("cm", cm);
};
TernProvider.prototype.register = function (cm, name) {
var _self = this;
var docMeta = _self.findDocByName(name);
//
// If the document has not been registered, then we set one up
//
if (!docMeta) {
docMeta = {
name: name,
cm: cm,
doc: cm.getDoc(),
changed: null
};
_self.docs.push(docMeta);
_self._server.addFile(docMeta.name, docMeta.doc.getValue());
}
//
// If the document exists but has not been registered, then we
// update the properties that need updating and setup up a change
// tracking callback.
// This particular case happens when a document is loaded as a
// dependency when resolved by tern, and later that document is
// opened in brackets, which will need registration for tracking
// changes.
//
// Only documents that are open in brackets and currently in focus
// are the ones that should be tracked for changes. Currently,
// if a document isn't open in brackets then we are going to assume
// that it is not being changed. I'm not worried about external
// editors opening documents and modifying them outside of the
// current instance of brackets.
//
else {
docMeta.cm = cm;
docMeta.doc = cm.getDoc();
docMeta.changed = null;
}
_self.currentDocument = docMeta;
TernDemo.setCurrentDocument(docMeta);
TernDemo.setDocs(_self.docs);
TernDemo.setServer(_self._server);
docMeta._trackChange = function (cm1, change) {
TernDemo.trackChange(docMeta.doc, change);
};
CodeMirror.on(docMeta.doc, "change", docMeta._trackChange);
return docMeta;
};
TernProvider.prototype.unregister = function (cm) {
var docMeta = this.findDocByCM(cm);
if (docMeta) {
delete docMeta.cm;
if (docMeta.doc && docMeta._trackChange) {
CodeMirror.off(docMeta.doc, "change", docMeta._trackChange);
}
}
};
/**
* Will read file from disk or remote http file, then will load the content
* into tern's server.
*
* * This will bypass the list of cached documents.
*/
TernProvider.prototype.addFile = function (name, root) {
var _self = this;
return fileLoader.fileMeta(name, root || _self.currentDocument.name || "").done(function(data) {
var docMeta = {
name: name, //data.fullPath,
doc: new CodeMirror.Doc(data.text, "javascript"),
changed: null
};
_self.docs.push(docMeta);
_self._server.addFile(docMeta.name, docMeta.doc.getValue());
});
};
/**
* Interface to operate against a local instance of tern
*/
function LocalProvider() {
var _self = this;
TernProvider.apply(_self, arguments);
//
// Load up all the definitions that we will need to start with.
//
require(["text!./reserved.json", "text!./tern/defs/ecma5.json", "text!./tern/defs/browser.json", "text!./tern/defs/jquery.json"], function() {
var defs = [];
Array.prototype.slice.call(arguments, 0).forEach(function(item, index){
defs[index] = JSON.parse(item);
});
_self._server = TernDemo.server({
getFile: function() {
return _self.getFile.apply(_self, arguments);
},
ready:_self.ready.resolve,
defs: defs,
debug: false,
async: true,
plugins: {requirejs: {}}
});
});
}
LocalProvider.prototype = new TernProvider();
LocalProvider.prototype.constructor = LocalProvider;
LocalProvider.prototype.query = function( cm, settings, allowFragments ) {
var _self = this;
var promise = $.Deferred();
// Throttle the query request so that doc changes have enough time to be processed
setTimeout(function(){
var query = TernDemo.buildRequest(cm, settings, allowFragments);
_self._server.request( query, function(error, data) {
if (error) {
promise.reject(error);
}
else {
query.doc = _self.findDocByCM(cm);
promise.resolve(data, query);
}
});
}, 1);
return promise.promise();
};
/**
* Gets a file from the list of cached documents. If the document isn't cached,
* it will get loaded either from local drive or remote via http. This newly
* retrieved document will be added to the list of cached documents.
*/
LocalProvider.prototype.getFile = function (name, root) {
var _self = this;
var docMeta = _self.findDocByName(name);
var deferred = $.Deferred();
if ( docMeta ) {
deferred.resolve(docMeta.doc.getValue());
}
else {
fileLoader.fileMeta(name, root || _self.currentDocument.name || "")
.done(function(data) {
var docMeta = {
name: name, //data.fullPath,
doc: new CodeMirror.Doc(data.text, "javascript"),
changed: null
};
_self.docs.push(docMeta);
deferred.resolve(data.text);
})
.fail(function(error){
deferred.reject(error);
});
}
return deferred;
};
/**
* Interface to operate against a remote tern server
*/
function RemoteProvider() {
var _self = this;
TernProvider.apply(_self, arguments);
require(['text!./tern/.tern-port'], function(ternport) {
_self.port = ternport;
_self.ping().done(function(){
console.log("Tern Remote Server is ready");
_self.ready.resolve(_self);
}).fail(function(){
throw "Tern Server is not running";
});
});
}
RemoteProvider.prototype = new TernProvider;
RemoteProvider.prototype.constructor = RemoteProvider;
RemoteProvider.prototype.ping = function (){
return $.ajax({
"url": "http://localhost:" + this.port + "/ping",
"type": "GET"
})
.promise();
};
RemoteProvider.prototype.query = function( cm, settings, allowFragments ) {
var promise = $.Deferred();
var query = TernDemo.buildRequest(cm, settings, allowFragments);
// Send query to the server
$.ajax({
"url": "http://localhost:" + this.port,
"type": "POST",
"contentType": "application/json; charset=utf-8",
"data": JSON.stringify(query)
})
.done(function(data){
promise.resolve(data, query);
})
.fail(function(error){
promise.reject(error);
});
return promise.promise();
};
return {
Remote: RemoteProvider,
Local: LocalProvider
};
});