-
Notifications
You must be signed in to change notification settings - Fork 4
/
Session.js
564 lines (516 loc) · 19.6 KB
/
Session.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
/*
* Copyright (c) 2013 Adobe Systems Incorporated. All rights reserved.
*
* 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.
*
*/
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */
/*global define, brackets, $ */
define(function (require, exports, module) {
"use strict";
var HintUtils = require("HintUtils"),
ScopeManager = require("ScopeManager");
/**
* Session objects encapsulate state associated with a hinting session
* and provide methods for updating and querying the session.
*
* @constructor
* @param {Editor} editor - the editor context for the session
*/
function Session(editor) {
this.editor = editor;
this.path = editor.document.file.fullPath;
}
/**
* Update the scope information assocated with the current session
*
* @param {Object} scopeInfo - scope information, including the scope and
* lists of identifiers, globals, literals and properties, and a set
* of associations
*/
Session.prototype.setScopeInfo = function (scopeInfo) {
this.scope = scopeInfo.scope;
this.identifiers = scopeInfo.identifiers;
this.globals = scopeInfo.globals;
this.literals = scopeInfo.literals;
this.properties = scopeInfo.properties;
this.associations = scopeInfo.associations;
};
/**
* Get the name of the file associated with the current session
*
* @return {string} - the full pathname of the file associated with the
* current session
*/
Session.prototype.getPath = function () {
return this.path;
};
/**
* Get the current cursor position.
*
* @return {{line: number, ch: number}} - the current cursor position
*/
Session.prototype.getCursor = function () {
return this.editor.getCursorPos();
};
/**
* Get the offset of the current cursor position
*
* @return {number} - the offset into the current document of the current
* cursor
*/
Session.prototype.getOffset = function () {
var cursor = this.getCursor();
return this.editor.indexFromPos(cursor);
};
/**
* Get the token at the given cursor position, or at the current cursor
* if none is given.
*
* @param {?{line: number, ch: number}} cursor - the cursor position
* at which to retrieve a token
* @return {Object} - the CodeMirror token at the given cursor position
*/
Session.prototype.getToken = function (cursor) {
var cm = this.editor._codeMirror;
if (cursor) {
return cm.getTokenAt(cursor);
} else {
return cm.getTokenAt(this.getCursor());
}
};
/**
* Get the next cursor position on the line, or null if there isn't one.
*
* @return {?{line: number, ch: number}} - the cursor position
* immediately following the current cursor position, or null if
* none exists.
*/
Session.prototype.getNextCursorOnLine = function () {
var cursor = this.getCursor(),
doc = this.editor.document,
line = doc.getLine(cursor.line);
if (cursor.ch < line.length) {
return {
ch : cursor.ch + 1,
line: cursor.line
};
} else {
return null;
}
};
/**
* Get the token before the one at the given cursor position
*
* @param {{line: number, ch: number}} cursor - cursor position after
* which a token should be retrieved
* @return {Object} - the CodeMirror token before the one at the given
* cursor position
*/
Session.prototype._getPreviousToken = function (cursor) {
var token = this.getToken(cursor),
prev = token,
doc = this.editor.document;
do {
if (prev.start < cursor.ch) {
cursor.ch = prev.start;
} else if (prev.start > 0) {
cursor.ch = prev.start - 1;
} else if (cursor.line > 0) {
cursor.ch = doc.getLine(cursor.line - 1).length;
cursor.line--;
} else {
break;
}
prev = this.getToken(cursor);
} while (prev.string.trim() === "");
return prev;
};
/**
* Calculate a query string relative to the current cursor position
* and token. E.g., from a state "identi<cursor>er", the query string is
* "identi".
*
* @return {string} - the query string for the current cursor position
*/
Session.prototype.getQuery = function () {
var cursor = this.getCursor(),
token = this.getToken(cursor),
query = "";
if (token) {
if (token.string !== ".") {
query = token.string.substring(0, token.string.length - (token.end - cursor.ch));
query = query.trim();
}
}
return query;
};
/**
* Find the context of a property lookup. For example, for a lookup
* foo(bar, baz(quux)).prop, foo is the context.
*
* @param {{line: number, ch: number}} cursor - the cursor position
* at which context information is to be retrieved
* @param {number} depth - the current depth of the parenthesis stack, or
* undefined if the depth is 0.
* @return {string} - the context for the property that was looked up
*/
Session.prototype.getContext = function (cursor, depth) {
var token = this.getToken(cursor);
if (depth === undefined) {
depth = 0;
}
if (token.string === ")") {
this._getPreviousToken(cursor);
return this.getContext(cursor, ++depth);
} else if (token.string === "(") {
this._getPreviousToken(cursor);
return this.getContext(cursor, --depth);
} else {
if (depth > 0 || token.string === ".") {
this._getPreviousToken(cursor);
return this.getContext(cursor, depth);
} else {
return token.string;
}
}
};
/**
* Get the type of the current session, i.e., whether it is a property
* lookup and, if so, what the context of the lookup is.
*
* @return {{property: boolean, context: string}} - a pair consisting
* of a {boolean} "property" that indicates whether or not the type of
* the session is a property lookup, and a {string} "context" that
* indicates the object context (as described in getContext above) of
* the property lookup, or null if there is none. The context is
* always null for non-property lookups.
*/
Session.prototype.getType = function () {
var propertyLookup = false,
context = null,
cursor = this.getCursor(),
token = this.getToken(cursor);
if (token) {
if (token.string === ".") {
propertyLookup = true;
context = this.getContext(cursor);
} else {
if (token.className === "property") {
propertyLookup = true;
}
token = this._getPreviousToken(cursor);
if (token && token.string === ".") {
propertyLookup = true;
context = this.getContext(cursor);
}
}
}
return {
property: propertyLookup,
context: context
};
};
/**
* Get a list of hints for the current session using the current scope
* information.
*
* @return {Array.<Object>} - the sorted list of hints for the current
* session.
*/
Session.prototype.getHints = function () {
/*
* Comparator for sorting tokens according to minimum distance from
* a given position
*
* @param {number} pos - the position to which a token's occurrences
* are compared
* @param {Function} - the comparator function
*/
function compareByPosition(pos) {
/*
* Compute the minimum distance between a token, with which is
* associated a sorted list of positions, and a given offset.
*
* @param {number} pos - the position to which a token's occurrences
* are compared.
* @param {Object} token - a hint token, annotated with a list of
* occurrence positions
* @return number - the least distance of an occurrence of token to
* pos, or Infinity if there are no occurrences
*/
function minDistToPos(pos, token) {
var arr = token.positions,
low = 0,
high = arr.length,
middle = Math.floor(high / 2),
dist;
if (high === 0) {
return Infinity;
} else {
// binary search for the position
while (low < middle && middle < high) {
if (arr[middle] < pos) {
low = middle;
middle += Math.floor((high - middle) / 2);
} else if (arr[middle] > pos) {
high = middle;
middle = low + Math.floor((middle - low) / 2);
} else {
break;
}
}
// the closest position is off by no more than one
dist = Math.abs(arr[middle] - pos);
if (middle > 0) {
dist = Math.min(dist, Math.abs(arr[middle - 1] - pos));
}
if (middle + 1 < arr.length) {
dist = Math.min(dist, Math.abs(arr[middle + 1] - pos));
}
return dist;
}
}
return function (a, b) {
/*
* Look up the cached minimum distance from an occurrence of
* the token to the given position, calculating and storing it
* if needed.
*
* @param {Object} token - the token from which the minimum
* distance to position pos is required
* @return {number} - the least distance of an occurrence of
* token to position pos
*/
function getDistToPos(token) {
var dist;
if (token.distToPos >= 0) {
dist = token.distToPos;
} else {
dist = minDistToPos(pos, token);
token.distToPos = dist;
}
return dist;
}
var aDist = getDistToPos(a),
bDist = getDistToPos(b);
if (aDist === Infinity) {
if (bDist === Infinity) {
return 0;
} else {
return 1;
}
} else {
if (bDist === Infinity) {
return -1;
} else {
return aDist - bDist;
}
}
};
}
/*
* Comparator for sorting tokens lexicographically according to scope,
* assuming the scope level has already been annotated.
*
* @param {Object} a - a token
* @param {Object} b - another token
* @param {number} - comparator value that indicates whether a is more
* tightly scoped than b
*/
function compareByScope(a, b) {
var adepth = a.level;
var bdepth = b.level;
if (adepth >= 0) {
if (bdepth >= 0) {
return adepth - bdepth;
} else {
return -1;
}
} else {
if (bdepth >= 0) {
return 1;
} else {
return 0;
}
}
}
/*
* Comparator for sorting tokens by name
*
* @param {Object} a - a token
* @param {Object} b - another token
* @return {number} - comparator value that indicates whether the name
* of token a is lexicographically lower than the name of token b
*/
function compareByName(a, b) {
if (a.value === b.value) {
return 0;
} else if (a.value < b.value) {
return -1;
} else {
return 1;
}
}
/*
* Comparator for sorting tokens by path, such that a <= b if
* a.path === path
*
* @param {string} path - the target path name
* @return {Function} - the comparator function
*/
function compareByPath(path) {
return function (a, b) {
if (a.path === path) {
if (b.path === path) {
return 0;
} else {
return -1;
}
} else {
if (b.path === path) {
return 1;
} else {
return 0;
}
}
};
}
/*
* Comparator for sorting properties w.r.t. an association object.
*
* @param {Object} assoc - an association object
* @return {Function} - the comparator function
*/
function compareByAssociation(assoc) {
return function (a, b) {
if (Object.prototype.hasOwnProperty.call(assoc, a.value)) {
if (Object.prototype.hasOwnProperty.call(assoc, b.value)) {
return assoc[a.value] - assoc[b.value];
} else {
return -1;
}
} else {
if (Object.prototype.hasOwnProperty.call(assoc, b.value)) {
return 1;
} else {
return 0;
}
}
};
}
/*
* Forms the lexicographical composition of comparators, i.e.,
* "a lex(c1,c2) b" iff "a c1 b or (a = b and a c2 b)"
*
* @param {Function} compare1 - a comparator
* @param {Function} compare2 - another comparator
* @return {Function} - the lexicographic composition of comparator1
* and comparator2
*/
function lexicographic(compare1, compare2) {
return function (a, b) {
var result = compare1(a, b);
if (result === 0) {
return compare2(a, b);
} else {
return result;
}
};
}
/*
* A comparator for identifiers: the lexicographic combination of
* scope, position and name.
*
* @param {number} pos - the target position by which identifiers are
* compared
* @return {Function} - the comparator function
*/
function compareIdentifiers(pos) {
return lexicographic(compareByScope,
lexicographic(compareByPosition(pos),
compareByName));
}
/*
* A comparator for properties: the lexicographic combination of
* association, path name, position, and name.
*
* @param {Object} assoc - the association by which properties are
* compared
* @param {string} path - the path name by which properties are
* compared
* @param {number} pos - the target position by which properties are
* compared
* @return {Function} - the comparator function
*/
function compareProperties(assoc, path, pos) {
return lexicographic(compareByAssociation(assoc),
lexicographic(compareByPath(path),
lexicographic(compareByPosition(pos),
compareByName)));
}
/*
* Clone a list of hints. (Used so that later annotations are not
* preserved when scope information changes.)
*
* @param {Array.<Object>} hints - an array of hint tokens
* @return {Array.<Object>} - a new array of objects that are clones of
* the objects in the input array
*/
function copyHints(hints) {
function cloneToken(token) {
var copy = {},
prop;
for (prop in token) {
if (Object.prototype.hasOwnProperty.call(token, prop)) {
copy[prop] = token[prop];
}
}
return copy;
}
return hints.map(cloneToken);
}
var cursor = this.editor.getCursorPos(),
offset = this.editor.indexFromPos(cursor),
type = this.getType(),
association,
hints;
if (type.property) {
hints = copyHints(this.properties);
if (type.context &&
Object.prototype.hasOwnProperty.call(this.associations, type.context)) {
association = this.associations[type.context];
hints = HintUtils.annotateWithAssociation(hints, association);
hints.sort(compareProperties(association, this.path, offset));
} else {
hints.sort(compareProperties({}, this.path, offset));
}
} else {
hints = copyHints(this.identifiers);
hints = HintUtils.annotateWithScope(hints, this.scope);
hints = hints.concat(this.literals);
hints.sort(compareIdentifiers(offset));
hints = hints.concat(this.globals);
hints = hints.concat(HintUtils.LITERALS);
hints = hints.concat(HintUtils.KEYWORDS);
}
return hints;
};
module.exports = Session;
});