add maxLength argument to Input.showRegion()
[sbp.git] / src / edu / berkeley / sbp / chr / CharInput.java
index 10150d5..3ebbb88 100644 (file)
@@ -59,17 +59,23 @@ public class CharInput extends Cartesian.Input<Character> {
 
     private final RollbackReader r;
     
-    public CharInput(String s)                { this(new StringReader(s)); }
-    public CharInput(Reader r)                { this(r, null); }
-    public CharInput(Reader r,      String s) { this.r = new RollbackReader(new BufferedReader(r)); }
-    public CharInput(InputStream i)           { this(i, null); }
-    public CharInput(InputStream i, String s) { this(new InputStreamReader(i), s); }
+    public CharInput(Reader r,      String s) {
+        this.name = s;
+        this.r = new RollbackReader(new BufferedReader(r));
+    }
+    public CharInput(String s)                  { this(new StringReader(s)); }
+    public CharInput(Reader r)                  { this(r, null); }
+    public CharInput(InputStream i)             { this(i, null); }
+    public CharInput(InputStream i, String s)   { this(new InputStreamReader(i), s); }
+    public CharInput(File f) throws IOException { this(new FileInputStream(f), f.getName()); }
 
     public CharInput(InputStream i, String s, boolean indent) {
         this(new InputStreamReader(i), s);
         this.indent = indent;
     }
+    public String getName() { return name; }
 
+    private String name;
     boolean cr = false;
     boolean indent = false;
     private int count = 0;
@@ -82,8 +88,6 @@ public class CharInput extends Cartesian.Input<Character> {
 
     int indentation = -1;
     int lastIndentation = 0;
-    int queuedIndentation = 0;
-    char queuedCharacter = ' ';
     int delta = 0;
 
     public boolean   isCR() { return cr; }
@@ -102,31 +106,10 @@ public class CharInput extends Cartesian.Input<Character> {
 
         cr = false;
 
-        if (queuedIndentation < 0) {
-            queuedIndentation++;
-            return CharAtom.right;
-        }
-        if (queuedSpaces > 0) {
-            queuedSpaces--;
-            return ' ';
-        }
-        if (queuedIndentation > 0) {
-            queuedIndentation--;
-            return CharAtom.left;
-        }
-
-        if (queuedCharacter != ' ') {
-            char ret = queuedCharacter;
-            queuedCharacter = ' ';
-            return ret;
-        }
-
         int i = r.read();
         if (i==-1) {
-            /*System.err.print("\r...done       \r"); */
             if (indent && indentation >= 0) {
                 redent(indentation - lastIndentation);
-                //System.err.println("\r                   \rindent: " + (indentation - lastIndentation));
                 lastIndentation = indentation;
                 indentation = -1;
                 return __next();
@@ -138,14 +121,27 @@ public class CharInput extends Cartesian.Input<Character> {
         cr = c=='\n';
 
         if (indent)
-            if (cr) {
-                indentation = 0;
-            } else if (c==' ' && indentation >= 0) {
-                indentation++;
-            } else if (indentation >= 0) {
-                //System.err.println("\r                   \rindent: " + (indentation - lastIndentation));
-                redent(indentation - lastIndentation);
-                r.unread(c);
+            if (cr && ignore) {
+                ignore = false;
+            } else if (cr) {
+                while(true) {
+                    indentation = 0;
+                    do { i = r.read(); if (i==' ') indentation++; } while (i==' ');
+                    if (i=='\n') { /* FIXME */ continue; }
+                    if (i==-1) { /* FIXME */ }
+                     if (indentation - lastIndentation > 0) {
+                        r.unread('\n');
+                        for(int j=0; j<indentation; j++) r.unread(' ');
+                        redent(indentation - lastIndentation);
+                    } else {
+                        redent(indentation - lastIndentation);
+                        r.unread('\n');
+                        for(int j=0; j<indentation; j++) r.unread(' ');
+                    }
+                    if (i != -1) r.unread((char)i);
+                    ignore = true;
+                    break;
+                }
                 lastIndentation = indentation;
                 indentation = -1;
                 return __next();
@@ -154,14 +150,14 @@ public class CharInput extends Cartesian.Input<Character> {
         return c;
     }
 
+    private boolean ignore = false;
+
     private void redent(int i) {
         if (i<0) { r.unread(CharAtom.right); redent(i+1); return; }
         if (i>0) { r.unread(CharAtom.left); redent(i-1); return; }
     }
 
-    int queuedSpaces = 0;
-
-    public String showRegion(Region<Character> rc) {
+    public String showRegion(Region<Character> rc, int maxLength) {
         if (cache == null) return null;
         Cartesian.Region r = (Cartesian.Region)rc;
         int start = r.getStart().getScalar()+1;
@@ -170,10 +166,10 @@ public class CharInput extends Cartesian.Input<Character> {
         if (end < start) end = start;
         if (end > cache.length()) end = cache.length();
         String ret;
-        if (end-start < 60) ret = cachesubstring(start, end);
-        else ret = cachesubstring(start, start+25) +
+        if (end-start < maxLength) ret = cachesubstring(start, end);
+        else ret = cachesubstring(start, start+(maxLength/2-5)) +
                  "..." +
-                 cachesubstring(end-25, end);
+                 cachesubstring(end-(maxLength/2-5), end);
         return StringUtil.escapify(ret, "\n\r");
     }