Home

Advertisement

My beanshell indenter, alpha version

  • May. 9th, 2008 at 12:59 AM
Just finished the first alpha version of my beanshell source code indenter for the TAS!
It can transform code like this:
   1:          print("Hello world");
   2:    if(player.someAttribute == "someValue") { if(true) {a++;} doFirst();
   3:     doIt();
   4:            for ( Object typedElement: foo )   {
   5:        print( typedElement );}
   6:        
   7:                  print("Hi Duke!");
   8:
   9: } else {
  10:     doThat(); if(false) {run(a); b--;}}
Into this:
   1:print("Hello world");
   2:if(player.someAttribute == "someValue") {
   3:    if(true) {
   4:        a++;
   5:    }
   6:    doFirst();
   7:    doIt();
   8:    for ( Object typedElement: foo ) {
   9:        print( typedElement );
  10:    }
  11:    
  12:    print("Hi Duke!");
  13:    
  14:}
  15:else {
  16:    doThat();
  17:    if(false) {
  18:        run(a);
  19:        b--;
  20:    }
  21:}
  22:
Here is the hackish code (forgive the quality of it, it had less than a couple of hours of my attention so far):
   1:/**
   2: * Created by Bruno Patini Furtado [http://bpfurtado.livejournal.com]
   3: * Created on 08/05/2008 21:50:46
   4: */
   5:package net.bpfurtado.bsh.indenter;
   6:
   7:import static net.bpfurtado.commons.io.FileUtils.linesOf;
   8:
   9:import java.io.File;
  10:import java.util.regex.Matcher;
  11:import java.util.regex.Pattern;
  12:
  13:public class Indenter
  14:{
  15:    private int level;
  16:
  17:    public String indent(File file)
  18:    {
  19:        StringBuilder buffer = new StringBuilder();
  20:        for (String line : linesOf(file)) {
  21:            buffer.append(indent(line));
  22:        }
  23:        return buffer.toString();
  24:    }
  25:
  26:    private String indent(String origLine)
  27:    {
  28:        StringBuilder lines = new StringBuilder();
  29:
  30:        origLine = origLine.trim();
  31:        Matcher regexMatcher = Pattern.compile("([\\{\\};])").matcher(origLine);
  32:
  33:        String line = null;
  34:        boolean found = regexMatcher.find();
  35:        if (!found) {
  36:            line = tabs(level) + origLine;
  37:            lines.append(line + "\n");
  38:            lines.toString();
  39:        }
  40:
  41:        int lastIdx = 0;
  42:        while (found) {
  43:            int idx = -1;
  44:            String sign = regexMatcher.group(0);
  45:            if (sign.equals("{")) {
  46:                this.level++;
  47:                idx = regexMatcher.start();
  48:                line = tabs(level - 1) + origLine.substring(lastIdx, idx).trim() + " " + sign + "\n";
  49:                lines.append(line);
  50:                lastIdx = idx + 1;
  51:            } else if (sign.equals("}")) {
  52:                idx = regexMatcher.start();
  53:                String lineCore = origLine.substring(lastIdx, idx).trim();
  54:                if (lineCore.length() > 0) {
  55:                    line = tabs(level) + lineCore + "\n";
  56:                    lines.append(line);
  57:                }
  58:
  59:                this.level--;
  60:                lines.append(tabs(level) + sign + "\n");
  61:                lastIdx = idx + 1;
  62:            } else {
  63:                idx = regexMatcher.start() + 1;
  64:                line = tabs(level) + origLine.substring(lastIdx, idx).trim() + "\n";
  65:                lines.append(line);
  66:                lastIdx = idx;
  67:            }
  68:            found = regexMatcher.find();
  69:        }
  70:        return lines.toString();
  71:    }
  72:
  73:    private static String tabs(int n)
  74:    {
  75:        StringBuilder b = new StringBuilder();
  76:        for (int i = 0; i < n; i++) {
  77:            b.append("    ");
  78:        }
  79:        return b.toString();
  80:    }
  81:}

Latest Month

November 2009
S M T W T F S
1234567
891011121314
15161718192021
22232425262728
2930     

Tags

Syndicate

RSS Atom
Powered by LiveJournal.com
Designed by Tiffany Chow