====== Differences ====== This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
script_autoformat [2014/07/22 21:34] lm13 created |
script_autoformat [2014/09/24 16:08] (current) lm13 [Issues and Hints] |
||
---|---|---|---|
Line 1: | Line 1: | ||
====== About the script ====== | ====== About the script ====== | ||
- | * Purpose : This script will autoindent and remove unneeded spaces and line breaks in other scripts | + | * Purpose : This script will autoindent and remove unneeded spaces and tabs in other scripts |
* Author : [[https://plus.google.com/+LukasMorawietz|LM13]] | * Author : [[https://plus.google.com/+LukasMorawietz|LM13]] | ||
- | * Current Version : 1.0 | + | * Current Version : 2.0 |
- | * Link : | + | * Link : https://plus.google.com/115366157037831519359/posts/RjizoNAYKsc |
====== Changelog ====== | ====== Changelog ====== | ||
* Version 1.0 (22/7/2014): initial release in wiki | * 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 | ||
Line 12: | Line 16: | ||
====== How to use the script ====== | ====== How to use the script ====== | ||
* Run from anywhere | * Run from anywhere | ||
- | * type in the exact name of the script you want to format | + | * choose something |
- | * copy/paste the output | + | * copy/paste the output if not using directWrite |
- | * test it, before replacing the old! | + | |
+ | ====== 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!// | //please report all bugs in the g+ community!// | ||
====== Script ====== | ====== Script ====== | ||
- | <sxh javascript;>var s=LL.getScriptByName(prompt("Which script?","")); | + | <sxh javascript;>//config |
- | try | + | 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 t=s.getText(); | + | 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(); | ||
} | } | ||
- | catch(NullPointerException) | + | |
- | { | + | //create Strings to display |
- | alert("script not found"); | + | var title="Which script?"; |
- | return; | + | var items=[]; |
- | } | + | var scripts=LL.getAllScriptMatching(Script.FLAG_ALL); |
- | var out=""; | + | for(var z=0;z<scripts.length;z++)items.push(scripts.getAt(z).getName()); |
- | var i=0; | + | items.sort(noCaseSort); |
- | var inString=false; | + | items.unshift("All Scripts (may need some time)"); |
- | var newLine=true; | + | |
- | var inFor=-1; | + | //initialize vars |
- | for(a=0;a<t.length;a++) | + | var switchLevel; |
- | { | + | var i; |
- | if(t[a]==" "&&!inString&&(t[a-1].toLowerCase()==t[a-1].toUpperCase()||t[a+1].toLowerCase()==t[a+1].toUpperCase()))continue; | + | var s; |
- | if(t[a]=="\n"&&!inString&&t[a+1]!="\n")continue; | + | var t; |
- | if(t[a]=="\t"&&!inString)continue; | + | var out; |
- | if(t[a]=="\""||t[a]=="\'")inString=!inString; | + | var noCode; |
- | if(!inString) | + | var starter; |
+ | var newLine; | ||
+ | |||
+ | //handle user selection | ||
+ | var listener=new DialogInterface.OnClickListener(){ | ||
+ | onClick:function(dialog,id) | ||
{ | { | ||
- | if(t[a]=="{") | + | 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++) | ||
{ | { | ||
- | out=out.concat("\n"); | + | s=LL.getScriptByName(selected[y]); |
- | for(b=0;b<i;b++)out=out.concat("\t"); | + | t=s.getText();//original text |
- | out=out.concat("{\n"); | + | out="";//output |
- | i++; | + | i=0;//indentionlevel |
- | newLine=true; | + | noCode=false;//detectionhelper,true if in comment,string etc. |
- | continue; | + | starter;//detectionhelper,contains the last noCode block starter |
- | } | + | newLine=true;//true if in a new line |
- | if(t[a]=="}") | + | switchLevel=0;//detectionhelper,needed for switch commands |
- | { | + | |
- | i--; | + | //go through the text |
- | for(b=0;b<i;b++)out=out.concat("\t"); | + | for(a=0;a<t.length;a++) |
- | out=out.concat("}\n"); | + | { |
- | newLine=true; | + | //donvt copy spaces and tabs, if not in a noCode block |
- | continue; | + | 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; |
- | } | + | |
- | if(newLine&&t[a]!="\n") | + | //detect wether the char is negated by a backslash or not |
- | { | + | var backslashed=false; |
- | for(b=0;b<i;b++)out=out.concat("\t"); | + | for(b=out.length-1;out[b]=="\\";b--)backslashed=!backslashed; |
- | newLine=false; | + | |
- | } | + | //detect start of a noCode block |
- | if(t[a]==";"&&inFor<1) | + | if((t[a]=="\""||t[a]=="\'")&&!noCode) |
- | { | + | { |
- | out=out.concat(";\n"); | + | noCode=true; |
- | newLine=true; | + | starter=t[a];if(out[out.length-1]=="\n")indentLine(); |
- | inFor=-1; | + | } |
- | continue; | + | else if(!noCode&&t[a]=="/"&&(t[a+1]=="/"||t[a+1]=="*")) |
- | } | + | { |
- | if(t.slice(a,a+4)=="for(") | + | noCode=true; |
- | { | + | starter=t.slice(a,a+2); |
- | inFor=0; | + | if(out[out.length-1]=="\n")indentLine(); |
- | } | + | } |
- | if(inFor>=0&&t[a]=="(") | + | //detect end of a noCode block |
- | { | + | else if(noCode&&(t[a]==starter||(starter=="/*"&&t.slice(a,a+2)=="*/")||(starter=="//"&&t[a]=="\n"))&&!backslashed) |
- | inFor+=1; | + | { |
- | } | + | noCode=false; |
- | if(inFor>=0&&t[a]==")") | + | } |
- | { | + | |
- | inFor-=1; | + | //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 | ||
} | } | ||
} | } | ||
- | out=out.concat(t[a]); | ||
} | } | ||
- | while(out[out.length-1]=="\n")out=out.slice(0,out.length-1); | + | |
- | prompt("",out); | + | //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> | </sxh> |