f2cbfaebd00ef55ea3d48ff5acaeb844.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. ace.define("ace/ext/code_lens",["require","exports","module","ace/line_widgets","ace/lib/event","ace/lib/lang","ace/lib/dom","ace/editor","ace/config"], function(require, exports, module){"use strict";
  2. var LineWidgets = require("../line_widgets").LineWidgets;
  3. var event = require("../lib/event");
  4. var lang = require("../lib/lang");
  5. var dom = require("../lib/dom");
  6. function clearLensElements(renderer) {
  7. var textLayer = renderer.$textLayer;
  8. var lensElements = textLayer.$lenses;
  9. if (lensElements)
  10. lensElements.forEach(function (el) { el.remove(); });
  11. textLayer.$lenses = null;
  12. }
  13. function renderWidgets(changes, renderer) {
  14. var changed = changes & renderer.CHANGE_LINES
  15. || changes & renderer.CHANGE_FULL
  16. || changes & renderer.CHANGE_SCROLL
  17. || changes & renderer.CHANGE_TEXT;
  18. if (!changed)
  19. return;
  20. var session = renderer.session;
  21. var lineWidgets = renderer.session.lineWidgets;
  22. var textLayer = renderer.$textLayer;
  23. var lensElements = textLayer.$lenses;
  24. if (!lineWidgets) {
  25. if (lensElements)
  26. clearLensElements(renderer);
  27. return;
  28. }
  29. var textCells = renderer.$textLayer.$lines.cells;
  30. var config = renderer.layerConfig;
  31. var padding = renderer.$padding;
  32. if (!lensElements)
  33. lensElements = textLayer.$lenses = [];
  34. var index = 0;
  35. for (var i = 0; i < textCells.length; i++) {
  36. var row = textCells[i].row;
  37. var widget = lineWidgets[row];
  38. var lenses = widget && widget.lenses;
  39. if (!lenses || !lenses.length)
  40. continue;
  41. var lensContainer = lensElements[index];
  42. if (!lensContainer) {
  43. lensContainer = lensElements[index]
  44. = dom.buildDom(["div", { class: "ace_codeLens" }], renderer.container);
  45. }
  46. lensContainer.style.height = config.lineHeight + "px";
  47. index++;
  48. for (var j = 0; j < lenses.length; j++) {
  49. var el = lensContainer.childNodes[2 * j];
  50. if (!el) {
  51. if (j != 0)
  52. lensContainer.appendChild(dom.createTextNode("\xa0|\xa0"));
  53. el = dom.buildDom(["a"], lensContainer);
  54. }
  55. el.textContent = lenses[j].title;
  56. el.lensCommand = lenses[j];
  57. }
  58. while (lensContainer.childNodes.length > 2 * j - 1)
  59. lensContainer.lastChild.remove();
  60. var top = renderer.$cursorLayer.getPixelPosition({
  61. row: row,
  62. column: 0
  63. }, true).top - config.lineHeight * widget.rowsAbove - config.offset;
  64. lensContainer.style.top = top + "px";
  65. var left = renderer.gutterWidth;
  66. var indent = session.getLine(row).search(/\S|$/);
  67. if (indent == -1)
  68. indent = 0;
  69. left += indent * config.characterWidth;
  70. lensContainer.style.paddingLeft = padding + left + "px";
  71. }
  72. while (index < lensElements.length)
  73. lensElements.pop().remove();
  74. }
  75. function clearCodeLensWidgets(session) {
  76. if (!session.lineWidgets)
  77. return;
  78. var widgetManager = session.widgetManager;
  79. session.lineWidgets.forEach(function (widget) {
  80. if (widget && widget.lenses)
  81. widgetManager.removeLineWidget(widget);
  82. });
  83. }
  84. exports.setLenses = function (session, lenses) {
  85. var firstRow = Number.MAX_VALUE;
  86. clearCodeLensWidgets(session);
  87. lenses && lenses.forEach(function (lens) {
  88. var row = lens.start.row;
  89. var column = lens.start.column;
  90. var widget = session.lineWidgets && session.lineWidgets[row];
  91. if (!widget || !widget.lenses) {
  92. widget = session.widgetManager.$registerLineWidget({
  93. rowCount: 1,
  94. rowsAbove: 1,
  95. row: row,
  96. column: column,
  97. lenses: []
  98. });
  99. }
  100. widget.lenses.push(lens.command);
  101. if (row < firstRow)
  102. firstRow = row;
  103. });
  104. session._emit("changeFold", { data: { start: { row: firstRow } } });
  105. return firstRow;
  106. };
  107. function attachToEditor(editor) {
  108. editor.codeLensProviders = [];
  109. editor.renderer.on("afterRender", renderWidgets);
  110. if (!editor.$codeLensClickHandler) {
  111. editor.$codeLensClickHandler = function (e) {
  112. var command = e.target.lensCommand;
  113. if (!command)
  114. return;
  115. editor.execCommand(command.id, command.arguments);
  116. editor._emit("codeLensClick", e);
  117. };
  118. event.addListener(editor.container, "click", editor.$codeLensClickHandler, editor);
  119. }
  120. editor.$updateLenses = function () {
  121. var session = editor.session;
  122. if (!session)
  123. return;
  124. if (!session.widgetManager) {
  125. session.widgetManager = new LineWidgets(session);
  126. session.widgetManager.attach(editor);
  127. }
  128. var providersToWaitNum = editor.codeLensProviders.length;
  129. var lenses = [];
  130. editor.codeLensProviders.forEach(function (provider) {
  131. provider.provideCodeLenses(session, function (err, payload) {
  132. if (err)
  133. return;
  134. payload.forEach(function (lens) {
  135. lenses.push(lens);
  136. });
  137. providersToWaitNum--;
  138. if (providersToWaitNum == 0) {
  139. applyLenses();
  140. }
  141. });
  142. });
  143. function applyLenses() {
  144. var cursor = session.selection.cursor;
  145. var oldRow = session.documentToScreenRow(cursor);
  146. var scrollTop = session.getScrollTop();
  147. var firstRow = exports.setLenses(session, lenses);
  148. var lastDelta = session.$undoManager && session.$undoManager.$lastDelta;
  149. if (lastDelta && lastDelta.action == "remove" && lastDelta.lines.length > 1)
  150. return;
  151. var row = session.documentToScreenRow(cursor);
  152. var lineHeight = editor.renderer.layerConfig.lineHeight;
  153. var top = session.getScrollTop() + (row - oldRow) * lineHeight;
  154. if (firstRow == 0 && scrollTop < lineHeight / 4 && scrollTop > -lineHeight / 4) {
  155. top = -lineHeight;
  156. }
  157. session.setScrollTop(top);
  158. }
  159. };
  160. var updateLenses = lang.delayedCall(editor.$updateLenses);
  161. editor.$updateLensesOnInput = function () {
  162. updateLenses.delay(250);
  163. };
  164. editor.on("input", editor.$updateLensesOnInput);
  165. }
  166. function detachFromEditor(editor) {
  167. editor.off("input", editor.$updateLensesOnInput);
  168. editor.renderer.off("afterRender", renderWidgets);
  169. if (editor.$codeLensClickHandler)
  170. editor.container.removeEventListener("click", editor.$codeLensClickHandler);
  171. }
  172. exports.registerCodeLensProvider = function (editor, codeLensProvider) {
  173. editor.setOption("enableCodeLens", true);
  174. editor.codeLensProviders.push(codeLensProvider);
  175. editor.$updateLensesOnInput();
  176. };
  177. exports.clear = function (session) {
  178. exports.setLenses(session, null);
  179. };
  180. var Editor = require("../editor").Editor;
  181. require("../config").defineOptions(Editor.prototype, "editor", {
  182. enableCodeLens: {
  183. set: function (val) {
  184. if (val) {
  185. attachToEditor(this);
  186. }
  187. else {
  188. detachFromEditor(this);
  189. }
  190. }
  191. }
  192. });
  193. dom.importCssString("\n.ace_codeLens {\n position: absolute;\n color: #aaa;\n font-size: 88%;\n background: inherit;\n width: 100%;\n display: flex;\n align-items: flex-end;\n pointer-events: none;\n}\n.ace_codeLens > a {\n cursor: pointer;\n pointer-events: auto;\n}\n.ace_codeLens > a:hover {\n color: #0000ff;\n text-decoration: underline;\n}\n.ace_dark > .ace_codeLens > a:hover {\n color: #4e94ce;\n}\n", "codelense.css", false);
  194. }); (function() {
  195. ace.require(["ace/ext/code_lens"], function(m) {
  196. if (typeof module == "object" && typeof exports == "object" && module) {
  197. module.exports = m;
  198. }
  199. });
  200. })();