forked from adobe/brackets
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSession.js
More file actions
426 lines (383 loc) · 14.8 KB
/
Session.js
File metadata and controls
426 lines (383 loc) · 14.8 KB
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
/*
* 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 StringMatch = brackets.getModule("utils/StringMatch"),
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;
this.ternHints = [];
this.ternProperties = [];
this.fnType = null;
this.builtins = ScopeManager.getBuiltins().map(function (builtin) {
// remove extension from name, if any.
var index = builtin.lastIndexOf(".");
if (index !== -1) {
builtin = builtin.substring(0, index);
}
return builtin;
});
this.builtins.push("requirejs.js"); // consider these globals as well.
}
/**
* 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 text of a line.
*
* @param {number} line - the line number
* @return {string} - the text of the line
*/
Session.prototype.getLine = function (line) {
var doc = this.editor.document;
return doc.getLine(line);
};
/**
* 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 !== "." && token.string !== "(" && 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,
showFunctionType:boolean,
context: string,
functionCallPos: {line:number, ch:number}}} - an Object 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.
* a {boolean} "showFunctionType" indicating if the function type should
* be displayed instead of normal hints. If "showFunctionType" is true, then
* then "functionCallPos" will be an object with line & col information of the
* function being called
*/
Session.prototype.getType = function () {
var propertyLookup = false,
inFunctionCall = false,
showFunctionType = false,
context = null,
cursor = this.getCursor(),
functionCallPos,
token = this.getToken(cursor);
if (token) {
if (token.state.lexical.info === "call") {
inFunctionCall = true;
if (this.getQuery().length > 0) {
inFunctionCall = false;
showFunctionType = false;
} else {
showFunctionType = true;
var col = token.state.lexical.column,
line,
e,
found;
for (line = this.getCursor().line, e = Math.max(0, line - 9), found = false; line >= e; --line) {
if (this.getLine(line).charAt(col) === "(") {
found = true;
break;
}
}
if (found) {
functionCallPos = {line: line, ch: col};
}
}
}
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);
}
}
if (propertyLookup) { showFunctionType = false; }
}
return {
property: propertyLookup,
inFunctionCall: inFunctionCall,
showFunctionType: showFunctionType,
functionCallPos: functionCallPos,
context: context
};
};
/**
* Get a list of hints for the current session using the current scope
* information.
*
* @param {string} query - the query prefix
* @param {StringMatcher} matcher - the class to find query matches and sort the results
* @return {Array.<Object>} - the sorted list of hints for the current
* session.
*/
Session.prototype.getHints = function (query, matcher) {
if (query === undefined) {
query = "";
}
var MAX_DISPLAYED_HINTS = 500,
type = this.getType(),
builtins = this.builtins,
hints;
/**
* Is the origin one of the builtin files.
*
* @param {string} origin
*/
function isBuiltin(origin) {
return builtins.indexOf(origin) !== -1;
}
/**
* Filter an array hints using a given query and matcher.
* The hints are returned in the format of the matcher.
* The matcher returns the value in the "label" property,
* the match score in "matchGoodness" property.
*
* @param {Array} hints - array of hints
* @param {StringMatcher} matcher
* @returns {Array} - array of matching hints.
*/
function filterWithQueryAndMatcher(hints, matcher) {
var matchResults = $.map(hints, function (hint) {
var searchResult = matcher.match(hint.value, query);
if (searchResult) {
searchResult.value = hint.value;
searchResult.guess = hint.guess;
if (hint.depth !== undefined) {
searchResult.depth = hint.depth;
}
if (!type.property && !type.showFunctionType && hint.origin &&
isBuiltin(hint.origin)) {
searchResult.builtin = 1;
} else {
searchResult.builtin = 0;
}
}
return searchResult;
});
return matchResults;
}
if (type.property) {
hints = this.ternHints || [];
hints = filterWithQueryAndMatcher(hints, matcher);
// If there are no hints then switch over to guesses.
if (hints.length === 0) {
hints = filterWithQueryAndMatcher(this.ternProperties, matcher);
}
StringMatch.multiFieldSort(hints, { matchGoodness: 0, value: 1 });
} else if (type.showFunctionType) {
hints = this.getFunctionTypeHint();
} else { // identifiers, literals, and keywords
hints = this.ternHints || [];
hints = hints.concat(HintUtils.LITERALS);
hints = hints.concat(HintUtils.KEYWORDS);
hints = filterWithQueryAndMatcher(hints, matcher);
StringMatch.multiFieldSort(hints, { matchGoodness: 0, depth: 1, builtin: 2, value: 3 });
}
if (hints.length > MAX_DISPLAYED_HINTS) {
hints = hints.slice(0, MAX_DISPLAYED_HINTS);
}
return hints;
};
Session.prototype.setTernHints = function (newHints) {
this.ternHints = newHints;
};
Session.prototype.setTernProperties = function (newProperties) {
this.ternProperties = newProperties;
};
Session.prototype.setFnType = function (newFnType) {
this.fnType = newFnType;
};
/**
* Get the function type hint. This will format the hint so
* that it has the called variable name instead of just "fn()".
*/
Session.prototype.getFunctionTypeHint = function () {
var fnHint = this.fnType,
hints = [];
if (fnHint && (fnHint.substring(0, 3) === "fn(")) {
var sessionType = this.getType(),
cursor = sessionType.functionCallPos,
token = cursor ? this.getToken(cursor) : undefined,
varName;
if (token) {
varName = token.string;
if (varName) {
fnHint = varName + fnHint.substr(2);
}
}
hints[0] = {value: fnHint, positions: []};
}
return hints;
};
module.exports = Session;
});