bug fixes and tests

This commit is contained in:
Mihai Sucan 2011-02-17 01:06:00 +08:00 • committed by Fabian Jakobs
commit bea0289a77
6 changed files with 174 additions and 18 deletions

View file

@ -94,22 +94,25 @@ var Mode = function() {
var startOuter = selection.start.column - 1; var startOuter = selection.start.column - 1;
var endOuter = selection.end.column + 1; var endOuter = selection.end.column + 1;
var line = session.getLine(selection.start.row); var line = session.getLine(selection.start.row);
var lineCols = line.length - 1; var lineCols = line.length;
var needle = line.substring(Math.max(startOuter, 0), var needle = line.substring(Math.max(startOuter, 0),
Math.min(endOuter, lineCols)); Math.min(endOuter, lineCols));
// Make sure the outer characters are not part of the word. // Make sure the outer characters are not part of the word.
if ((startOuter >= 0 && !/[^\w\d]/.test(needle.charAt(0))) || if ((startOuter >= 0 && /^[\w\d]/.test(needle)) ||
(endOuter <= lineCols && !/[^\w\d]/.test(needle.charAt(needle.length - 1)))) (endOuter <= lineCols && /[\w\d]$/.test(needle)))
return; return;
needle = line.substring(selection.start.column, selection.end.column); needle = line.substring(selection.start.column, selection.end.column);
if (!/^[\w\d]+$/.test(needle)) if (!/^[\w\d]+$/.test(needle))
return; return;
var cursor = editor.getCursorPosition();
var newOptions = { var newOptions = {
wrap: true, wrap: true,
wholeWord: true, wholeWord: true,
caseSensitive: true,
needle: needle needle: needle
}; };
@ -117,9 +120,8 @@ var Mode = function() {
editor.$search.set(newOptions); editor.$search.set(newOptions);
var ranges = editor.$search.findAll(session); var ranges = editor.$search.findAll(session);
session.$selectionOccurrences = [];
ranges.forEach(function(range) { ranges.forEach(function(range) {
if (!range.contains(selection.start.row, selection.start.column)) { if (!range.contains(cursor.row, cursor.column)) {
var marker = session.addMarker(range, "ace_selected_word"); var marker = session.addMarker(range, "ace_selected_word");
session.$selectionOccurrences.push(marker); session.$selectionOccurrences.push(marker);
} }
@ -135,6 +137,8 @@ var Mode = function() {
editor.session.$selectionOccurrences.forEach(function(marker) { editor.session.$selectionOccurrences.forEach(function(marker) {
editor.session.removeMarker(marker); editor.session.removeMarker(marker);
}); });
editor.session.$selectionOccurrences = [];
}; };
}).call(Mode.prototype); }).call(Mode.prototype);

View file

@ -1,4 +1,5 @@
/* ***** BEGIN LICENSE BLOCK ***** /* vim:ts=4:sts=4:sw=4:
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
* *
* The contents of this file are subject to the Mozilla Public License Version * The contents of this file are subject to the Mozilla Public License Version
@ -20,6 +21,7 @@
* *
* Contributor(s): * Contributor(s):
* Fabian Jakobs <fabian AT ajax DOT org> * Fabian Jakobs <fabian AT ajax DOT org>
* Mihai Sucan <mihai DOT sucan AT gmail DOT com>
* *
* Alternatively, the contents of this file may be used under the terms of * Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or * either the GNU General Public License Version 2 or later (the "GPL"), or
@ -219,12 +221,16 @@ Search.SELECTION = 2;
var lastRow = searchSelection ? range.end.row : session.getLength() - 1; var lastRow = searchSelection ? range.end.row : session.getLength() - 1;
var wrap = this.$options.wrap; var wrap = this.$options.wrap;
var inWrap = false;
function getLine(row) { function getLine(row) {
var line = session.getLine(row); var line = session.getLine(row);
if (searchSelection && row == range.end.row) { if (searchSelection && row == range.end.row) {
line = line.substring(0, range.end.column); line = line.substring(0, range.end.column);
} }
if (inWrap && row == start.row) {
line = line.substring(0, start.column);
}
return line; return line;
} }
@ -236,6 +242,7 @@ Search.SELECTION = 2;
var startIndex = start.column; var startIndex = start.column;
var stop = false; var stop = false;
inWrap = false;
while (!callback(line, startIndex, row)) { while (!callback(line, startIndex, row)) {
@ -250,6 +257,7 @@ Search.SELECTION = 2;
if (wrap) { if (wrap) {
row = firstRow; row = firstRow;
startIndex = firstColumn; startIndex = firstColumn;
inWrap = true;
} else { } else {
return; return;
} }
@ -283,6 +291,7 @@ Search.SELECTION = 2;
var line = session.getLine(row).substring(0, start.column); var line = session.getLine(row).substring(0, start.column);
var startIndex = 0; var startIndex = 0;
var stop = false; var stop = false;
var inWrap = false;
while (!callback(line, startIndex, row)) { while (!callback(line, startIndex, row)) {
@ -295,6 +304,7 @@ Search.SELECTION = 2;
if (row < firstRow) { if (row < firstRow) {
if (wrap) { if (wrap) {
row = lastRow; row = lastRow;
inWrap = true;
} else { } else {
return; return;
} }
@ -310,6 +320,9 @@ Search.SELECTION = 2;
else if (row == lastRow) else if (row == lastRow)
line = line.substring(0, range.end.column); line = line.substring(0, range.end.column);
} }
if (inWrap && row == start.row)
startIndex = start.column;
} }
} }
}; };

View file

@ -51,6 +51,7 @@ async.concat(
require("./search_test"), require("./search_test"),
require("./selection_test"), require("./selection_test"),
require("./text_edit_test"), require("./text_edit_test"),
require("./highlight_selected_word_test"),
require("./mode/css_test"), require("./mode/css_test"),
require("./mode/css_tokenizer_test"), require("./mode/css_tokenizer_test"),
require("./mode/html_test"), require("./mode/html_test"),
@ -59,6 +60,5 @@ async.concat(
require("./mode/javascript_tokenizer_test"), require("./mode/javascript_tokenizer_test"),
require("./mode/text_test"), require("./mode/text_test"),
require("./mode/xml_test"), require("./mode/xml_test"),
require("./mode/xml_tokenizer_test"), require("./mode/xml_tokenizer_test")
require("./issue57_test")
).exec(); ).exec();

View file

@ -20,7 +20,7 @@ async.concat(
require("./selection_test"), require("./selection_test"),
require("./text_edit_test"), require("./text_edit_test"),
require("./virtual_renderer_test"), require("./virtual_renderer_test"),
require("./issue57_test"), require("./highlight_selected_word_test"),
require("./mode/css_test"), require("./mode/css_test"),
require("./mode/css_tokenizer_test"), require("./mode/css_tokenizer_test"),
require("./mode/html_test"), require("./mode/html_test"),

View file

@ -63,21 +63,23 @@ var lipsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. " +
"felis sodales. In dignissim magna eget nunc lobortis non " + "felis sodales. In dignissim magna eget nunc lobortis non " +
"fringilla nibh ullamcorper. Donec facilisis malesuada elit " + "fringilla nibh ullamcorper. Donec facilisis malesuada elit " +
"at egestas. Etiam bibendum, diam vitae tempor aliquet, dui " + "at egestas. Etiam bibendum, diam vitae tempor aliquet, dui " +
"libero vehicula odio, eget bibendum mauris velit eu lorem."; "libero vehicula odio, eget bibendum mauris velit eu lorem.\n" +
"consectetur";
var Test = { var Test = {
setUp: function() { setUp: function() {
this.session = new EditSession(lipsum); this.session = new EditSession(lipsum);
this.editor = new Editor(new MockRenderer(), this.session); this.editor = new Editor(new MockRenderer(), this.session);
this.selection = this.session.getSelection(); this.selection = this.session.getSelection();
this.search = this.editor.$search;
}, },
"issue 57: highlight selected words by default": function() { "test: highlight selected words by default": function() {
assert.equal(this.editor.getHighlightSelectedWord(), true); assert.equal(this.editor.getHighlightSelectedWord(), true);
}, },
"issue 57: higlight a word": function() { "test: highlight a word": function() {
this.selection.moveCursorTo(0, 9); this.editor.moveCursorTo(0, 9);
this.selection.selectWord(); this.selection.selectWord();
var range = this.selection.getRange(); var range = this.selection.getRange();
@ -85,7 +87,19 @@ var Test = {
assert.equal(this.session.$selectionOccurrences.length, 1); assert.equal(this.session.$selectionOccurrences.length, 1);
}, },
"issue 57: higlight another word": function() { "test: highlight a word and clear highlight": function() {
this.editor.moveCursorTo(0, 8);
this.selection.selectWord();
var range = this.selection.getRange();
assert.equal(this.session.getTextRange(range), "ipsum");
assert.equal(this.session.$selectionOccurrences.length, 1);
this.session.getMode().clearSelectionHighlight(this.editor);
assert.equal(this.session.$selectionOccurrences.length, 0);
},
"test: highlight another word": function() {
this.selection.moveCursorTo(0, 14); this.selection.moveCursorTo(0, 14);
this.selection.selectWord(); this.selection.selectWord();
@ -94,12 +108,12 @@ var Test = {
assert.equal(this.session.$selectionOccurrences.length, 3); assert.equal(this.session.$selectionOccurrences.length, 3);
}, },
"issue 57: no selection, no highlight": function() { "test: no selection, no highlight": function() {
this.selection.clearSelection(); this.selection.clearSelection();
assert.equal(this.session.$selectionOccurrences.length, 0); assert.equal(this.session.$selectionOccurrences.length, 0);
}, },
"issue 57: select a word, no highlight": function() { "test: select a word, no highlight": function() {
this.editor.setHighlightSelectedWord(false); this.editor.setHighlightSelectedWord(false);
this.selection.moveCursorTo(0, 14); this.selection.moveCursorTo(0, 14);
this.selection.selectWord(); this.selection.selectWord();
@ -107,7 +121,86 @@ var Test = {
var range = this.selection.getRange(); var range = this.selection.getRange();
assert.equal(this.session.getTextRange(range), "dolor"); assert.equal(this.session.getTextRange(range), "dolor");
assert.equal(this.session.$selectionOccurrences.length, 0); assert.equal(this.session.$selectionOccurrences.length, 0);
} },
"test: select a word with no matches": function() {
this.editor.setHighlightSelectedWord(true);
var currentOptions = this.search.getOptions();
var newOptions = {
wrap: true,
wholeWord: true,
caseSensitive: true,
needle: "Mauris"
};
this.search.set(newOptions);
var match = this.search.find(this.session);
assert.notEqual(match, null, "found a match for 'Mauris'");
this.search.set(currentOptions);
this.selection.setSelectionRange(match);
assert.equal(this.session.getTextRange(match), "Mauris");
assert.equal(this.session.$selectionOccurrences.length, 0);
},
"test: partial word selection 1": function() {
this.selection.moveCursorTo(0, 14);
this.selection.selectWord();
this.selection.selectLeft();
var range = this.selection.getRange();
assert.equal(this.session.getTextRange(range), "dolo");
assert.equal(this.session.$selectionOccurrences.length, 0);
},
"test: partial word selection 2": function() {
this.selection.moveCursorTo(0, 13);
this.selection.selectWord();
this.selection.selectRight();
var range = this.selection.getRange();
assert.equal(this.session.getTextRange(range), "dolor ");
assert.equal(this.session.$selectionOccurrences.length, 0);
},
"test: partial word selection 3": function() {
this.selection.moveCursorTo(0, 14);
this.selection.selectWord();
this.selection.selectLeft();
this.selection.shiftSelection(1);
var range = this.selection.getRange();
assert.equal(this.session.getTextRange(range), "olor");
assert.equal(this.session.$selectionOccurrences.length, 0);
},
"test: select last word": function() {
this.selection.moveCursorTo(0, 1);
var currentOptions = this.search.getOptions();
var newOptions = {
wrap: true,
wholeWord: true,
caseSensitive: true,
backwards: true,
needle: "consectetur"
};
this.search.set(newOptions);
var match = this.search.find(this.session);
assert.notEqual(match, null, "found a match for 'consectetur'");
assert.position(match.start, 1, 0);
this.search.set(currentOptions);
this.selection.setSelectionRange(match);
assert.equal(this.session.getTextRange(match), "consectetur");
assert.equal(this.session.$selectionOccurrences.length, 2);
},
}; };
module.exports = require("asyncjs/test").testcase(Test); module.exports = require("asyncjs/test").testcase(Test);

View file

@ -20,6 +20,7 @@
* *
* Contributor(s): * Contributor(s):
* Fabian Jakobs <fabian AT ajax DOT org> * Fabian Jakobs <fabian AT ajax DOT org>
* Mihai Sucan <mihai DOT sucan AT gmail DOT com>
* *
* Alternatively, the contents of this file may be used under the terms of * Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or * either the GNU General Public License Version 2 or later (the "GPL"), or
@ -341,7 +342,52 @@ var Test = {
assert.equal(search.replace("ab12", "cd$1"), "cd12"); assert.equal(search.replace("ab12", "cd$1"), "cd12");
assert.equal(search.replace("ab12", "-$&-"), "-ab12-"); assert.equal(search.replace("ab12", "-$&-"), "-ab12-");
assert.equal(search.replace("ab12", "$$"), "$"); assert.equal(search.replace("ab12", "$$"), "$");
} },
"test: find all matches in a line" : function() {
var session = new EditSession("foo bar foo baz foobar foo");
var search = new Search().set({
needle: "foo",
wrap: true,
wholeWord: true,
});
session.getSelection().moveCursorTo(0, 4);
var ranges = search.findAll(session);
assert.equal(ranges.length, 3);
assert.position(ranges[0].start, 0, 8);
assert.position(ranges[0].end, 0, 11);
assert.position(ranges[1].start, 0, 23);
assert.position(ranges[1].end, 0, 26);
assert.position(ranges[2].start, 0, 0);
assert.position(ranges[2].end, 0, 3);
},
"test: find all matches in a line backwards" : function() {
var session = new EditSession("foo bar foo baz foobar foo");
var search = new Search().set({
needle: "foo",
wrap: true,
wholeWord: true,
backwards: true,
});
session.getSelection().moveCursorTo(0, 13);
var ranges = search.findAll(session);
assert.equal(ranges.length, 3);
assert.position(ranges[0].start, 0, 8);
assert.position(ranges[0].end, 0, 11);
assert.position(ranges[1].start, 0, 0);
assert.position(ranges[1].end, 0, 3);
assert.position(ranges[2].start, 0, 23);
assert.position(ranges[2].end, 0, 26);
},
}; };
module.exports = require("asyncjs/test").testcase(Test) module.exports = require("asyncjs/test").testcase(Test)