Merge pull request #1865 from ajaxorg/autocomplete-noselect
Add autoSelect option to auto completion
This commit is contained in:
commit
65822b59fe
1 changed files with 23 additions and 16 deletions
|
|
@ -40,6 +40,7 @@ var snippetManager = require("./snippets").snippetManager;
|
||||||
|
|
||||||
var Autocomplete = function() {
|
var Autocomplete = function() {
|
||||||
this.autoInsert = true;
|
this.autoInsert = true;
|
||||||
|
this.autoSelect = true;
|
||||||
this.keyboardHandler = new HashHandler();
|
this.keyboardHandler = new HashHandler();
|
||||||
this.keyboardHandler.bindKeys(this.commands);
|
this.keyboardHandler.bindKeys(this.commands);
|
||||||
|
|
||||||
|
|
@ -47,7 +48,7 @@ var Autocomplete = function() {
|
||||||
this.changeListener = this.changeListener.bind(this);
|
this.changeListener = this.changeListener.bind(this);
|
||||||
this.mousedownListener = this.mousedownListener.bind(this);
|
this.mousedownListener = this.mousedownListener.bind(this);
|
||||||
this.mousewheelListener = this.mousewheelListener.bind(this);
|
this.mousewheelListener = this.mousewheelListener.bind(this);
|
||||||
|
|
||||||
this.changeTimer = lang.delayedCall(function() {
|
this.changeTimer = lang.delayedCall(function() {
|
||||||
this.updateCompletions(true);
|
this.updateCompletions(true);
|
||||||
}.bind(this))
|
}.bind(this))
|
||||||
|
|
@ -69,15 +70,15 @@ var Autocomplete = function() {
|
||||||
this.popup.setData(this.completions.filtered);
|
this.popup.setData(this.completions.filtered);
|
||||||
|
|
||||||
var renderer = editor.renderer;
|
var renderer = editor.renderer;
|
||||||
|
this.popup.setRow(this.autoSelect ? 0 : -1);
|
||||||
if (!keepPopupPosition) {
|
if (!keepPopupPosition) {
|
||||||
this.popup.setRow(0);
|
|
||||||
this.popup.setFontSize(editor.getFontSize());
|
this.popup.setFontSize(editor.getFontSize());
|
||||||
|
|
||||||
var lineHeight = renderer.layerConfig.lineHeight;
|
var lineHeight = renderer.layerConfig.lineHeight;
|
||||||
|
|
||||||
var pos = renderer.$cursorLayer.getPixelPosition(this.base, true);
|
var pos = renderer.$cursorLayer.getPixelPosition(this.base, true);
|
||||||
pos.left -= this.popup.getTextLeftOffset();
|
pos.left -= this.popup.getTextLeftOffset();
|
||||||
|
|
||||||
var rect = editor.container.getBoundingClientRect();
|
var rect = editor.container.getBoundingClientRect();
|
||||||
pos.top += rect.top - renderer.layerConfig.offset;
|
pos.top += rect.top - renderer.layerConfig.offset;
|
||||||
pos.left += rect.left - editor.renderer.scrollLeft;
|
pos.left += rect.left - editor.renderer.scrollLeft;
|
||||||
|
|
@ -94,7 +95,7 @@ var Autocomplete = function() {
|
||||||
this.editor.off("mousedown", this.mousedownListener);
|
this.editor.off("mousedown", this.mousedownListener);
|
||||||
this.editor.off("mousewheel", this.mousewheelListener);
|
this.editor.off("mousewheel", this.mousewheelListener);
|
||||||
this.changeTimer.cancel();
|
this.changeTimer.cancel();
|
||||||
|
|
||||||
if (this.popup)
|
if (this.popup)
|
||||||
this.popup.hide();
|
this.popup.hide();
|
||||||
|
|
||||||
|
|
@ -171,7 +172,13 @@ var Autocomplete = function() {
|
||||||
|
|
||||||
"Esc": function(editor) { editor.completer.detach(); },
|
"Esc": function(editor) { editor.completer.detach(); },
|
||||||
"Space": function(editor) { editor.completer.detach(); editor.insert(" ");},
|
"Space": function(editor) { editor.completer.detach(); editor.insert(" ");},
|
||||||
"Return": function(editor) { editor.completer.insertMatch(); },
|
"Return": function(editor) {
|
||||||
|
if (editor.completer.popup.getRow() > -1) {
|
||||||
|
editor.completer.insertMatch();
|
||||||
|
} else {
|
||||||
|
editor.insert("\n");
|
||||||
|
}
|
||||||
|
},
|
||||||
"Shift-Return": function(editor) { editor.completer.insertMatch(true); },
|
"Shift-Return": function(editor) { editor.completer.insertMatch(true); },
|
||||||
"Tab": function(editor) { editor.completer.insertMatch(); },
|
"Tab": function(editor) { editor.completer.insertMatch(); },
|
||||||
|
|
||||||
|
|
@ -182,10 +189,10 @@ var Autocomplete = function() {
|
||||||
this.gatherCompletions = function(editor, callback) {
|
this.gatherCompletions = function(editor, callback) {
|
||||||
var session = editor.getSession();
|
var session = editor.getSession();
|
||||||
var pos = editor.getCursorPosition();
|
var pos = editor.getCursorPosition();
|
||||||
|
|
||||||
var line = session.getLine(pos.row);
|
var line = session.getLine(pos.row);
|
||||||
var prefix = util.retrievePrecedingIdentifier(line, pos.column);
|
var prefix = util.retrievePrecedingIdentifier(line, pos.column);
|
||||||
|
|
||||||
this.base = editor.getCursorPosition();
|
this.base = editor.getCursorPosition();
|
||||||
this.base.column -= prefix.length;
|
this.base.column -= prefix.length;
|
||||||
|
|
||||||
|
|
@ -208,7 +215,7 @@ var Autocomplete = function() {
|
||||||
this.showPopup = function(editor) {
|
this.showPopup = function(editor) {
|
||||||
if (this.editor)
|
if (this.editor)
|
||||||
this.detach();
|
this.detach();
|
||||||
|
|
||||||
this.activated = true;
|
this.activated = true;
|
||||||
|
|
||||||
this.editor = editor;
|
this.editor = editor;
|
||||||
|
|
@ -223,10 +230,10 @@ var Autocomplete = function() {
|
||||||
editor.on("blur", this.blurListener);
|
editor.on("blur", this.blurListener);
|
||||||
editor.on("mousedown", this.mousedownListener);
|
editor.on("mousedown", this.mousedownListener);
|
||||||
editor.on("mousewheel", this.mousewheelListener);
|
editor.on("mousewheel", this.mousewheelListener);
|
||||||
|
|
||||||
this.updateCompletions();
|
this.updateCompletions();
|
||||||
};
|
};
|
||||||
|
|
||||||
this.updateCompletions = function(keepPopupPosition) {
|
this.updateCompletions = function(keepPopupPosition) {
|
||||||
if (keepPopupPosition && this.base && this.completions) {
|
if (keepPopupPosition && this.base && this.completions) {
|
||||||
var pos = this.editor.getCursorPosition();
|
var pos = this.editor.getCursorPosition();
|
||||||
|
|
@ -243,7 +250,7 @@ var Autocomplete = function() {
|
||||||
var matches = results && results.matches;
|
var matches = results && results.matches;
|
||||||
if (!matches || !matches.length)
|
if (!matches || !matches.length)
|
||||||
return this.detach();
|
return this.detach();
|
||||||
// TODO reenable this when we have proper change tracking
|
// TODO reenable this when we have proper change tracking
|
||||||
// if (matches.length == 1)
|
// if (matches.length == 1)
|
||||||
// return this.insertMatch(matches[0]);
|
// return this.insertMatch(matches[0]);
|
||||||
|
|
||||||
|
|
@ -299,16 +306,16 @@ var FilteredList = function(array, filterText, mutateData) {
|
||||||
matches = matches.sort(function(a, b) {
|
matches = matches.sort(function(a, b) {
|
||||||
return b.exactMatch - a.exactMatch || b.score - a.score;
|
return b.exactMatch - a.exactMatch || b.score - a.score;
|
||||||
});
|
});
|
||||||
|
|
||||||
// make unique
|
// make unique
|
||||||
var prev = null;
|
var prev = null;
|
||||||
matches = matches.filter(function(item){
|
matches = matches.filter(function(item){
|
||||||
var caption = item.value || item.caption || item.snippet;
|
var caption = item.value || item.caption || item.snippet;
|
||||||
if (caption === prev) return false;
|
if (caption === prev) return false;
|
||||||
prev = caption;
|
prev = caption;
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
|
|
||||||
this.filtered = matches;
|
this.filtered = matches;
|
||||||
};
|
};
|
||||||
this.filterCompletions = function(items, needle) {
|
this.filterCompletions = function(items, needle) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue