@@ -124,6 +124,17 @@ define(function (require, exports, module) {
124
124
}
125
125
}
126
126
127
+ /**
128
+ * Send a message to the tern worker - if the worker is being initialized,
129
+ * the message will not be posted until initialization is complete
130
+ */
131
+ function postMessage ( msg ) {
132
+ ternPromise . done ( function ( ternWorker ) {
133
+ ternWorker . postMessage ( msg ) ;
134
+ } ) ;
135
+ }
136
+
137
+
127
138
/**
128
139
* Get a Promise for the definition from TernJS, for the file & offset passed in.
129
140
* @return {jQuery.Promise } - a promise that will resolve to definition when
@@ -132,10 +143,10 @@ define(function (require, exports, module) {
132
143
function getJumptoDef ( dir , file , offset , text ) {
133
144
postMessage ( {
134
145
type : HintUtils . TERN_JUMPTODEF_MSG ,
135
- dir :dir ,
136
- file :file ,
137
- offset :offset ,
138
- text :text
146
+ dir : dir ,
147
+ file : file ,
148
+ offset : offset ,
149
+ text : text
139
150
} ) ;
140
151
141
152
var $deferredJump = $ . Deferred ( ) ;
@@ -160,7 +171,7 @@ define(function (require, exports, module) {
160
171
161
172
var ternPromise = getJumptoDef ( dir , file , offset , document . getText ( ) ) ;
162
173
163
- return { promise :ternPromise } ;
174
+ return { promise : ternPromise } ;
164
175
}
165
176
166
177
/**
@@ -176,74 +187,35 @@ define(function (require, exports, module) {
176
187
177
188
pendingTernRequests [ file ] = null ;
178
189
179
- if ( $deferredJump ) {
190
+ if ( $deferredJump ) {
180
191
$deferredJump . resolveWith ( null , [ response ] ) ;
181
192
}
182
193
}
183
-
194
+
184
195
/**
185
- * Request hints from Tern .
196
+ * Add a pending request waiting for the tern-worker to complete .
186
197
*
187
- * Note that successive calls to getScope may return the same objects, so
188
- * clients that wish to modify those objects (e.g., by annotating them based
189
- * on some temporary context) should copy them first. See, e.g.,
190
- * Session.getHints().
191
- *
192
- * @param {Document } document - the document for which scope info is
193
- * desired
194
- * @param {number } offset - the offset into the document at which scope
195
- * info is desired
196
- * @return {jQuery.Promise } - The promise will not complete until the tern
197
- * hints have completed.
198
+ * @param {string } file - the name of the file
199
+ * @param {string } type - the type of request
200
+ * @param {jQuery.Deferred } deferredRequest - the $.Deferred object to save
198
201
*/
199
- function requestHints ( session , document , offset ) {
200
- var path = document . file . fullPath ,
201
- split = HintUtils . splitPath ( path ) ,
202
- dir = split . dir ,
203
- file = split . file ;
204
-
205
- var $deferredHints = $ . Deferred ( ) ,
206
- hintPromise ,
207
- fnTypePromise ,
208
- propsPromise ;
209
-
210
- hintPromise = getTernHints ( dir , file , offset , document . getText ( ) ) ;
211
- var sessionType = session . getType ( ) ;
212
- if ( sessionType . property ) {
213
- propsPromise = getTernProperties ( dir , file , offset , document . getText ( ) ) ;
202
+ function addPendingRequest ( file , offset , type ) {
203
+ var requests ,
204
+ key = file + "@" + offset ,
205
+ $deferredRequest ;
206
+ if ( Object . prototype . hasOwnProperty . call ( pendingTernRequests , key ) ) {
207
+ requests = pendingTernRequests [ key ] ;
214
208
} else {
215
- var $propsDeferred = $ . Deferred ( ) ;
216
- propsPromise = $propsDeferred . promise ( ) ;
217
- $propsDeferred . resolveWith ( null ) ;
209
+ requests = { } ;
210
+ pendingTernRequests [ key ] = requests ;
218
211
}
219
212
220
- if ( sessionType . showFunctionType ) {
221
- // Show function sig
222
- fnTypePromise = getTernFunctionType ( dir , file , sessionType . functionCallPos , offset , document . getText ( ) ) ;
213
+ if ( Object . prototype . hasOwnProperty . call ( requests , type ) ) {
214
+ $deferredRequest = requests [ type ] ;
223
215
} else {
224
- var $fnTypeDeferred = $ . Deferred ( ) ;
225
- fnTypePromise = $fnTypeDeferred . promise ( ) ;
226
- $fnTypeDeferred . resolveWith ( null ) ;
216
+ requests [ type ] = $deferredRequest = $ . Deferred ( ) ;
227
217
}
228
- $ . when ( hintPromise , fnTypePromise , propsPromise ) . done (
229
- function ( completions , fnType , properties ) {
230
- session . setTernHints ( completions ) ;
231
- session . setFnType ( fnType ) ;
232
- session . setTernProperties ( properties ) ;
233
-
234
- $deferredHints . resolveWith ( null ) ;
235
- } ) ;
236
- return { promise :$deferredHints . promise ( ) } ;
237
- }
238
-
239
- /**
240
- * Send a message to the tern worker - if the worker is being initialized,
241
- * the message will not be posted until initialization is complete
242
- */
243
- function postMessage ( msg ) {
244
- ternPromise . done ( function ( ternWorker ) {
245
- ternWorker . postMessage ( msg ) ;
246
- } ) ;
218
+ return $deferredRequest . promise ( ) ;
247
219
}
248
220
249
221
/**
@@ -254,13 +226,13 @@ define(function (require, exports, module) {
254
226
function getTernHints ( dir , file , offset , text ) {
255
227
postMessage ( {
256
228
type : HintUtils . TERN_COMPLETIONS_MSG ,
257
- dir :dir ,
258
- file :file ,
259
- offset :offset ,
260
- text :text
229
+ dir : dir ,
230
+ file : file ,
231
+ offset : offset ,
232
+ text : text
261
233
} ) ;
262
234
263
- return addPendingRequest ( file , offset , HintUtils . TERN_COMPLETIONS_MSG ) ;
235
+ return addPendingRequest ( file , offset , HintUtils . TERN_COMPLETIONS_MSG ) ;
264
236
}
265
237
266
238
/**
@@ -273,10 +245,10 @@ define(function (require, exports, module) {
273
245
function getTernProperties ( dir , file , offset , text ) {
274
246
postMessage ( {
275
247
type : HintUtils . TERN_GET_PROPERTIES_MSG ,
276
- dir :dir ,
277
- file :file ,
248
+ dir : dir ,
249
+ file : file ,
278
250
offset : offset ,
279
- text :text
251
+ text : text
280
252
} ) ;
281
253
282
254
return addPendingRequest ( file , offset , HintUtils . TERN_GET_PROPERTIES_MSG ) ;
@@ -289,55 +261,86 @@ define(function (require, exports, module) {
289
261
function getTernFunctionType ( dir , file , pos , offset , text ) {
290
262
postMessage ( {
291
263
type : HintUtils . TERN_CALLED_FUNC_TYPE_MSG ,
292
- dir :dir ,
293
- file :file ,
294
- pos :pos ,
295
- offset :offset ,
296
- text :text
264
+ dir : dir ,
265
+ file : file ,
266
+ pos : pos ,
267
+ offset : offset ,
268
+ text : text
297
269
} ) ;
298
270
299
- return addPendingRequest ( file , offset , HintUtils . TERN_CALLED_FUNC_TYPE_MSG ) ;
271
+ return addPendingRequest ( file , offset , HintUtils . TERN_CALLED_FUNC_TYPE_MSG ) ;
300
272
}
301
273
274
+
302
275
/**
303
- * Add a pending request waiting for the tern-worker to complete .
276
+ * Request hints from Tern .
304
277
*
305
- * @param {string } file - the name of the file
306
- * @param {string } type - the type of request
307
- * @param {jQuery.Deferred } deferredRequest - the $.Deferred object to save
308
- */
309
- function addPendingRequest ( file , offset , type ) {
310
- var requests ,
311
- key = file + "@" + offset ,
312
- $deferredRequest ;
313
- if ( Object . prototype . hasOwnProperty . call ( pendingTernRequests , key ) ) {
314
- requests = pendingTernRequests [ key ] ;
278
+ * Note that successive calls to getScope may return the same objects, so
279
+ * clients that wish to modify those objects (e.g., by annotating them based
280
+ * on some temporary context) should copy them first. See, e.g.,
281
+ * Session.getHints().
282
+ *
283
+ * @param {Document } document - the document for which scope info is
284
+ * desired
285
+ * @param {number } offset - the offset into the document at which scope
286
+ * info is desired
287
+ * @return {jQuery.Promise } - The promise will not complete until the tern
288
+ * hints have completed.
289
+ */
290
+ function requestHints ( session , document , offset ) {
291
+ var path = document . file . fullPath ,
292
+ split = HintUtils . splitPath ( path ) ,
293
+ dir = split . dir ,
294
+ file = split . file ;
295
+
296
+ var $deferredHints = $ . Deferred ( ) ,
297
+ hintPromise ,
298
+ fnTypePromise ,
299
+ propsPromise ;
300
+
301
+ hintPromise = getTernHints ( dir , file , offset , document . getText ( ) ) ;
302
+ var sessionType = session . getType ( ) ;
303
+ if ( sessionType . property ) {
304
+ propsPromise = getTernProperties ( dir , file , offset , document . getText ( ) ) ;
315
305
} else {
316
- requests = { } ;
317
- pendingTernRequests [ key ] = requests ;
306
+ var $propsDeferred = $ . Deferred ( ) ;
307
+ propsPromise = $propsDeferred . promise ( ) ;
308
+ $propsDeferred . resolveWith ( null ) ;
318
309
}
319
310
320
- if ( Object . prototype . hasOwnProperty . call ( requests , type ) ) {
321
- $deferredRequest = requests [ type ] ;
311
+ if ( sessionType . showFunctionType ) {
312
+ // Show function sig
313
+ fnTypePromise = getTernFunctionType ( dir , file , sessionType . functionCallPos , offset , document . getText ( ) ) ;
322
314
} else {
323
- requests [ type ] = $deferredRequest = $ . Deferred ( ) ;
315
+ var $fnTypeDeferred = $ . Deferred ( ) ;
316
+ fnTypePromise = $fnTypeDeferred . promise ( ) ;
317
+ $fnTypeDeferred . resolveWith ( null ) ;
324
318
}
325
- return $deferredRequest . promise ( ) ;
326
- }
327
-
319
+ $ . when ( hintPromise , fnTypePromise , propsPromise ) . done (
320
+ function ( completions , fnType , properties ) {
321
+ session . setTernHints ( completions ) ;
322
+ session . setFnType ( fnType ) ;
323
+ session . setTernProperties ( properties ) ;
324
+
325
+ $deferredHints . resolveWith ( null ) ;
326
+ }
327
+ ) ;
328
+ return { promise : $deferredHints . promise ( ) } ;
329
+ }
330
+
328
331
/**
329
332
* Get any pending $.Deferred object waiting on the specified file and request type
330
333
* @param {string } file - the file
331
334
* @param {string } type - the type of request
332
335
* @param {jQuery.Deferred } - the $.Deferred for the request
333
- */
336
+ */
334
337
function getPendingRequest ( file , offset , type ) {
335
- var key = file + "@" + offset ;
336
- if ( Object . prototype . hasOwnProperty . call ( pendingTernRequests , key ) ) {
338
+ var key = file + "@" + offset ;
339
+ if ( Object . prototype . hasOwnProperty . call ( pendingTernRequests , key ) ) {
337
340
var requests = pendingTernRequests [ key ] ;
338
- return requests [ type ] ;
339
- }
340
- }
341
+ return requests [ type ] ;
342
+ }
343
+ }
341
344
342
345
/**
343
346
* Handle the response from the tern web worker when
@@ -352,17 +355,17 @@ define(function (require, exports, module) {
352
355
completions = response . completions ,
353
356
properties = response . properties ,
354
357
fnType = response . fnType ,
355
- type = response . type ,
358
+ type = response . type ,
356
359
$deferredHints = getPendingRequest ( file , offset , type ) ;
357
360
358
- if ( $deferredHints ) {
359
- if ( completions ) {
361
+ if ( $deferredHints ) {
362
+ if ( completions ) {
360
363
$deferredHints . resolveWith ( null , [ completions ] ) ;
361
- } else if ( properties ) {
364
+ } else if ( properties ) {
362
365
$deferredHints . resolveWith ( null , [ properties ] ) ;
363
- } else if ( fnType ) {
366
+ } else if ( fnType ) {
364
367
$deferredHints . resolveWith ( null , [ fnType ] ) ;
365
- }
368
+ }
366
369
}
367
370
}
368
371
@@ -384,33 +387,33 @@ define(function (require, exports, module) {
384
387
385
388
function replyWith ( name , txt ) {
386
389
postMessage ( {
387
- type :HintUtils . TERN_GET_FILE_MSG ,
388
- file :name ,
389
- text :txt
390
+ type : HintUtils . TERN_GET_FILE_MSG ,
391
+ file : name ,
392
+ text : txt
390
393
} ) ;
391
394
}
392
395
393
396
var name = request . file ;
394
- DocumentManager . getDocumentForPath ( rootTernDir + name ) . done ( function ( document ) {
397
+ DocumentManager . getDocumentForPath ( rootTernDir + name ) . done ( function ( document ) {
395
398
resolvedFiles [ name ] = rootTernDir + name ;
396
399
replyWith ( name , document . getText ( ) ) ;
397
400
} )
398
- . fail ( function ( ) {
399
- if ( projectRoot ) {
400
- // Try relative to project root
401
- DocumentManager . getDocumentForPath ( projectRoot + name ) . done ( function ( document ) {
402
- resolvedFiles [ name ] = projectRoot + name ;
403
- replyWith ( name , document . getText ( ) ) ;
404
- } )
405
- . fail ( function ( ) {
401
+ . fail ( function ( ) {
402
+ if ( projectRoot ) {
403
+ // Try relative to project root
404
+ DocumentManager . getDocumentForPath ( projectRoot + name ) . done ( function ( document ) {
405
+ resolvedFiles [ name ] = projectRoot + name ;
406
+ replyWith ( name , document . getText ( ) ) ;
407
+ } )
408
+ . fail ( function ( ) {
409
+ replyWith ( name , "" ) ;
410
+ } ) ;
411
+ } else {
412
+ // Need to send something back to tern - it will wait
413
+ // until all the files have been retrieved before doing its calculations
406
414
replyWith ( name , "" ) ;
407
- } ) ;
408
- } else {
409
- // Need to send something back to tern - it will wait
410
- // until all the files have been retrieved before doing its calculations
411
- replyWith ( name , "" ) ;
412
- }
413
- } ) ;
415
+ }
416
+ } ) ;
414
417
}
415
418
416
419
/**
@@ -475,15 +478,15 @@ define(function (require, exports, module) {
475
478
var response = e . data ,
476
479
type = response . type ;
477
480
478
- if ( type === HintUtils . TERN_COMPLETIONS_MSG ||
479
- type === HintUtils . TERN_CALLED_FUNC_TYPE_MSG ||
480
- type === HintUtils . TERN_GET_PROPERTIES_MSG ) {
481
+ if ( type === HintUtils . TERN_COMPLETIONS_MSG ||
482
+ type === HintUtils . TERN_CALLED_FUNC_TYPE_MSG ||
483
+ type === HintUtils . TERN_GET_PROPERTIES_MSG ) {
481
484
// handle any completions the worker calculated
482
485
handleTernCompletions ( response ) ;
483
- } else if ( type === HintUtils . TERN_GET_FILE_MSG ) {
486
+ } else if ( type === HintUtils . TERN_GET_FILE_MSG ) {
484
487
// handle a request for the contents of a file
485
488
handleTernGetFile ( response ) ;
486
- } else if ( type === HintUtils . TERN_JUMPTODEF_MSG ) {
489
+ } else if ( type === HintUtils . TERN_JUMPTODEF_MSG ) {
487
490
handleJumptoDef ( response ) ;
488
491
} else {
489
492
console . log ( "Worker: " + ( response . log || response ) ) ;
0 commit comments