More fixes for issue 57.

- move the selection highlight code to the Text mode.
- store $selectionOccurrences in the editor session.
- add option to toggle selection highlighting. In demo page as well.
This commit is contained in:
Mihai Sucan 2011-02-14 20:43:54 +02:00
commit be0d9a2be7
4 changed files with 89 additions and 53 deletions

View file

@ -257,6 +257,10 @@ exports.launch = function(env) {
env.editor.renderer.setShowPrintMargin(checked);
});
bindCheckbox("highlight_selected_word", function(checked) {
env.editor.setHighlightSelectedWord(checked);
});
function bindCheckbox(id, callback) {
var el = document.getElementById(id);
var onCheck = function() {

View file

@ -108,6 +108,10 @@
<label for="show_print_margin">Show Print Margin</label>
<input type="checkbox" id="show_print_margin" checked>
</td>
<td align="right">
<label for="highlight_selected_word">Highlight selected word</label>
<input type="checkbox" id="highlight_selected_word" checked>
</td>
</tr>
</table>

View file

@ -23,7 +23,6 @@
* Fabian Jakobs <fabian AT ajax DOT org>
* Irakli Gozalishvili <rfobic@gmail.com> (http://jeditoolkit.com)
* Julian Viereck <julian.viereck@gmail.com>
* Mihai Sucan <mihai.sucan@gmail.com>
*
* 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
@ -75,7 +74,6 @@ var Editor =function(renderer, session) {
this.$selectionMarker = null;
this.$highlightLineMarker = null;
this.$blockScrolling = 0;
this.$selectionOccurrences = [];
this.$search = new Search().set({
wrap: true
@ -237,55 +235,6 @@ var Editor =function(renderer, session) {
}, 10);
};
this.highlightSelection = function() {
if (this.$selectionOccurrences.length)
this.clearSelectionHighlight();
var selection = this.getSelectionRange();
if (selection.isEmpty() || selection.isMultiLine())
return;
var startOuter = selection.start.column - 1;
var endOuter = selection.end.column + 1;
var line = this.session.getLine(selection.start.row);
var lineCols = line.length - 1;
var needle = line.substring(Math.max(startOuter, 0),
Math.min(endOuter, lineCols));
// Make sure the outer characters are not part of the word.
if ((startOuter >= 0 && !/[^\w\d]/.test(needle.charAt(0))) ||
(endOuter <= lineCols && !/[^\w\d]/.test(needle.charAt(needle.length - 1))))
return;
needle = line.substring(selection.start.column, selection.end.column);
if (!/^[\w\d]+$/.test(needle))
return;
var newOptions = {
wrap: true,
wholeWord: true,
needle: needle
};
var currentOptions = this.$search.getOptions();
this.$search.set(newOptions);
var ranges = this.$search.findAll(this.session);
this.$selectionOccurrences = [];
ranges.forEach(function(range) {
if (!range.contains(selection.start.row, selection.start.column))
this.$selectionOccurrences.push(this.renderer.addMarker(range, "ace_selected_word"));
}, this);
this.$search.set(currentOptions);
};
this.clearSelectionHighlight = function() {
this.$selectionOccurrences.forEach(function(marker) {
this.renderer.removeMarker(marker);
}, this);
};
this.focus = function() {
// Safari need the timeout
// iOS and Firefox need it called immediately
@ -374,7 +323,9 @@ var Editor =function(renderer, session) {
}
this.onCursorChange(e);
this.highlightSelection();
if (this.$highlightSelectedWord)
this.mode.highlightSelection(this);
};
this.onDocumentChangeBreakpoint = function() {
@ -559,6 +510,22 @@ var Editor =function(renderer, session) {
return this.$highlightActiveLine;
};
this.$highlightSelectedWord = true;
this.setHighlightSelectedWord = function(shouldHighlight) {
if (this.$highlightSelectedWord == shouldHighlight)
return;
this.$highlightSelectedWord = shouldHighlight;
if (shouldHighlight)
this.mode.highlightSelection(this);
else
this.mode.clearSelectionHighlight(this);
};
this.getHighlightSelectedWord = function() {
return this.$highlightSelectedWord;
};
this.setShowInvisibles = function(showInvisibles) {
if (this.getShowInvisibles() == showInvisibles)
return;

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
*
* The contents of this file are subject to the Mozilla Public License Version
@ -20,6 +21,7 @@
*
* Contributor(s):
* 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
* either the GNU General Public License Version 2 or later (the "GPL"), or
@ -77,6 +79,65 @@ var Mode = function() {
return null;
};
this.highlightSelection = function(editor) {
var session = editor.session;
if (!session.$selectionOccurrences)
session.$selectionOccurrences = [];
if (session.$selectionOccurrences.length)
this.clearSelectionHighlight(editor);
var selection = editor.getSelectionRange();
if (selection.isEmpty() || selection.isMultiLine())
return;
var startOuter = selection.start.column - 1;
var endOuter = selection.end.column + 1;
var line = session.getLine(selection.start.row);
var lineCols = line.length - 1;
var needle = line.substring(Math.max(startOuter, 0),
Math.min(endOuter, lineCols));
// Make sure the outer characters are not part of the word.
if ((startOuter >= 0 && !/[^\w\d]/.test(needle.charAt(0))) ||
(endOuter <= lineCols && !/[^\w\d]/.test(needle.charAt(needle.length - 1))))
return;
needle = line.substring(selection.start.column, selection.end.column);
if (!/^[\w\d]+$/.test(needle))
return;
var newOptions = {
wrap: true,
wholeWord: true,
needle: needle
};
var currentOptions = editor.$search.getOptions();
editor.$search.set(newOptions);
var ranges = editor.$search.findAll(session);
session.$selectionOccurrences = [];
ranges.forEach(function(range) {
if (!range.contains(selection.start.row, selection.start.column)) {
var marker = editor.renderer.addMarker(range,
"ace_selected_word");
session.$selectionOccurrences.push(marker);
}
});
editor.$search.set(currentOptions);
};
this.clearSelectionHighlight = function(editor) {
if (!editor.session.$selectionOccurrences)
return;
editor.session.$selectionOccurrences.forEach(function(marker) {
editor.renderer.removeMarker(marker);
});
};
}).call(Mode.prototype);
exports.Mode = Mode;