diff --git a/demo/demo.js b/demo/demo.js index 0437c78c..5b4b883f 100644 --- a/demo/demo.js +++ b/demo/demo.js @@ -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() { diff --git a/index.html b/index.html index d4557f3b..9fa06ea9 100644 --- a/index.html +++ b/index.html @@ -108,6 +108,10 @@ + + + + diff --git a/lib/ace/editor.js b/lib/ace/editor.js index 8f712f59..ad95be22 100644 --- a/lib/ace/editor.js +++ b/lib/ace/editor.js @@ -23,7 +23,6 @@ * Fabian Jakobs * Irakli Gozalishvili (http://jeditoolkit.com) * Julian Viereck - * Mihai Sucan * * 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; diff --git a/lib/ace/mode/text.js b/lib/ace/mode/text.js index fe1c9f17..6fc68588 100644 --- a/lib/ace/mode/text.js +++ b/lib/ace/mode/text.js @@ -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 + * Mihai Sucan * * 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;