Merge pull request #1346 from ajaxorg/tmSnippets

snippet bugfix
This commit is contained in:
Lennart Kats 2013-04-05 00:52:51 -07:00
commit 202ecaac90
4 changed files with 34 additions and 30 deletions

View file

@ -129,9 +129,9 @@ env.editor.commands.addCommands([{
editor.cmdLine.focus();
return;
}
var s = SnippetManager.getSnippetByName(needle, editor);
var s = snippetManager.getSnippetByName(needle, editor);
if (s)
SnippetManager.insertSnippet(editor, s.content);
snippetManager.insertSnippet(editor, s.content);
},
readOnly: true
}, {
@ -471,15 +471,14 @@ net.loadScript("https://rawgithub.com/nightwing/emmet-core/master/emmet.js", fun
require("ace/placeholder").PlaceHolder;
var SnippetManager = require("ace/snippets").SnippetManager
var snippetManager = require("ace/snippets").snippetManager
var jsSnippets = require("ace/snippets/javascript");
window.SnippetManager = SnippetManager
window.snippetManager = snippetManager
saveSnippets()
function saveSnippets() {
jsSnippets.snippets = SnippetManager.parseSnippetFile(jsSnippets.snippetText);
SnippetManager.snipp
SnippetManager.register(jsSnippets.snippets, "javascript")
jsSnippets.snippets = snippetManager.parseSnippetFile(jsSnippets.snippetText);
snippetManager.register(jsSnippets.snippets, "javascript")
}
env.editSnippets = function() {
@ -507,7 +506,7 @@ env.editSnippets = function() {
}
ace.commands.bindKey("Tab", function(editor) {
var success = SnippetManager.expandWithTab(editor);
var success = snippetManager.expandWithTab(editor);
if (!success)
editor.execCommand("indent");
})

View file

@ -97,8 +97,10 @@ var SnippetManager = function() {
}, next: "start"},
{regex: "/(" + escape("/") + "+)/(?:(" + escape("/") + "*)/)(\\w*):?",
onMatch: function(val, state, stack) {
val = this.splitRegex.exec(val);
var ts = stack[0];
ts.fmtString = val;
val = this.splitRegex.exec(val);
ts.guard = val[1];
ts.fmt = val[2];
ts.flag = val[3];
@ -230,7 +232,7 @@ var SnippetManager = function() {
continue;
} else if (ch.text) {
var value = this.getVariableValue(editor, ch.text);
if (value && ch.fmt)
if (value && ch.fmtString)
value = this.tmStrFormat(value, ch);
ch.processed = i;
if (ch.expectIf == null) {
@ -482,6 +484,7 @@ var SnippetManager = function() {
snippets.forEach(removeSnippet);
};
this.parseSnippetFile = function(str) {
str = str.replace(/\r/, "");
var list = [], snippet = {};
var re = /^#.*|^({[\s\S]*})\s*$|^(\S+) (.*)$|^((?:\n*\t.*)+)/gm;
var m;
@ -498,13 +501,13 @@ var SnippetManager = function() {
} else {
var key = m[2], val = m[3];
if (key == "regex") {
val = val.split(/\/((?:[^\/\\]|\\.)*)\/?/);
snippet.guard = val[1];
snippet.trigger = val[2];
snippet.endTrigger = val[3];
snippet.endGuard = val[4];
var guardRe = /\/((?:[^\/\\]|\\.)*)|$/g;
snippet.guard = guardRe.exec(val)[1];
snippet.trigger = guardRe.exec(val)[1];
snippet.endTrigger = guardRe.exec(val)[1];
snippet.endGuard = guardRe.exec(val)[1];
} else if (key == "snippet") {
snippet.tabTrigger = val.split(/^(\S*)(?:\s(.*))?$/)[1];
snippet.tabTrigger = val.match(/^\S*/)[0];
if (!snippet.name)
snippet.name = val;
} else {
@ -629,7 +632,7 @@ var TabstopManager = function(editor) {
var range = ts[i];
if (!range.linked)
continue;
var fmt = exports.SnippetManager.tmStrFormat(text, range.original)
var fmt = exports.snippetManager.tmStrFormat(text, range.original)
session.replace(range, fmt);
}
this.$inChange = false;
@ -708,7 +711,7 @@ var TabstopManager = function(editor) {
range.tabstop = ts;
ranges.push(range);
ts[i] = range;
if (p.fmt) {
if (p.fmtString) {
range.linked = true;
ts.hasLinkedRanges = true;
} else if (!ts.firstNonLinked)
@ -757,7 +760,8 @@ var TabstopManager = function(editor) {
"Esc": function(ed) {
ed.tabstopManager.detach();
},
"Return": function(ed) {
// todo multiselectExec doesn't handle command return values
"!Return": function(ed) {
//ed.tabstopManager.tabNext(1);
return false;
},
@ -787,7 +791,7 @@ require("./lib/dom").importCssString("\
position: absolute;\
}");
exports.SnippetManager = new SnippetManager();
exports.snippetManager = new SnippetManager();
});

View file

@ -75,8 +75,9 @@ regex /([,{[])|^\s*/:f/
${0}
}${3:,}
# setTimeout function
snippet timeout
setTimeout(function() {${3}}${2}, ${1:10});
snippet setTimeout
regex /\b/st|timeout|setTimeo?u?t?/
setTimeout(function() {${3:$TM_SELECTED_TEXT}}, ${1:10});
# Get Elements
snippet gett
getElementsBy${1:TagName}('${2}')${3}
@ -166,12 +167,12 @@ snippet for-
}
# for (...) {...}
snippet for
for (var ${1:i} = 0; $1 < ${2:Things}.length; $1$++) {
for (var ${1:i} = 0; $1 < ${2:Things}.length; $1++) {
${3:$2[$1]}$0
}
# for (...) {...} (Improved Native For-Loop)
snippet forr
for (var ${1:i} = ${2:Things}.length - 1; $1 >= 0; $1$--) {
for (var ${1:i} = ${2:Things}.length - 1; $1 >= 0; $1--) {
${3:$2[$1]}$0
}

View file

@ -35,13 +35,13 @@ if (typeof process !== "undefined") {
define(function(require, exports, module) {
"use strict";
var SnippetManager = require("./snippets").SnippetManager;
var snippetManager = require("./snippets").snippetManager;
var assert = require("./test/assertions");
module.exports = {
"test: textmate style format strings" : function() {
var fmt = SnippetManager.tmStrFormat;
SnippetManager.tmStrFormat("hello", {
var fmt = snippetManager.tmStrFormat;
snippetManager.tmStrFormat("hello", {
guard: "(..)(.)(.)",
flag:"g",
fmt: "a\\UO\\l$1\\E$2"
@ -63,7 +63,7 @@ module.exports = {
var parsed = SnippetManager.parseSnippetFile(
var parsed = snippetManager.parseSnippetFile(
"name a\nregex /(?:(=)|(:))?\s*)/\\(?f/\\)/\n\t{$0}" +
"\n\t\n\n#function\nsnippet f function\n\tfunction"
);
@ -72,13 +72,13 @@ module.exports = {
},
"test: parse snippet": function() {
var content = "-\\$$2a${1:x${$2:y$3\\}\\n\\}$TM_SELECTION}";
var tokens = SnippetManager.tokenizeTmSnippet(content);
var tokens = snippetManager.tokenizeTmSnippet(content);
assert.equal(tokens.length, 15);
assert.equal(tokens[4], tokens[14]);
assert.equal(tokens[2].tabstopId, 2);
var content = "\\}${var/as\\/d/\\ul\\//g:s}"
var tokens = SnippetManager.tokenizeTmSnippet(content);
var tokens = snippetManager.tokenizeTmSnippet(content);
assert.equal(tokens.length, 4);
assert.equal(tokens[1], tokens[3]);
assert.equal(tokens[2], "s");