Merge remote-tracking branch 'remotes/pull/1789'

Conflicts:
	lib/ace/autocomplete.js
This commit is contained in:
nightwing 2014-03-21 15:35:04 +04:00
commit 8e5bdc512c
4 changed files with 145 additions and 29 deletions

View file

@ -592,6 +592,7 @@ env.editSnippets = function() {
require("ace/ext/language_tools"); require("ace/ext/language_tools");
env.editor.setOptions({ env.editor.setOptions({
enableBasicAutocompletion: true, enableBasicAutocompletion: true,
enableLiveAutocomplete: true,
enableSnippets: true enableSnippets: true
}); });

View file

@ -55,6 +55,8 @@ var Autocomplete = function() {
}; };
(function() { (function() {
this.gatherCompletionsId = 0;
this.$init = function() { this.$init = function() {
this.popup = new AcePopup(document.body || document.documentElement); this.popup = new AcePopup(document.body || document.documentElement);
this.popup.on("click", function(e) { this.popup.on("click", function(e) {
@ -72,6 +74,7 @@ var Autocomplete = function() {
var renderer = editor.renderer; var renderer = editor.renderer;
this.popup.setRow(this.autoSelect ? 0 : -1); this.popup.setRow(this.autoSelect ? 0 : -1);
if (!keepPopupPosition) { if (!keepPopupPosition) {
this.popup.setTheme(editor.getTheme());
this.popup.setFontSize(editor.getFontSize()); this.popup.setFontSize(editor.getFontSize());
var lineHeight = renderer.layerConfig.lineHeight; var lineHeight = renderer.layerConfig.lineHeight;
@ -96,6 +99,10 @@ var Autocomplete = function() {
this.editor.off("mousewheel", this.mousewheelListener); this.editor.off("mousewheel", this.mousewheelListener);
this.changeTimer.cancel(); this.changeTimer.cancel();
if (this.popup && this.popup.isOpen) {
this.gatherCompletionsId = this.gatherCompletionsId + 1;
}
if (this.popup) if (this.popup)
this.popup.hide(); this.popup.hide();
@ -146,6 +153,7 @@ var Autocomplete = function() {
data = this.popup.getData(this.popup.getRow()); data = this.popup.getData(this.popup.getRow());
if (!data) if (!data)
return false; return false;
if (data.completer && data.completer.insertMatch) { if (data.completer && data.completer.insertMatch) {
data.completer.insertMatch(this.editor); data.completer.insertMatch(this.editor);
} else { } else {
@ -197,18 +205,18 @@ var Autocomplete = function() {
this.base.column -= prefix.length; this.base.column -= prefix.length;
var matches = []; var matches = [];
util.parForEach(editor.completers, function(completer, next) { var total = editor.completers.length;
editor.completers.forEach(function(completer, i) {
completer.getCompletions(editor, session, pos, prefix, function(err, results) { completer.getCompletions(editor, session, pos, prefix, function(err, results) {
if (!err) if (!err)
matches = matches.concat(results); matches = matches.concat(results);
next();
});
}, function() {
callback(null, { callback(null, {
prefix: prefix, prefix: prefix,
matches: matches matches: matches,
finished: (--total === 0)
}); });
}); });
});
return true; return true;
}; };
@ -243,10 +251,30 @@ var Autocomplete = function() {
this.completions.setFilter(prefix); this.completions.setFilter(prefix);
if (!this.completions.filtered.length) if (!this.completions.filtered.length)
return this.detach(); return this.detach();
if (this.completions.filtered.length == 1
&& this.completions.filtered[0].value == prefix
&& !this.completions.filtered[0].snippet)
return this.detach();
this.openPopup(this.editor, prefix, keepPopupPosition); this.openPopup(this.editor, prefix, keepPopupPosition);
return; return;
} }
// Save current gatherCompletions session, session is close when a match is insert
var _id = this.gatherCompletionsId;
this.gatherCompletions(this.editor, function(err, results) { this.gatherCompletions(this.editor, function(err, results) {
// Only detach if result gathering is finished
var doDetach = function() {
if (!results.finished) return;
return this.detach();
}.bind(this);
// Calcul prefix
var session = this.editor.getSession();
var pos = this.editor.getCursorPosition();
var line = session.getLine(pos.row);
var prefix = util.retrievePrecedingIdentifier(line, pos.column);
// Results matches
var matches = results && results.matches; var matches = results && results.matches;
if (!matches || !matches.length) if (!matches || !matches.length)
return this.detach(); return this.detach();
@ -254,14 +282,32 @@ var Autocomplete = function() {
// if (matches.length == 1) // if (matches.length == 1)
// return this.insertMatch(matches[0]); // return this.insertMatch(matches[0]);
// No prefix or no results -> close
if (!prefix || !prefix.length || !matches || !matches.length)
return doDetach();
// Wrong prefix or wrong session -> ignore
if (prefix.indexOf(results.prefix) != 0
|| _id != this.gatherCompletionsId)
return;
this.completions = new FilteredList(matches); this.completions = new FilteredList(matches);
this.completions.setFilter(results.prefix); this.completions.setFilter(prefix);
var filtered = this.completions.filtered; var filtered = this.completions.filtered;
// No results
if (!filtered.length) if (!filtered.length)
return this.detach(); return doDetach();
// One result equals to the prefix
if (filtered.length == 1 && filtered[0].value == prefix && !filtered[0].snippet)
return doDetach();
// Autoinsert if one result
if (this.autoInsert && filtered.length == 1) if (this.autoInsert && filtered.length == 1)
return this.insertMatch(filtered[0]); return this.insertMatch(filtered[0]);
this.openPopup(this.editor, results.prefix, keepPopupPosition);
this.openPopup(this.editor, prefix, keepPopupPosition);
}.bind(this)); }.bind(this));
}; };

View file

@ -290,19 +290,24 @@ var AcePopup = function(parentNode) {
}; };
dom.importCssString("\ dom.importCssString("\
.ace_autocomplete.ace-tm .ace_marker-layer .ace_active-line {\ .ace_editor.ace_autocomplete .ace_marker-layer .ace_active-line {\
background-color: #CAD6FA;\ background-color: #CAD6FA;\
z-index: 1;\ z-index: 1;\
}\ }\
.ace_autocomplete.ace-tm .ace_line-hover {\ .ace_editor.ace_autocomplete .ace_line-hover {\
border: 1px solid #abbffe;\ border: 1px solid #abbffe;\
margin-top: -1px;\ margin-top: -1px;\
background: rgba(233,233,253,0.4);\ background: rgba(233,233,253,0.4);\
}\ }\
.ace_autocomplete .ace_line-hover {\ .ace_editor.ace_autocomplete .ace_line-hover {\
position: absolute;\ position: absolute;\
z-index: 2;\ z-index: 2;\
}\ }\
.ace_editor.ace_autocomplete .ace_scroller {\
background: none;\
border: none;\
box-shadow: none;\
}\
.ace_rightAlignedText {\ .ace_rightAlignedText {\
color: gray;\ color: gray;\
display: inline-block;\ display: inline-block;\
@ -311,11 +316,11 @@ dom.importCssString("\
text-align: right;\ text-align: right;\
z-index: -1;\ z-index: -1;\
}\ }\
.ace_autocomplete .ace_completion-highlight{\ .ace_editor.ace_autocomplete .ace_completion-highlight{\
color: #000;\ color: #000;\
text-shadow: 0 0 0.01em;\ text-shadow: 0 0 0.01em;\
}\ }\
.ace_autocomplete {\ .ace_editor.ace_autocomplete {\
width: 280px;\ width: 280px;\
z-index: 200000;\ z-index: 200000;\
background: #fbfbfb;\ background: #fbfbfb;\

View file

@ -34,6 +34,7 @@ define(function(require, exports, module) {
var snippetManager = require("../snippets").snippetManager; var snippetManager = require("../snippets").snippetManager;
var Autocomplete = require("../autocomplete").Autocomplete; var Autocomplete = require("../autocomplete").Autocomplete;
var config = require("../config"); var config = require("../config");
var util = require("../autocomplete/util");
var textCompleter = require("../autocomplete/text_completer"); var textCompleter = require("../autocomplete/text_completer");
var keyWordCompleter = { var keyWordCompleter = {
@ -114,6 +115,58 @@ var loadSnippetFile = function(id) {
}); });
}; };
var doLiveAutocomplete = function(e) {
var editor = e.editor;
var session = editor.getSession();
var pos = editor.getCursorPosition();
var line = session.getLine(pos.row);
var hasCompleter = (editor.completer && editor.completer.activated);
var text = e.args || "";
// Is the user entering text
// we only want to automatically show the autocomplete dialog
// whenever the user is typing in text not pasting, deleting, ...
var typing = (e.command.name === "insertstring" && text.length === 1);
// We don't want to autocomplete with no prefix
if(
e.command.name === 'backspace' &&
util.retrievePrecedingIdentifier(line, pos.column) === ''
) {
if(hasCompleter) editor.completer.detach();
return;
}
// we don't want to autocomplete on paste events
if(!typing) {
return;
}
// The prefix to autocomplete for
var prefix = util.retrievePrecedingIdentifier(line, pos.column);
// Only autocomplete if there's a prefix that can be matched
if(prefix !== '' && !(hasCompleter)) {
if (!editor.completer) {
// Create new autocompleter
editor.completer = new Autocomplete();
// Disable autoInsert
editor.completer.autoInsert = false;
}
editor.completer.showPopup(editor);
// needed for firefox on mac
editor.completer.cancelContextMenu();
} else if(prefix === '' && hasCompleter) {
// When the prefix is empty
// close the autocomplete dialog
editor.completer.detach();
}
};
var Editor = require("../editor").Editor; var Editor = require("../editor").Editor;
require("../config").defineOptions(Editor.prototype, "editor", { require("../config").defineOptions(Editor.prototype, "editor", {
enableBasicAutocompletion: { enableBasicAutocompletion: {
@ -127,6 +180,17 @@ require("../config").defineOptions(Editor.prototype, "editor", {
}, },
value: false value: false
}, },
enableLiveAutocomplete: {
set: function(val) {
if (val) {
// On each change automatically trigger the autocomplete
this.commands.on('afterExec', doLiveAutocomplete);
} else {
this.commands.removeListener('afterExec', doLiveAutocomplete);
}
},
value: false
},
enableSnippets: { enableSnippets: {
set: function(val) { set: function(val) {
if (val) { if (val) {