add support for TextMate snippets

This commit is contained in:
nightwing 2013-01-06 17:46:04 +04:00
commit b8eb5a1fec
5 changed files with 653 additions and 1 deletions

67
tool/tmsnippets.js Normal file
View file

@ -0,0 +1,67 @@
var fs = require('fs')
var plist = require('plist')
var snippets = [];
var path = process.argv[2] || process.cwd();
function readSnippet(path, name) {
if (name)
path += name
if (!/\.tmSnippet$/i.test(path))
return
console.log(name)
var plistString = fs.readFileSync(path, "utf8");
plist.parseString(plistString, function(_, plist){
snippets.push(plist)
})
}
// read
if (fs.statSync(path).isDirectory()) {
path += "/"
fs.readdirSync(path).forEach(function(name) {
readSnippet(path, name)
})
} else {
readSnippet(path)
}
// transform
snippets = snippets.map(function(s) {
if (s.length == 1)
s = s[0]
if (s.scope)
s.scope = s.scope.replace(/source\./g, "")
delete s.uuid
return s
})
// stringify
var indent = ""
var text = JSON.stringify(snippets, null, 1)
// .replace(/(\n\s*)"(\w+)"\:/g, "$1$2:")
.replace(/(\n\s*)\},\n\s*{/g, "$1}, {")
.replace(/\[\n\s*\{\n/g, "[{\n").replace(/(\n\s*)\}\n\s*\]/g, "$1}]")
.replace(/\[\n\s*[^\[\{\}\]]{0,100}\]/g, function(x){return x.replace(/\n\s*/g, " ")})
.replace(/\:\s*\{\n\s*(.*)\n\s*\}/g, ": {$1}")
.split(/\n\s*/).map(function(x){
if (x[0] == "}" || x[0] == "]")
indent = indent.substr(1)
if (x.slice(-1) == "{" || x.slice(-1) == "[") {
indent += "\t"
return indent.substr(1) + x
}
return indent +x
}).join("\n")
.replace(/\\[\\tnr]/g, function(a){
if (a[1] == "\\")
return a
else if (a[1] == "t")
return "\t"
else
return "\\n"+"\\" + "\n"
})
fs.writeFileSync(path += "./ace.snippets.js", text)
console.log(path)