dev_util.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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(require, exports, module) {
  31. var dom = require("ace/lib/dom");
  32. var Range = require("ace/range").Range;
  33. function warn() {
  34. var s = (new Error()).stack || "";
  35. s = s.split("\n");
  36. if (s[1] == "Error") s.shift(); // remove error description on chrome
  37. s.shift(); // remove warn
  38. s.shift(); // remove the getter
  39. s = s.join("\n");
  40. // allow easy access to ace in console, but not in ace code
  41. if (!/at Object.InjectedScript.|@debugger eval|snippets:\/{3}|<anonymous>:\d+:\d+/.test(s)) {
  42. console.error("trying to access to global variable");
  43. }
  44. }
  45. function def(o, key, get) {
  46. try {
  47. Object.defineProperty(o, key, {
  48. configurable: true,
  49. get: get,
  50. set: function(val) {
  51. delete o[key];
  52. o[key] = val;
  53. }
  54. });
  55. } catch(e) {
  56. console.error(e);
  57. }
  58. }
  59. def(window, "ace", function(){ warn(); return window.env.editor });
  60. def(window, "editor", function(){ warn(); return window.env.editor });
  61. def(window, "session", function(){ warn(); return window.env.editor.session });
  62. def(window, "split", function(){ warn(); return window.env.split });
  63. def(window, "devUtil", function(){ warn(); return exports });
  64. exports.showTextArea = function(argument) {
  65. dom.importCssString("\
  66. .ace_text-input {\
  67. position: absolute;\
  68. z-index: 10!important;\
  69. width: 6em!important;\
  70. height: 1em;\
  71. opacity: 1!important;\
  72. background: rgba(0, 92, 255, 0.11);\
  73. border: none;\
  74. font: inherit;\
  75. padding: 0 1px;\
  76. margin: 0 -1px;\
  77. text-indent: 0em;\
  78. }\
  79. ");
  80. };
  81. exports.addGlobals = function() {
  82. window.oop = require("ace/lib/oop");
  83. window.dom = require("ace/lib/dom");
  84. window.Range = require("ace/range").Range;
  85. window.Editor = require("ace/editor").Editor;
  86. window.assert = require("ace/test/asyncjs/assert");
  87. window.asyncjs = require("ace/test/asyncjs/async");
  88. window.UndoManager = require("ace/undomanager").UndoManager;
  89. window.EditSession = require("ace/edit_session").EditSession;
  90. window.MockRenderer = require("ace/test/mockrenderer").MockRenderer;
  91. window.EventEmitter = require("ace/lib/event_emitter").EventEmitter;
  92. window.getSelection = getSelection;
  93. window.setSelection = setSelection;
  94. window.testSelection = testSelection;
  95. };
  96. function getSelection(editor) {
  97. var data = editor.multiSelect.toJSON();
  98. if (!data.length) data = [data];
  99. data = data.map(function(x) {
  100. var a, c;
  101. if (x.isBackwards) {
  102. a = x.end;
  103. c = x.start;
  104. } else {
  105. c = x.end;
  106. a = x.start;
  107. }
  108. return Range.comparePoints(a, c)
  109. ? [a.row, a.column, c.row, c.column]
  110. : [a.row, a.column];
  111. });
  112. return data.length > 1 ? data : data[0];
  113. }
  114. function setSelection(editor, data) {
  115. if (typeof data[0] == "number")
  116. data = [data];
  117. editor.selection.fromJSON(data.map(function(x) {
  118. var start = {row: x[0], column: x[1]};
  119. var end = x.length == 2 ? start : {row: x[2], column: x[3]};
  120. var isBackwards = Range.comparePoints(start, end) > 0;
  121. return isBackwards ? {
  122. start: end,
  123. end: start,
  124. isBackwards: true
  125. } : {
  126. start: start,
  127. end: end,
  128. isBackwards: true
  129. };
  130. }));
  131. }
  132. function testSelection(editor, data) {
  133. assert.equal(getSelection(editor) + "", data + "");
  134. }
  135. exports.recordTestCase = function() {
  136. exports.addGlobals();
  137. var editor = window.editor;
  138. var testcase = window.testcase = [];
  139. var assert;
  140. testcase.push({
  141. type: "setValue",
  142. data: editor.getValue()
  143. }, {
  144. type: "setSelection",
  145. data: getSelection(editor)
  146. });
  147. editor.commands.on("afterExec", function(e) {
  148. testcase.push({
  149. type: "exec",
  150. data: e
  151. });
  152. testcase.push({
  153. type: "value",
  154. data: editor.getValue()
  155. });
  156. testcase.push({
  157. type: "selection",
  158. data: getSelection(editor)
  159. });
  160. });
  161. editor.on("mouseup", function() {
  162. testcase.push({
  163. type: "setSelection",
  164. data: getSelection(editor)
  165. });
  166. });
  167. testcase.toString = function() {
  168. var lastValue = "";
  169. // var lastSelection = ""
  170. var str = this.map(function(x) {
  171. var data = x.data;
  172. switch (x.type) {
  173. case "exec":
  174. return 'editor.execCommand("'
  175. + data.command.name
  176. + (data.args ? '", ' + JSON.stringify(data.args) : '"')
  177. + ')';
  178. case "setSelection":
  179. return 'setSelection(editor, ' + JSON.stringify(data) + ')';
  180. case "setValue":
  181. if (lastValue != data) {
  182. lastValue = data;
  183. return 'editor.setValue(' + JSON.stringify(data) + ', -1)';
  184. }
  185. return;
  186. case "selection":
  187. return 'testSelection(editor, ' + JSON.stringify(data) + ')';
  188. case "value":
  189. if (lastValue != data) {
  190. lastValue = data;
  191. return 'assert.equal('
  192. + 'editor.getValue(),'
  193. + JSON.stringify(data)
  194. + ')';
  195. }
  196. return;
  197. }
  198. }).filter(Boolean).join("\n");
  199. return getSelection + "\n"
  200. + testSelection + "\n"
  201. + setSelection + "\n"
  202. + "\n" + str + "\n";
  203. };
  204. };
  205. });