====== Differences ====== This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
script_autoformat [2014/07/23 15:40] lm13 |
script_autoformat [2014/09/24 16:08] (current) lm13 [Issues and Hints] |
||
---|---|---|---|
Line 2: | Line 2: | ||
* Purpose : This script will autoindent and remove unneeded spaces and tabs 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.1 | + | * 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 | + | * 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 13: | 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 noCode=false; | + | 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 starter; | ||
- | var newLine=true; | + | var newLine; |
- | for(a=0;a<t.length;a++) | + | |
- | { | + | //handle user selection |
- | if(!noCode&&((t[a]==" "&&(out[out.length-1].toLowerCase()==out[out.length-1].toUpperCase()||t[a+1].toLowerCase()==t[a+1].toUpperCase()))||(t[a]=="\t")))continue; | + | var listener=new DialogInterface.OnClickListener(){ |
- | if((t[a]=="\""||t[a]=="\'")&&!noCode) | + | onClick:function(dialog,id) |
{ | { | ||
- | noCode=true; | + | var selected; |
- | starter=t[a]; | + | if(id==0)selected=items.slice(1,items.length);//all scripts selected |
- | } | + | else selected=[items[id]];//single script selected |
- | else if(!noCode&&t[a]=="/"&&(t[a+1]=="/"||t[a+1]=="*")) | + | for(var y=0;y<selected.length;y++) |
- | { | + | |
- | noCode=true; | + | |
- | starter=t.slice(a,a+2); | + | |
- | } | + | |
- | else if(noCode&&(t[a]==starter||(starter=="/*"&&t.slice(a,a+2)=="*/")||(starter=="//"&&t[a]=="\n"))&&(t[a-1]!="\\"||t.slice(a-2,a)=="\\\\")) | + | |
- | { | + | |
- | noCode=false; | + | |
- | } | + | |
- | if(!noCode) | + | |
- | { | + | |
- | if(t[a]=="\n") | + | |
{ | { | ||
- | newLine=true; | + | s=LL.getScriptByName(selected[y]); |
- | out=out.concat(t[a]); | + | t=s.getText();//original text |
- | continue; | + | 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 | ||
} | } | ||
- | if(t[a]=="}")i--; | ||
- | if(newLine&&t[a]!="\n") | ||
- | { | ||
- | for(b=0;b<i;b++) | ||
- | out=out.concat("\t"); | ||
- | newLine=false; | ||
- | } | ||
- | if(t[a]=="{")i++; | ||
} | } | ||
- | 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> |