ext-code_lens.js 7.6 KB

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