User Tools

Site Tools


script_autoformat

**This is an old revision of the document!** ----

A PCRE internal error occured. This might be caused by a faulty plugin

====== About the script ====== * Purpose : This script will autoindent and remove unneeded spaces and tabs in other scripts * Author : [[https://plus.google.com/+LukasMorawietz|LM13]] * Current Version : 1.2 * Link : https://plus.google.com/115366157037831519359/posts/RjizoNAYKsc ====== Changelog ====== * Version 1.0 (22/7/2014): initial release in wiki * Version 1.1 (23/7/2014): line breaks stay as they are, fix for strings and comments, double spaces and backslashes * Version 1.2 (25/7/2014): fix for backslashes, keywords, added switch support ====== How to use the script ====== * Run from anywhere * type in the exact name of the script you want to format * copy/paste the output * test it, before replacing the old! //please report all bugs in the g+ community!// ====== Script ====== <sxh javascript;>var s=LL.getScriptByName(prompt("Which script?","")); try { var t=s.getText(); } catch(NullPointerException) { alert("script not found"); return; } var out=""; var i=0; var noCode=false; var starter; var newLine=true; var switchLevel=0; for(a=0;a<t.length;a++) { if(!noCode&&((t[a]==" "&&(out[out.length-1].toLowerCase()==out[out.length-1].toUpperCase()||t[a+1].toLowerCase()==t[a+1].toUpperCase())&&out.slice(out.length-4,out.length)!="case"&&out.slice(out.length-6,out.length)!="return")||(t[a]=="\t")))continue; var backslashed=false; for(b=out.length-1;out[b]=="\\";b--)backslashed=!backslashed;if((t[a]=="\""||t[a]=="\'")&&!noCode) { noCode=true; starter=t[a]; } else if(!noCode&&t[a]=="/"&&(t[a+1]=="/"||t[a+1]=="*")) { noCode=true; starter=t.slice(a,a+2); } else if(noCode&&(t[a]==starter||(starter=="/*"&&t.slice(a,a+2)=="*/")||(starter=="//"&&t[a]=="\n"))&&!backslashed) { noCode=false; } if(!noCode) { if(t[a]=="\n") { newLine=true; out=out.concat(t[a]); continue; } if(t[a]=="}") { i--; if(i<=switchLevel&&switchLevel>0) { switchLevel=0; i--; } } if(newLine&&t[a]!="\n") { for(b=(switchLevel!=0&&(t.slice(a,a+4)=="case"||t.slice(a,a+7)=="default")?1:0);b<i;b++) out=out.concat("\t"); newLine=false; } if(t[a]=="{")i++; if(t.slice(a,a+7)=="switch(")switchLevel=++i; } out=out.concat(t[a]); } while(out[out.length-1]=="\n")out=out.slice(0,out.length-1); prompt("",out); </sxh>

script_autoformat.1406297046.txt.gz · Last modified: 2014/07/25 14:04 by lm13