forked from adobe/brackets
/
CodeHintManager.js
443 lines (383 loc) · 15.6 KB
/
CodeHintManager.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
/*
* Copyright (c) 2012 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, $, window, brackets */
define(function (require, exports, module) {
"use strict";
// Load dependent modules
var HTMLUtils = require("language/HTMLUtils"),
Menus = require("command/Menus"),
StringUtils = require("utils/StringUtils"),
EditorManager = require("editor/EditorManager"),
PopUpManager = require("widgets/PopUpManager"),
ViewUtils = require("utils/ViewUtils");
var hintProviders = [],
hintList, // initialized by htmlContentLoadComplete handler
shouldShowHintsOnKeyUp = false;
/**
* @constructor
*
* Displays a popup list of code completions.
* Currently only HTML tags are supported, but this will greatly be extended in coming sprint
* to include: extensibility API, HTML attributes hints, JavaScript hints, CSS hints
*/
function CodeHintList() {
this.currentProvider = null;
this.query = {queryStr: null};
this.displayList = [];
this.options = {
maxResults: 999
};
this.opened = false;
this.selectedIndex = -1;
this.editor = null;
this.$hintMenu = $("<li class='dropdown codehint-menu'></li>");
var $toggle = $("<a href='#' class='dropdown-toggle'></a>")
.hide();
this.$hintMenu.append($toggle)
.append("<ul class='dropdown-menu'></ul>");
}
/**
* @private
* Enters the code completion text into the editor
* @string {string} completion - text to insert into current code editor
*/
CodeHintList.prototype._handleItemClick = function (completion) {
this.currentProvider.handleSelect(completion, this.editor, this.editor.getCursorPos());
this.close();
};
/**
* Adds a single item to the hint list
* @param {string} name
*/
CodeHintList.prototype.addItem = function (name) {
var self = this;
var displayName = name.replace(
new RegExp(StringUtils.regexEscape(this.query.queryStr), "i"),
"<strong>$&</strong>"
);
var $item = $("<li><a href='#'><span class='codehint-item'>" + displayName + "</span></a></li>")
.on("click", function (e) {
// Don't let the click propagate upward (otherwise it will hit the close handler in
// bootstrap-dropdown).
e.stopPropagation();
self._handleItemClick(name);
});
this.$hintMenu.find("ul.dropdown-menu")
.append($item);
};
/**
* Rebuilds the hint list based on this.query
*/
CodeHintList.prototype.updateList = function () {
this.displayList = this.currentProvider.search(this.query);
this.buildListView();
};
/**
* Removes all list items from hint list
*/
CodeHintList.prototype.clearList = function () {
this.$hintMenu.find("li").remove();
};
/**
* Rebuilds the list items for the hint list based on this.displayList
*/
CodeHintList.prototype.buildListView = function () {
this.clearList();
var self = this;
var count = 0;
$.each(this.displayList, function (index, item) {
if (count > self.options.maxResults) {
return false;
}
self.addItem(item);
count++;
});
if (count === 0) {
this.close();
} else {
// Select the first item in the list
this.setSelectedIndex(0);
}
};
/**
* Selects the item in the hint list specified by index
* @param {Number} index
*/
CodeHintList.prototype.setSelectedIndex = function (index) {
var items = this.$hintMenu.find("li");
// Range check
index = Math.max(0, Math.min(index, items.length - 1));
// Clear old highlight
if (this.selectedIndex !== -1) {
$(items[this.selectedIndex]).find("a").removeClass("highlight");
}
// Highlight the new selected item
this.selectedIndex = index;
if (this.selectedIndex !== -1) {
var $item = $(items[this.selectedIndex]);
var $view = this.$hintMenu.find("ul.dropdown-menu");
ViewUtils.scrollElementIntoView($view, $item, false);
$item.find("a").addClass("highlight");
}
};
/**
* Handles key presses when the hint list is being displayed
* @param {Editor} editor
* @param {KeyBoardEvent} keyEvent
*/
CodeHintList.prototype.handleKeyEvent = function (editor, event) {
var keyCode = event.keyCode;
// Up arrow, down arrow and enter key are always handled here
if (keyCode === 38 || keyCode === 40 || keyCode === 13 ||
(keyCode >= 33 && keyCode <= 36)) {
if (event.type === "keydown") {
if (keyCode === 38) {
// Up arrow
this.setSelectedIndex(this.selectedIndex - 1);
} else if (keyCode === 40) {
// Down arrow
this.setSelectedIndex(this.selectedIndex + 1);
} else if (keyCode === 33) {
// Page Up
this.setSelectedIndex(this.selectedIndex - this.getItemsPerPage());
} else if (keyCode === 34) {
// Page Down
this.setSelectedIndex(this.selectedIndex + this.getItemsPerPage());
} else if (keyCode === 35) {
// End
this.setSelectedIndex(this.options.maxResults);
} else if (keyCode === 36) {
// Home
this.setSelectedIndex(0);
} else {
// Enter/return key
// Trigger a click handler to commmit the selected item
$(this.$hintMenu.find("li")[this.selectedIndex]).triggerHandler("click");
}
}
event.preventDefault();
return;
}
// All other key events trigger a rebuild of the list, but only
// on keyup events
if (event.type !== "keyup") {
return;
}
this.query = this.currentProvider.getQueryInfo(this.editor, this.editor.getCursorPos());
this.updateList();
// Update the CodeHintList location
if (this.displayList.length) {
var hintPos = this.calcHintListLocation();
this.$hintMenu.css({"left": hintPos.left, "top": hintPos.top});
}
};
/**
* Return true if the CodeHintList is open.
* @return {Boolean}
*/
CodeHintList.prototype.isOpen = function () {
// We don't get a notification when the dropdown closes. The best
// we can do is keep an "opened" flag and check to see if we
// still have the "open" class applied.
if (this.opened && !this.$hintMenu.hasClass("open")) {
this.opened = false;
}
return this.opened;
};
/**
* Displays the hint list at the current cursor position
* @param {Editor} editor
*/
CodeHintList.prototype.open = function (editor) {
var self = this;
this.editor = editor;
Menus.closeAll();
this.currentProvider = null;
$.each(hintProviders, function (index, item) {
var query = item.getQueryInfo(self.editor, self.editor.getCursorPos());
if (query.queryStr !== null) {
self.query = query;
self.currentProvider = item;
return false;
}
});
if (!this.currentProvider) {
return;
}
this.updateList();
if (this.displayList.length) {
// Need to add the menu to the DOM before trying to calculate its ideal location.
$("#codehint-menu-bar > ul").append(this.$hintMenu);
var hintPos = this.calcHintListLocation();
this.$hintMenu.addClass("open")
.css({"left": hintPos.left, "top": hintPos.top});
this.opened = true;
PopUpManager.addPopUp(this.$hintMenu, function () {
self.close();
});
}
};
/**
* Closes the hint list
*/
CodeHintList.prototype.close = function () {
// TODO: Due to #1381, this won't get called if the user clicks out of the code hint menu.
// That's (sort of) okay right now since it doesn't really matter if a single old invisible
// code hint list is lying around (it'll get closed the next time the user pops up a code
// hint). Once #1381 is fixed this issue should go away.
this.$hintMenu.removeClass("open");
this.opened = false;
this.currentProvider = null;
PopUpManager.removePopUp(this.$hintMenu);
this.$hintMenu.remove();
if (hintList === this) {
hintList = null;
}
};
/**
* Computes top left location for hint list so that the list is not clipped by the window
* @return {Object.<left: Number, top: Number> }
*/
CodeHintList.prototype.calcHintListLocation = function () {
var cursor = this.editor._codeMirror.cursorCoords(),
posTop = cursor.y,
posLeft = cursor.x,
$window = $(window),
$menuWindow = this.$hintMenu.children("ul");
// TODO Ty: factor out menu repositioning logic so code hints and Context menus share code
// adjust positioning so menu is not clipped off bottom or right
var bottomOverhang = posTop + 25 + $menuWindow.height() - $window.height();
if (bottomOverhang > 0) {
posTop -= (27 + $menuWindow.height());
}
// todo: should be shifted by line height
posTop -= 15; // shift top for hidden parent element
//posLeft += 5;
var rightOverhang = posLeft + $menuWindow.width() - $window.width();
if (rightOverhang > 0) {
posLeft = Math.max(0, posLeft - rightOverhang);
}
return {left: posLeft, top: posTop};
};
/**
* @private
* Calculate the number of items per scroll page. Used for PageUp and PageDown.
* @return {number}
*/
CodeHintList.prototype.getItemsPerPage = function () {
var itemsPerPage = 1,
$items = this.$hintMenu.find("li"),
$view = this.$hintMenu.find("ul.dropdown-menu"),
itemHeight;
if ($items.length !== 0) {
itemHeight = $($items[0]).height();
if (itemHeight) {
// round down to integer value
itemsPerPage = Math.floor($view.height() / itemHeight);
itemsPerPage = Math.max(1, Math.min(itemsPerPage, $items.length));
}
}
return itemsPerPage;
};
/**
* Show the code hint list near the current cursor position for the specified editor
* @param {Editor}
*/
function showHint(editor) {
if (hintList) {
hintList.close();
}
hintList = new CodeHintList();
hintList.open(editor);
}
/**
* Handles keys related to displaying, searching, and navigating the hint list
* @param {Editor} editor
* @param {KeyboardEvent} event
*/
function handleKeyEvent(editor, event) {
var provider = null;
// Check for Control+Space
if (event.type === "keydown" && event.keyCode === 32 && event.ctrlKey) {
showHint(editor);
event.preventDefault();
} else if (event.type === "keypress") {
// Check if any provider wants to show hints on this key.
$.each(hintProviders, function (index, item) {
if (item.shouldShowHintsOnKey(String.fromCharCode(event.charCode))) {
provider = item;
return false;
}
});
shouldShowHintsOnKeyUp = !!provider;
} else if (event.type === "keyup") {
if (shouldShowHintsOnKeyUp) {
shouldShowHintsOnKeyUp = false;
showHint(editor);
}
}
// Pass to the hint list, if it's open
if (hintList && hintList.isOpen()) {
shouldShowHintsOnKeyUp = false;
hintList.handleKeyEvent(editor, event);
}
}
/**
* Registers an object that is able to provide code hints. When the user requests a code
* hint getQueryInfo() will be called on every hint provider. Providers should examine
* the state of the editor and return a search query object with a filter string if hints
* can be provided. search() will then be called allowing the hint provider to create a
* list of hints for the search query. When the user chooses a hint handleSelect() is called
* so that the hint provider can insert the hint into the editor.
*
* @param {Object.< getQueryInfo: function(editor, cursor),
* search: function(string),
* handleSelect: function(string, Editor, cursor),
* shouldShowHintsOnKey: function(string)>}
*
* Parameter Details:
* - getQueryInfo - examines cursor location of editor and returns an object representing
* the search query to be used for hinting. queryStr is a required property of the search object
* and a client may provide other properties on the object to carry more context about the query.
* - search - takes a query object and returns an array of hint strings based on the queryStr property
* of the query object.
* - handleSelect - takes a completion string and inserts it into the editor near the cursor
* position
* - shouldShowHintsOnKey - inspects the char code and returns true if it wants to show code hints on that key.
*/
function registerHintProvider(providerInfo) {
hintProviders.push(providerInfo);
}
/**
* Expose CodeHintList for unit testing
*/
function _getCodeHintList() {
return hintList;
}
// Define public API
exports.handleKeyEvent = handleKeyEvent;
exports.showHint = showHint;
exports._getCodeHintList = _getCodeHintList;
exports.registerHintProvider = registerHintProvider;
});