ext-emmet.js 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331
  1. define("ace/snippets",["require","exports","module","ace/lib/dom","ace/lib/oop","ace/lib/event_emitter","ace/lib/lang","ace/range","ace/range_list","ace/keyboard/hash_handler","ace/tokenizer","ace/clipboard","ace/editor"], function(require, exports, module) {
  2. "use strict";
  3. var dom = require("./lib/dom");
  4. var oop = require("./lib/oop");
  5. var EventEmitter = require("./lib/event_emitter").EventEmitter;
  6. var lang = require("./lib/lang");
  7. var Range = require("./range").Range;
  8. var RangeList = require("./range_list").RangeList;
  9. var HashHandler = require("./keyboard/hash_handler").HashHandler;
  10. var Tokenizer = require("./tokenizer").Tokenizer;
  11. var clipboard = require("./clipboard");
  12. var VARIABLES = {
  13. CURRENT_WORD: function(editor) {
  14. return editor.session.getTextRange(editor.session.getWordRange());
  15. },
  16. SELECTION: function(editor, name, indentation) {
  17. var text = editor.session.getTextRange();
  18. if (indentation)
  19. return text.replace(/\n\r?([ \t]*\S)/g, "\n" + indentation + "$1");
  20. return text;
  21. },
  22. CURRENT_LINE: function(editor) {
  23. return editor.session.getLine(editor.getCursorPosition().row);
  24. },
  25. PREV_LINE: function(editor) {
  26. return editor.session.getLine(editor.getCursorPosition().row - 1);
  27. },
  28. LINE_INDEX: function(editor) {
  29. return editor.getCursorPosition().row;
  30. },
  31. LINE_NUMBER: function(editor) {
  32. return editor.getCursorPosition().row + 1;
  33. },
  34. SOFT_TABS: function(editor) {
  35. return editor.session.getUseSoftTabs() ? "YES" : "NO";
  36. },
  37. TAB_SIZE: function(editor) {
  38. return editor.session.getTabSize();
  39. },
  40. CLIPBOARD: function(editor) {
  41. return clipboard.getText && clipboard.getText();
  42. },
  43. FILENAME: function(editor) {
  44. return /[^/\\]*$/.exec(this.FILEPATH(editor))[0];
  45. },
  46. FILENAME_BASE: function(editor) {
  47. return /[^/\\]*$/.exec(this.FILEPATH(editor))[0].replace(/\.[^.]*$/, "");
  48. },
  49. DIRECTORY: function(editor) {
  50. return this.FILEPATH(editor).replace(/[^/\\]*$/, "");
  51. },
  52. FILEPATH: function(editor) { return "/not implemented.txt"; },
  53. WORKSPACE_NAME: function() { return "Unknown"; },
  54. FULLNAME: function() { return "Unknown"; },
  55. BLOCK_COMMENT_START: function(editor) {
  56. var mode = editor.session.$mode || {};
  57. return mode.blockComment && mode.blockComment.start || "";
  58. },
  59. BLOCK_COMMENT_END: function(editor) {
  60. var mode = editor.session.$mode || {};
  61. return mode.blockComment && mode.blockComment.end || "";
  62. },
  63. LINE_COMMENT: function(editor) {
  64. var mode = editor.session.$mode || {};
  65. return mode.lineCommentStart || "";
  66. },
  67. CURRENT_YEAR: date.bind(null, {year: "numeric"}),
  68. CURRENT_YEAR_SHORT: date.bind(null, {year: "2-digit"}),
  69. CURRENT_MONTH: date.bind(null, {month: "numeric"}),
  70. CURRENT_MONTH_NAME: date.bind(null, {month: "long"}),
  71. CURRENT_MONTH_NAME_SHORT: date.bind(null, {month: "short"}),
  72. CURRENT_DATE: date.bind(null, {day: "2-digit"}),
  73. CURRENT_DAY_NAME: date.bind(null, {weekday: "long"}),
  74. CURRENT_DAY_NAME_SHORT: date.bind(null, {weekday: "short"}),
  75. CURRENT_HOUR: date.bind(null, {hour: "2-digit", hour12: false}),
  76. CURRENT_MINUTE: date.bind(null, {minute: "2-digit"}),
  77. CURRENT_SECOND: date.bind(null, {second: "2-digit"})
  78. };
  79. VARIABLES.SELECTED_TEXT = VARIABLES.SELECTION;
  80. function date(dateFormat) {
  81. var str = new Date().toLocaleString("en-us", dateFormat);
  82. return str.length == 1 ? "0" + str : str;
  83. }
  84. var SnippetManager = function() {
  85. this.snippetMap = {};
  86. this.snippetNameMap = {};
  87. };
  88. (function() {
  89. oop.implement(this, EventEmitter);
  90. this.getTokenizer = function() {
  91. return SnippetManager.$tokenizer || this.createTokenizer();
  92. };
  93. this.createTokenizer = function() {
  94. function TabstopToken(str) {
  95. str = str.substr(1);
  96. if (/^\d+$/.test(str))
  97. return [{tabstopId: parseInt(str, 10)}];
  98. return [{text: str}];
  99. }
  100. function escape(ch) {
  101. return "(?:[^\\\\" + ch + "]|\\\\.)";
  102. }
  103. var formatMatcher = {
  104. regex: "/(" + escape("/") + "+)/",
  105. onMatch: function(val, state, stack) {
  106. var ts = stack[0];
  107. ts.fmtString = true;
  108. ts.guard = val.slice(1, -1);
  109. ts.flag = "";
  110. return "";
  111. },
  112. next: "formatString"
  113. };
  114. SnippetManager.$tokenizer = new Tokenizer({
  115. start: [
  116. {regex: /\\./, onMatch: function(val, state, stack) {
  117. var ch = val[1];
  118. if (ch == "}" && stack.length) {
  119. val = ch;
  120. } else if ("`$\\".indexOf(ch) != -1) {
  121. val = ch;
  122. }
  123. return [val];
  124. }},
  125. {regex: /}/, onMatch: function(val, state, stack) {
  126. return [stack.length ? stack.shift() : val];
  127. }},
  128. {regex: /\$(?:\d+|\w+)/, onMatch: TabstopToken},
  129. {regex: /\$\{[\dA-Z_a-z]+/, onMatch: function(str, state, stack) {
  130. var t = TabstopToken(str.substr(1));
  131. stack.unshift(t[0]);
  132. return t;
  133. }, next: "snippetVar"},
  134. {regex: /\n/, token: "newline", merge: false}
  135. ],
  136. snippetVar: [
  137. {regex: "\\|" + escape("\\|") + "*\\|", onMatch: function(val, state, stack) {
  138. var choices = val.slice(1, -1).replace(/\\[,|\\]|,/g, function(operator) {
  139. return operator.length == 2 ? operator[1] : "\x00";
  140. }).split("\x00").map(function(value){
  141. return {value: value};
  142. });
  143. stack[0].choices = choices;
  144. return [choices[0]];
  145. }, next: "start"},
  146. formatMatcher,
  147. {regex: "([^:}\\\\]|\\\\.)*:?", token: "", next: "start"}
  148. ],
  149. formatString: [
  150. {regex: /:/, onMatch: function(val, state, stack) {
  151. if (stack.length && stack[0].expectElse) {
  152. stack[0].expectElse = false;
  153. stack[0].ifEnd = {elseEnd: stack[0]};
  154. return [stack[0].ifEnd];
  155. }
  156. return ":";
  157. }},
  158. {regex: /\\./, onMatch: function(val, state, stack) {
  159. var ch = val[1];
  160. if (ch == "}" && stack.length)
  161. val = ch;
  162. else if ("`$\\".indexOf(ch) != -1)
  163. val = ch;
  164. else if (ch == "n")
  165. val = "\n";
  166. else if (ch == "t")
  167. val = "\t";
  168. else if ("ulULE".indexOf(ch) != -1)
  169. val = {changeCase: ch, local: ch > "a"};
  170. return [val];
  171. }},
  172. {regex: "/\\w*}", onMatch: function(val, state, stack) {
  173. var next = stack.shift();
  174. if (next)
  175. next.flag = val.slice(1, -1);
  176. this.next = next && next.tabstopId ? "start" : "";
  177. return [next || val];
  178. }, next: "start"},
  179. {regex: /\$(?:\d+|\w+)/, onMatch: function(val, state, stack) {
  180. return [{text: val.slice(1)}];
  181. }},
  182. {regex: /\${\w+/, onMatch: function(val, state, stack) {
  183. var token = {text: val.slice(2)};
  184. stack.unshift(token);
  185. return [token];
  186. }, next: "formatStringVar"},
  187. {regex: /\n/, token: "newline", merge: false},
  188. {regex: /}/, onMatch: function(val, state, stack) {
  189. var next = stack.shift();
  190. this.next = next && next.tabstopId ? "start" : "";
  191. return [next || val];
  192. }, next: "start"}
  193. ],
  194. formatStringVar: [
  195. {regex: /:\/\w+}/, onMatch: function(val, state, stack) {
  196. var ts = stack[0];
  197. ts.formatFunction = val.slice(2, -1);
  198. return [stack.shift()];
  199. }, next: "formatString"},
  200. formatMatcher,
  201. {regex: /:[\?\-+]?/, onMatch: function(val, state, stack) {
  202. if (val[1] == "+")
  203. stack[0].ifEnd = stack[0];
  204. if (val[1] == "?")
  205. stack[0].expectElse = true;
  206. }, next: "formatString"},
  207. {regex: "([^:}\\\\]|\\\\.)*:?", token: "", next: "formatString"}
  208. ]
  209. });
  210. return SnippetManager.$tokenizer;
  211. };
  212. this.tokenizeTmSnippet = function(str, startState) {
  213. return this.getTokenizer().getLineTokens(str, startState).tokens.map(function(x) {
  214. return x.value || x;
  215. });
  216. };
  217. this.getVariableValue = function(editor, name, indentation) {
  218. if (/^\d+$/.test(name))
  219. return (this.variables.__ || {})[name] || "";
  220. if (/^[A-Z]\d+$/.test(name))
  221. return (this.variables[name[0] + "__"] || {})[name.substr(1)] || "";
  222. name = name.replace(/^TM_/, "");
  223. if (!this.variables.hasOwnProperty(name))
  224. return "";
  225. var value = this.variables[name];
  226. if (typeof value == "function")
  227. value = this.variables[name](editor, name, indentation);
  228. return value == null ? "" : value;
  229. };
  230. this.variables = VARIABLES;
  231. this.tmStrFormat = function(str, ch, editor) {
  232. if (!ch.fmt) return str;
  233. var flag = ch.flag || "";
  234. var re = ch.guard;
  235. re = new RegExp(re, flag.replace(/[^gim]/g, ""));
  236. var fmtTokens = typeof ch.fmt == "string" ? this.tokenizeTmSnippet(ch.fmt, "formatString") : ch.fmt;
  237. var _self = this;
  238. var formatted = str.replace(re, function() {
  239. var oldArgs = _self.variables.__;
  240. _self.variables.__ = [].slice.call(arguments);
  241. var fmtParts = _self.resolveVariables(fmtTokens, editor);
  242. var gChangeCase = "E";
  243. for (var i = 0; i < fmtParts.length; i++) {
  244. var ch = fmtParts[i];
  245. if (typeof ch == "object") {
  246. fmtParts[i] = "";
  247. if (ch.changeCase && ch.local) {
  248. var next = fmtParts[i + 1];
  249. if (next && typeof next == "string") {
  250. if (ch.changeCase == "u")
  251. fmtParts[i] = next[0].toUpperCase();
  252. else
  253. fmtParts[i] = next[0].toLowerCase();
  254. fmtParts[i + 1] = next.substr(1);
  255. }
  256. } else if (ch.changeCase) {
  257. gChangeCase = ch.changeCase;
  258. }
  259. } else if (gChangeCase == "U") {
  260. fmtParts[i] = ch.toUpperCase();
  261. } else if (gChangeCase == "L") {
  262. fmtParts[i] = ch.toLowerCase();
  263. }
  264. }
  265. _self.variables.__ = oldArgs;
  266. return fmtParts.join("");
  267. });
  268. return formatted;
  269. };
  270. this.tmFormatFunction = function(str, ch, editor) {
  271. if (ch.formatFunction == "upcase")
  272. return str.toUpperCase();
  273. if (ch.formatFunction == "downcase")
  274. return str.toLowerCase();
  275. return str;
  276. };
  277. this.resolveVariables = function(snippet, editor) {
  278. var result = [];
  279. var indentation = "";
  280. var afterNewLine = true;
  281. for (var i = 0; i < snippet.length; i++) {
  282. var ch = snippet[i];
  283. if (typeof ch == "string") {
  284. result.push(ch);
  285. if (ch == "\n") {
  286. afterNewLine = true;
  287. indentation = "";
  288. }
  289. else if (afterNewLine) {
  290. indentation = /^\t*/.exec(ch)[0];
  291. afterNewLine = /\S/.test(ch);
  292. }
  293. continue;
  294. }
  295. if (!ch) continue;
  296. afterNewLine = false;
  297. if (ch.fmtString) {
  298. var j = snippet.indexOf(ch, i + 1);
  299. if (j == -1) j = snippet.length;
  300. ch.fmt = snippet.slice(i + 1, j);
  301. i = j;
  302. }
  303. if (ch.text) {
  304. var value = this.getVariableValue(editor, ch.text, indentation) + "";
  305. if (ch.fmtString)
  306. value = this.tmStrFormat(value, ch, editor);
  307. if (ch.formatFunction)
  308. value = this.tmFormatFunction(value, ch, editor);
  309. if (value && !ch.ifEnd) {
  310. result.push(value);
  311. gotoNext(ch);
  312. } else if (!value && ch.ifEnd) {
  313. gotoNext(ch.ifEnd);
  314. }
  315. } else if (ch.elseEnd) {
  316. gotoNext(ch.elseEnd);
  317. } else if (ch.tabstopId != null) {
  318. result.push(ch);
  319. } else if (ch.changeCase != null) {
  320. result.push(ch);
  321. }
  322. }
  323. function gotoNext(ch) {
  324. var i1 = snippet.indexOf(ch, i + 1);
  325. if (i1 != -1)
  326. i = i1;
  327. }
  328. return result;
  329. };
  330. this.insertSnippetForSelection = function(editor, snippetText) {
  331. var cursor = editor.getCursorPosition();
  332. var line = editor.session.getLine(cursor.row);
  333. var tabString = editor.session.getTabString();
  334. var indentString = line.match(/^\s*/)[0];
  335. if (cursor.column < indentString.length)
  336. indentString = indentString.slice(0, cursor.column);
  337. snippetText = snippetText.replace(/\r/g, "");
  338. var tokens = this.tokenizeTmSnippet(snippetText);
  339. tokens = this.resolveVariables(tokens, editor);
  340. tokens = tokens.map(function(x) {
  341. if (x == "\n")
  342. return x + indentString;
  343. if (typeof x == "string")
  344. return x.replace(/\t/g, tabString);
  345. return x;
  346. });
  347. var tabstops = [];
  348. tokens.forEach(function(p, i) {
  349. if (typeof p != "object")
  350. return;
  351. var id = p.tabstopId;
  352. var ts = tabstops[id];
  353. if (!ts) {
  354. ts = tabstops[id] = [];
  355. ts.index = id;
  356. ts.value = "";
  357. ts.parents = {};
  358. }
  359. if (ts.indexOf(p) !== -1)
  360. return;
  361. if (p.choices && !ts.choices)
  362. ts.choices = p.choices;
  363. ts.push(p);
  364. var i1 = tokens.indexOf(p, i + 1);
  365. if (i1 === -1)
  366. return;
  367. var value = tokens.slice(i + 1, i1);
  368. var isNested = value.some(function(t) {return typeof t === "object";});
  369. if (isNested && !ts.value) {
  370. ts.value = value;
  371. } else if (value.length && (!ts.value || typeof ts.value !== "string")) {
  372. ts.value = value.join("");
  373. }
  374. });
  375. tabstops.forEach(function(ts) {ts.length = 0;});
  376. var expanding = {};
  377. function copyValue(val) {
  378. var copy = [];
  379. for (var i = 0; i < val.length; i++) {
  380. var p = val[i];
  381. if (typeof p == "object") {
  382. if (expanding[p.tabstopId])
  383. continue;
  384. var j = val.lastIndexOf(p, i - 1);
  385. p = copy[j] || {tabstopId: p.tabstopId};
  386. }
  387. copy[i] = p;
  388. }
  389. return copy;
  390. }
  391. for (var i = 0; i < tokens.length; i++) {
  392. var p = tokens[i];
  393. if (typeof p != "object")
  394. continue;
  395. var id = p.tabstopId;
  396. var ts = tabstops[id];
  397. var i1 = tokens.indexOf(p, i + 1);
  398. if (expanding[id]) {
  399. if (expanding[id] === p) {
  400. delete expanding[id];
  401. Object.keys(expanding).forEach(function(parentId) {
  402. ts.parents[parentId] = true;
  403. });
  404. }
  405. continue;
  406. }
  407. expanding[id] = p;
  408. var value = ts.value;
  409. if (typeof value !== "string")
  410. value = copyValue(value);
  411. else if (p.fmt)
  412. value = this.tmStrFormat(value, p, editor);
  413. tokens.splice.apply(tokens, [i + 1, Math.max(0, i1 - i)].concat(value, p));
  414. if (ts.indexOf(p) === -1)
  415. ts.push(p);
  416. }
  417. var row = 0, column = 0;
  418. var text = "";
  419. tokens.forEach(function(t) {
  420. if (typeof t === "string") {
  421. var lines = t.split("\n");
  422. if (lines.length > 1){
  423. column = lines[lines.length - 1].length;
  424. row += lines.length - 1;
  425. } else
  426. column += t.length;
  427. text += t;
  428. } else if (t) {
  429. if (!t.start)
  430. t.start = {row: row, column: column};
  431. else
  432. t.end = {row: row, column: column};
  433. }
  434. });
  435. var range = editor.getSelectionRange();
  436. var end = editor.session.replace(range, text);
  437. var tabstopManager = new TabstopManager(editor);
  438. var selectionId = editor.inVirtualSelectionMode && editor.selection.index;
  439. tabstopManager.addTabstops(tabstops, range.start, end, selectionId);
  440. };
  441. this.insertSnippet = function(editor, snippetText) {
  442. var self = this;
  443. if (editor.inVirtualSelectionMode)
  444. return self.insertSnippetForSelection(editor, snippetText);
  445. editor.forEachSelection(function() {
  446. self.insertSnippetForSelection(editor, snippetText);
  447. }, null, {keepOrder: true});
  448. if (editor.tabstopManager)
  449. editor.tabstopManager.tabNext();
  450. };
  451. this.$getScope = function(editor) {
  452. var scope = editor.session.$mode.$id || "";
  453. scope = scope.split("/").pop();
  454. if (scope === "html" || scope === "php") {
  455. if (scope === "php" && !editor.session.$mode.inlinePhp)
  456. scope = "html";
  457. var c = editor.getCursorPosition();
  458. var state = editor.session.getState(c.row);
  459. if (typeof state === "object") {
  460. state = state[0];
  461. }
  462. if (state.substring) {
  463. if (state.substring(0, 3) == "js-")
  464. scope = "javascript";
  465. else if (state.substring(0, 4) == "css-")
  466. scope = "css";
  467. else if (state.substring(0, 4) == "php-")
  468. scope = "php";
  469. }
  470. }
  471. return scope;
  472. };
  473. this.getActiveScopes = function(editor) {
  474. var scope = this.$getScope(editor);
  475. var scopes = [scope];
  476. var snippetMap = this.snippetMap;
  477. if (snippetMap[scope] && snippetMap[scope].includeScopes) {
  478. scopes.push.apply(scopes, snippetMap[scope].includeScopes);
  479. }
  480. scopes.push("_");
  481. return scopes;
  482. };
  483. this.expandWithTab = function(editor, options) {
  484. var self = this;
  485. var result = editor.forEachSelection(function() {
  486. return self.expandSnippetForSelection(editor, options);
  487. }, null, {keepOrder: true});
  488. if (result && editor.tabstopManager)
  489. editor.tabstopManager.tabNext();
  490. return result;
  491. };
  492. this.expandSnippetForSelection = function(editor, options) {
  493. var cursor = editor.getCursorPosition();
  494. var line = editor.session.getLine(cursor.row);
  495. var before = line.substring(0, cursor.column);
  496. var after = line.substr(cursor.column);
  497. var snippetMap = this.snippetMap;
  498. var snippet;
  499. this.getActiveScopes(editor).some(function(scope) {
  500. var snippets = snippetMap[scope];
  501. if (snippets)
  502. snippet = this.findMatchingSnippet(snippets, before, after);
  503. return !!snippet;
  504. }, this);
  505. if (!snippet)
  506. return false;
  507. if (options && options.dryRun)
  508. return true;
  509. editor.session.doc.removeInLine(cursor.row,
  510. cursor.column - snippet.replaceBefore.length,
  511. cursor.column + snippet.replaceAfter.length
  512. );
  513. this.variables.M__ = snippet.matchBefore;
  514. this.variables.T__ = snippet.matchAfter;
  515. this.insertSnippetForSelection(editor, snippet.content);
  516. this.variables.M__ = this.variables.T__ = null;
  517. return true;
  518. };
  519. this.findMatchingSnippet = function(snippetList, before, after) {
  520. for (var i = snippetList.length; i--;) {
  521. var s = snippetList[i];
  522. if (s.startRe && !s.startRe.test(before))
  523. continue;
  524. if (s.endRe && !s.endRe.test(after))
  525. continue;
  526. if (!s.startRe && !s.endRe)
  527. continue;
  528. s.matchBefore = s.startRe ? s.startRe.exec(before) : [""];
  529. s.matchAfter = s.endRe ? s.endRe.exec(after) : [""];
  530. s.replaceBefore = s.triggerRe ? s.triggerRe.exec(before)[0] : "";
  531. s.replaceAfter = s.endTriggerRe ? s.endTriggerRe.exec(after)[0] : "";
  532. return s;
  533. }
  534. };
  535. this.snippetMap = {};
  536. this.snippetNameMap = {};
  537. this.register = function(snippets, scope) {
  538. var snippetMap = this.snippetMap;
  539. var snippetNameMap = this.snippetNameMap;
  540. var self = this;
  541. if (!snippets)
  542. snippets = [];
  543. function wrapRegexp(src) {
  544. if (src && !/^\^?\(.*\)\$?$|^\\b$/.test(src))
  545. src = "(?:" + src + ")";
  546. return src || "";
  547. }
  548. function guardedRegexp(re, guard, opening) {
  549. re = wrapRegexp(re);
  550. guard = wrapRegexp(guard);
  551. if (opening) {
  552. re = guard + re;
  553. if (re && re[re.length - 1] != "$")
  554. re = re + "$";
  555. } else {
  556. re = re + guard;
  557. if (re && re[0] != "^")
  558. re = "^" + re;
  559. }
  560. return new RegExp(re);
  561. }
  562. function addSnippet(s) {
  563. if (!s.scope)
  564. s.scope = scope || "_";
  565. scope = s.scope;
  566. if (!snippetMap[scope]) {
  567. snippetMap[scope] = [];
  568. snippetNameMap[scope] = {};
  569. }
  570. var map = snippetNameMap[scope];
  571. if (s.name) {
  572. var old = map[s.name];
  573. if (old)
  574. self.unregister(old);
  575. map[s.name] = s;
  576. }
  577. snippetMap[scope].push(s);
  578. if (s.prefix)
  579. s.tabTrigger = s.prefix;
  580. if (!s.content && s.body)
  581. s.content = Array.isArray(s.body) ? s.body.join("\n") : s.body;
  582. if (s.tabTrigger && !s.trigger) {
  583. if (!s.guard && /^\w/.test(s.tabTrigger))
  584. s.guard = "\\b";
  585. s.trigger = lang.escapeRegExp(s.tabTrigger);
  586. }
  587. if (!s.trigger && !s.guard && !s.endTrigger && !s.endGuard)
  588. return;
  589. s.startRe = guardedRegexp(s.trigger, s.guard, true);
  590. s.triggerRe = new RegExp(s.trigger);
  591. s.endRe = guardedRegexp(s.endTrigger, s.endGuard, true);
  592. s.endTriggerRe = new RegExp(s.endTrigger);
  593. }
  594. if (Array.isArray(snippets)) {
  595. snippets.forEach(addSnippet);
  596. } else {
  597. Object.keys(snippets).forEach(function(key) {
  598. addSnippet(snippets[key]);
  599. });
  600. }
  601. this._signal("registerSnippets", {scope: scope});
  602. };
  603. this.unregister = function(snippets, scope) {
  604. var snippetMap = this.snippetMap;
  605. var snippetNameMap = this.snippetNameMap;
  606. function removeSnippet(s) {
  607. var nameMap = snippetNameMap[s.scope||scope];
  608. if (nameMap && nameMap[s.name]) {
  609. delete nameMap[s.name];
  610. var map = snippetMap[s.scope||scope];
  611. var i = map && map.indexOf(s);
  612. if (i >= 0)
  613. map.splice(i, 1);
  614. }
  615. }
  616. if (snippets.content)
  617. removeSnippet(snippets);
  618. else if (Array.isArray(snippets))
  619. snippets.forEach(removeSnippet);
  620. };
  621. this.parseSnippetFile = function(str) {
  622. str = str.replace(/\r/g, "");
  623. var list = [], snippet = {};
  624. var re = /^#.*|^({[\s\S]*})\s*$|^(\S+) (.*)$|^((?:\n*\t.*)+)/gm;
  625. var m;
  626. while (m = re.exec(str)) {
  627. if (m[1]) {
  628. try {
  629. snippet = JSON.parse(m[1]);
  630. list.push(snippet);
  631. } catch (e) {}
  632. } if (m[4]) {
  633. snippet.content = m[4].replace(/^\t/gm, "");
  634. list.push(snippet);
  635. snippet = {};
  636. } else {
  637. var key = m[2], val = m[3];
  638. if (key == "regex") {
  639. var guardRe = /\/((?:[^\/\\]|\\.)*)|$/g;
  640. snippet.guard = guardRe.exec(val)[1];
  641. snippet.trigger = guardRe.exec(val)[1];
  642. snippet.endTrigger = guardRe.exec(val)[1];
  643. snippet.endGuard = guardRe.exec(val)[1];
  644. } else if (key == "snippet") {
  645. snippet.tabTrigger = val.match(/^\S*/)[0];
  646. if (!snippet.name)
  647. snippet.name = val;
  648. } else if (key) {
  649. snippet[key] = val;
  650. }
  651. }
  652. }
  653. return list;
  654. };
  655. this.getSnippetByName = function(name, editor) {
  656. var snippetMap = this.snippetNameMap;
  657. var snippet;
  658. this.getActiveScopes(editor).some(function(scope) {
  659. var snippets = snippetMap[scope];
  660. if (snippets)
  661. snippet = snippets[name];
  662. return !!snippet;
  663. }, this);
  664. return snippet;
  665. };
  666. }).call(SnippetManager.prototype);
  667. var TabstopManager = function(editor) {
  668. if (editor.tabstopManager)
  669. return editor.tabstopManager;
  670. editor.tabstopManager = this;
  671. this.$onChange = this.onChange.bind(this);
  672. this.$onChangeSelection = lang.delayedCall(this.onChangeSelection.bind(this)).schedule;
  673. this.$onChangeSession = this.onChangeSession.bind(this);
  674. this.$onAfterExec = this.onAfterExec.bind(this);
  675. this.attach(editor);
  676. };
  677. (function() {
  678. this.attach = function(editor) {
  679. this.index = 0;
  680. this.ranges = [];
  681. this.tabstops = [];
  682. this.$openTabstops = null;
  683. this.selectedTabstop = null;
  684. this.editor = editor;
  685. this.editor.on("change", this.$onChange);
  686. this.editor.on("changeSelection", this.$onChangeSelection);
  687. this.editor.on("changeSession", this.$onChangeSession);
  688. this.editor.commands.on("afterExec", this.$onAfterExec);
  689. this.editor.keyBinding.addKeyboardHandler(this.keyboardHandler);
  690. };
  691. this.detach = function() {
  692. this.tabstops.forEach(this.removeTabstopMarkers, this);
  693. this.ranges = null;
  694. this.tabstops = null;
  695. this.selectedTabstop = null;
  696. this.editor.removeListener("change", this.$onChange);
  697. this.editor.removeListener("changeSelection", this.$onChangeSelection);
  698. this.editor.removeListener("changeSession", this.$onChangeSession);
  699. this.editor.commands.removeListener("afterExec", this.$onAfterExec);
  700. this.editor.keyBinding.removeKeyboardHandler(this.keyboardHandler);
  701. this.editor.tabstopManager = null;
  702. this.editor = null;
  703. };
  704. this.onChange = function(delta) {
  705. var isRemove = delta.action[0] == "r";
  706. var selectedTabstop = this.selectedTabstop || {};
  707. var parents = selectedTabstop.parents || {};
  708. var tabstops = (this.tabstops || []).slice();
  709. for (var i = 0; i < tabstops.length; i++) {
  710. var ts = tabstops[i];
  711. var active = ts == selectedTabstop || parents[ts.index];
  712. ts.rangeList.$bias = active ? 0 : 1;
  713. if (delta.action == "remove" && ts !== selectedTabstop) {
  714. var parentActive = ts.parents && ts.parents[selectedTabstop.index];
  715. var startIndex = ts.rangeList.pointIndex(delta.start, parentActive);
  716. startIndex = startIndex < 0 ? -startIndex - 1 : startIndex + 1;
  717. var endIndex = ts.rangeList.pointIndex(delta.end, parentActive);
  718. endIndex = endIndex < 0 ? -endIndex - 1 : endIndex - 1;
  719. var toRemove = ts.rangeList.ranges.slice(startIndex, endIndex);
  720. for (var j = 0; j < toRemove.length; j++)
  721. this.removeRange(toRemove[j]);
  722. }
  723. ts.rangeList.$onChange(delta);
  724. }
  725. var session = this.editor.session;
  726. if (!this.$inChange && isRemove && session.getLength() == 1 && !session.getValue())
  727. this.detach();
  728. };
  729. this.updateLinkedFields = function() {
  730. var ts = this.selectedTabstop;
  731. if (!ts || !ts.hasLinkedRanges || !ts.firstNonLinked)
  732. return;
  733. this.$inChange = true;
  734. var session = this.editor.session;
  735. var text = session.getTextRange(ts.firstNonLinked);
  736. for (var i = 0; i < ts.length; i++) {
  737. var range = ts[i];
  738. if (!range.linked)
  739. continue;
  740. var original = range.original;
  741. var fmt = exports.snippetManager.tmStrFormat(text, original, this.editor);
  742. session.replace(range, fmt);
  743. }
  744. this.$inChange = false;
  745. };
  746. this.onAfterExec = function(e) {
  747. if (e.command && !e.command.readOnly)
  748. this.updateLinkedFields();
  749. };
  750. this.onChangeSelection = function() {
  751. if (!this.editor)
  752. return;
  753. var lead = this.editor.selection.lead;
  754. var anchor = this.editor.selection.anchor;
  755. var isEmpty = this.editor.selection.isEmpty();
  756. for (var i = 0; i < this.ranges.length; i++) {
  757. if (this.ranges[i].linked)
  758. continue;
  759. var containsLead = this.ranges[i].contains(lead.row, lead.column);
  760. var containsAnchor = isEmpty || this.ranges[i].contains(anchor.row, anchor.column);
  761. if (containsLead && containsAnchor)
  762. return;
  763. }
  764. this.detach();
  765. };
  766. this.onChangeSession = function() {
  767. this.detach();
  768. };
  769. this.tabNext = function(dir) {
  770. var max = this.tabstops.length;
  771. var index = this.index + (dir || 1);
  772. index = Math.min(Math.max(index, 1), max);
  773. if (index == max)
  774. index = 0;
  775. this.selectTabstop(index);
  776. if (index === 0)
  777. this.detach();
  778. };
  779. this.selectTabstop = function(index) {
  780. this.$openTabstops = null;
  781. var ts = this.tabstops[this.index];
  782. if (ts)
  783. this.addTabstopMarkers(ts);
  784. this.index = index;
  785. ts = this.tabstops[this.index];
  786. if (!ts || !ts.length)
  787. return;
  788. this.selectedTabstop = ts;
  789. var range = ts.firstNonLinked || ts;
  790. if (ts.choices) range.cursor = range.start;
  791. if (!this.editor.inVirtualSelectionMode) {
  792. var sel = this.editor.multiSelect;
  793. sel.toSingleRange(range);
  794. for (var i = 0; i < ts.length; i++) {
  795. if (ts.hasLinkedRanges && ts[i].linked)
  796. continue;
  797. sel.addRange(ts[i].clone(), true);
  798. }
  799. } else {
  800. this.editor.selection.fromOrientedRange(range);
  801. }
  802. this.editor.keyBinding.addKeyboardHandler(this.keyboardHandler);
  803. if (this.selectedTabstop && this.selectedTabstop.choices)
  804. this.editor.execCommand("startAutocomplete", {matches: this.selectedTabstop.choices});
  805. };
  806. this.addTabstops = function(tabstops, start, end) {
  807. var useLink = this.useLink || !this.editor.getOption("enableMultiselect");
  808. if (!this.$openTabstops)
  809. this.$openTabstops = [];
  810. if (!tabstops[0]) {
  811. var p = Range.fromPoints(end, end);
  812. moveRelative(p.start, start);
  813. moveRelative(p.end, start);
  814. tabstops[0] = [p];
  815. tabstops[0].index = 0;
  816. }
  817. var i = this.index;
  818. var arg = [i + 1, 0];
  819. var ranges = this.ranges;
  820. tabstops.forEach(function(ts, index) {
  821. var dest = this.$openTabstops[index] || ts;
  822. for (var i = 0; i < ts.length; i++) {
  823. var p = ts[i];
  824. var range = Range.fromPoints(p.start, p.end || p.start);
  825. movePoint(range.start, start);
  826. movePoint(range.end, start);
  827. range.original = p;
  828. range.tabstop = dest;
  829. ranges.push(range);
  830. if (dest != ts)
  831. dest.unshift(range);
  832. else
  833. dest[i] = range;
  834. if (p.fmtString || (dest.firstNonLinked && useLink)) {
  835. range.linked = true;
  836. dest.hasLinkedRanges = true;
  837. } else if (!dest.firstNonLinked)
  838. dest.firstNonLinked = range;
  839. }
  840. if (!dest.firstNonLinked)
  841. dest.hasLinkedRanges = false;
  842. if (dest === ts) {
  843. arg.push(dest);
  844. this.$openTabstops[index] = dest;
  845. }
  846. this.addTabstopMarkers(dest);
  847. dest.rangeList = dest.rangeList || new RangeList();
  848. dest.rangeList.$bias = 0;
  849. dest.rangeList.addList(dest);
  850. }, this);
  851. if (arg.length > 2) {
  852. if (this.tabstops.length)
  853. arg.push(arg.splice(2, 1)[0]);
  854. this.tabstops.splice.apply(this.tabstops, arg);
  855. }
  856. };
  857. this.addTabstopMarkers = function(ts) {
  858. var session = this.editor.session;
  859. ts.forEach(function(range) {
  860. if (!range.markerId)
  861. range.markerId = session.addMarker(range, "ace_snippet-marker", "text");
  862. });
  863. };
  864. this.removeTabstopMarkers = function(ts) {
  865. var session = this.editor.session;
  866. ts.forEach(function(range) {
  867. session.removeMarker(range.markerId);
  868. range.markerId = null;
  869. });
  870. };
  871. this.removeRange = function(range) {
  872. var i = range.tabstop.indexOf(range);
  873. if (i != -1) range.tabstop.splice(i, 1);
  874. i = this.ranges.indexOf(range);
  875. if (i != -1) this.ranges.splice(i, 1);
  876. i = range.tabstop.rangeList.ranges.indexOf(range);
  877. if (i != -1) range.tabstop.splice(i, 1);
  878. this.editor.session.removeMarker(range.markerId);
  879. if (!range.tabstop.length) {
  880. i = this.tabstops.indexOf(range.tabstop);
  881. if (i != -1)
  882. this.tabstops.splice(i, 1);
  883. if (!this.tabstops.length)
  884. this.detach();
  885. }
  886. };
  887. this.keyboardHandler = new HashHandler();
  888. this.keyboardHandler.bindKeys({
  889. "Tab": function(editor) {
  890. if (exports.snippetManager && exports.snippetManager.expandWithTab(editor))
  891. return;
  892. editor.tabstopManager.tabNext(1);
  893. editor.renderer.scrollCursorIntoView();
  894. },
  895. "Shift-Tab": function(editor) {
  896. editor.tabstopManager.tabNext(-1);
  897. editor.renderer.scrollCursorIntoView();
  898. },
  899. "Esc": function(editor) {
  900. editor.tabstopManager.detach();
  901. }
  902. });
  903. }).call(TabstopManager.prototype);
  904. var movePoint = function(point, diff) {
  905. if (point.row == 0)
  906. point.column += diff.column;
  907. point.row += diff.row;
  908. };
  909. var moveRelative = function(point, start) {
  910. if (point.row == start.row)
  911. point.column -= start.column;
  912. point.row -= start.row;
  913. };
  914. dom.importCssString("\
  915. .ace_snippet-marker {\
  916. -moz-box-sizing: border-box;\
  917. box-sizing: border-box;\
  918. background: rgba(194, 193, 208, 0.09);\
  919. border: 1px dotted rgba(211, 208, 235, 0.62);\
  920. position: absolute;\
  921. }", "snippets.css", false);
  922. exports.snippetManager = new SnippetManager();
  923. var Editor = require("./editor").Editor;
  924. (function() {
  925. this.insertSnippet = function(content, options) {
  926. return exports.snippetManager.insertSnippet(this, content, options);
  927. };
  928. this.expandSnippet = function(options) {
  929. return exports.snippetManager.expandWithTab(this, options);
  930. };
  931. }).call(Editor.prototype);
  932. });
  933. define("ace/ext/emmet",["require","exports","module","ace/keyboard/hash_handler","ace/editor","ace/snippets","ace/range","ace/config","resources","resources","tabStops","resources","utils","actions"], function(require, exports, module) {
  934. "use strict";
  935. var HashHandler = require("../keyboard/hash_handler").HashHandler;
  936. var Editor = require("../editor").Editor;
  937. var snippetManager = require("../snippets").snippetManager;
  938. var Range = require("../range").Range;
  939. var config = require("../config");
  940. var emmet, emmetPath;
  941. function AceEmmetEditor() {}
  942. AceEmmetEditor.prototype = {
  943. setupContext: function(editor) {
  944. this.ace = editor;
  945. this.indentation = editor.session.getTabString();
  946. if (!emmet)
  947. emmet = window.emmet;
  948. var resources = emmet.resources || emmet.require("resources");
  949. resources.setVariable("indentation", this.indentation);
  950. this.$syntax = null;
  951. this.$syntax = this.getSyntax();
  952. },
  953. getSelectionRange: function() {
  954. var range = this.ace.getSelectionRange();
  955. var doc = this.ace.session.doc;
  956. return {
  957. start: doc.positionToIndex(range.start),
  958. end: doc.positionToIndex(range.end)
  959. };
  960. },
  961. createSelection: function(start, end) {
  962. var doc = this.ace.session.doc;
  963. this.ace.selection.setRange({
  964. start: doc.indexToPosition(start),
  965. end: doc.indexToPosition(end)
  966. });
  967. },
  968. getCurrentLineRange: function() {
  969. var ace = this.ace;
  970. var row = ace.getCursorPosition().row;
  971. var lineLength = ace.session.getLine(row).length;
  972. var index = ace.session.doc.positionToIndex({row: row, column: 0});
  973. return {
  974. start: index,
  975. end: index + lineLength
  976. };
  977. },
  978. getCaretPos: function(){
  979. var pos = this.ace.getCursorPosition();
  980. return this.ace.session.doc.positionToIndex(pos);
  981. },
  982. setCaretPos: function(index){
  983. var pos = this.ace.session.doc.indexToPosition(index);
  984. this.ace.selection.moveToPosition(pos);
  985. },
  986. getCurrentLine: function() {
  987. var row = this.ace.getCursorPosition().row;
  988. return this.ace.session.getLine(row);
  989. },
  990. replaceContent: function(value, start, end, noIndent) {
  991. if (end == null)
  992. end = start == null ? this.getContent().length : start;
  993. if (start == null)
  994. start = 0;
  995. var editor = this.ace;
  996. var doc = editor.session.doc;
  997. var range = Range.fromPoints(doc.indexToPosition(start), doc.indexToPosition(end));
  998. editor.session.remove(range);
  999. range.end = range.start;
  1000. value = this.$updateTabstops(value);
  1001. snippetManager.insertSnippet(editor, value);
  1002. },
  1003. getContent: function(){
  1004. return this.ace.getValue();
  1005. },
  1006. getSyntax: function() {
  1007. if (this.$syntax)
  1008. return this.$syntax;
  1009. var syntax = this.ace.session.$modeId.split("/").pop();
  1010. if (syntax == "html" || syntax == "php") {
  1011. var cursor = this.ace.getCursorPosition();
  1012. var state = this.ace.session.getState(cursor.row);
  1013. if (typeof state != "string")
  1014. state = state[0];
  1015. if (state) {
  1016. state = state.split("-");
  1017. if (state.length > 1)
  1018. syntax = state[0];
  1019. else if (syntax == "php")
  1020. syntax = "html";
  1021. }
  1022. }
  1023. return syntax;
  1024. },
  1025. getProfileName: function() {
  1026. var resources = emmet.resources || emmet.require("resources");
  1027. switch (this.getSyntax()) {
  1028. case "css": return "css";
  1029. case "xml":
  1030. case "xsl":
  1031. return "xml";
  1032. case "html":
  1033. var profile = resources.getVariable("profile");
  1034. if (!profile)
  1035. profile = this.ace.session.getLines(0,2).join("").search(/<!DOCTYPE[^>]+XHTML/i) != -1 ? "xhtml": "html";
  1036. return profile;
  1037. default:
  1038. var mode = this.ace.session.$mode;
  1039. return mode.emmetConfig && mode.emmetConfig.profile || "xhtml";
  1040. }
  1041. },
  1042. prompt: function(title) {
  1043. return prompt(title); // eslint-disable-line no-alert
  1044. },
  1045. getSelection: function() {
  1046. return this.ace.session.getTextRange();
  1047. },
  1048. getFilePath: function() {
  1049. return "";
  1050. },
  1051. $updateTabstops: function(value) {
  1052. var base = 1000;
  1053. var zeroBase = 0;
  1054. var lastZero = null;
  1055. var ts = emmet.tabStops || emmet.require('tabStops');
  1056. var resources = emmet.resources || emmet.require("resources");
  1057. var settings = resources.getVocabulary("user");
  1058. var tabstopOptions = {
  1059. tabstop: function(data) {
  1060. var group = parseInt(data.group, 10);
  1061. var isZero = group === 0;
  1062. if (isZero)
  1063. group = ++zeroBase;
  1064. else
  1065. group += base;
  1066. var placeholder = data.placeholder;
  1067. if (placeholder) {
  1068. placeholder = ts.processText(placeholder, tabstopOptions);
  1069. }
  1070. var result = '${' + group + (placeholder ? ':' + placeholder : '') + '}';
  1071. if (isZero) {
  1072. lastZero = [data.start, result];
  1073. }
  1074. return result;
  1075. },
  1076. escape: function(ch) {
  1077. if (ch == '$') return '\\$';
  1078. if (ch == '\\') return '\\\\';
  1079. return ch;
  1080. }
  1081. };
  1082. value = ts.processText(value, tabstopOptions);
  1083. if (settings.variables['insert_final_tabstop'] && !/\$\{0\}$/.test(value)) {
  1084. value += '${0}';
  1085. } else if (lastZero) {
  1086. var common = emmet.utils ? emmet.utils.common : emmet.require('utils');
  1087. value = common.replaceSubstring(value, '${0}', lastZero[0], lastZero[1]);
  1088. }
  1089. return value;
  1090. }
  1091. };
  1092. var keymap = {
  1093. expand_abbreviation: {"mac": "ctrl+alt+e", "win": "alt+e"},
  1094. match_pair_outward: {"mac": "ctrl+d", "win": "ctrl+,"},
  1095. match_pair_inward: {"mac": "ctrl+j", "win": "ctrl+shift+0"},
  1096. matching_pair: {"mac": "ctrl+alt+j", "win": "alt+j"},
  1097. next_edit_point: "alt+right",
  1098. prev_edit_point: "alt+left",
  1099. toggle_comment: {"mac": "command+/", "win": "ctrl+/"},
  1100. split_join_tag: {"mac": "shift+command+'", "win": "shift+ctrl+`"},
  1101. remove_tag: {"mac": "command+'", "win": "shift+ctrl+;"},
  1102. evaluate_math_expression: {"mac": "shift+command+y", "win": "shift+ctrl+y"},
  1103. increment_number_by_1: "ctrl+up",
  1104. decrement_number_by_1: "ctrl+down",
  1105. increment_number_by_01: "alt+up",
  1106. decrement_number_by_01: "alt+down",
  1107. increment_number_by_10: {"mac": "alt+command+up", "win": "shift+alt+up"},
  1108. decrement_number_by_10: {"mac": "alt+command+down", "win": "shift+alt+down"},
  1109. select_next_item: {"mac": "shift+command+.", "win": "shift+ctrl+."},
  1110. select_previous_item: {"mac": "shift+command+,", "win": "shift+ctrl+,"},
  1111. reflect_css_value: {"mac": "shift+command+r", "win": "shift+ctrl+r"},
  1112. encode_decode_data_url: {"mac": "shift+ctrl+d", "win": "ctrl+'"},
  1113. expand_abbreviation_with_tab: "Tab",
  1114. wrap_with_abbreviation: {"mac": "shift+ctrl+a", "win": "shift+ctrl+a"}
  1115. };
  1116. var editorProxy = new AceEmmetEditor();
  1117. exports.commands = new HashHandler();
  1118. exports.runEmmetCommand = function runEmmetCommand(editor) {
  1119. if (this.action == "expand_abbreviation_with_tab") {
  1120. if (!editor.selection.isEmpty())
  1121. return false;
  1122. var pos = editor.selection.lead;
  1123. var token = editor.session.getTokenAt(pos.row, pos.column);
  1124. if (token && /\btag\b/.test(token.type))
  1125. return false;
  1126. }
  1127. try {
  1128. editorProxy.setupContext(editor);
  1129. var actions = emmet.actions || emmet.require("actions");
  1130. if (this.action == "wrap_with_abbreviation") {
  1131. return setTimeout(function() {
  1132. actions.run("wrap_with_abbreviation", editorProxy);
  1133. }, 0);
  1134. }
  1135. var result = actions.run(this.action, editorProxy);
  1136. } catch(e) {
  1137. if (!emmet) {
  1138. var loading = exports.load(runEmmetCommand.bind(this, editor));
  1139. if (this.action == "expand_abbreviation_with_tab")
  1140. return false;
  1141. return loading;
  1142. }
  1143. editor._signal("changeStatus", typeof e == "string" ? e : e.message);
  1144. config.warn(e);
  1145. result = false;
  1146. }
  1147. return result;
  1148. };
  1149. for (var command in keymap) {
  1150. exports.commands.addCommand({
  1151. name: "emmet:" + command,
  1152. action: command,
  1153. bindKey: keymap[command],
  1154. exec: exports.runEmmetCommand,
  1155. multiSelectAction: "forEach"
  1156. });
  1157. }
  1158. exports.updateCommands = function(editor, enabled) {
  1159. if (enabled) {
  1160. editor.keyBinding.addKeyboardHandler(exports.commands);
  1161. } else {
  1162. editor.keyBinding.removeKeyboardHandler(exports.commands);
  1163. }
  1164. };
  1165. exports.isSupportedMode = function(mode) {
  1166. if (!mode) return false;
  1167. if (mode.emmetConfig) return true;
  1168. var id = mode.$id || mode;
  1169. return /css|less|scss|sass|stylus|html|php|twig|ejs|handlebars/.test(id);
  1170. };
  1171. exports.isAvailable = function(editor, command) {
  1172. if (/(evaluate_math_expression|expand_abbreviation)$/.test(command))
  1173. return true;
  1174. var mode = editor.session.$mode;
  1175. var isSupported = exports.isSupportedMode(mode);
  1176. if (isSupported && mode.$modes) {
  1177. try {
  1178. editorProxy.setupContext(editor);
  1179. if (/js|php/.test(editorProxy.getSyntax()))
  1180. isSupported = false;
  1181. } catch(e) {}
  1182. }
  1183. return isSupported;
  1184. };
  1185. var onChangeMode = function(e, target) {
  1186. var editor = target;
  1187. if (!editor)
  1188. return;
  1189. var enabled = exports.isSupportedMode(editor.session.$mode);
  1190. if (e.enableEmmet === false)
  1191. enabled = false;
  1192. if (enabled)
  1193. exports.load();
  1194. exports.updateCommands(editor, enabled);
  1195. };
  1196. exports.load = function(cb) {
  1197. if (typeof emmetPath !== "string") {
  1198. config.warn("script for emmet-core is not loaded");
  1199. return false;
  1200. }
  1201. config.loadModule(emmetPath, function() {
  1202. emmetPath = null;
  1203. cb && cb();
  1204. });
  1205. return true;
  1206. };
  1207. exports.AceEmmetEditor = AceEmmetEditor;
  1208. config.defineOptions(Editor.prototype, "editor", {
  1209. enableEmmet: {
  1210. set: function(val) {
  1211. this[val ? "on" : "removeListener"]("changeMode", onChangeMode);
  1212. onChangeMode({enableEmmet: !!val}, this);
  1213. },
  1214. value: true
  1215. }
  1216. });
  1217. exports.setCore = function(e) {
  1218. if (typeof e == "string")
  1219. emmetPath = e;
  1220. else
  1221. emmet = e;
  1222. };
  1223. }); (function() {
  1224. window.require(["ace/ext/emmet"], function(m) {
  1225. if (typeof module == "object" && typeof exports == "object" && module) {
  1226. module.exports = m;
  1227. }
  1228. });
  1229. })();