User Tools

Site Tools


script_autoformat

====== Differences ====== This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
script_autoformat [2014/08/29 11:57]
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.3+  * Current Version : 2.0
   * Link : https://​plus.google.com/​115366157037831519359/​posts/​RjizoNAYKsc   * Link : https://​plus.google.com/​115366157037831519359/​posts/​RjizoNAYKsc
  
Line 10: Line 10:
   * Version 1.2 (25/​7/​2014):​ fix for backslashes,​ keywords, added switch support   * 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 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 15: 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 if not using directWrite   * 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!//​ //please report all bugs in the g+ community!//​
Line 25: Line 32:
 //endconfig //endconfig
  
-var s=LL.getScriptByName(prompt("Which script?",""​)); +//​Initialize List 
-try+//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; 
-var switchLevel=0; + 
-for(a=0;​a<​t.length;​a++) +//handle user selection 
-+var listener=new DialogInterface.OnClickListener(){ 
- 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;​ + onClick:​function(dialog,id)
- var backslashed=false;​ +
- for(b=out.length-1;​out[b]=="​\\";​b--)backslashed=!backslashed;​if((t[a]=="​\""​||t[a]=="​\'"​)&&​!noCode)+
  {  {
- 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"​))&&​!backslashed) +
- { +
- noCode=false;​ +
-+
- if(!noCode) +
-+
- if(t[a]=="​\n"​) +
-+
- newLine=true; +
- out=out.concat(t[a]); +
- continue;​ +
-+
- if(t[a]=="​}"​)+
  {  {
- i--+ s=LL.getScriptByName(selected[y])
- if(i<=switchLevel&&​switchLevel>​0)+ 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++)
  {  {
- switchLevel=0;​ + //donvt copy spaces and tabs, if not in a noCode block 
- i--;+ 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(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); + 
-if(directWrite)+//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)
 { {
- s.setText(out); + if(a.toLowerCase()>​b.toLowerCase())return 1
- Android.makeNewToast("​Done!",​true).show();+ if(a.toLowerCase()<b.toLowerCase())return -1; 
 + return 0;
 } }
-else prompt("",​out);​ 
 </​sxh>​ </​sxh>​
script_autoformat.1409313478.txt.gz · Last modified: 2014/08/29 11:57 by lm13