ext-prompt.js 95 KB

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