Skip to content

Commit b811f1d

Browse files
gjtorikiannightwing
authored andcommitted
Fix rerender on text insertion
1 parent cd19233 commit b811f1d

4 files changed

Lines changed: 164 additions & 112 deletions

File tree

lib/ace/autocomplete.js

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ var Autocomplete = function() {
163163

164164
el.style.left = pos.left + "px";
165165
el.style.display = "";
166+
167+
renderer.updateText();
166168
};
167169

168170
this.detach = function() {
@@ -173,10 +175,15 @@ var Autocomplete = function() {
173175

174176
if (this.popup)
175177
this.popup.container.style.display = "none";
178+
179+
this.editor.Autocomplete.activated = false;
176180
};
177181

178182
this.changeListener = function(e) {
179-
//console.log(e)
183+
if (this.editor.Autocomplete.activated)
184+
Autocomplete.startCommand.exec(this.editor);
185+
else
186+
this.detach();
180187
};
181188

182189
this.blurListener = function() {
@@ -215,6 +222,8 @@ var Autocomplete = function() {
215222
};
216223

217224
this.insertMatch = function(row) {
225+
this.detach();
226+
218227
if (row == undefined)
219228
row = this.popup.getRow();
220229
var text = this.completions.filtered[row];
@@ -224,7 +233,6 @@ var Autocomplete = function() {
224233
// should be good enough, otherwise we can use getDocument().removeInLine
225234
this.editor.removeWordLeft();
226235
this.editor.insert(text);
227-
this.detach();
228236
};
229237

230238
this.commands = {
@@ -237,16 +245,7 @@ var Autocomplete = function() {
237245
"space": function(editor) { editor.Autocomplete.detach(); editor.insert(" ");},
238246
"Return": function(editor) { editor.Autocomplete.insertMatch(); },
239247
"Shift-Return": function(editor) { editor.Autocomplete.insertMatch(true); },
240-
"Tab": function(editor) { editor.Autocomplete.insertMatch(); },
241-
"backspace": function(editor) {
242-
var doc = editor.session.getDocument(),
243-
cursor = editor.getCursorPosition();
244-
245-
editor.Autocomplete.detach();
246-
// delete one char, and reevaluate
247-
editor.remove("left");
248-
editor.Autocomplete.complete(editor);
249-
}
248+
"Tab": function(editor) { editor.Autocomplete.insertMatch(); }
250249
};
251250

252251
this.complete = function(editor) {
@@ -266,15 +265,19 @@ var Autocomplete = function() {
266265
editor.on("blur", this.$blurListener);
267266
editor.on("mousedown", this.$mousedownListener);
268267

269-
worker.attachToDocument(editor.session.getDocument(), {cursor: editor.getCursorPosition(), keywords: editor.session.getMode().getKeywords(true)}, true);
268+
worker.attachToDocument(editor.session.getDocument(), {cursor: editor.getCursorPosition(), keywords: editor.session.getMode().getKeywords()}, true);
270269

271270
worker.on("complete", function(data) {
272271
var matches = data.data.matches;
273-
_self.completions = new FilteredList(matches);
274-
_self.completions.setFilter("a");
275272

276273
if (matches.length) {
277-
_self.openPopup(editor); }
274+
_self.completions = new FilteredList(matches);
275+
_self.completions.setFilter("a");
276+
_self.openPopup(editor);
277+
}
278+
else {
279+
_self.detach();
280+
}
278281
});
279282
};
280283

@@ -377,6 +380,7 @@ Autocomplete.startCommand = {
377380
if (!editor.Autocomplete)
378381
editor.Autocomplete = new Autocomplete();
379382
editor.Autocomplete.complete(editor);
383+
editor.Autocomplete.activated = true;
380384
},
381385
bindKey: "Ctrl-Space|Shift-Space|Alt-Space"
382386
}

lib/ace/autocomplete/autocomplete_worker.js

Lines changed: 17 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -45,73 +45,32 @@ var AutocompleteWorker = exports.AutocompleteWorker = function(sender) {
4545
oop.inherits(AutocompleteWorker, Mirror);
4646

4747
(function() {
48-
// For code completion
49-
function removeDuplicateMatches(matches) {
50-
// First sort
51-
matches.sort(function(a, b) {
52-
if (a.name < b.name)
53-
return 1;
54-
else if (a.name > b.name)
55-
return -1;
56-
else
57-
return 0;
58-
});
59-
60-
for(var i = 1; i < matches.length; ){
61-
if (matches[i - 1] == matches[i]){
62-
matches.splice(i, 1);
63-
} else {
64-
i++;
65-
}
66-
}
67-
68-
return matches;
69-
};
70-
7148
this.onUpdate = function() {
7249
var _self = this;
7350

7451
var doc = this.doc.getValue();
7552
var pos = this.data.cursor;
7653

7754
var currentPos = { line: pos.row, col: pos.column };
78-
var matches = [];
7955

8056
completer.complete(_self.doc, this.data.cursor, this.data.keywords, function(identifier, completions) {
81-
if (completions)
82-
matches = matches.concat(completions);
83-
removeDuplicateMatches(matches);
84-
// Sort by priority, score
85-
matches.sort(function(a, b) {
86-
if (a.priority < b.priority)
87-
return 1;
88-
else if (a.priority > b.priority)
89-
return -1;
90-
else if (a.score < b.score)
91-
return 1;
92-
else if (a.score > b.score)
93-
return -1;
94-
else if (a.id && a.id === b.id) {
95-
if (a.isFunction)
96-
return -1;
97-
else if (b.isFunction)
98-
return 1;
99-
}
100-
if (a.name < b.name)
101-
return -1;
102-
else if(a.name > b.name)
103-
return 1;
104-
else
105-
return 0;
106-
});
107-
_self.sender.emit("complete", {
108-
startRow: pos.row,
109-
startColumn: pos.column - identifier.length,
110-
endRow: pos.row,
111-
endColumn: Infinity,
112-
matches: matches,
113-
line: _self.doc.getLine(pos.row)
114-
});
57+
if (!identifier) {
58+
_self.sender.emit("complete", {
59+
matches: []
60+
});
61+
}
62+
63+
else {
64+
_self.sender.emit("complete", {
65+
startRow: pos.row,
66+
startColumn: pos.column - identifier.length,
67+
endRow: pos.row,
68+
endColumn: Infinity,
69+
matches: completions,
70+
line: _self.doc.getLine(pos.row)
71+
});
72+
}
73+
11574
return;
11675
});
11776
};

lib/ace/autocomplete/complete_util.js

Lines changed: 88 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,33 @@
1+
/* ***** BEGIN LICENSE BLOCK *****
2+
* Distributed under the BSD license:
3+
*
4+
* Copyright (c) 2012, Ajax.org B.V.
5+
* All rights reserved.
6+
*
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
* * Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
* * Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in the
13+
* documentation and/or other materials provided with the distribution.
14+
* * Neither the name of Ajax.org B.V. nor the
15+
* names of its contributors may be used to endorse or promote products
16+
* derived from this software without specific prior written permission.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
* DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
22+
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25+
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28+
*
29+
* ***** END LICENSE BLOCK ***** */
30+
131
define(function(require, exports, module) {
232

333
var ID_REGEX = /[a-zA-Z_0-9\$]/;
@@ -26,36 +56,71 @@ function retrieveFollowingIdentifier(text, pos, regex) {
2656
return buf;
2757
}
2858

29-
function prefixBinarySearch(items, prefix) {
30-
var startIndex = 0;
31-
var stopIndex = items.length - 1;
32-
var middle = Math.floor((stopIndex + startIndex) / 2);
33-
34-
while (stopIndex > startIndex && middle >= 0 && items[middle].indexOf(prefix) !== 0) {
35-
if (prefix < items[middle]) {
36-
stopIndex = middle - 1;
37-
}
38-
else if (prefix > items[middle]) {
39-
startIndex = middle + 1;
59+
// filched from jQuery
60+
function grep( elems, callback, inv ) {
61+
var retVal,
62+
ret = [],
63+
i = 0,
64+
length = elems.length;
65+
inv = !!inv;
66+
67+
// Go through the array, only saving the items
68+
// that pass the validator function
69+
for ( ; i < length; i++ ) {
70+
retVal = !!callback( elems[ i ], i );
71+
if ( inv !== retVal ) {
72+
ret.push( elems[ i ] );
4073
}
41-
middle = Math.floor((stopIndex + stopIndex) / 2);
4274
}
75+
76+
return ret;
77+
};
78+
79+
function sortByScore(items, identDict) {
4380

44-
// Look back to make sure we haven't skipped any
45-
while (middle > 0 && items[middle-1].indexOf(prefix) === 0)
46-
middle--;
47-
return middle >= 0 ? middle : 0; // ensure we're not returning a negative index
48-
}
81+
return items.sort(function(a, b) {
82+
var scoreA = identDict[a],
83+
scoreB = identDict[b];
84+
85+
if (a < b)
86+
return 1;
87+
else if (a > b)
88+
return -1;
89+
else
90+
return 0;
91+
});
92+
};
93+
94+
function findCompletions(prefix, identDict, allIdentifiers) {
95+
var _self = this,
96+
fuzzyMatcher = function (prefix, item) {
97+
return ~item.toLowerCase().indexOf(prefix.toLowerCase());
98+
};
99+
100+
var matches = grep(allIdentifiers, function (item) {
101+
return fuzzyMatcher(prefix, item);
102+
});
103+
104+
matches = sortByScore(matches, identDict);
49105

50-
function findCompletions(prefix, allIdentifiers) {
51-
allIdentifiers.sort();
52-
var startIdx = prefixBinarySearch(allIdentifiers, prefix);
53-
var matches = [];
54-
for (var i = startIdx; i < allIdentifiers.length && allIdentifiers[i].indexOf(prefix) === 0; i++)
55-
matches.push(allIdentifiers[i]);
56106
return matches;
57107
}
58108

109+
exports.removeDuplicateWords = function(matches) {
110+
// First, sort
111+
matches = matches.sort();
112+
113+
for (var i = 1; i < matches.length; ){
114+
if (matches[i - 1] == matches[i]){
115+
matches.splice(i, 1);
116+
} else {
117+
i++;
118+
}
119+
}
120+
121+
return matches;
122+
};
123+
59124
exports.retrievePrecedingIdentifier = retrievePrecedingIdentifier;
60125
exports.retrieveFollowingIdentifier = retrieveFollowingIdentifier;
61126
exports.findCompletions = findCompletions;

0 commit comments

Comments
 (0)