User Tools

Site Tools


script_autoformat

====== 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 : 2.0 * 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 * Version 1.3 (29/8/2014): option to write directly to script files (default: on) * Version 2.0 (14/9/2014): new UI, option to format all Scripts ====== How to use the script ====== * Run from anywhere * choose something * copy/paste the output if not using directWrite ====== Issues and Hints ====== * When formatting all scripts it really may take some time! Don't click anywhere and when a Dialog pops up "LLX is not reacting" tap wait. **//This Standalone Version is discontinued! Use [[script_multitool|Multitool]] instead, it's included there.//** //please report all bugs in the g+ community!// ====== Script ====== <sxh javascript;>//config var directWrite=true;//wether to show a prompt or write directly to the script //endconfig //Initialize List //import java classes LL.bindClass("android.app.AlertDialog"); LL.bindClass("android.content.DialogInterface"); //function to display a List in a Popup, where the user can select one item list=function(items,listener,title) { var builder=new AlertDialog.Builder(LL.getContext()); builder.setItems(items,listener); builder.setCancelable(true); builder.setTitle(title); builder.setNegativeButton("Cancel",new DialogInterface.OnClickListener(){onClick:function(dialog,id){dialog.cancel();}});//it has a Cancel Button builder.create().show(); } //create Strings to display var title="Which script?"; var items=[]; var scripts=LL.getAllScriptMatching(Script.FLAG_ALL); for(var z=0;z<scripts.length;z++)items.push(scripts.getAt(z).getName()); items.sort(noCaseSort); items.unshift("All Scripts (may need some time)"); //initialize vars var switchLevel; var i; var s; var t; var out; var noCode; var starter; var newLine; //handle user selection var listener=new DialogInterface.OnClickListener(){ onClick:function(dialog,id) { var selected; if(id==0)selected=items.slice(1,items.length);//all scripts selected else selected=[items[id]];//single script selected for(var y=0;y<selected.length;y++) { s=LL.getScriptByName(selected[y]); t=s.getText();//original text out="";//output i=0;//indentionlevel noCode=false;//detectionhelper,true if in comment,string etc. starter;//detectionhelper,contains the last noCode block starter newLine=true;//true if in a new line switchLevel=0;//detectionhelper,needed for switch commands //go through the text for(a=0;a<t.length;a++) { //donvt copy spaces and tabs, if not in a noCode block 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; //detect wether the char is negated by a backslash or not var backslashed=false; for(b=out.length-1;out[b]=="\\";b--)backslashed=!backslashed; //detect start of a noCode block if((t[a]=="\""||t[a]=="\'")&&!noCode) { noCode=true; starter=t[a];if(out[out.length-1]=="\n")indentLine(); } else if(!noCode&&t[a]=="/"&&(t[a+1]=="/"||t[a+1]=="*")) { noCode=true; starter=t.slice(a,a+2); if(out[out.length-1]=="\n")indentLine(); } //detect end of a noCode block else if(noCode&&(t[a]==starter||(starter=="/*"&&t.slice(a,a+2)=="*/")||(starter=="//"&&t[a]=="\n"))&&!backslashed) { noCode=false; } //handle keychars if(!noCode) { if(t[a]=="\n")//line end { newLine=true; out=out.concat(t[a]); continue; } if(t[a]=="}")//function block end { i--; if(i<=switchLevel&&switchLevel>0)//special handling when in switch command { switchLevel=0; i--; } } if(newLine&&t[a]!="\n")//indent the next line { indentLine(); } if(t[a]=="{")i++;//function block start if(t.slice(a,a+7)=="switch(")switchLevel=++i;//start of switch command } out=out.concat(t[a]);//concat the char } while(out[out.length-1]=="\n")out=out.slice(0,out.length-1);//remove unnessecary line brakes at the end of the script if(directWrite)//set output to the script { s.setText(out); if(y==selected.length-1)Android.makeNewToast("Done!",true).show();//notify if finished } else prompt("",out);//show output } } } //show selection list list(items,listener,title); //creates tabs at the beginning of a line function indentLine() { 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; } //helper function for sorting labels function noCaseSort(a,b) { if(a.toLowerCase()>b.toLowerCase())return 1; if(a.toLowerCase()<b.toLowerCase())return -1; return 0; } </sxh>

script_autoformat.txt · Last modified: 2014/09/24 16:08 by lm13