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