occur.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Distributed under the BSD license:
  3. *
  4. * Copyright (c) 2010, 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. define(function(ace_require, exports, module) {
  31. "use strict";
  32. var oop = ace_require("./lib/oop");
  33. var Range = ace_require("./range").Range;
  34. var Search = ace_require("./search").Search;
  35. var EditSession = ace_require("./edit_session").EditSession;
  36. var SearchHighlight = ace_require("./search_highlight").SearchHighlight;
  37. /**
  38. * @class Occur
  39. *
  40. * Finds all lines matching a search term in the current [[Document
  41. * `Document`]] and displays them instead of the original `Document`. Keeps
  42. * track of the mapping between the occur doc and the original doc.
  43. *
  44. **/
  45. /**
  46. * Creates a new `Occur` object.
  47. *
  48. * @constructor
  49. **/
  50. function Occur() {}
  51. oop.inherits(Occur, Search);
  52. (function() {
  53. /**
  54. * Enables occur mode. expects that `options.needle` is a search term.
  55. * This search term is used to filter out all the lines that include it
  56. * and these are then used as the content of a new [[Document
  57. * `Document`]]. The current cursor position of editor will be translated
  58. * so that the cursor is on the matching row/column as it was before.
  59. * @param {Editor} editor
  60. * @param {Object} options options.needle should be a String
  61. * @return {Boolean} Whether occur activation was successful
  62. *
  63. **/
  64. this.enter = function(editor, options) {
  65. if (!options.needle) return false;
  66. var pos = editor.getCursorPosition();
  67. this.displayOccurContent(editor, options);
  68. var translatedPos = this.originalToOccurPosition(editor.session, pos);
  69. editor.moveCursorToPosition(translatedPos);
  70. return true;
  71. };
  72. /**
  73. * Disables occur mode. Resets the [[Sessions `EditSession`]] [[Document
  74. * `Document`]] back to the original doc. If options.translatePosition is
  75. * truthy also maps the [[Editors `Editor`]] cursor position accordingly.
  76. * @param {Editor} editor
  77. * @param {Object} options options.translatePosition
  78. * @return {Boolean} Whether occur deactivation was successful
  79. *
  80. **/
  81. this.exit = function(editor, options) {
  82. var pos = options.translatePosition && editor.getCursorPosition();
  83. var translatedPos = pos && this.occurToOriginalPosition(editor.session, pos);
  84. this.displayOriginalContent(editor);
  85. if (translatedPos)
  86. editor.moveCursorToPosition(translatedPos);
  87. return true;
  88. };
  89. this.highlight = function(sess, regexp) {
  90. var hl = sess.$occurHighlight = sess.$occurHighlight || sess.addDynamicMarker(
  91. new SearchHighlight(null, "ace_occur-highlight", "text"));
  92. hl.setRegexp(regexp);
  93. sess._emit("changeBackMarker"); // force highlight layer redraw
  94. };
  95. this.displayOccurContent = function(editor, options) {
  96. // this.setSession(session || new EditSession(""))
  97. this.$originalSession = editor.session;
  98. var found = this.matchingLines(editor.session, options);
  99. var lines = found.map(function(foundLine) { return foundLine.content; });
  100. var occurSession = new EditSession(lines.join('\n'));
  101. occurSession.$occur = this;
  102. occurSession.$occurMatchingLines = found;
  103. editor.setSession(occurSession);
  104. this.$useEmacsStyleLineStart = this.$originalSession.$useEmacsStyleLineStart;
  105. occurSession.$useEmacsStyleLineStart = this.$useEmacsStyleLineStart;
  106. this.highlight(occurSession, options.re);
  107. occurSession._emit('changeBackMarker');
  108. };
  109. this.displayOriginalContent = function(editor) {
  110. editor.setSession(this.$originalSession);
  111. this.$originalSession.$useEmacsStyleLineStart = this.$useEmacsStyleLineStart;
  112. };
  113. /**
  114. * Translates the position from the original document to the occur lines in
  115. * the document or the beginning if the doc {row: 0, column: 0} if not
  116. * found.
  117. * @param {EditSession} session The occur session
  118. * @param {Object} pos The position in the original document
  119. * @return {Object} position in occur doc
  120. **/
  121. this.originalToOccurPosition = function(session, pos) {
  122. var lines = session.$occurMatchingLines;
  123. var nullPos = {row: 0, column: 0};
  124. if (!lines) return nullPos;
  125. for (var i = 0; i < lines.length; i++) {
  126. if (lines[i].row === pos.row)
  127. return {row: i, column: pos.column};
  128. }
  129. return nullPos;
  130. };
  131. /**
  132. * Translates the position from the occur document to the original document
  133. * or `pos` if not found.
  134. * @param {EditSession} session The occur session
  135. * @param {Object} pos The position in the occur session document
  136. * @return {Object} position
  137. **/
  138. this.occurToOriginalPosition = function(session, pos) {
  139. var lines = session.$occurMatchingLines;
  140. if (!lines || !lines[pos.row])
  141. return pos;
  142. return {row: lines[pos.row].row, column: pos.column};
  143. };
  144. this.matchingLines = function(session, options) {
  145. options = oop.mixin({}, options);
  146. if (!session || !options.needle) return [];
  147. var search = new Search();
  148. search.set(options);
  149. return search.findAll(session).reduce(function(lines, range) {
  150. var row = range.start.row;
  151. var last = lines[lines.length-1];
  152. return last && last.row === row ?
  153. lines :
  154. lines.concat({row: row, content: session.getLine(row)});
  155. }, []);
  156. };
  157. }).call(Occur.prototype);
  158. var dom = ace_require('./lib/dom');
  159. dom.importCssString(".ace_occur-highlight {\n\
  160. border-radius: 4px;\n\
  161. background-color: rgba(87, 255, 8, 0.25);\n\
  162. position: absolute;\n\
  163. z-index: 4;\n\
  164. box-sizing: border-box;\n\
  165. box-shadow: 0 0 4px rgb(91, 255, 50);\n\
  166. }\n\
  167. .ace_dark .ace_occur-highlight {\n\
  168. background-color: rgb(80, 140, 85);\n\
  169. box-shadow: 0 0 4px rgb(60, 120, 70);\n\
  170. }\n", "incremental-occur-highlighting");
  171. exports.Occur = Occur;
  172. });