Some added UX behaviors
This commit is contained in:
parent
ef19fdbcb5
commit
818dba5dba
5 changed files with 46 additions and 40 deletions
|
|
@ -38,7 +38,9 @@ var UndoManager = require("ace/undomanager").UndoManager;
|
||||||
var dom = require("ace/lib/dom");
|
var dom = require("ace/lib/dom");
|
||||||
var HashHandler = require("ace/keyboard/hash_handler").HashHandler;
|
var HashHandler = require("ace/keyboard/hash_handler").HashHandler;
|
||||||
var TextMode = require("ace/mode/text").Mode;
|
var TextMode = require("ace/mode/text").Mode;
|
||||||
|
|
||||||
var WorkerClient = require("./worker/worker_client").WorkerClient;
|
var WorkerClient = require("./worker/worker_client").WorkerClient;
|
||||||
|
var worker = new WorkerClient(["ace"], "ace/autocomplete/autocomplete_worker", "AutocompleteWorker");
|
||||||
|
|
||||||
var mode = new TextMode();
|
var mode = new TextMode();
|
||||||
mode.$tokenizer = {
|
mode.$tokenizer = {
|
||||||
|
|
@ -46,7 +48,6 @@ mode.$tokenizer = {
|
||||||
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
var worker = new WorkerClient(["ace"], "ace/autocomplete/autocomplete_worker", "AutocompleteWorker");
|
|
||||||
|
|
||||||
var Autocomplete = function() {
|
var Autocomplete = function() {
|
||||||
this.keyboardHandler = new HashHandler();
|
this.keyboardHandler = new HashHandler();
|
||||||
|
|
@ -82,7 +83,6 @@ var Autocomplete = function() {
|
||||||
|
|
||||||
popup.renderer.maxLines = 6
|
popup.renderer.maxLines = 6
|
||||||
popup.renderer.$keepTextAreaAtCursor=false
|
popup.renderer.$keepTextAreaAtCursor=false
|
||||||
|
|
||||||
|
|
||||||
popup.setHighlightActiveLine(true);
|
popup.setHighlightActiveLine(true);
|
||||||
popup.setSession(new EditSession(""));
|
popup.setSession(new EditSession(""));
|
||||||
|
|
@ -104,6 +104,7 @@ var Autocomplete = function() {
|
||||||
line = -1;
|
line = -1;
|
||||||
return line;
|
return line;
|
||||||
};
|
};
|
||||||
|
|
||||||
popup.setRow = function(line) {
|
popup.setRow = function(line) {
|
||||||
popup.setHighlightActiveLine(line != -1);
|
popup.setHighlightActiveLine(line != -1);
|
||||||
popup.gotoLine(line + 1);
|
popup.gotoLine(line + 1);
|
||||||
|
|
@ -165,7 +166,9 @@ var Autocomplete = function() {
|
||||||
this.editor.keyBinding.removeKeyboardHandler(this.keyboardHandler);
|
this.editor.keyBinding.removeKeyboardHandler(this.keyboardHandler);
|
||||||
this.editor.removeEventListener("changeSelection", this.changeListener);
|
this.editor.removeEventListener("changeSelection", this.changeListener);
|
||||||
this.editor.removeEventListener("blur", this.changeListener);
|
this.editor.removeEventListener("blur", this.changeListener);
|
||||||
this.popup.container.style.display = "none";
|
|
||||||
|
if (this.popup)
|
||||||
|
this.popup.container.style.display = "none";
|
||||||
};
|
};
|
||||||
|
|
||||||
this.changeListener = function(e) {
|
this.changeListener = function(e) {
|
||||||
|
|
@ -203,6 +206,9 @@ var Autocomplete = function() {
|
||||||
var text = this.completions.filtered[row];
|
var text = this.completions.filtered[row];
|
||||||
if (text.value)
|
if (text.value)
|
||||||
text = text.value;
|
text = text.value;
|
||||||
|
|
||||||
|
// should be good enough, otherwise we can use getDocument().removeInLine
|
||||||
|
this.editor.removeWordLeft();
|
||||||
this.editor.insert(text);
|
this.editor.insert(text);
|
||||||
this.detach();
|
this.detach();
|
||||||
};
|
};
|
||||||
|
|
@ -214,6 +220,7 @@ var Autocomplete = function() {
|
||||||
"ctrl-down": function(editor) { editor.Autocomplete.goTo("end"); },
|
"ctrl-down": function(editor) { editor.Autocomplete.goTo("end"); },
|
||||||
|
|
||||||
"esc": function(editor) { editor.Autocomplete.detach(); },
|
"esc": function(editor) { editor.Autocomplete.detach(); },
|
||||||
|
"space": function(editor) { editor.Autocomplete.detach(); editor.insert(" ");},
|
||||||
"Return": function(editor) { editor.Autocomplete.insertMatch(); },
|
"Return": function(editor) { editor.Autocomplete.insertMatch(); },
|
||||||
"Shift-Return": function(editor) { editor.Autocomplete.insertMatch(true); },
|
"Shift-Return": function(editor) { editor.Autocomplete.insertMatch(true); },
|
||||||
"Tab": function(editor) { editor.Autocomplete.insertMatch(); },
|
"Tab": function(editor) { editor.Autocomplete.insertMatch(); },
|
||||||
|
|
@ -236,22 +243,15 @@ var Autocomplete = function() {
|
||||||
editor.on("changeSelection", this.$changeListener);
|
editor.on("changeSelection", this.$changeListener);
|
||||||
editor.on("blur", this.$blurListener);
|
editor.on("blur", this.$blurListener);
|
||||||
|
|
||||||
worker.attachToDocument(editor.session.getDocument(), {cursor: editor.getCursorPosition()});
|
worker.attachToDocument(editor.session.getDocument(), {cursor: editor.getCursorPosition()}, true);
|
||||||
|
|
||||||
worker.on("complete", function(data) {
|
worker.on("complete", function(data) {
|
||||||
_self.completions = new FilteredList(data.data.matches);
|
var matches = data.data.matches;
|
||||||
|
_self.completions = new FilteredList(matches);
|
||||||
_self.completions.setFilter("a");
|
_self.completions.setFilter("a");
|
||||||
|
|
||||||
if (data) {
|
if (matches.length)
|
||||||
if (data.length == 1)
|
_self.openPopup(editor);
|
||||||
_self.insertMatch(0);
|
|
||||||
else
|
|
||||||
_self.openPopup(editor);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
worker.on("terminate", function() {
|
|
||||||
console.log("term");
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
/* ***** BEGIN LICENSE BLOCK *****
|
/* ***** BEGIN LICENSE BLOCK *****
|
||||||
* Distributed under the BSD license:
|
* Distributed under the BSD license:
|
||||||
*
|
*
|
||||||
* Copyright (c) 2010, Ajax.org B.V.
|
* Copyright (c) 2012, Ajax.org B.V.
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
|
@ -38,7 +38,9 @@ var SyntaxDetector = require("./syntax_detector");
|
||||||
var completer = require("./text_completer");
|
var completer = require("./text_completer");
|
||||||
|
|
||||||
var AutocompleteWorker = exports.AutocompleteWorker = function(sender) {
|
var AutocompleteWorker = exports.AutocompleteWorker = function(sender) {
|
||||||
|
this.setTimeout(0);
|
||||||
Mirror.call(this, sender);
|
Mirror.call(this, sender);
|
||||||
|
this.setDeferredUpdate(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
oop.inherits(AutocompleteWorker, Mirror);
|
oop.inherits(AutocompleteWorker, Mirror);
|
||||||
|
|
@ -55,24 +57,16 @@ oop.inherits(AutocompleteWorker, Mirror);
|
||||||
else
|
else
|
||||||
return 0;
|
return 0;
|
||||||
});
|
});
|
||||||
for (var i = 0; i < matches.length - 1; i++) {
|
|
||||||
var a = matches[i];
|
for(var i = 1; i < matches.length; ){
|
||||||
var b = matches[i + 1];
|
if (matches[i - 1] == matches[i]){
|
||||||
if (a.name === b.name) {
|
matches.splice(i, 1);
|
||||||
// Duplicate!
|
} else {
|
||||||
if (a.priority < b.priority)
|
i++;
|
||||||
matches.splice(i, 1);
|
|
||||||
else if (a.priority > b.priority)
|
|
||||||
matches.splice(i+1, 1);
|
|
||||||
else if (a.score < b.score)
|
|
||||||
matches.splice(i, 1);
|
|
||||||
else if (a.score > b.score)
|
|
||||||
matches.splice(i+1, 1);
|
|
||||||
else
|
|
||||||
matches.splice(i, 1);
|
|
||||||
i--;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return matches;
|
||||||
};
|
};
|
||||||
|
|
||||||
this.onUpdate = function() {
|
this.onUpdate = function() {
|
||||||
|
|
@ -87,7 +81,7 @@ oop.inherits(AutocompleteWorker, Mirror);
|
||||||
var currentNode = null;
|
var currentNode = null;
|
||||||
var matches = [], ast = null;
|
var matches = [], ast = null;
|
||||||
|
|
||||||
completer.complete(_self.doc, ast, this.data.cursor, currentNode, function(completions) {
|
completer.complete(_self.doc, ast, this.data.cursor, currentNode, function(identifier, completions) {
|
||||||
if (completions)
|
if (completions)
|
||||||
matches = matches.concat(completions);
|
matches = matches.concat(completions);
|
||||||
removeDuplicateMatches(matches);
|
removeDuplicateMatches(matches);
|
||||||
|
|
@ -115,7 +109,10 @@ oop.inherits(AutocompleteWorker, Mirror);
|
||||||
return 0;
|
return 0;
|
||||||
});
|
});
|
||||||
_self.sender.emit("complete", {
|
_self.sender.emit("complete", {
|
||||||
pos: pos,
|
startRow: pos.row,
|
||||||
|
startColumn: pos.column - identifier.length,
|
||||||
|
endRow: pos.row,
|
||||||
|
endColumn: Infinity,
|
||||||
matches: matches,
|
matches: matches,
|
||||||
line: _self.doc.getLine(pos.row)
|
line: _self.doc.getLine(pos.row)
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -53,13 +53,16 @@ define(function(require, exports, module) {
|
||||||
var line = doc.getLine(pos.row);
|
var line = doc.getLine(pos.row);
|
||||||
var identifier = completeUtil.retrievePrecedingIdentifier(line, pos.column);
|
var identifier = completeUtil.retrievePrecedingIdentifier(line, pos.column);
|
||||||
|
|
||||||
|
if (identifier === "")
|
||||||
|
return callback(null);
|
||||||
|
|
||||||
var allIdentifiers = [];
|
var allIdentifiers = [];
|
||||||
for (var ident in identDict) {
|
for (var ident in identDict) {
|
||||||
allIdentifiers.push(ident);
|
allIdentifiers.push(ident);
|
||||||
}
|
}
|
||||||
var matches = completeUtil.findCompletions(identifier, allIdentifiers);
|
var matches = completeUtil.findCompletions(identifier, allIdentifiers);
|
||||||
|
|
||||||
callback(matches);
|
callback(identifier, matches);
|
||||||
/*callback(matches.map(function(m) {
|
/*callback(matches.map(function(m) {
|
||||||
return {
|
return {
|
||||||
name : m,
|
name : m,
|
||||||
|
|
|
||||||
|
|
@ -10,22 +10,28 @@ var Mirror = exports.Mirror = function(sender) {
|
||||||
this.data = {};
|
this.data = {};
|
||||||
|
|
||||||
var deferredUpdate = this.deferredUpdate = lang.delayedCall(this.onUpdate.bind(this));
|
var deferredUpdate = this.deferredUpdate = lang.delayedCall(this.onUpdate.bind(this));
|
||||||
|
|
||||||
var _self = this;
|
var _self = this;
|
||||||
sender.on("change", function(e) {
|
sender.on("change", function(e) {
|
||||||
doc.applyDeltas(e.data);
|
doc.applyDeltas(e.data);
|
||||||
deferredUpdate.schedule(_self.$timeout);
|
if (_self.$defer)
|
||||||
|
deferredUpdate.schedule(_self.$timeout);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
|
|
||||||
this.$timeout = 500;
|
this.$timeout = 500;
|
||||||
|
this.$defer = true;
|
||||||
|
|
||||||
this.setTimeout = function(timeout) {
|
this.setTimeout = function(timeout) {
|
||||||
this.$timeout = timeout;
|
this.$timeout = timeout;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
this.setDeferredUpdate = function(defer) {
|
||||||
|
this.$defer = defer;
|
||||||
|
};
|
||||||
|
|
||||||
this.setValue = function(value, data) {
|
this.setValue = function(value, data) {
|
||||||
this.doc.setValue(value);
|
this.doc.setValue(value);
|
||||||
this.data = data;
|
this.data = data;
|
||||||
|
|
|
||||||
|
|
@ -144,8 +144,8 @@ var WorkerClient = function(topLevelNamespaces, mod, classname) {
|
||||||
catch(ex) {}
|
catch(ex) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
this.attachToDocument = function(doc, data) {
|
this.attachToDocument = function(doc, data, ignore) {
|
||||||
if (this.$doc)
|
if (this.$doc && !ignore)
|
||||||
this.terminate();
|
this.terminate();
|
||||||
|
|
||||||
this.$doc = doc;
|
this.$doc = doc;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue