Created
March 21, 2012 11:04
-
-
Save evaisse/2146215 to your computer and use it in GitHub Desktop.
[KOMODO] Run snippets from keyword
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Get and run a snippets from your toolbox. | |
* | |
* First snippet name is guess by word under your cursor or before your | |
* cursor if none selected. In our example : "fooBar" | |
* The script search a snippet named : | |
* 1. the word join with the language name (lowercase) -> "php_fooBar" | |
* 2. the snippet name with preceding joker (def "any_") -> "any_fooBar" | |
* | |
* the snippet is run if found, and nothing happen otherwise (except a nice | |
* log message in your status bar) | |
*/ | |
var view = ko.views.manager.currentView, | |
document = view.koDoc, | |
path = document.file.displayPath, | |
dir = document.file.dirName, | |
filename = document.baseName, | |
runSvc, | |
process, | |
output, | |
snippetName, | |
replacements = {}, | |
trim, | |
ENV_VARS = ['USER_NAME', 'USER_LOG', 'USER_EMAIL', 'USER_ORG'], | |
i = 0, | |
snippetOrigValue; | |
trim = function (str, charlist) { | |
var whitespace, l = 0, | |
i = 0; | |
str += ''; | |
if (!charlist) { | |
// default list | |
whitespace = " \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000"; | |
} else { | |
// preg_quote custom list | |
charlist += ''; | |
whitespace = charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '$1'); | |
} | |
l = str.length; | |
for (i = 0; i < l; i++) { | |
if (whitespace.indexOf(str.charAt(i)) === -1) { | |
str = str.substring(i); | |
break; | |
} | |
} | |
l = str.length; | |
for (i = l - 1; i >= 0; i--) { | |
if (whitespace.indexOf(str.charAt(i)) === -1) { | |
str = str.substring(0, i + 1); | |
break; | |
} | |
} | |
return whitespace.indexOf(str.charAt(0)) === -1 ? str : ''; | |
} | |
/* | |
START | |
*/ | |
komodo.view.setFocus(); | |
if (!snippetName) { | |
view.scimoz.wordPartLeftExtend(); | |
snippetName = ko.interpolate.getWordUnderCursor(); | |
} | |
var snippet = komodo.findPart("snippet", | |
document.language.toLowerCase() + "_" + snippetName, | |
"container"); | |
if( !snippet ) { | |
var snippet = komodo.findPart("snippet", snippetName, "*"); | |
} | |
if (!snippet) { | |
StatusBar_AddMessage("no snippet found named '" + snippetName + "'", | |
"debugger", 5000, true); | |
return false; | |
} | |
view.scimoz.wordPartLeftExtend(); | |
runSvc = Components.classes["@activestate.com/koRunService;1"]. | |
createInstance(Components.interfaces.koIRunService); | |
snippetOrigValue = snippet.value; | |
try { | |
for (i = 0; i < ENV_VARS.length; i++) { | |
process = runSvc.RunAndNotify('echo $' + ENV_VARS[i], '', '', ''); | |
output = trim(process.getStdout()); // need to remove endofline | |
search = '[[%(env:' + ENV_VARS[i] + ')]]'; | |
while (snippet.value.indexOf(search) !== -1) { | |
snippet.value = snippet.value.replace(search, output); // replace snippets contents | |
} | |
} | |
ko.projects.snippetInsert(snippet); | |
} catch (e) { | |
alert(e); | |
} | |
snippet.value = snippetOrigValue; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment