X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2Fteditor%2FDocument.java;h=bd8e91f7a48fa575747fca4e7a0cfb8e22c4248a;hb=615a0d99fd0aa4437116dd083147f9150d5e6527;hp=42082e55ad21a670b31a91e8ba6e9b7e0950344e;hpb=71a389c9810382e014682dde52e94d3f34e385fa;p=fanfix.git diff --git a/src/jexer/teditor/Document.java b/src/jexer/teditor/Document.java index 42082e5..bd8e91f 100644 --- a/src/jexer/teditor/Document.java +++ b/src/jexer/teditor/Document.java @@ -41,6 +41,10 @@ import jexer.bits.CellAttributes; */ public class Document { + // ------------------------------------------------------------------------ + // Variables -------------------------------------------------------------- + // ------------------------------------------------------------------------ + /** * The list of lines. */ @@ -72,6 +76,32 @@ public class Document { */ private Highlighter highlighter = new Highlighter(); + // ------------------------------------------------------------------------ + // Constructors ----------------------------------------------------------- + // ------------------------------------------------------------------------ + + /** + * Construct a new Document from an existing text string. + * + * @param str the text string + * @param defaultColor the color for unhighlighted text + */ + public Document(final String str, final CellAttributes defaultColor) { + this.defaultColor = defaultColor; + + // TODO: set different colors based on file extension + highlighter.setJavaColors(); + + String [] rawLines = str.split("\n"); + for (int i = 0; i < rawLines.length; i++) { + lines.add(new Line(rawLines[i], this.defaultColor, highlighter)); + } + } + + // ------------------------------------------------------------------------ + // Document --------------------------------------------------------------- + // ------------------------------------------------------------------------ + /** * Get the overwrite flag. * @@ -185,24 +215,10 @@ public class Document { * @param cursor the new cursor position */ public void setCursor(final int cursor) { - lines.get(lineNumber).setCursor(cursor); - } - - /** - * Construct a new Document from an existing text string. - * - * @param str the text string - * @param defaultColor the color for unhighlighted text - */ - public Document(final String str, final CellAttributes defaultColor) { - this.defaultColor = defaultColor; - - // TODO: set different colors based on file extension - highlighter.setJavaColors(); - - String [] rawLines = str.split("\n"); - for (int i = 0; i < rawLines.length; i++) { - lines.add(new Line(rawLines[i], this.defaultColor, highlighter)); + if (cursor >= lines.get(lineNumber).getDisplayLength()) { + lines.get(lineNumber).end(); + } else { + lines.get(lineNumber).setCursor(cursor); } } @@ -215,7 +231,7 @@ public class Document { if (lineNumber < lines.size() - 1) { int x = lines.get(lineNumber).getCursor(); lineNumber++; - if (x > lines.get(lineNumber).getDisplayLength()) { + if (x >= lines.get(lineNumber).getDisplayLength()) { lines.get(lineNumber).end(); } else { lines.get(lineNumber).setCursor(x); @@ -239,7 +255,7 @@ public class Document { if (lineNumber > lines.size() - 1) { lineNumber = lines.size() - 1; } - if (x > lines.get(lineNumber).getDisplayLength()) { + if (x >= lines.get(lineNumber).getDisplayLength()) { lines.get(lineNumber).end(); } else { lines.get(lineNumber).setCursor(x); @@ -258,7 +274,7 @@ public class Document { if (lineNumber > 0) { int x = lines.get(lineNumber).getCursor(); lineNumber--; - if (x > lines.get(lineNumber).getDisplayLength()) { + if (x >= lines.get(lineNumber).getDisplayLength()) { lines.get(lineNumber).end(); } else { lines.get(lineNumber).setCursor(x); @@ -282,7 +298,7 @@ public class Document { if (lineNumber < 0) { lineNumber = 0; } - if (x > lines.get(lineNumber).getDisplayLength()) { + if (x >= lines.get(lineNumber).getDisplayLength()) { lines.get(lineNumber).end(); } else { lines.get(lineNumber).setCursor(x); @@ -298,7 +314,15 @@ public class Document { * @return true if the cursor position changed */ public boolean left() { - return lines.get(lineNumber).left(); + if (!lines.get(lineNumber).left()) { + // We are on the leftmost column, wrap + if (up()) { + end(); + } else { + return false; + } + } + return true; } /** @@ -307,7 +331,15 @@ public class Document { * @return true if the cursor position changed */ public boolean right() { - return lines.get(lineNumber).right(); + if (!lines.get(lineNumber).right()) { + // We are on the rightmost column, wrap + if (down()) { + home(); + } else { + return false; + } + } + return true; } /** @@ -333,7 +365,19 @@ public class Document { */ public void del() { dirty = true; - lines.get(lineNumber).del(); + int cursor = lines.get(lineNumber).getCursor(); + if (cursor < lines.get(lineNumber).getDisplayLength() - 1) { + lines.get(lineNumber).del(); + } else if (lineNumber < lines.size() - 2) { + // Join two lines + StringBuilder newLine = new StringBuilder(lines. + get(lineNumber).getRawString()); + newLine.append(lines.get(lineNumber + 1).getRawString()); + lines.set(lineNumber, new Line(newLine.toString(), + defaultColor, highlighter)); + lines.get(lineNumber).setCursor(cursor); + lines.remove(lineNumber + 1); + } } /** @@ -341,7 +385,43 @@ public class Document { */ public void backspace() { dirty = true; - lines.get(lineNumber).backspace(); + int cursor = lines.get(lineNumber).getCursor(); + if (cursor > 0) { + lines.get(lineNumber).backspace(); + } else if (lineNumber > 0) { + // Join two lines + lineNumber--; + String firstLine = lines.get(lineNumber).getRawString(); + if (firstLine.length() > 0) { + // Backspacing combining two lines + StringBuilder newLine = new StringBuilder(firstLine); + newLine.append(lines.get(lineNumber + 1).getRawString()); + lines.set(lineNumber, new Line(newLine.toString(), + defaultColor, highlighter)); + lines.get(lineNumber).setCursor(firstLine.length()); + lines.remove(lineNumber + 1); + } else { + // Backspacing an empty line + lines.remove(lineNumber); + lines.get(lineNumber).setCursor(0); + } + } + } + + /** + * Split the current line into two, like pressing the enter key. + */ + public void enter() { + dirty = true; + int cursor = lines.get(lineNumber).getCursor(); + String original = lines.get(lineNumber).getRawString(); + String firstLine = original.substring(0, cursor); + String secondLine = original.substring(cursor); + lines.add(lineNumber + 1, new Line(secondLine, defaultColor, + highlighter)); + lines.set(lineNumber, new Line(firstLine, defaultColor, highlighter)); + lineNumber++; + lines.get(lineNumber).home(); } /** @@ -392,4 +472,13 @@ public class Document { return n; } + /** + * Get the current line length. + * + * @return the number of cells needed to display the current line + */ + public int getLineLength() { + return lines.get(lineNumber).getDisplayLength(); + } + }