ee297be348e4487e1384616451eefed1.js 98 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516
  1. ace.define("ace/ext/menu_tools/get_editor_keyboard_shortcuts",["require","exports","module","ace/lib/keys"], function(require, exports, module){/*jslint indent: 4, maxerr: 50, white: true, browser: true, vars: true*/
  2. "use strict";
  3. var keys = require("../../lib/keys");
  4. module.exports.getEditorKeybordShortcuts = function (editor) {
  5. var KEY_MODS = keys.KEY_MODS;
  6. var keybindings = [];
  7. var commandMap = {};
  8. editor.keyBinding.$handlers.forEach(function (handler) {
  9. var ckb = handler.commandKeyBinding;
  10. for (var i in ckb) {
  11. var key = i.replace(/(^|-)\w/g, function (x) { return x.toUpperCase(); });
  12. var commands = ckb[i];
  13. if (!Array.isArray(commands))
  14. commands = [commands];
  15. commands.forEach(function (command) {
  16. if (typeof command != "string")
  17. command = command.name;
  18. if (commandMap[command]) {
  19. commandMap[command].key += "|" + key;
  20. }
  21. else {
  22. commandMap[command] = { key: key, command: command };
  23. keybindings.push(commandMap[command]);
  24. }
  25. });
  26. }
  27. });
  28. return keybindings;
  29. };
  30. });
  31. ace.define("ace/autocomplete/popup",["require","exports","module","ace/virtual_renderer","ace/editor","ace/range","ace/lib/event","ace/lib/lang","ace/lib/dom"], function(require, exports, module){"use strict";
  32. var Renderer = require("../virtual_renderer").VirtualRenderer;
  33. var Editor = require("../editor").Editor;
  34. var Range = require("../range").Range;
  35. var event = require("../lib/event");
  36. var lang = require("../lib/lang");
  37. var dom = require("../lib/dom");
  38. var $singleLineEditor = function (el) {
  39. var renderer = new Renderer(el);
  40. renderer.$maxLines = 4;
  41. var editor = new Editor(renderer);
  42. editor.setHighlightActiveLine(false);
  43. editor.setShowPrintMargin(false);
  44. editor.renderer.setShowGutter(false);
  45. editor.renderer.setHighlightGutterLine(false);
  46. editor.$mouseHandler.$focusTimeout = 0;
  47. editor.$highlightTagPending = true;
  48. return editor;
  49. };
  50. var AcePopup = function (parentNode) {
  51. var el = dom.createElement("div");
  52. var popup = new $singleLineEditor(el);
  53. if (parentNode)
  54. parentNode.appendChild(el);
  55. el.style.display = "none";
  56. popup.renderer.content.style.cursor = "default";
  57. popup.renderer.setStyle("ace_autocomplete");
  58. popup.setOption("displayIndentGuides", false);
  59. popup.setOption("dragDelay", 150);
  60. var noop = function () { };
  61. popup.focus = noop;
  62. popup.$isFocused = true;
  63. popup.renderer.$cursorLayer.restartTimer = noop;
  64. popup.renderer.$cursorLayer.element.style.opacity = 0;
  65. popup.renderer.$maxLines = 8;
  66. popup.renderer.$keepTextAreaAtCursor = false;
  67. popup.setHighlightActiveLine(false);
  68. popup.session.highlight("");
  69. popup.session.$searchHighlight.clazz = "ace_highlight-marker";
  70. popup.on("mousedown", function (e) {
  71. var pos = e.getDocumentPosition();
  72. popup.selection.moveToPosition(pos);
  73. selectionMarker.start.row = selectionMarker.end.row = pos.row;
  74. e.stop();
  75. });
  76. var lastMouseEvent;
  77. var hoverMarker = new Range(-1, 0, -1, Infinity);
  78. var selectionMarker = new Range(-1, 0, -1, Infinity);
  79. selectionMarker.id = popup.session.addMarker(selectionMarker, "ace_active-line", "fullLine");
  80. popup.setSelectOnHover = function (val) {
  81. if (!val) {
  82. hoverMarker.id = popup.session.addMarker(hoverMarker, "ace_line-hover", "fullLine");
  83. }
  84. else if (hoverMarker.id) {
  85. popup.session.removeMarker(hoverMarker.id);
  86. hoverMarker.id = null;
  87. }
  88. };
  89. popup.setSelectOnHover(false);
  90. popup.on("mousemove", function (e) {
  91. if (!lastMouseEvent) {
  92. lastMouseEvent = e;
  93. return;
  94. }
  95. if (lastMouseEvent.x == e.x && lastMouseEvent.y == e.y) {
  96. return;
  97. }
  98. lastMouseEvent = e;
  99. lastMouseEvent.scrollTop = popup.renderer.scrollTop;
  100. var row = lastMouseEvent.getDocumentPosition().row;
  101. if (hoverMarker.start.row != row) {
  102. if (!hoverMarker.id)
  103. popup.setRow(row);
  104. setHoverMarker(row);
  105. }
  106. });
  107. popup.renderer.on("beforeRender", function () {
  108. if (lastMouseEvent && hoverMarker.start.row != -1) {
  109. lastMouseEvent.$pos = null;
  110. var row = lastMouseEvent.getDocumentPosition().row;
  111. if (!hoverMarker.id)
  112. popup.setRow(row);
  113. setHoverMarker(row, true);
  114. }
  115. });
  116. popup.renderer.on("afterRender", function () {
  117. var row = popup.getRow();
  118. var t = popup.renderer.$textLayer;
  119. var selected = t.element.childNodes[row - t.config.firstRow];
  120. if (selected !== t.selectedNode && t.selectedNode)
  121. dom.removeCssClass(t.selectedNode, "ace_selected");
  122. t.selectedNode = selected;
  123. if (selected)
  124. dom.addCssClass(selected, "ace_selected");
  125. });
  126. var hideHoverMarker = function () { setHoverMarker(-1); };
  127. var setHoverMarker = function (row, suppressRedraw) {
  128. if (row !== hoverMarker.start.row) {
  129. hoverMarker.start.row = hoverMarker.end.row = row;
  130. if (!suppressRedraw)
  131. popup.session._emit("changeBackMarker");
  132. popup._emit("changeHoverMarker");
  133. }
  134. };
  135. popup.getHoveredRow = function () {
  136. return hoverMarker.start.row;
  137. };
  138. event.addListener(popup.container, "mouseout", hideHoverMarker);
  139. popup.on("hide", hideHoverMarker);
  140. popup.on("changeSelection", hideHoverMarker);
  141. popup.session.doc.getLength = function () {
  142. return popup.data.length;
  143. };
  144. popup.session.doc.getLine = function (i) {
  145. var data = popup.data[i];
  146. if (typeof data == "string")
  147. return data;
  148. return (data && data.value) || "";
  149. };
  150. var bgTokenizer = popup.session.bgTokenizer;
  151. bgTokenizer.$tokenizeRow = function (row) {
  152. var data = popup.data[row];
  153. var tokens = [];
  154. if (!data)
  155. return tokens;
  156. if (typeof data == "string")
  157. data = { value: data };
  158. var caption = data.caption || data.value || data.name;
  159. function addToken(value, className) {
  160. value && tokens.push({
  161. type: (data.className || "") + (className || ""),
  162. value: value
  163. });
  164. }
  165. var lower = caption.toLowerCase();
  166. var filterText = (popup.filterText || "").toLowerCase();
  167. var lastIndex = 0;
  168. var lastI = 0;
  169. for (var i = 0; i <= filterText.length; i++) {
  170. if (i != lastI && (data.matchMask & (1 << i) || i == filterText.length)) {
  171. var sub = filterText.slice(lastI, i);
  172. lastI = i;
  173. var index = lower.indexOf(sub, lastIndex);
  174. if (index == -1)
  175. continue;
  176. addToken(caption.slice(lastIndex, index), "");
  177. lastIndex = index + sub.length;
  178. addToken(caption.slice(index, lastIndex), "completion-highlight");
  179. }
  180. }
  181. addToken(caption.slice(lastIndex, caption.length), "");
  182. if (data.meta)
  183. tokens.push({ type: "completion-meta", value: data.meta });
  184. if (data.message)
  185. tokens.push({ type: "completion-message", value: data.message });
  186. return tokens;
  187. };
  188. bgTokenizer.$updateOnChange = noop;
  189. bgTokenizer.start = noop;
  190. popup.session.$computeWidth = function () {
  191. return this.screenWidth = 0;
  192. };
  193. popup.isOpen = false;
  194. popup.isTopdown = false;
  195. popup.autoSelect = true;
  196. popup.filterText = "";
  197. popup.data = [];
  198. popup.setData = function (list, filterText) {
  199. popup.filterText = filterText || "";
  200. popup.setValue(lang.stringRepeat("\n", list.length), -1);
  201. popup.data = list || [];
  202. popup.setRow(0);
  203. };
  204. popup.getData = function (row) {
  205. return popup.data[row];
  206. };
  207. popup.getRow = function () {
  208. return selectionMarker.start.row;
  209. };
  210. popup.setRow = function (line) {
  211. line = Math.max(this.autoSelect ? 0 : -1, Math.min(this.data.length, line));
  212. if (selectionMarker.start.row != line) {
  213. popup.selection.clearSelection();
  214. selectionMarker.start.row = selectionMarker.end.row = line || 0;
  215. popup.session._emit("changeBackMarker");
  216. popup.moveCursorTo(line || 0, 0);
  217. if (popup.isOpen)
  218. popup._signal("select");
  219. }
  220. };
  221. popup.on("changeSelection", function () {
  222. if (popup.isOpen)
  223. popup.setRow(popup.selection.lead.row);
  224. popup.renderer.scrollCursorIntoView();
  225. });
  226. popup.hide = function () {
  227. this.container.style.display = "none";
  228. this._signal("hide");
  229. popup.isOpen = false;
  230. };
  231. popup.show = function (pos, lineHeight, topdownOnly) {
  232. var el = this.container;
  233. var screenHeight = window.innerHeight;
  234. var screenWidth = window.innerWidth;
  235. var renderer = this.renderer;
  236. var maxH = renderer.$maxLines * lineHeight * 1.4;
  237. var top = pos.top + this.$borderSize;
  238. var allowTopdown = top > screenHeight / 2 && !topdownOnly;
  239. if (allowTopdown && top + lineHeight + maxH > screenHeight) {
  240. renderer.$maxPixelHeight = top - 2 * this.$borderSize;
  241. el.style.top = "";
  242. el.style.bottom = screenHeight - top + "px";
  243. popup.isTopdown = false;
  244. }
  245. else {
  246. top += lineHeight;
  247. renderer.$maxPixelHeight = screenHeight - top - 0.2 * lineHeight;
  248. el.style.top = top + "px";
  249. el.style.bottom = "";
  250. popup.isTopdown = true;
  251. }
  252. el.style.display = "";
  253. var left = pos.left;
  254. if (left + el.offsetWidth > screenWidth)
  255. left = screenWidth - el.offsetWidth;
  256. el.style.left = left + "px";
  257. this._signal("show");
  258. lastMouseEvent = null;
  259. popup.isOpen = true;
  260. };
  261. popup.goTo = function (where) {
  262. var row = this.getRow();
  263. var max = this.session.getLength() - 1;
  264. switch (where) {
  265. case "up":
  266. row = row <= 0 ? max : row - 1;
  267. break;
  268. case "down":
  269. row = row >= max ? -1 : row + 1;
  270. break;
  271. case "start":
  272. row = 0;
  273. break;
  274. case "end":
  275. row = max;
  276. break;
  277. }
  278. this.setRow(row);
  279. };
  280. popup.getTextLeftOffset = function () {
  281. return this.$borderSize + this.renderer.$padding + this.$imageSize;
  282. };
  283. popup.$imageSize = 0;
  284. popup.$borderSize = 1;
  285. return popup;
  286. };
  287. dom.importCssString("\n.ace_editor.ace_autocomplete .ace_marker-layer .ace_active-line {\n background-color: #CAD6FA;\n z-index: 1;\n}\n.ace_dark.ace_editor.ace_autocomplete .ace_marker-layer .ace_active-line {\n background-color: #3a674e;\n}\n.ace_editor.ace_autocomplete .ace_line-hover {\n border: 1px solid #abbffe;\n margin-top: -1px;\n background: rgba(233,233,253,0.4);\n position: absolute;\n z-index: 2;\n}\n.ace_dark.ace_editor.ace_autocomplete .ace_line-hover {\n border: 1px solid rgba(109, 150, 13, 0.8);\n background: rgba(58, 103, 78, 0.62);\n}\n.ace_completion-meta {\n opacity: 0.5;\n margin: 0.9em;\n}\n.ace_completion-message {\n color: blue;\n}\n.ace_editor.ace_autocomplete .ace_completion-highlight{\n color: #2d69c7;\n}\n.ace_dark.ace_editor.ace_autocomplete .ace_completion-highlight{\n color: #93ca12;\n}\n.ace_editor.ace_autocomplete {\n width: 300px;\n z-index: 200000;\n border: 1px lightgray solid;\n position: fixed;\n box-shadow: 2px 3px 5px rgba(0,0,0,.2);\n line-height: 1.4;\n background: #fefefe;\n color: #111;\n}\n.ace_dark.ace_editor.ace_autocomplete {\n border: 1px #484747 solid;\n box-shadow: 2px 3px 5px rgba(0, 0, 0, 0.51);\n line-height: 1.4;\n background: #25282c;\n color: #c1c1c1;\n}", "autocompletion.css", false);
  288. exports.AcePopup = AcePopup;
  289. exports.$singleLineEditor = $singleLineEditor;
  290. });
  291. ace.define("ace/autocomplete/util",["require","exports","module"], function(require, exports, module){"use strict";
  292. exports.parForEach = function (array, fn, callback) {
  293. var completed = 0;
  294. var arLength = array.length;
  295. if (arLength === 0)
  296. callback();
  297. for (var i = 0; i < arLength; i++) {
  298. fn(array[i], function (result, err) {
  299. completed++;
  300. if (completed === arLength)
  301. callback(result, err);
  302. });
  303. }
  304. };
  305. var ID_REGEX = /[a-zA-Z_0-9\$\-\u00A2-\u2000\u2070-\uFFFF]/;
  306. exports.retrievePrecedingIdentifier = function (text, pos, regex) {
  307. regex = regex || ID_REGEX;
  308. var buf = [];
  309. for (var i = pos - 1; i >= 0; i--) {
  310. if (regex.test(text[i]))
  311. buf.push(text[i]);
  312. else
  313. break;
  314. }
  315. return buf.reverse().join("");
  316. };
  317. exports.retrieveFollowingIdentifier = function (text, pos, regex) {
  318. regex = regex || ID_REGEX;
  319. var buf = [];
  320. for (var i = pos; i < text.length; i++) {
  321. if (regex.test(text[i]))
  322. buf.push(text[i]);
  323. else
  324. break;
  325. }
  326. return buf;
  327. };
  328. exports.getCompletionPrefix = function (editor) {
  329. var pos = editor.getCursorPosition();
  330. var line = editor.session.getLine(pos.row);
  331. var prefix;
  332. editor.completers.forEach(function (completer) {
  333. if (completer.identifierRegexps) {
  334. completer.identifierRegexps.forEach(function (identifierRegex) {
  335. if (!prefix && identifierRegex)
  336. prefix = this.retrievePrecedingIdentifier(line, pos.column, identifierRegex);
  337. }.bind(this));
  338. }
  339. }.bind(this));
  340. return prefix || this.retrievePrecedingIdentifier(line, pos.column);
  341. };
  342. });
  343. ace.define("ace/snippets",["require","exports","module","ace/lib/dom","ace/lib/oop","ace/lib/event_emitter","ace/lib/lang","ace/range","ace/range_list","ace/keyboard/hash_handler","ace/tokenizer","ace/clipboard","ace/editor"], function(require, exports, module){"use strict";
  344. var dom = require("./lib/dom");
  345. var oop = require("./lib/oop");
  346. var EventEmitter = require("./lib/event_emitter").EventEmitter;
  347. var lang = require("./lib/lang");
  348. var Range = require("./range").Range;
  349. var RangeList = require("./range_list").RangeList;
  350. var HashHandler = require("./keyboard/hash_handler").HashHandler;
  351. var Tokenizer = require("./tokenizer").Tokenizer;
  352. var clipboard = require("./clipboard");
  353. var VARIABLES = {
  354. CURRENT_WORD: function (editor) {
  355. return editor.session.getTextRange(editor.session.getWordRange());
  356. },
  357. SELECTION: function (editor, name, indentation) {
  358. var text = editor.session.getTextRange();
  359. if (indentation)
  360. return text.replace(/\n\r?([ \t]*\S)/g, "\n" + indentation + "$1");
  361. return text;
  362. },
  363. CURRENT_LINE: function (editor) {
  364. return editor.session.getLine(editor.getCursorPosition().row);
  365. },
  366. PREV_LINE: function (editor) {
  367. return editor.session.getLine(editor.getCursorPosition().row - 1);
  368. },
  369. LINE_INDEX: function (editor) {
  370. return editor.getCursorPosition().row;
  371. },
  372. LINE_NUMBER: function (editor) {
  373. return editor.getCursorPosition().row + 1;
  374. },
  375. SOFT_TABS: function (editor) {
  376. return editor.session.getUseSoftTabs() ? "YES" : "NO";
  377. },
  378. TAB_SIZE: function (editor) {
  379. return editor.session.getTabSize();
  380. },
  381. CLIPBOARD: function (editor) {
  382. return clipboard.getText && clipboard.getText();
  383. },
  384. FILENAME: function (editor) {
  385. return /[^/\\]*$/.exec(this.FILEPATH(editor))[0];
  386. },
  387. FILENAME_BASE: function (editor) {
  388. return /[^/\\]*$/.exec(this.FILEPATH(editor))[0].replace(/\.[^.]*$/, "");
  389. },
  390. DIRECTORY: function (editor) {
  391. return this.FILEPATH(editor).replace(/[^/\\]*$/, "");
  392. },
  393. FILEPATH: function (editor) { return "/not implemented.txt"; },
  394. WORKSPACE_NAME: function () { return "Unknown"; },
  395. FULLNAME: function () { return "Unknown"; },
  396. BLOCK_COMMENT_START: function (editor) {
  397. var mode = editor.session.$mode || {};
  398. return mode.blockComment && mode.blockComment.start || "";
  399. },
  400. BLOCK_COMMENT_END: function (editor) {
  401. var mode = editor.session.$mode || {};
  402. return mode.blockComment && mode.blockComment.end || "";
  403. },
  404. LINE_COMMENT: function (editor) {
  405. var mode = editor.session.$mode || {};
  406. return mode.lineCommentStart || "";
  407. },
  408. CURRENT_YEAR: date.bind(null, { year: "numeric" }),
  409. CURRENT_YEAR_SHORT: date.bind(null, { year: "2-digit" }),
  410. CURRENT_MONTH: date.bind(null, { month: "numeric" }),
  411. CURRENT_MONTH_NAME: date.bind(null, { month: "long" }),
  412. CURRENT_MONTH_NAME_SHORT: date.bind(null, { month: "short" }),
  413. CURRENT_DATE: date.bind(null, { day: "2-digit" }),
  414. CURRENT_DAY_NAME: date.bind(null, { weekday: "long" }),
  415. CURRENT_DAY_NAME_SHORT: date.bind(null, { weekday: "short" }),
  416. CURRENT_HOUR: date.bind(null, { hour: "2-digit", hour12: false }),
  417. CURRENT_MINUTE: date.bind(null, { minute: "2-digit" }),
  418. CURRENT_SECOND: date.bind(null, { second: "2-digit" })
  419. };
  420. VARIABLES.SELECTED_TEXT = VARIABLES.SELECTION;
  421. function date(dateFormat) {
  422. var str = new Date().toLocaleString("en-us", dateFormat);
  423. return str.length == 1 ? "0" + str : str;
  424. }
  425. var SnippetManager = function () {
  426. this.snippetMap = {};
  427. this.snippetNameMap = {};
  428. };
  429. (function () {
  430. oop.implement(this, EventEmitter);
  431. this.getTokenizer = function () {
  432. return SnippetManager.$tokenizer || this.createTokenizer();
  433. };
  434. this.createTokenizer = function () {
  435. function TabstopToken(str) {
  436. str = str.substr(1);
  437. if (/^\d+$/.test(str))
  438. return [{ tabstopId: parseInt(str, 10) }];
  439. return [{ text: str }];
  440. }
  441. function escape(ch) {
  442. return "(?:[^\\\\" + ch + "]|\\\\.)";
  443. }
  444. var formatMatcher = {
  445. regex: "/(" + escape("/") + "+)/",
  446. onMatch: function (val, state, stack) {
  447. var ts = stack[0];
  448. ts.fmtString = true;
  449. ts.guard = val.slice(1, -1);
  450. ts.flag = "";
  451. return "";
  452. },
  453. next: "formatString"
  454. };
  455. SnippetManager.$tokenizer = new Tokenizer({
  456. start: [
  457. { regex: /\\./, onMatch: function (val, state, stack) {
  458. var ch = val[1];
  459. if (ch == "}" && stack.length) {
  460. val = ch;
  461. }
  462. else if ("`$\\".indexOf(ch) != -1) {
  463. val = ch;
  464. }
  465. return [val];
  466. } },
  467. { regex: /}/, onMatch: function (val, state, stack) {
  468. return [stack.length ? stack.shift() : val];
  469. } },
  470. { regex: /\$(?:\d+|\w+)/, onMatch: TabstopToken },
  471. { regex: /\$\{[\dA-Z_a-z]+/, onMatch: function (str, state, stack) {
  472. var t = TabstopToken(str.substr(1));
  473. stack.unshift(t[0]);
  474. return t;
  475. }, next: "snippetVar" },
  476. { regex: /\n/, token: "newline", merge: false }
  477. ],
  478. snippetVar: [
  479. { regex: "\\|" + escape("\\|") + "*\\|", onMatch: function (val, state, stack) {
  480. var choices = val.slice(1, -1).replace(/\\[,|\\]|,/g, function (operator) {
  481. return operator.length == 2 ? operator[1] : "\x00";
  482. }).split("\x00").map(function (value) {
  483. return { value: value };
  484. });
  485. stack[0].choices = choices;
  486. return [choices[0]];
  487. }, next: "start" },
  488. formatMatcher,
  489. { regex: "([^:}\\\\]|\\\\.)*:?", token: "", next: "start" }
  490. ],
  491. formatString: [
  492. { regex: /:/, onMatch: function (val, state, stack) {
  493. if (stack.length && stack[0].expectElse) {
  494. stack[0].expectElse = false;
  495. stack[0].ifEnd = { elseEnd: stack[0] };
  496. return [stack[0].ifEnd];
  497. }
  498. return ":";
  499. } },
  500. { regex: /\\./, onMatch: function (val, state, stack) {
  501. var ch = val[1];
  502. if (ch == "}" && stack.length)
  503. val = ch;
  504. else if ("`$\\".indexOf(ch) != -1)
  505. val = ch;
  506. else if (ch == "n")
  507. val = "\n";
  508. else if (ch == "t")
  509. val = "\t";
  510. else if ("ulULE".indexOf(ch) != -1)
  511. val = { changeCase: ch, local: ch > "a" };
  512. return [val];
  513. } },
  514. { regex: "/\\w*}", onMatch: function (val, state, stack) {
  515. var next = stack.shift();
  516. if (next)
  517. next.flag = val.slice(1, -1);
  518. this.next = next && next.tabstopId ? "start" : "";
  519. return [next || val];
  520. }, next: "start" },
  521. { regex: /\$(?:\d+|\w+)/, onMatch: function (val, state, stack) {
  522. return [{ text: val.slice(1) }];
  523. } },
  524. { regex: /\${\w+/, onMatch: function (val, state, stack) {
  525. var token = { text: val.slice(2) };
  526. stack.unshift(token);
  527. return [token];
  528. }, next: "formatStringVar" },
  529. { regex: /\n/, token: "newline", merge: false },
  530. { regex: /}/, onMatch: function (val, state, stack) {
  531. var next = stack.shift();
  532. this.next = next && next.tabstopId ? "start" : "";
  533. return [next || val];
  534. }, next: "start" }
  535. ],
  536. formatStringVar: [
  537. { regex: /:\/\w+}/, onMatch: function (val, state, stack) {
  538. var ts = stack[0];
  539. ts.formatFunction = val.slice(2, -1);
  540. return [stack.shift()];
  541. }, next: "formatString" },
  542. formatMatcher,
  543. { regex: /:[\?\-+]?/, onMatch: function (val, state, stack) {
  544. if (val[1] == "+")
  545. stack[0].ifEnd = stack[0];
  546. if (val[1] == "?")
  547. stack[0].expectElse = true;
  548. }, next: "formatString" },
  549. { regex: "([^:}\\\\]|\\\\.)*:?", token: "", next: "formatString" }
  550. ]
  551. });
  552. return SnippetManager.$tokenizer;
  553. };
  554. this.tokenizeTmSnippet = function (str, startState) {
  555. return this.getTokenizer().getLineTokens(str, startState).tokens.map(function (x) {
  556. return x.value || x;
  557. });
  558. };
  559. this.getVariableValue = function (editor, name, indentation) {
  560. if (/^\d+$/.test(name))
  561. return (this.variables.__ || {})[name] || "";
  562. if (/^[A-Z]\d+$/.test(name))
  563. return (this.variables[name[0] + "__"] || {})[name.substr(1)] || "";
  564. name = name.replace(/^TM_/, "");
  565. if (!this.variables.hasOwnProperty(name))
  566. return "";
  567. var value = this.variables[name];
  568. if (typeof value == "function")
  569. value = this.variables[name](editor, name, indentation);
  570. return value == null ? "" : value;
  571. };
  572. this.variables = VARIABLES;
  573. this.tmStrFormat = function (str, ch, editor) {
  574. if (!ch.fmt)
  575. return str;
  576. var flag = ch.flag || "";
  577. var re = ch.guard;
  578. re = new RegExp(re, flag.replace(/[^gim]/g, ""));
  579. var fmtTokens = typeof ch.fmt == "string" ? this.tokenizeTmSnippet(ch.fmt, "formatString") : ch.fmt;
  580. var _self = this;
  581. var formatted = str.replace(re, function () {
  582. var oldArgs = _self.variables.__;
  583. _self.variables.__ = [].slice.call(arguments);
  584. var fmtParts = _self.resolveVariables(fmtTokens, editor);
  585. var gChangeCase = "E";
  586. for (var i = 0; i < fmtParts.length; i++) {
  587. var ch = fmtParts[i];
  588. if (typeof ch == "object") {
  589. fmtParts[i] = "";
  590. if (ch.changeCase && ch.local) {
  591. var next = fmtParts[i + 1];
  592. if (next && typeof next == "string") {
  593. if (ch.changeCase == "u")
  594. fmtParts[i] = next[0].toUpperCase();
  595. else
  596. fmtParts[i] = next[0].toLowerCase();
  597. fmtParts[i + 1] = next.substr(1);
  598. }
  599. }
  600. else if (ch.changeCase) {
  601. gChangeCase = ch.changeCase;
  602. }
  603. }
  604. else if (gChangeCase == "U") {
  605. fmtParts[i] = ch.toUpperCase();
  606. }
  607. else if (gChangeCase == "L") {
  608. fmtParts[i] = ch.toLowerCase();
  609. }
  610. }
  611. _self.variables.__ = oldArgs;
  612. return fmtParts.join("");
  613. });
  614. return formatted;
  615. };
  616. this.tmFormatFunction = function (str, ch, editor) {
  617. if (ch.formatFunction == "upcase")
  618. return str.toUpperCase();
  619. if (ch.formatFunction == "downcase")
  620. return str.toLowerCase();
  621. return str;
  622. };
  623. this.resolveVariables = function (snippet, editor) {
  624. var result = [];
  625. var indentation = "";
  626. var afterNewLine = true;
  627. for (var i = 0; i < snippet.length; i++) {
  628. var ch = snippet[i];
  629. if (typeof ch == "string") {
  630. result.push(ch);
  631. if (ch == "\n") {
  632. afterNewLine = true;
  633. indentation = "";
  634. }
  635. else if (afterNewLine) {
  636. indentation = /^\t*/.exec(ch)[0];
  637. afterNewLine = /\S/.test(ch);
  638. }
  639. continue;
  640. }
  641. if (!ch)
  642. continue;
  643. afterNewLine = false;
  644. if (ch.fmtString) {
  645. var j = snippet.indexOf(ch, i + 1);
  646. if (j == -1)
  647. j = snippet.length;
  648. ch.fmt = snippet.slice(i + 1, j);
  649. i = j;
  650. }
  651. if (ch.text) {
  652. var value = this.getVariableValue(editor, ch.text, indentation) + "";
  653. if (ch.fmtString)
  654. value = this.tmStrFormat(value, ch, editor);
  655. if (ch.formatFunction)
  656. value = this.tmFormatFunction(value, ch, editor);
  657. if (value && !ch.ifEnd) {
  658. result.push(value);
  659. gotoNext(ch);
  660. }
  661. else if (!value && ch.ifEnd) {
  662. gotoNext(ch.ifEnd);
  663. }
  664. }
  665. else if (ch.elseEnd) {
  666. gotoNext(ch.elseEnd);
  667. }
  668. else if (ch.tabstopId != null) {
  669. result.push(ch);
  670. }
  671. else if (ch.changeCase != null) {
  672. result.push(ch);
  673. }
  674. }
  675. function gotoNext(ch) {
  676. var i1 = snippet.indexOf(ch, i + 1);
  677. if (i1 != -1)
  678. i = i1;
  679. }
  680. return result;
  681. };
  682. this.insertSnippetForSelection = function (editor, snippetText) {
  683. var cursor = editor.getCursorPosition();
  684. var line = editor.session.getLine(cursor.row);
  685. var tabString = editor.session.getTabString();
  686. var indentString = line.match(/^\s*/)[0];
  687. if (cursor.column < indentString.length)
  688. indentString = indentString.slice(0, cursor.column);
  689. snippetText = snippetText.replace(/\r/g, "");
  690. var tokens = this.tokenizeTmSnippet(snippetText);
  691. tokens = this.resolveVariables(tokens, editor);
  692. tokens = tokens.map(function (x) {
  693. if (x == "\n")
  694. return x + indentString;
  695. if (typeof x == "string")
  696. return x.replace(/\t/g, tabString);
  697. return x;
  698. });
  699. var tabstops = [];
  700. tokens.forEach(function (p, i) {
  701. if (typeof p != "object")
  702. return;
  703. var id = p.tabstopId;
  704. var ts = tabstops[id];
  705. if (!ts) {
  706. ts = tabstops[id] = [];
  707. ts.index = id;
  708. ts.value = "";
  709. ts.parents = {};
  710. }
  711. if (ts.indexOf(p) !== -1)
  712. return;
  713. if (p.choices && !ts.choices)
  714. ts.choices = p.choices;
  715. ts.push(p);
  716. var i1 = tokens.indexOf(p, i + 1);
  717. if (i1 === -1)
  718. return;
  719. var value = tokens.slice(i + 1, i1);
  720. var isNested = value.some(function (t) { return typeof t === "object"; });
  721. if (isNested && !ts.value) {
  722. ts.value = value;
  723. }
  724. else if (value.length && (!ts.value || typeof ts.value !== "string")) {
  725. ts.value = value.join("");
  726. }
  727. });
  728. tabstops.forEach(function (ts) { ts.length = 0; });
  729. var expanding = {};
  730. function copyValue(val) {
  731. var copy = [];
  732. for (var i = 0; i < val.length; i++) {
  733. var p = val[i];
  734. if (typeof p == "object") {
  735. if (expanding[p.tabstopId])
  736. continue;
  737. var j = val.lastIndexOf(p, i - 1);
  738. p = copy[j] || { tabstopId: p.tabstopId };
  739. }
  740. copy[i] = p;
  741. }
  742. return copy;
  743. }
  744. for (var i = 0; i < tokens.length; i++) {
  745. var p = tokens[i];
  746. if (typeof p != "object")
  747. continue;
  748. var id = p.tabstopId;
  749. var ts = tabstops[id];
  750. var i1 = tokens.indexOf(p, i + 1);
  751. if (expanding[id]) {
  752. if (expanding[id] === p) {
  753. delete expanding[id];
  754. Object.keys(expanding).forEach(function (parentId) {
  755. ts.parents[parentId] = true;
  756. });
  757. }
  758. continue;
  759. }
  760. expanding[id] = p;
  761. var value = ts.value;
  762. if (typeof value !== "string")
  763. value = copyValue(value);
  764. else if (p.fmt)
  765. value = this.tmStrFormat(value, p, editor);
  766. tokens.splice.apply(tokens, [i + 1, Math.max(0, i1 - i)].concat(value, p));
  767. if (ts.indexOf(p) === -1)
  768. ts.push(p);
  769. }
  770. var row = 0, column = 0;
  771. var text = "";
  772. tokens.forEach(function (t) {
  773. if (typeof t === "string") {
  774. var lines = t.split("\n");
  775. if (lines.length > 1) {
  776. column = lines[lines.length - 1].length;
  777. row += lines.length - 1;
  778. }
  779. else
  780. column += t.length;
  781. text += t;
  782. }
  783. else if (t) {
  784. if (!t.start)
  785. t.start = { row: row, column: column };
  786. else
  787. t.end = { row: row, column: column };
  788. }
  789. });
  790. var range = editor.getSelectionRange();
  791. var end = editor.session.replace(range, text);
  792. var tabstopManager = new TabstopManager(editor);
  793. var selectionId = editor.inVirtualSelectionMode && editor.selection.index;
  794. tabstopManager.addTabstops(tabstops, range.start, end, selectionId);
  795. };
  796. this.insertSnippet = function (editor, snippetText) {
  797. var self = this;
  798. if (editor.inVirtualSelectionMode)
  799. return self.insertSnippetForSelection(editor, snippetText);
  800. editor.forEachSelection(function () {
  801. self.insertSnippetForSelection(editor, snippetText);
  802. }, null, { keepOrder: true });
  803. if (editor.tabstopManager)
  804. editor.tabstopManager.tabNext();
  805. };
  806. this.$getScope = function (editor) {
  807. var scope = editor.session.$mode.$id || "";
  808. scope = scope.split("/").pop();
  809. if (scope === "html" || scope === "php") {
  810. if (scope === "php" && !editor.session.$mode.inlinePhp)
  811. scope = "html";
  812. var c = editor.getCursorPosition();
  813. var state = editor.session.getState(c.row);
  814. if (typeof state === "object") {
  815. state = state[0];
  816. }
  817. if (state.substring) {
  818. if (state.substring(0, 3) == "js-")
  819. scope = "javascript";
  820. else if (state.substring(0, 4) == "css-")
  821. scope = "css";
  822. else if (state.substring(0, 4) == "php-")
  823. scope = "php";
  824. }
  825. }
  826. return scope;
  827. };
  828. this.getActiveScopes = function (editor) {
  829. var scope = this.$getScope(editor);
  830. var scopes = [scope];
  831. var snippetMap = this.snippetMap;
  832. if (snippetMap[scope] && snippetMap[scope].includeScopes) {
  833. scopes.push.apply(scopes, snippetMap[scope].includeScopes);
  834. }
  835. scopes.push("_");
  836. return scopes;
  837. };
  838. this.expandWithTab = function (editor, options) {
  839. var self = this;
  840. var result = editor.forEachSelection(function () {
  841. return self.expandSnippetForSelection(editor, options);
  842. }, null, { keepOrder: true });
  843. if (result && editor.tabstopManager)
  844. editor.tabstopManager.tabNext();
  845. return result;
  846. };
  847. this.expandSnippetForSelection = function (editor, options) {
  848. var cursor = editor.getCursorPosition();
  849. var line = editor.session.getLine(cursor.row);
  850. var before = line.substring(0, cursor.column);
  851. var after = line.substr(cursor.column);
  852. var snippetMap = this.snippetMap;
  853. var snippet;
  854. this.getActiveScopes(editor).some(function (scope) {
  855. var snippets = snippetMap[scope];
  856. if (snippets)
  857. snippet = this.findMatchingSnippet(snippets, before, after);
  858. return !!snippet;
  859. }, this);
  860. if (!snippet)
  861. return false;
  862. if (options && options.dryRun)
  863. return true;
  864. editor.session.doc.removeInLine(cursor.row, cursor.column - snippet.replaceBefore.length, cursor.column + snippet.replaceAfter.length);
  865. this.variables.M__ = snippet.matchBefore;
  866. this.variables.T__ = snippet.matchAfter;
  867. this.insertSnippetForSelection(editor, snippet.content);
  868. this.variables.M__ = this.variables.T__ = null;
  869. return true;
  870. };
  871. this.findMatchingSnippet = function (snippetList, before, after) {
  872. for (var i = snippetList.length; i--;) {
  873. var s = snippetList[i];
  874. if (s.startRe && !s.startRe.test(before))
  875. continue;
  876. if (s.endRe && !s.endRe.test(after))
  877. continue;
  878. if (!s.startRe && !s.endRe)
  879. continue;
  880. s.matchBefore = s.startRe ? s.startRe.exec(before) : [""];
  881. s.matchAfter = s.endRe ? s.endRe.exec(after) : [""];
  882. s.replaceBefore = s.triggerRe ? s.triggerRe.exec(before)[0] : "";
  883. s.replaceAfter = s.endTriggerRe ? s.endTriggerRe.exec(after)[0] : "";
  884. return s;
  885. }
  886. };
  887. this.snippetMap = {};
  888. this.snippetNameMap = {};
  889. this.register = function (snippets, scope) {
  890. var snippetMap = this.snippetMap;
  891. var snippetNameMap = this.snippetNameMap;
  892. var self = this;
  893. if (!snippets)
  894. snippets = [];
  895. function wrapRegexp(src) {
  896. if (src && !/^\^?\(.*\)\$?$|^\\b$/.test(src))
  897. src = "(?:" + src + ")";
  898. return src || "";
  899. }
  900. function guardedRegexp(re, guard, opening) {
  901. re = wrapRegexp(re);
  902. guard = wrapRegexp(guard);
  903. if (opening) {
  904. re = guard + re;
  905. if (re && re[re.length - 1] != "$")
  906. re = re + "$";
  907. }
  908. else {
  909. re = re + guard;
  910. if (re && re[0] != "^")
  911. re = "^" + re;
  912. }
  913. return new RegExp(re);
  914. }
  915. function addSnippet(s) {
  916. if (!s.scope)
  917. s.scope = scope || "_";
  918. scope = s.scope;
  919. if (!snippetMap[scope]) {
  920. snippetMap[scope] = [];
  921. snippetNameMap[scope] = {};
  922. }
  923. var map = snippetNameMap[scope];
  924. if (s.name) {
  925. var old = map[s.name];
  926. if (old)
  927. self.unregister(old);
  928. map[s.name] = s;
  929. }
  930. snippetMap[scope].push(s);
  931. if (s.prefix)
  932. s.tabTrigger = s.prefix;
  933. if (!s.content && s.body)
  934. s.content = Array.isArray(s.body) ? s.body.join("\n") : s.body;
  935. if (s.tabTrigger && !s.trigger) {
  936. if (!s.guard && /^\w/.test(s.tabTrigger))
  937. s.guard = "\\b";
  938. s.trigger = lang.escapeRegExp(s.tabTrigger);
  939. }
  940. if (!s.trigger && !s.guard && !s.endTrigger && !s.endGuard)
  941. return;
  942. s.startRe = guardedRegexp(s.trigger, s.guard, true);
  943. s.triggerRe = new RegExp(s.trigger);
  944. s.endRe = guardedRegexp(s.endTrigger, s.endGuard, true);
  945. s.endTriggerRe = new RegExp(s.endTrigger);
  946. }
  947. if (Array.isArray(snippets)) {
  948. snippets.forEach(addSnippet);
  949. }
  950. else {
  951. Object.keys(snippets).forEach(function (key) {
  952. addSnippet(snippets[key]);
  953. });
  954. }
  955. this._signal("registerSnippets", { scope: scope });
  956. };
  957. this.unregister = function (snippets, scope) {
  958. var snippetMap = this.snippetMap;
  959. var snippetNameMap = this.snippetNameMap;
  960. function removeSnippet(s) {
  961. var nameMap = snippetNameMap[s.scope || scope];
  962. if (nameMap && nameMap[s.name]) {
  963. delete nameMap[s.name];
  964. var map = snippetMap[s.scope || scope];
  965. var i = map && map.indexOf(s);
  966. if (i >= 0)
  967. map.splice(i, 1);
  968. }
  969. }
  970. if (snippets.content)
  971. removeSnippet(snippets);
  972. else if (Array.isArray(snippets))
  973. snippets.forEach(removeSnippet);
  974. };
  975. this.parseSnippetFile = function (str) {
  976. str = str.replace(/\r/g, "");
  977. var list = [], snippet = {};
  978. var re = /^#.*|^({[\s\S]*})\s*$|^(\S+) (.*)$|^((?:\n*\t.*)+)/gm;
  979. var m;
  980. while (m = re.exec(str)) {
  981. if (m[1]) {
  982. try {
  983. snippet = JSON.parse(m[1]);
  984. list.push(snippet);
  985. }
  986. catch (e) { }
  987. }
  988. if (m[4]) {
  989. snippet.content = m[4].replace(/^\t/gm, "");
  990. list.push(snippet);
  991. snippet = {};
  992. }
  993. else {
  994. var key = m[2], val = m[3];
  995. if (key == "regex") {
  996. var guardRe = /\/((?:[^\/\\]|\\.)*)|$/g;
  997. snippet.guard = guardRe.exec(val)[1];
  998. snippet.trigger = guardRe.exec(val)[1];
  999. snippet.endTrigger = guardRe.exec(val)[1];
  1000. snippet.endGuard = guardRe.exec(val)[1];
  1001. }
  1002. else if (key == "snippet") {
  1003. snippet.tabTrigger = val.match(/^\S*/)[0];
  1004. if (!snippet.name)
  1005. snippet.name = val;
  1006. }
  1007. else if (key) {
  1008. snippet[key] = val;
  1009. }
  1010. }
  1011. }
  1012. return list;
  1013. };
  1014. this.getSnippetByName = function (name, editor) {
  1015. var snippetMap = this.snippetNameMap;
  1016. var snippet;
  1017. this.getActiveScopes(editor).some(function (scope) {
  1018. var snippets = snippetMap[scope];
  1019. if (snippets)
  1020. snippet = snippets[name];
  1021. return !!snippet;
  1022. }, this);
  1023. return snippet;
  1024. };
  1025. }).call(SnippetManager.prototype);
  1026. var TabstopManager = function (editor) {
  1027. if (editor.tabstopManager)
  1028. return editor.tabstopManager;
  1029. editor.tabstopManager = this;
  1030. this.$onChange = this.onChange.bind(this);
  1031. this.$onChangeSelection = lang.delayedCall(this.onChangeSelection.bind(this)).schedule;
  1032. this.$onChangeSession = this.onChangeSession.bind(this);
  1033. this.$onAfterExec = this.onAfterExec.bind(this);
  1034. this.attach(editor);
  1035. };
  1036. (function () {
  1037. this.attach = function (editor) {
  1038. this.index = 0;
  1039. this.ranges = [];
  1040. this.tabstops = [];
  1041. this.$openTabstops = null;
  1042. this.selectedTabstop = null;
  1043. this.editor = editor;
  1044. this.editor.on("change", this.$onChange);
  1045. this.editor.on("changeSelection", this.$onChangeSelection);
  1046. this.editor.on("changeSession", this.$onChangeSession);
  1047. this.editor.commands.on("afterExec", this.$onAfterExec);
  1048. this.editor.keyBinding.addKeyboardHandler(this.keyboardHandler);
  1049. };
  1050. this.detach = function () {
  1051. this.tabstops.forEach(this.removeTabstopMarkers, this);
  1052. this.ranges = null;
  1053. this.tabstops = null;
  1054. this.selectedTabstop = null;
  1055. this.editor.removeListener("change", this.$onChange);
  1056. this.editor.removeListener("changeSelection", this.$onChangeSelection);
  1057. this.editor.removeListener("changeSession", this.$onChangeSession);
  1058. this.editor.commands.removeListener("afterExec", this.$onAfterExec);
  1059. this.editor.keyBinding.removeKeyboardHandler(this.keyboardHandler);
  1060. this.editor.tabstopManager = null;
  1061. this.editor = null;
  1062. };
  1063. this.onChange = function (delta) {
  1064. var isRemove = delta.action[0] == "r";
  1065. var selectedTabstop = this.selectedTabstop || {};
  1066. var parents = selectedTabstop.parents || {};
  1067. var tabstops = (this.tabstops || []).slice();
  1068. for (var i = 0; i < tabstops.length; i++) {
  1069. var ts = tabstops[i];
  1070. var active = ts == selectedTabstop || parents[ts.index];
  1071. ts.rangeList.$bias = active ? 0 : 1;
  1072. if (delta.action == "remove" && ts !== selectedTabstop) {
  1073. var parentActive = ts.parents && ts.parents[selectedTabstop.index];
  1074. var startIndex = ts.rangeList.pointIndex(delta.start, parentActive);
  1075. startIndex = startIndex < 0 ? -startIndex - 1 : startIndex + 1;
  1076. var endIndex = ts.rangeList.pointIndex(delta.end, parentActive);
  1077. endIndex = endIndex < 0 ? -endIndex - 1 : endIndex - 1;
  1078. var toRemove = ts.rangeList.ranges.slice(startIndex, endIndex);
  1079. for (var j = 0; j < toRemove.length; j++)
  1080. this.removeRange(toRemove[j]);
  1081. }
  1082. ts.rangeList.$onChange(delta);
  1083. }
  1084. var session = this.editor.session;
  1085. if (!this.$inChange && isRemove && session.getLength() == 1 && !session.getValue())
  1086. this.detach();
  1087. };
  1088. this.updateLinkedFields = function () {
  1089. var ts = this.selectedTabstop;
  1090. if (!ts || !ts.hasLinkedRanges || !ts.firstNonLinked)
  1091. return;
  1092. this.$inChange = true;
  1093. var session = this.editor.session;
  1094. var text = session.getTextRange(ts.firstNonLinked);
  1095. for (var i = 0; i < ts.length; i++) {
  1096. var range = ts[i];
  1097. if (!range.linked)
  1098. continue;
  1099. var original = range.original;
  1100. var fmt = exports.snippetManager.tmStrFormat(text, original, this.editor);
  1101. session.replace(range, fmt);
  1102. }
  1103. this.$inChange = false;
  1104. };
  1105. this.onAfterExec = function (e) {
  1106. if (e.command && !e.command.readOnly)
  1107. this.updateLinkedFields();
  1108. };
  1109. this.onChangeSelection = function () {
  1110. if (!this.editor)
  1111. return;
  1112. var lead = this.editor.selection.lead;
  1113. var anchor = this.editor.selection.anchor;
  1114. var isEmpty = this.editor.selection.isEmpty();
  1115. for (var i = 0; i < this.ranges.length; i++) {
  1116. if (this.ranges[i].linked)
  1117. continue;
  1118. var containsLead = this.ranges[i].contains(lead.row, lead.column);
  1119. var containsAnchor = isEmpty || this.ranges[i].contains(anchor.row, anchor.column);
  1120. if (containsLead && containsAnchor)
  1121. return;
  1122. }
  1123. this.detach();
  1124. };
  1125. this.onChangeSession = function () {
  1126. this.detach();
  1127. };
  1128. this.tabNext = function (dir) {
  1129. var max = this.tabstops.length;
  1130. var index = this.index + (dir || 1);
  1131. index = Math.min(Math.max(index, 1), max);
  1132. if (index == max)
  1133. index = 0;
  1134. this.selectTabstop(index);
  1135. if (index === 0)
  1136. this.detach();
  1137. };
  1138. this.selectTabstop = function (index) {
  1139. this.$openTabstops = null;
  1140. var ts = this.tabstops[this.index];
  1141. if (ts)
  1142. this.addTabstopMarkers(ts);
  1143. this.index = index;
  1144. ts = this.tabstops[this.index];
  1145. if (!ts || !ts.length)
  1146. return;
  1147. this.selectedTabstop = ts;
  1148. var range = ts.firstNonLinked || ts;
  1149. if (ts.choices)
  1150. range.cursor = range.start;
  1151. if (!this.editor.inVirtualSelectionMode) {
  1152. var sel = this.editor.multiSelect;
  1153. sel.toSingleRange(range);
  1154. for (var i = 0; i < ts.length; i++) {
  1155. if (ts.hasLinkedRanges && ts[i].linked)
  1156. continue;
  1157. sel.addRange(ts[i].clone(), true);
  1158. }
  1159. }
  1160. else {
  1161. this.editor.selection.fromOrientedRange(range);
  1162. }
  1163. this.editor.keyBinding.addKeyboardHandler(this.keyboardHandler);
  1164. if (this.selectedTabstop && this.selectedTabstop.choices)
  1165. this.editor.execCommand("startAutocomplete", { matches: this.selectedTabstop.choices });
  1166. };
  1167. this.addTabstops = function (tabstops, start, end) {
  1168. var useLink = this.useLink || !this.editor.getOption("enableMultiselect");
  1169. if (!this.$openTabstops)
  1170. this.$openTabstops = [];
  1171. if (!tabstops[0]) {
  1172. var p = Range.fromPoints(end, end);
  1173. moveRelative(p.start, start);
  1174. moveRelative(p.end, start);
  1175. tabstops[0] = [p];
  1176. tabstops[0].index = 0;
  1177. }
  1178. var i = this.index;
  1179. var arg = [i + 1, 0];
  1180. var ranges = this.ranges;
  1181. tabstops.forEach(function (ts, index) {
  1182. var dest = this.$openTabstops[index] || ts;
  1183. for (var i = 0; i < ts.length; i++) {
  1184. var p = ts[i];
  1185. var range = Range.fromPoints(p.start, p.end || p.start);
  1186. movePoint(range.start, start);
  1187. movePoint(range.end, start);
  1188. range.original = p;
  1189. range.tabstop = dest;
  1190. ranges.push(range);
  1191. if (dest != ts)
  1192. dest.unshift(range);
  1193. else
  1194. dest[i] = range;
  1195. if (p.fmtString || (dest.firstNonLinked && useLink)) {
  1196. range.linked = true;
  1197. dest.hasLinkedRanges = true;
  1198. }
  1199. else if (!dest.firstNonLinked)
  1200. dest.firstNonLinked = range;
  1201. }
  1202. if (!dest.firstNonLinked)
  1203. dest.hasLinkedRanges = false;
  1204. if (dest === ts) {
  1205. arg.push(dest);
  1206. this.$openTabstops[index] = dest;
  1207. }
  1208. this.addTabstopMarkers(dest);
  1209. dest.rangeList = dest.rangeList || new RangeList();
  1210. dest.rangeList.$bias = 0;
  1211. dest.rangeList.addList(dest);
  1212. }, this);
  1213. if (arg.length > 2) {
  1214. if (this.tabstops.length)
  1215. arg.push(arg.splice(2, 1)[0]);
  1216. this.tabstops.splice.apply(this.tabstops, arg);
  1217. }
  1218. };
  1219. this.addTabstopMarkers = function (ts) {
  1220. var session = this.editor.session;
  1221. ts.forEach(function (range) {
  1222. if (!range.markerId)
  1223. range.markerId = session.addMarker(range, "ace_snippet-marker", "text");
  1224. });
  1225. };
  1226. this.removeTabstopMarkers = function (ts) {
  1227. var session = this.editor.session;
  1228. ts.forEach(function (range) {
  1229. session.removeMarker(range.markerId);
  1230. range.markerId = null;
  1231. });
  1232. };
  1233. this.removeRange = function (range) {
  1234. var i = range.tabstop.indexOf(range);
  1235. if (i != -1)
  1236. range.tabstop.splice(i, 1);
  1237. i = this.ranges.indexOf(range);
  1238. if (i != -1)
  1239. this.ranges.splice(i, 1);
  1240. i = range.tabstop.rangeList.ranges.indexOf(range);
  1241. if (i != -1)
  1242. range.tabstop.splice(i, 1);
  1243. this.editor.session.removeMarker(range.markerId);
  1244. if (!range.tabstop.length) {
  1245. i = this.tabstops.indexOf(range.tabstop);
  1246. if (i != -1)
  1247. this.tabstops.splice(i, 1);
  1248. if (!this.tabstops.length)
  1249. this.detach();
  1250. }
  1251. };
  1252. this.keyboardHandler = new HashHandler();
  1253. this.keyboardHandler.bindKeys({
  1254. "Tab": function (editor) {
  1255. if (exports.snippetManager && exports.snippetManager.expandWithTab(editor))
  1256. return;
  1257. editor.tabstopManager.tabNext(1);
  1258. editor.renderer.scrollCursorIntoView();
  1259. },
  1260. "Shift-Tab": function (editor) {
  1261. editor.tabstopManager.tabNext(-1);
  1262. editor.renderer.scrollCursorIntoView();
  1263. },
  1264. "Esc": function (editor) {
  1265. editor.tabstopManager.detach();
  1266. }
  1267. });
  1268. }).call(TabstopManager.prototype);
  1269. var movePoint = function (point, diff) {
  1270. if (point.row == 0)
  1271. point.column += diff.column;
  1272. point.row += diff.row;
  1273. };
  1274. var moveRelative = function (point, start) {
  1275. if (point.row == start.row)
  1276. point.column -= start.column;
  1277. point.row -= start.row;
  1278. };
  1279. dom.importCssString("\n.ace_snippet-marker {\n -moz-box-sizing: border-box;\n box-sizing: border-box;\n background: rgba(194, 193, 208, 0.09);\n border: 1px dotted rgba(211, 208, 235, 0.62);\n position: absolute;\n}", "snippets.css", false);
  1280. exports.snippetManager = new SnippetManager();
  1281. var Editor = require("./editor").Editor;
  1282. (function () {
  1283. this.insertSnippet = function (content, options) {
  1284. return exports.snippetManager.insertSnippet(this, content, options);
  1285. };
  1286. this.expandSnippet = function (options) {
  1287. return exports.snippetManager.expandWithTab(this, options);
  1288. };
  1289. }).call(Editor.prototype);
  1290. });
  1291. ace.define("ace/autocomplete",["require","exports","module","ace/keyboard/hash_handler","ace/autocomplete/popup","ace/autocomplete/util","ace/lib/lang","ace/lib/dom","ace/snippets","ace/config"], function(require, exports, module){"use strict";
  1292. var HashHandler = require("./keyboard/hash_handler").HashHandler;
  1293. var AcePopup = require("./autocomplete/popup").AcePopup;
  1294. var util = require("./autocomplete/util");
  1295. var lang = require("./lib/lang");
  1296. var dom = require("./lib/dom");
  1297. var snippetManager = require("./snippets").snippetManager;
  1298. var config = require("./config");
  1299. var Autocomplete = function () {
  1300. this.autoInsert = false;
  1301. this.autoSelect = true;
  1302. this.exactMatch = false;
  1303. this.gatherCompletionsId = 0;
  1304. this.keyboardHandler = new HashHandler();
  1305. this.keyboardHandler.bindKeys(this.commands);
  1306. this.blurListener = this.blurListener.bind(this);
  1307. this.changeListener = this.changeListener.bind(this);
  1308. this.mousedownListener = this.mousedownListener.bind(this);
  1309. this.mousewheelListener = this.mousewheelListener.bind(this);
  1310. this.changeTimer = lang.delayedCall(function () {
  1311. this.updateCompletions(true);
  1312. }.bind(this));
  1313. this.tooltipTimer = lang.delayedCall(this.updateDocTooltip.bind(this), 50);
  1314. };
  1315. (function () {
  1316. this.$init = function () {
  1317. this.popup = new AcePopup(document.body || document.documentElement);
  1318. this.popup.on("click", function (e) {
  1319. this.insertMatch();
  1320. e.stop();
  1321. }.bind(this));
  1322. this.popup.focus = this.editor.focus.bind(this.editor);
  1323. this.popup.on("show", this.tooltipTimer.bind(null, null));
  1324. this.popup.on("select", this.tooltipTimer.bind(null, null));
  1325. this.popup.on("changeHoverMarker", this.tooltipTimer.bind(null, null));
  1326. return this.popup;
  1327. };
  1328. this.getPopup = function () {
  1329. return this.popup || this.$init();
  1330. };
  1331. this.openPopup = function (editor, prefix, keepPopupPosition) {
  1332. if (!this.popup)
  1333. this.$init();
  1334. this.popup.autoSelect = this.autoSelect;
  1335. this.popup.setData(this.completions.filtered, this.completions.filterText);
  1336. editor.keyBinding.addKeyboardHandler(this.keyboardHandler);
  1337. var renderer = editor.renderer;
  1338. this.popup.setRow(this.autoSelect ? 0 : -1);
  1339. if (!keepPopupPosition) {
  1340. this.popup.setTheme(editor.getTheme());
  1341. this.popup.setFontSize(editor.getFontSize());
  1342. var lineHeight = renderer.layerConfig.lineHeight;
  1343. var pos = renderer.$cursorLayer.getPixelPosition(this.base, true);
  1344. pos.left -= this.popup.getTextLeftOffset();
  1345. var rect = editor.container.getBoundingClientRect();
  1346. pos.top += rect.top - renderer.layerConfig.offset;
  1347. pos.left += rect.left - editor.renderer.scrollLeft;
  1348. pos.left += renderer.gutterWidth;
  1349. this.popup.show(pos, lineHeight);
  1350. }
  1351. else if (keepPopupPosition && !prefix) {
  1352. this.detach();
  1353. }
  1354. this.changeTimer.cancel();
  1355. };
  1356. this.detach = function () {
  1357. this.editor.keyBinding.removeKeyboardHandler(this.keyboardHandler);
  1358. this.editor.off("changeSelection", this.changeListener);
  1359. this.editor.off("blur", this.blurListener);
  1360. this.editor.off("mousedown", this.mousedownListener);
  1361. this.editor.off("mousewheel", this.mousewheelListener);
  1362. this.changeTimer.cancel();
  1363. this.hideDocTooltip();
  1364. this.gatherCompletionsId += 1;
  1365. if (this.popup && this.popup.isOpen)
  1366. this.popup.hide();
  1367. if (this.base)
  1368. this.base.detach();
  1369. this.activated = false;
  1370. this.completions = this.base = null;
  1371. };
  1372. this.changeListener = function (e) {
  1373. var cursor = this.editor.selection.lead;
  1374. if (cursor.row != this.base.row || cursor.column < this.base.column) {
  1375. this.detach();
  1376. }
  1377. if (this.activated)
  1378. this.changeTimer.schedule();
  1379. else
  1380. this.detach();
  1381. };
  1382. this.blurListener = function (e) {
  1383. var el = document.activeElement;
  1384. var text = this.editor.textInput.getElement();
  1385. var fromTooltip = e.relatedTarget && this.tooltipNode && this.tooltipNode.contains(e.relatedTarget);
  1386. var container = this.popup && this.popup.container;
  1387. if (el != text && el.parentNode != container && !fromTooltip
  1388. && el != this.tooltipNode && e.relatedTarget != text) {
  1389. this.detach();
  1390. }
  1391. };
  1392. this.mousedownListener = function (e) {
  1393. this.detach();
  1394. };
  1395. this.mousewheelListener = function (e) {
  1396. this.detach();
  1397. };
  1398. this.goTo = function (where) {
  1399. this.popup.goTo(where);
  1400. };
  1401. this.insertMatch = function (data, options) {
  1402. if (!data)
  1403. data = this.popup.getData(this.popup.getRow());
  1404. if (!data)
  1405. return false;
  1406. var completions = this.completions;
  1407. this.editor.startOperation({ command: { name: "insertMatch" } });
  1408. if (data.completer && data.completer.insertMatch) {
  1409. data.completer.insertMatch(this.editor, data);
  1410. }
  1411. else {
  1412. if (!completions)
  1413. return false;
  1414. if (completions.filterText) {
  1415. var ranges = this.editor.selection.getAllRanges();
  1416. for (var i = 0, range; range = ranges[i]; i++) {
  1417. range.start.column -= completions.filterText.length;
  1418. this.editor.session.remove(range);
  1419. }
  1420. }
  1421. if (data.snippet)
  1422. snippetManager.insertSnippet(this.editor, data.snippet);
  1423. else
  1424. this.editor.execCommand("insertstring", data.value || data);
  1425. }
  1426. if (this.completions == completions)
  1427. this.detach();
  1428. this.editor.endOperation();
  1429. };
  1430. this.commands = {
  1431. "Up": function (editor) { editor.completer.goTo("up"); },
  1432. "Down": function (editor) { editor.completer.goTo("down"); },
  1433. "Ctrl-Up|Ctrl-Home": function (editor) { editor.completer.goTo("start"); },
  1434. "Ctrl-Down|Ctrl-End": function (editor) { editor.completer.goTo("end"); },
  1435. "Esc": function (editor) { editor.completer.detach(); },
  1436. "Return": function (editor) { return editor.completer.insertMatch(); },
  1437. "Shift-Return": function (editor) { editor.completer.insertMatch(null, { deleteSuffix: true }); },
  1438. "Tab": function (editor) {
  1439. var result = editor.completer.insertMatch();
  1440. if (!result && !editor.tabstopManager)
  1441. editor.completer.goTo("down");
  1442. else
  1443. return result;
  1444. },
  1445. "PageUp": function (editor) { editor.completer.popup.gotoPageUp(); },
  1446. "PageDown": function (editor) { editor.completer.popup.gotoPageDown(); }
  1447. };
  1448. this.gatherCompletions = function (editor, callback) {
  1449. var session = editor.getSession();
  1450. var pos = editor.getCursorPosition();
  1451. var prefix = util.getCompletionPrefix(editor);
  1452. this.base = session.doc.createAnchor(pos.row, pos.column - prefix.length);
  1453. this.base.$insertRight = true;
  1454. var matches = [];
  1455. var total = editor.completers.length;
  1456. editor.completers.forEach(function (completer, i) {
  1457. completer.getCompletions(editor, session, pos, prefix, function (err, results) {
  1458. if (!err && results)
  1459. matches = matches.concat(results);
  1460. callback(null, {
  1461. prefix: util.getCompletionPrefix(editor),
  1462. matches: matches,
  1463. finished: (--total === 0)
  1464. });
  1465. });
  1466. });
  1467. return true;
  1468. };
  1469. this.showPopup = function (editor, options) {
  1470. if (this.editor)
  1471. this.detach();
  1472. this.activated = true;
  1473. this.editor = editor;
  1474. if (editor.completer != this) {
  1475. if (editor.completer)
  1476. editor.completer.detach();
  1477. editor.completer = this;
  1478. }
  1479. editor.on("changeSelection", this.changeListener);
  1480. editor.on("blur", this.blurListener);
  1481. editor.on("mousedown", this.mousedownListener);
  1482. editor.on("mousewheel", this.mousewheelListener);
  1483. this.updateCompletions(false, options);
  1484. };
  1485. this.updateCompletions = function (keepPopupPosition, options) {
  1486. if (keepPopupPosition && this.base && this.completions) {
  1487. var pos = this.editor.getCursorPosition();
  1488. var prefix = this.editor.session.getTextRange({ start: this.base, end: pos });
  1489. if (prefix == this.completions.filterText)
  1490. return;
  1491. this.completions.setFilter(prefix);
  1492. if (!this.completions.filtered.length)
  1493. return this.detach();
  1494. if (this.completions.filtered.length == 1
  1495. && this.completions.filtered[0].value == prefix
  1496. && !this.completions.filtered[0].snippet)
  1497. return this.detach();
  1498. this.openPopup(this.editor, prefix, keepPopupPosition);
  1499. return;
  1500. }
  1501. if (options && options.matches) {
  1502. var pos = this.editor.getSelectionRange().start;
  1503. this.base = this.editor.session.doc.createAnchor(pos.row, pos.column);
  1504. this.base.$insertRight = true;
  1505. this.completions = new FilteredList(options.matches);
  1506. return this.openPopup(this.editor, "", keepPopupPosition);
  1507. }
  1508. var _id = this.gatherCompletionsId;
  1509. var detachIfFinished = function (results) {
  1510. if (!results.finished)
  1511. return;
  1512. return this.detach();
  1513. }.bind(this);
  1514. var processResults = function (results) {
  1515. var prefix = results.prefix;
  1516. var matches = results.matches;
  1517. this.completions = new FilteredList(matches);
  1518. if (this.exactMatch)
  1519. this.completions.exactMatch = true;
  1520. this.completions.setFilter(prefix);
  1521. var filtered = this.completions.filtered;
  1522. if (!filtered.length)
  1523. return detachIfFinished(results);
  1524. if (filtered.length == 1 && filtered[0].value == prefix && !filtered[0].snippet)
  1525. return detachIfFinished(results);
  1526. if (this.autoInsert && filtered.length == 1 && results.finished)
  1527. return this.insertMatch(filtered[0]);
  1528. this.openPopup(this.editor, prefix, keepPopupPosition);
  1529. }.bind(this);
  1530. var isImmediate = true;
  1531. var immediateResults = null;
  1532. this.gatherCompletions(this.editor, function (err, results) {
  1533. var prefix = results.prefix;
  1534. var matches = results && results.matches;
  1535. if (!matches || !matches.length)
  1536. return detachIfFinished(results);
  1537. if (prefix.indexOf(results.prefix) !== 0 || _id != this.gatherCompletionsId)
  1538. return;
  1539. if (isImmediate) {
  1540. immediateResults = results;
  1541. return;
  1542. }
  1543. processResults(results);
  1544. }.bind(this));
  1545. isImmediate = false;
  1546. if (immediateResults) {
  1547. var results = immediateResults;
  1548. immediateResults = null;
  1549. processResults(results);
  1550. }
  1551. };
  1552. this.cancelContextMenu = function () {
  1553. this.editor.$mouseHandler.cancelContextMenu();
  1554. };
  1555. this.updateDocTooltip = function () {
  1556. var popup = this.popup;
  1557. var all = popup.data;
  1558. var selected = all && (all[popup.getHoveredRow()] || all[popup.getRow()]);
  1559. var doc = null;
  1560. if (!selected || !this.editor || !this.popup.isOpen)
  1561. return this.hideDocTooltip();
  1562. this.editor.completers.some(function (completer) {
  1563. if (completer.getDocTooltip)
  1564. doc = completer.getDocTooltip(selected);
  1565. return doc;
  1566. });
  1567. if (!doc && typeof selected != "string")
  1568. doc = selected;
  1569. if (typeof doc == "string")
  1570. doc = { docText: doc };
  1571. if (!doc || !(doc.docHTML || doc.docText))
  1572. return this.hideDocTooltip();
  1573. this.showDocTooltip(doc);
  1574. };
  1575. this.showDocTooltip = function (item) {
  1576. if (!this.tooltipNode) {
  1577. this.tooltipNode = dom.createElement("div");
  1578. this.tooltipNode.className = "ace_tooltip ace_doc-tooltip";
  1579. this.tooltipNode.style.margin = 0;
  1580. this.tooltipNode.style.pointerEvents = "auto";
  1581. this.tooltipNode.tabIndex = -1;
  1582. this.tooltipNode.onblur = this.blurListener.bind(this);
  1583. this.tooltipNode.onclick = this.onTooltipClick.bind(this);
  1584. }
  1585. var tooltipNode = this.tooltipNode;
  1586. if (item.docHTML) {
  1587. tooltipNode.innerHTML = item.docHTML;
  1588. }
  1589. else if (item.docText) {
  1590. tooltipNode.textContent = item.docText;
  1591. }
  1592. if (!tooltipNode.parentNode)
  1593. document.body.appendChild(tooltipNode);
  1594. var popup = this.popup;
  1595. var rect = popup.container.getBoundingClientRect();
  1596. tooltipNode.style.top = popup.container.style.top;
  1597. tooltipNode.style.bottom = popup.container.style.bottom;
  1598. tooltipNode.style.display = "block";
  1599. if (window.innerWidth - rect.right < 320) {
  1600. if (rect.left < 320) {
  1601. if (popup.isTopdown) {
  1602. tooltipNode.style.top = rect.bottom + "px";
  1603. tooltipNode.style.left = rect.left + "px";
  1604. tooltipNode.style.right = "";
  1605. tooltipNode.style.bottom = "";
  1606. }
  1607. else {
  1608. tooltipNode.style.top = popup.container.offsetTop - tooltipNode.offsetHeight + "px";
  1609. tooltipNode.style.left = rect.left + "px";
  1610. tooltipNode.style.right = "";
  1611. tooltipNode.style.bottom = "";
  1612. }
  1613. }
  1614. else {
  1615. tooltipNode.style.right = window.innerWidth - rect.left + "px";
  1616. tooltipNode.style.left = "";
  1617. }
  1618. }
  1619. else {
  1620. tooltipNode.style.left = (rect.right + 1) + "px";
  1621. tooltipNode.style.right = "";
  1622. }
  1623. };
  1624. this.hideDocTooltip = function () {
  1625. this.tooltipTimer.cancel();
  1626. if (!this.tooltipNode)
  1627. return;
  1628. var el = this.tooltipNode;
  1629. if (!this.editor.isFocused() && document.activeElement == el)
  1630. this.editor.focus();
  1631. this.tooltipNode = null;
  1632. if (el.parentNode)
  1633. el.parentNode.removeChild(el);
  1634. };
  1635. this.onTooltipClick = function (e) {
  1636. var a = e.target;
  1637. while (a && a != this.tooltipNode) {
  1638. if (a.nodeName == "A" && a.href) {
  1639. a.rel = "noreferrer";
  1640. a.target = "_blank";
  1641. break;
  1642. }
  1643. a = a.parentNode;
  1644. }
  1645. };
  1646. this.destroy = function () {
  1647. this.detach();
  1648. if (this.popup) {
  1649. this.popup.destroy();
  1650. var el = this.popup.container;
  1651. if (el && el.parentNode)
  1652. el.parentNode.removeChild(el);
  1653. }
  1654. if (this.editor && this.editor.completer == this)
  1655. this.editor.completer == null;
  1656. this.popup = null;
  1657. };
  1658. }).call(Autocomplete.prototype);
  1659. Autocomplete.for = function (editor) {
  1660. if (editor.completer) {
  1661. return editor.completer;
  1662. }
  1663. if (config.get("sharedPopups")) {
  1664. if (!Autocomplete.$shared)
  1665. Autocomplete.$sharedInstance = new Autocomplete();
  1666. editor.completer = Autocomplete.$sharedInstance;
  1667. }
  1668. else {
  1669. editor.completer = new Autocomplete();
  1670. editor.once("destroy", function (e, editor) {
  1671. editor.completer.destroy();
  1672. });
  1673. }
  1674. return editor.completer;
  1675. };
  1676. Autocomplete.startCommand = {
  1677. name: "startAutocomplete",
  1678. exec: function (editor, options) {
  1679. var completer = Autocomplete.for(editor);
  1680. completer.autoInsert = false;
  1681. completer.autoSelect = true;
  1682. completer.showPopup(editor, options);
  1683. completer.cancelContextMenu();
  1684. },
  1685. bindKey: "Ctrl-Space|Ctrl-Shift-Space|Alt-Space"
  1686. };
  1687. var FilteredList = function (array, filterText) {
  1688. this.all = array;
  1689. this.filtered = array;
  1690. this.filterText = filterText || "";
  1691. this.exactMatch = false;
  1692. };
  1693. (function () {
  1694. this.setFilter = function (str) {
  1695. if (str.length > this.filterText && str.lastIndexOf(this.filterText, 0) === 0)
  1696. var matches = this.filtered;
  1697. else
  1698. var matches = this.all;
  1699. this.filterText = str;
  1700. matches = this.filterCompletions(matches, this.filterText);
  1701. matches = matches.sort(function (a, b) {
  1702. return b.exactMatch - a.exactMatch || b.$score - a.$score
  1703. || (a.caption || a.value).localeCompare(b.caption || b.value);
  1704. });
  1705. var prev = null;
  1706. matches = matches.filter(function (item) {
  1707. var caption = item.snippet || item.caption || item.value;
  1708. if (caption === prev)
  1709. return false;
  1710. prev = caption;
  1711. return true;
  1712. });
  1713. this.filtered = matches;
  1714. };
  1715. this.filterCompletions = function (items, needle) {
  1716. var results = [];
  1717. var upper = needle.toUpperCase();
  1718. var lower = needle.toLowerCase();
  1719. loop: for (var i = 0, item; item = items[i]; i++) {
  1720. var caption = item.caption || item.value || item.snippet;
  1721. if (!caption)
  1722. continue;
  1723. var lastIndex = -1;
  1724. var matchMask = 0;
  1725. var penalty = 0;
  1726. var index, distance;
  1727. if (this.exactMatch) {
  1728. if (needle !== caption.substr(0, needle.length))
  1729. continue loop;
  1730. }
  1731. else {
  1732. var fullMatchIndex = caption.toLowerCase().indexOf(lower);
  1733. if (fullMatchIndex > -1) {
  1734. penalty = fullMatchIndex;
  1735. }
  1736. else {
  1737. for (var j = 0; j < needle.length; j++) {
  1738. var i1 = caption.indexOf(lower[j], lastIndex + 1);
  1739. var i2 = caption.indexOf(upper[j], lastIndex + 1);
  1740. index = (i1 >= 0) ? ((i2 < 0 || i1 < i2) ? i1 : i2) : i2;
  1741. if (index < 0)
  1742. continue loop;
  1743. distance = index - lastIndex - 1;
  1744. if (distance > 0) {
  1745. if (lastIndex === -1)
  1746. penalty += 10;
  1747. penalty += distance;
  1748. matchMask = matchMask | (1 << j);
  1749. }
  1750. lastIndex = index;
  1751. }
  1752. }
  1753. }
  1754. item.matchMask = matchMask;
  1755. item.exactMatch = penalty ? 0 : 1;
  1756. item.$score = (item.score || 0) - penalty;
  1757. results.push(item);
  1758. }
  1759. return results;
  1760. };
  1761. }).call(FilteredList.prototype);
  1762. exports.Autocomplete = Autocomplete;
  1763. exports.FilteredList = FilteredList;
  1764. });
  1765. ace.define("ace/ext/menu_tools/settings_menu.css",["require","exports","module"], function(require, exports, module){module.exports = "#ace_settingsmenu, #kbshortcutmenu {\n background-color: #F7F7F7;\n color: black;\n box-shadow: -5px 4px 5px rgba(126, 126, 126, 0.55);\n padding: 1em 0.5em 2em 1em;\n overflow: auto;\n position: absolute;\n margin: 0;\n bottom: 0;\n right: 0;\n top: 0;\n z-index: 9991;\n cursor: default;\n}\n\n.ace_dark #ace_settingsmenu, .ace_dark #kbshortcutmenu {\n box-shadow: -20px 10px 25px rgba(126, 126, 126, 0.25);\n background-color: rgba(255, 255, 255, 0.6);\n color: black;\n}\n\n.ace_optionsMenuEntry:hover {\n background-color: rgba(100, 100, 100, 0.1);\n transition: all 0.3s\n}\n\n.ace_closeButton {\n background: rgba(245, 146, 146, 0.5);\n border: 1px solid #F48A8A;\n border-radius: 50%;\n padding: 7px;\n position: absolute;\n right: -8px;\n top: -8px;\n z-index: 100000;\n}\n.ace_closeButton{\n background: rgba(245, 146, 146, 0.9);\n}\n.ace_optionsMenuKey {\n color: darkslateblue;\n font-weight: bold;\n}\n.ace_optionsMenuCommand {\n color: darkcyan;\n font-weight: normal;\n}\n.ace_optionsMenuEntry input, .ace_optionsMenuEntry button {\n vertical-align: middle;\n}\n\n.ace_optionsMenuEntry button[ace_selected_button=true] {\n background: #e7e7e7;\n box-shadow: 1px 0px 2px 0px #adadad inset;\n border-color: #adadad;\n}\n.ace_optionsMenuEntry button {\n background: white;\n border: 1px solid lightgray;\n margin: 0px;\n}\n.ace_optionsMenuEntry button:hover{\n background: #f0f0f0;\n}";
  1766. });
  1767. ace.define("ace/ext/menu_tools/overlay_page",["require","exports","module","ace/lib/dom","ace/ext/menu_tools/settings_menu.css"], function(require, exports, module){/*jslint indent: 4, maxerr: 50, white: true, browser: true, vars: true*/
  1768. 'use strict';
  1769. var dom = require("../../lib/dom");
  1770. var cssText = require("./settings_menu.css");
  1771. dom.importCssString(cssText, "settings_menu.css", false);
  1772. module.exports.overlayPage = function overlayPage(editor, contentElement, callback) {
  1773. var closer = document.createElement('div');
  1774. var ignoreFocusOut = false;
  1775. function documentEscListener(e) {
  1776. if (e.keyCode === 27) {
  1777. close();
  1778. }
  1779. }
  1780. function close() {
  1781. if (!closer)
  1782. return;
  1783. document.removeEventListener('keydown', documentEscListener);
  1784. closer.parentNode.removeChild(closer);
  1785. if (editor) {
  1786. editor.focus();
  1787. }
  1788. closer = null;
  1789. callback && callback();
  1790. }
  1791. function setIgnoreFocusOut(ignore) {
  1792. ignoreFocusOut = ignore;
  1793. if (ignore) {
  1794. closer.style.pointerEvents = "none";
  1795. contentElement.style.pointerEvents = "auto";
  1796. }
  1797. }
  1798. closer.style.cssText = 'margin: 0; padding: 0; ' +
  1799. 'position: fixed; top:0; bottom:0; left:0; right:0;' +
  1800. 'z-index: 9990; ' +
  1801. (editor ? 'background-color: rgba(0, 0, 0, 0.3);' : '');
  1802. closer.addEventListener('click', function (e) {
  1803. if (!ignoreFocusOut) {
  1804. close();
  1805. }
  1806. });
  1807. document.addEventListener('keydown', documentEscListener);
  1808. contentElement.addEventListener('click', function (e) {
  1809. e.stopPropagation();
  1810. });
  1811. closer.appendChild(contentElement);
  1812. document.body.appendChild(closer);
  1813. if (editor) {
  1814. editor.blur();
  1815. }
  1816. return {
  1817. close: close,
  1818. setIgnoreFocusOut: setIgnoreFocusOut
  1819. };
  1820. };
  1821. });
  1822. ace.define("ace/ext/modelist",["require","exports","module"], function(require, exports, module){"use strict";
  1823. var modes = [];
  1824. function getModeForPath(path) {
  1825. var mode = modesByName.text;
  1826. var fileName = path.split(/[\/\\]/).pop();
  1827. for (var i = 0; i < modes.length; i++) {
  1828. if (modes[i].supportsFile(fileName)) {
  1829. mode = modes[i];
  1830. break;
  1831. }
  1832. }
  1833. return mode;
  1834. }
  1835. var Mode = function (name, caption, extensions) {
  1836. this.name = name;
  1837. this.caption = caption;
  1838. this.mode = "ace/mode/" + name;
  1839. this.extensions = extensions;
  1840. var re;
  1841. if (/\^/.test(extensions)) {
  1842. re = extensions.replace(/\|(\^)?/g, function (a, b) {
  1843. return "$|" + (b ? "^" : "^.*\\.");
  1844. }) + "$";
  1845. }
  1846. else {
  1847. re = "^.*\\.(" + extensions + ")$";
  1848. }
  1849. this.extRe = new RegExp(re, "gi");
  1850. };
  1851. Mode.prototype.supportsFile = function (filename) {
  1852. return filename.match(this.extRe);
  1853. };
  1854. var supportedModes = {
  1855. ABAP: ["abap"],
  1856. ABC: ["abc"],
  1857. ActionScript: ["as"],
  1858. ADA: ["ada|adb"],
  1859. Alda: ["alda"],
  1860. Apache_Conf: ["^htaccess|^htgroups|^htpasswd|^conf|htaccess|htgroups|htpasswd"],
  1861. Apex: ["apex|cls|trigger|tgr"],
  1862. AQL: ["aql"],
  1863. AsciiDoc: ["asciidoc|adoc"],
  1864. ASL: ["dsl|asl|asl.json"],
  1865. Assembly_x86: ["asm|a"],
  1866. AutoHotKey: ["ahk"],
  1867. BatchFile: ["bat|cmd"],
  1868. C_Cpp: ["cpp|c|cc|cxx|h|hh|hpp|ino"],
  1869. C9Search: ["c9search_results"],
  1870. Cirru: ["cirru|cr"],
  1871. Clojure: ["clj|cljs"],
  1872. Cobol: ["CBL|COB"],
  1873. coffee: ["coffee|cf|cson|^Cakefile"],
  1874. ColdFusion: ["cfm"],
  1875. Crystal: ["cr"],
  1876. CSharp: ["cs"],
  1877. Csound_Document: ["csd"],
  1878. Csound_Orchestra: ["orc"],
  1879. Csound_Score: ["sco"],
  1880. CSS: ["css"],
  1881. Curly: ["curly"],
  1882. D: ["d|di"],
  1883. Dart: ["dart"],
  1884. Diff: ["diff|patch"],
  1885. Dockerfile: ["^Dockerfile"],
  1886. Dot: ["dot"],
  1887. Drools: ["drl"],
  1888. Edifact: ["edi"],
  1889. Eiffel: ["e|ge"],
  1890. EJS: ["ejs"],
  1891. Elixir: ["ex|exs"],
  1892. Elm: ["elm"],
  1893. Erlang: ["erl|hrl"],
  1894. Forth: ["frt|fs|ldr|fth|4th"],
  1895. Fortran: ["f|f90"],
  1896. FSharp: ["fsi|fs|ml|mli|fsx|fsscript"],
  1897. FSL: ["fsl"],
  1898. FTL: ["ftl"],
  1899. Gcode: ["gcode"],
  1900. Gherkin: ["feature"],
  1901. Gitignore: ["^.gitignore"],
  1902. Glsl: ["glsl|frag|vert"],
  1903. Gobstones: ["gbs"],
  1904. golang: ["go"],
  1905. GraphQLSchema: ["gql"],
  1906. Groovy: ["groovy"],
  1907. HAML: ["haml"],
  1908. Handlebars: ["hbs|handlebars|tpl|mustache"],
  1909. Haskell: ["hs"],
  1910. Haskell_Cabal: ["cabal"],
  1911. haXe: ["hx"],
  1912. Hjson: ["hjson"],
  1913. HTML: ["html|htm|xhtml|vue|we|wpy"],
  1914. HTML_Elixir: ["eex|html.eex"],
  1915. HTML_Ruby: ["erb|rhtml|html.erb"],
  1916. INI: ["ini|conf|cfg|prefs"],
  1917. Io: ["io"],
  1918. Ion: ["ion"],
  1919. Jack: ["jack"],
  1920. Jade: ["jade|pug"],
  1921. Java: ["java"],
  1922. JavaScript: ["js|jsm|jsx|cjs|mjs"],
  1923. JSON: ["json"],
  1924. JSON5: ["json5"],
  1925. JSONiq: ["jq"],
  1926. JSP: ["jsp"],
  1927. JSSM: ["jssm|jssm_state"],
  1928. JSX: ["jsx"],
  1929. Julia: ["jl"],
  1930. Kotlin: ["kt|kts"],
  1931. LaTeX: ["tex|latex|ltx|bib"],
  1932. Latte: ["latte"],
  1933. LESS: ["less"],
  1934. Liquid: ["liquid"],
  1935. Lisp: ["lisp"],
  1936. LiveScript: ["ls"],
  1937. Log: ["log"],
  1938. LogiQL: ["logic|lql"],
  1939. LSL: ["lsl"],
  1940. Lua: ["lua"],
  1941. LuaPage: ["lp"],
  1942. Lucene: ["lucene"],
  1943. Makefile: ["^Makefile|^GNUmakefile|^makefile|^OCamlMakefile|make"],
  1944. Markdown: ["md|markdown"],
  1945. Mask: ["mask"],
  1946. MATLAB: ["matlab"],
  1947. Maze: ["mz"],
  1948. MediaWiki: ["wiki|mediawiki"],
  1949. MEL: ["mel"],
  1950. MIPS: ["s|asm"],
  1951. MIXAL: ["mixal"],
  1952. MUSHCode: ["mc|mush"],
  1953. MySQL: ["mysql"],
  1954. Nginx: ["nginx|conf"],
  1955. Nim: ["nim"],
  1956. Nix: ["nix"],
  1957. NSIS: ["nsi|nsh"],
  1958. Nunjucks: ["nunjucks|nunjs|nj|njk"],
  1959. ObjectiveC: ["m|mm"],
  1960. OCaml: ["ml|mli"],
  1961. PartiQL: ["partiql|pql"],
  1962. Pascal: ["pas|p"],
  1963. Perl: ["pl|pm"],
  1964. pgSQL: ["pgsql"],
  1965. PHP_Laravel_blade: ["blade.php"],
  1966. PHP: ["php|inc|phtml|shtml|php3|php4|php5|phps|phpt|aw|ctp|module"],
  1967. Pig: ["pig"],
  1968. Powershell: ["ps1"],
  1969. Praat: ["praat|praatscript|psc|proc"],
  1970. Prisma: ["prisma"],
  1971. Prolog: ["plg|prolog"],
  1972. Properties: ["properties"],
  1973. Protobuf: ["proto"],
  1974. Puppet: ["epp|pp"],
  1975. Python: ["py"],
  1976. QML: ["qml"],
  1977. R: ["r"],
  1978. Raku: ["raku|rakumod|rakutest|p6|pl6|pm6"],
  1979. Razor: ["cshtml|asp"],
  1980. RDoc: ["Rd"],
  1981. Red: ["red|reds"],
  1982. RHTML: ["Rhtml"],
  1983. Robot: ["robot|resource"],
  1984. RST: ["rst"],
  1985. Ruby: ["rb|ru|gemspec|rake|^Guardfile|^Rakefile|^Gemfile"],
  1986. Rust: ["rs"],
  1987. SaC: ["sac"],
  1988. SASS: ["sass"],
  1989. SCAD: ["scad"],
  1990. Scala: ["scala|sbt"],
  1991. Scheme: ["scm|sm|rkt|oak|scheme"],
  1992. Scrypt: ["scrypt"],
  1993. SCSS: ["scss"],
  1994. SH: ["sh|bash|^.bashrc"],
  1995. SJS: ["sjs"],
  1996. Slim: ["slim|skim"],
  1997. Smarty: ["smarty|tpl"],
  1998. Smithy: ["smithy"],
  1999. snippets: ["snippets"],
  2000. Soy_Template: ["soy"],
  2001. Space: ["space"],
  2002. SQL: ["sql"],
  2003. SQLServer: ["sqlserver"],
  2004. Stylus: ["styl|stylus"],
  2005. SVG: ["svg"],
  2006. Swift: ["swift"],
  2007. Tcl: ["tcl"],
  2008. Terraform: ["tf", "tfvars", "terragrunt"],
  2009. Tex: ["tex"],
  2010. Text: ["txt"],
  2011. Textile: ["textile"],
  2012. Toml: ["toml"],
  2013. TSX: ["tsx"],
  2014. Twig: ["twig|swig"],
  2015. Typescript: ["ts|typescript|str"],
  2016. Vala: ["vala"],
  2017. VBScript: ["vbs|vb"],
  2018. Velocity: ["vm"],
  2019. Verilog: ["v|vh|sv|svh"],
  2020. VHDL: ["vhd|vhdl"],
  2021. Visualforce: ["vfp|component|page"],
  2022. Wollok: ["wlk|wpgm|wtest"],
  2023. XML: ["xml|rdf|rss|wsdl|xslt|atom|mathml|mml|xul|xbl|xaml"],
  2024. XQuery: ["xq"],
  2025. YAML: ["yaml|yml"],
  2026. Zeek: ["zeek|bro"],
  2027. Django: ["html"]
  2028. };
  2029. var nameOverrides = {
  2030. ObjectiveC: "Objective-C",
  2031. CSharp: "C#",
  2032. golang: "Go",
  2033. C_Cpp: "C and C++",
  2034. Csound_Document: "Csound Document",
  2035. Csound_Orchestra: "Csound",
  2036. Csound_Score: "Csound Score",
  2037. coffee: "CoffeeScript",
  2038. HTML_Ruby: "HTML (Ruby)",
  2039. HTML_Elixir: "HTML (Elixir)",
  2040. FTL: "FreeMarker",
  2041. PHP_Laravel_blade: "PHP (Blade Template)",
  2042. Perl6: "Perl 6",
  2043. AutoHotKey: "AutoHotkey / AutoIt"
  2044. };
  2045. var modesByName = {};
  2046. for (var name in supportedModes) {
  2047. var data = supportedModes[name];
  2048. var displayName = (nameOverrides[name] || name).replace(/_/g, " ");
  2049. var filename = name.toLowerCase();
  2050. var mode = new Mode(filename, displayName, data[0]);
  2051. modesByName[filename] = mode;
  2052. modes.push(mode);
  2053. }
  2054. module.exports = {
  2055. getModeForPath: getModeForPath,
  2056. modes: modes,
  2057. modesByName: modesByName
  2058. };
  2059. });
  2060. ace.define("ace/ext/prompt",["require","exports","module","ace/range","ace/lib/dom","ace/ext/menu_tools/get_editor_keyboard_shortcuts","ace/autocomplete","ace/autocomplete/popup","ace/autocomplete/popup","ace/undomanager","ace/tokenizer","ace/ext/menu_tools/overlay_page","ace/ext/modelist"], function(require, exports, module){/**
  2061. * Prompt plugin is used for getting input from user.
  2062. *
  2063. * @param {Object} editor Ouside editor related to this prompt. Will be blurred when prompt is open.
  2064. * @param {String} message Predefined value of prompt input box.
  2065. * @param {Object} options Cusomizable options for this prompt.
  2066. * @param {String} options.name Prompt name.
  2067. * @param {String} options.$type Use prompt of specific type (gotoLine|commands|modes or default if empty).
  2068. * @param {[start, end]} options.selection Defines which part of the predefined value should be highlited.
  2069. * @param {Boolean} options.hasDescription Set to true if prompt has description below input box.
  2070. * @param {String} options.prompt Description below input box.
  2071. * @param {String} options.placeholder Placeholder for value.
  2072. * @param {Object} options.$rules Specific rules for input like password or regexp.
  2073. * @param {Boolean} options.ignoreFocusOut Set to true to keep the prompt open when focus moves to another part of the editor.
  2074. * @param {Function} options.getCompletions Function for defining list of options for value.
  2075. * @param {Function} options.getPrefix Function for defining current value prefix.
  2076. * @param {Function} options.onAccept Function called when Enter is pressed.
  2077. * @param {Function} options.onInput Function called when input is added to prompt input box.
  2078. * @param {Function} options.onCancel Function called when Esc|Shift-Esc is pressed.
  2079. * @param {Function} callback Function called after done.
  2080. * */
  2081. "use strict";
  2082. var Range = require("../range").Range;
  2083. var dom = require("../lib/dom");
  2084. var shortcuts = require("../ext/menu_tools/get_editor_keyboard_shortcuts");
  2085. var FilteredList = require("../autocomplete").FilteredList;
  2086. var AcePopup = require('../autocomplete/popup').AcePopup;
  2087. var $singleLineEditor = require('../autocomplete/popup').$singleLineEditor;
  2088. var UndoManager = require("../undomanager").UndoManager;
  2089. var Tokenizer = require("../tokenizer").Tokenizer;
  2090. var overlayPage = require("./menu_tools/overlay_page").overlayPage;
  2091. var modelist = require("./modelist");
  2092. var openPrompt;
  2093. function prompt(editor, message, options, callback) {
  2094. if (typeof message == "object") {
  2095. return prompt(editor, "", message, options);
  2096. }
  2097. if (openPrompt) {
  2098. var lastPrompt = openPrompt;
  2099. editor = lastPrompt.editor;
  2100. lastPrompt.close();
  2101. if (lastPrompt.name && lastPrompt.name == options.name)
  2102. return;
  2103. }
  2104. if (options.$type)
  2105. return prompt[options.$type](editor, callback);
  2106. var cmdLine = $singleLineEditor();
  2107. cmdLine.session.setUndoManager(new UndoManager());
  2108. var el = dom.buildDom(["div", { class: "ace_prompt_container" + (options.hasDescription ? " input-box-with-description" : "") }]);
  2109. var overlay = overlayPage(editor, el, done);
  2110. el.appendChild(cmdLine.container);
  2111. if (editor) {
  2112. editor.cmdLine = cmdLine;
  2113. cmdLine.setOption("fontSize", editor.getOption("fontSize"));
  2114. }
  2115. if (message) {
  2116. cmdLine.setValue(message, 1);
  2117. }
  2118. if (options.selection) {
  2119. cmdLine.selection.setRange({
  2120. start: cmdLine.session.doc.indexToPosition(options.selection[0]),
  2121. end: cmdLine.session.doc.indexToPosition(options.selection[1])
  2122. });
  2123. }
  2124. if (options.getCompletions) {
  2125. var popup = new AcePopup();
  2126. popup.renderer.setStyle("ace_autocomplete_inline");
  2127. popup.container.style.display = "block";
  2128. popup.container.style.maxWidth = "600px";
  2129. popup.container.style.width = "100%";
  2130. popup.container.style.marginTop = "3px";
  2131. popup.renderer.setScrollMargin(2, 2, 0, 0);
  2132. popup.autoSelect = false;
  2133. popup.renderer.$maxLines = 15;
  2134. popup.setRow(-1);
  2135. popup.on("click", function (e) {
  2136. var data = popup.getData(popup.getRow());
  2137. if (!data.error) {
  2138. cmdLine.setValue(data.value || data.name || data);
  2139. accept();
  2140. e.stop();
  2141. }
  2142. });
  2143. el.appendChild(popup.container);
  2144. updateCompletions();
  2145. }
  2146. if (options.$rules) {
  2147. var tokenizer = new Tokenizer(options.$rules);
  2148. cmdLine.session.bgTokenizer.setTokenizer(tokenizer);
  2149. }
  2150. if (options.placeholder) {
  2151. cmdLine.setOption("placeholder", options.placeholder);
  2152. }
  2153. if (options.hasDescription) {
  2154. var promptTextContainer = dom.buildDom(["div", { class: "ace_prompt_text_container" }]);
  2155. dom.buildDom(options.prompt || "Press 'Enter' to confirm or 'Escape' to cancel", promptTextContainer);
  2156. el.appendChild(promptTextContainer);
  2157. }
  2158. overlay.setIgnoreFocusOut(options.ignoreFocusOut);
  2159. function accept() {
  2160. var val;
  2161. if (popup && popup.getCursorPosition().row > 0) {
  2162. val = valueFromRecentList();
  2163. }
  2164. else {
  2165. val = cmdLine.getValue();
  2166. }
  2167. var curData = popup ? popup.getData(popup.getRow()) : val;
  2168. if (curData && !curData.error) {
  2169. done();
  2170. options.onAccept && options.onAccept({
  2171. value: val,
  2172. item: curData
  2173. }, cmdLine);
  2174. }
  2175. }
  2176. var keys = {
  2177. "Enter": accept,
  2178. "Esc|Shift-Esc": function () {
  2179. options.onCancel && options.onCancel(cmdLine.getValue(), cmdLine);
  2180. done();
  2181. }
  2182. };
  2183. if (popup) {
  2184. Object.assign(keys, {
  2185. "Up": function (editor) { popup.goTo("up"); valueFromRecentList(); },
  2186. "Down": function (editor) { popup.goTo("down"); valueFromRecentList(); },
  2187. "Ctrl-Up|Ctrl-Home": function (editor) { popup.goTo("start"); valueFromRecentList(); },
  2188. "Ctrl-Down|Ctrl-End": function (editor) { popup.goTo("end"); valueFromRecentList(); },
  2189. "Tab": function (editor) {
  2190. popup.goTo("down");
  2191. valueFromRecentList();
  2192. },
  2193. "PageUp": function (editor) { popup.gotoPageUp(); valueFromRecentList(); },
  2194. "PageDown": function (editor) { popup.gotoPageDown(); valueFromRecentList(); }
  2195. });
  2196. }
  2197. cmdLine.commands.bindKeys(keys);
  2198. function done() {
  2199. overlay.close();
  2200. callback && callback();
  2201. openPrompt = null;
  2202. }
  2203. cmdLine.on("input", function () {
  2204. options.onInput && options.onInput();
  2205. updateCompletions();
  2206. });
  2207. function updateCompletions() {
  2208. if (options.getCompletions) {
  2209. var prefix;
  2210. if (options.getPrefix) {
  2211. prefix = options.getPrefix(cmdLine);
  2212. }
  2213. var completions = options.getCompletions(cmdLine);
  2214. popup.setData(completions, prefix);
  2215. popup.resize(true);
  2216. }
  2217. }
  2218. function valueFromRecentList() {
  2219. var current = popup.getData(popup.getRow());
  2220. if (current && !current.error)
  2221. return current.value || current.caption || current;
  2222. }
  2223. cmdLine.resize(true);
  2224. if (popup) {
  2225. popup.resize(true);
  2226. }
  2227. cmdLine.focus();
  2228. openPrompt = {
  2229. close: done,
  2230. name: options.name,
  2231. editor: editor
  2232. };
  2233. }
  2234. prompt.gotoLine = function (editor, callback) {
  2235. function stringifySelection(selection) {
  2236. if (!Array.isArray(selection))
  2237. selection = [selection];
  2238. return selection.map(function (r) {
  2239. var cursor = r.isBackwards ? r.start : r.end;
  2240. var anchor = r.isBackwards ? r.end : r.start;
  2241. var row = anchor.row;
  2242. var s = (row + 1) + ":" + anchor.column;
  2243. if (anchor.row == cursor.row) {
  2244. if (anchor.column != cursor.column)
  2245. s += ">" + ":" + cursor.column;
  2246. }
  2247. else {
  2248. s += ">" + (cursor.row + 1) + ":" + cursor.column;
  2249. }
  2250. return s;
  2251. }).reverse().join(", ");
  2252. }
  2253. prompt(editor, ":" + stringifySelection(editor.selection.toJSON()), {
  2254. name: "gotoLine",
  2255. selection: [1, Number.MAX_VALUE],
  2256. onAccept: function (data) {
  2257. var value = data.value;
  2258. var _history = prompt.gotoLine._history;
  2259. if (!_history)
  2260. prompt.gotoLine._history = _history = [];
  2261. if (_history.indexOf(value) != -1)
  2262. _history.splice(_history.indexOf(value), 1);
  2263. _history.unshift(value);
  2264. if (_history.length > 20)
  2265. _history.length = 20;
  2266. var pos = editor.getCursorPosition();
  2267. var ranges = [];
  2268. value.replace(/^:/, "").split(/,/).map(function (str) {
  2269. var parts = str.split(/([<>:+-]|c?\d+)|[^c\d<>:+-]+/).filter(Boolean);
  2270. var i = 0;
  2271. function readPosition() {
  2272. var c = parts[i++];
  2273. if (!c)
  2274. return;
  2275. if (c[0] == "c") {
  2276. var index = parseInt(c.slice(1)) || 0;
  2277. return editor.session.doc.indexToPosition(index);
  2278. }
  2279. var row = pos.row;
  2280. var column = 0;
  2281. if (/\d/.test(c)) {
  2282. row = parseInt(c) - 1;
  2283. c = parts[i++];
  2284. }
  2285. if (c == ":") {
  2286. c = parts[i++];
  2287. if (/\d/.test(c)) {
  2288. column = parseInt(c) || 0;
  2289. }
  2290. }
  2291. return { row: row, column: column };
  2292. }
  2293. pos = readPosition();
  2294. var range = Range.fromPoints(pos, pos);
  2295. if (parts[i] == ">") {
  2296. i++;
  2297. range.end = readPosition();
  2298. }
  2299. else if (parts[i] == "<") {
  2300. i++;
  2301. range.start = readPosition();
  2302. }
  2303. ranges.unshift(range);
  2304. });
  2305. editor.selection.fromJSON(ranges);
  2306. var scrollTop = editor.renderer.scrollTop;
  2307. editor.renderer.scrollSelectionIntoView(editor.selection.anchor, editor.selection.cursor, 0.5);
  2308. editor.renderer.animateScrolling(scrollTop);
  2309. },
  2310. history: function () {
  2311. var undoManager = editor.session.getUndoManager();
  2312. if (!prompt.gotoLine._history)
  2313. return [];
  2314. return prompt.gotoLine._history;
  2315. },
  2316. getCompletions: function (cmdLine) {
  2317. var value = cmdLine.getValue();
  2318. var m = value.replace(/^:/, "").split(":");
  2319. var row = Math.min(parseInt(m[0]) || 1, editor.session.getLength()) - 1;
  2320. var line = editor.session.getLine(row);
  2321. var current = value + " " + line;
  2322. return [current].concat(this.history());
  2323. },
  2324. $rules: {
  2325. start: [{
  2326. regex: /\d+/,
  2327. token: "string"
  2328. }, {
  2329. regex: /[:,><+\-c]/,
  2330. token: "keyword"
  2331. }]
  2332. }
  2333. });
  2334. };
  2335. prompt.commands = function (editor, callback) {
  2336. function normalizeName(name) {
  2337. return (name || "").replace(/^./, function (x) {
  2338. return x.toUpperCase(x);
  2339. }).replace(/[a-z][A-Z]/g, function (x) {
  2340. return x[0] + " " + x[1].toLowerCase(x);
  2341. });
  2342. }
  2343. function getEditorCommandsByName(excludeCommands) {
  2344. var commandsByName = [];
  2345. var commandMap = {};
  2346. editor.keyBinding.$handlers.forEach(function (handler) {
  2347. var platform = handler.platform;
  2348. var cbn = handler.byName;
  2349. for (var i in cbn) {
  2350. var key = cbn[i].bindKey;
  2351. if (typeof key !== "string") {
  2352. key = key && key[platform] || "";
  2353. }
  2354. var commands = cbn[i];
  2355. var description = commands.description || normalizeName(commands.name);
  2356. if (!Array.isArray(commands))
  2357. commands = [commands];
  2358. commands.forEach(function (command) {
  2359. if (typeof command != "string")
  2360. command = command.name;
  2361. var needle = excludeCommands.find(function (el) {
  2362. return el === command;
  2363. });
  2364. if (!needle) {
  2365. if (commandMap[command]) {
  2366. commandMap[command].key += "|" + key;
  2367. }
  2368. else {
  2369. commandMap[command] = { key: key, command: command, description: description };
  2370. commandsByName.push(commandMap[command]);
  2371. }
  2372. }
  2373. });
  2374. }
  2375. });
  2376. return commandsByName;
  2377. }
  2378. var excludeCommandsList = ["insertstring", "inserttext", "setIndentation", "paste"];
  2379. var shortcutsArray = getEditorCommandsByName(excludeCommandsList);
  2380. shortcutsArray = shortcutsArray.map(function (item) {
  2381. return { value: item.description, meta: item.key, command: item.command };
  2382. });
  2383. prompt(editor, "", {
  2384. name: "commands",
  2385. selection: [0, Number.MAX_VALUE],
  2386. maxHistoryCount: 5,
  2387. onAccept: function (data) {
  2388. if (data.item) {
  2389. var commandName = data.item.command;
  2390. this.addToHistory(data.item);
  2391. editor.execCommand(commandName);
  2392. }
  2393. },
  2394. addToHistory: function (item) {
  2395. var history = this.history();
  2396. history.unshift(item);
  2397. delete item.message;
  2398. for (var i = 1; i < history.length; i++) {
  2399. if (history[i]["command"] == item.command) {
  2400. history.splice(i, 1);
  2401. break;
  2402. }
  2403. }
  2404. if (this.maxHistoryCount > 0 && history.length > this.maxHistoryCount) {
  2405. history.splice(history.length - 1, 1);
  2406. }
  2407. prompt.commands.history = history;
  2408. },
  2409. history: function () {
  2410. return prompt.commands.history || [];
  2411. },
  2412. getPrefix: function (cmdLine) {
  2413. var currentPos = cmdLine.getCursorPosition();
  2414. var filterValue = cmdLine.getValue();
  2415. return filterValue.substring(0, currentPos.column);
  2416. },
  2417. getCompletions: function (cmdLine) {
  2418. function getFilteredCompletions(commands, prefix) {
  2419. var resultCommands = JSON.parse(JSON.stringify(commands));
  2420. var filtered = new FilteredList(resultCommands);
  2421. return filtered.filterCompletions(resultCommands, prefix);
  2422. }
  2423. function getUniqueCommandList(commands, usedCommands) {
  2424. if (!usedCommands || !usedCommands.length) {
  2425. return commands;
  2426. }
  2427. var excludeCommands = [];
  2428. usedCommands.forEach(function (item) {
  2429. excludeCommands.push(item.command);
  2430. });
  2431. var resultCommands = [];
  2432. commands.forEach(function (item) {
  2433. if (excludeCommands.indexOf(item.command) === -1) {
  2434. resultCommands.push(item);
  2435. }
  2436. });
  2437. return resultCommands;
  2438. }
  2439. var prefix = this.getPrefix(cmdLine);
  2440. var recentlyUsedCommands = getFilteredCompletions(this.history(), prefix);
  2441. var otherCommands = getUniqueCommandList(shortcutsArray, recentlyUsedCommands);
  2442. otherCommands = getFilteredCompletions(otherCommands, prefix);
  2443. if (recentlyUsedCommands.length && otherCommands.length) {
  2444. recentlyUsedCommands[0]["message"] = " Recently used";
  2445. otherCommands[0]["message"] = " Other commands";
  2446. }
  2447. var completions = recentlyUsedCommands.concat(otherCommands);
  2448. return completions.length > 0 ? completions : [{
  2449. value: "No matching commands",
  2450. error: 1
  2451. }];
  2452. }
  2453. });
  2454. };
  2455. prompt.modes = function (editor, callback) {
  2456. var modesArray = modelist.modes;
  2457. modesArray = modesArray.map(function (item) {
  2458. return { value: item.caption, mode: item.name };
  2459. });
  2460. prompt(editor, "", {
  2461. name: "modes",
  2462. selection: [0, Number.MAX_VALUE],
  2463. onAccept: function (data) {
  2464. if (data.item) {
  2465. var modeName = "ace/mode/" + data.item.mode;
  2466. editor.session.setMode(modeName);
  2467. }
  2468. },
  2469. getPrefix: function (cmdLine) {
  2470. var currentPos = cmdLine.getCursorPosition();
  2471. var filterValue = cmdLine.getValue();
  2472. return filterValue.substring(0, currentPos.column);
  2473. },
  2474. getCompletions: function (cmdLine) {
  2475. function getFilteredCompletions(modes, prefix) {
  2476. var resultCommands = JSON.parse(JSON.stringify(modes));
  2477. var filtered = new FilteredList(resultCommands);
  2478. return filtered.filterCompletions(resultCommands, prefix);
  2479. }
  2480. var prefix = this.getPrefix(cmdLine);
  2481. var completions = getFilteredCompletions(modesArray, prefix);
  2482. return completions.length > 0 ? completions : [{
  2483. "caption": "No mode matching",
  2484. "value": "No mode matching",
  2485. "error": 1
  2486. }];
  2487. }
  2488. });
  2489. };
  2490. dom.importCssString(".ace_prompt_container {\n max-width: 600px;\n width: 100%;\n margin: 20px auto;\n padding: 3px;\n background: white;\n border-radius: 2px;\n box-shadow: 0px 2px 3px 0px #555;\n}", "promtp.css", false);
  2491. exports.prompt = prompt;
  2492. }); (function() {
  2493. ace.require(["ace/ext/prompt"], function(m) {
  2494. if (typeof module == "object" && typeof exports == "object" && module) {
  2495. module.exports = m;
  2496. }
  2497. });
  2498. })();