#35 editor wip
[fanfix.git] / src / jexer / teditor / Line.java
index de1265982c67e3190a122a1dae0e692ba7e2a17e..345cfcdfd5048ad1c12b0d5622cca11d157cf1f3 100644 (file)
@@ -3,7 +3,7 @@
  *
  * The MIT License (MIT)
  *
- * Copyright (C) 2017 Kevin Lamonte
+ * Copyright (C) 2019 Kevin Lamonte
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
@@ -32,6 +32,7 @@ import java.util.ArrayList;
 import java.util.List;
 
 import jexer.bits.CellAttributes;
+import jexer.bits.StringUtils;
 
 /**
  * A Line represents a single line of text on the screen, as a collection of
@@ -39,6 +40,10 @@ import jexer.bits.CellAttributes;
  */
 public class Line {
 
+    // ------------------------------------------------------------------------
+    // Variables --------------------------------------------------------------
+    // ------------------------------------------------------------------------
+
     /**
      * The list of words.
      */
@@ -60,14 +65,55 @@ public class Line {
     private int cursor = 0;
 
     /**
-     * The current word that the cursor position is in.
+     * The raw text of this line, what is passed to Word to determine
+     * highlighting behavior.
      */
-    private Word currentWord;
+    private StringBuilder rawText;
+
+    // ------------------------------------------------------------------------
+    // Constructors -----------------------------------------------------------
+    // ------------------------------------------------------------------------
 
     /**
-     * We use getDisplayLength() a lot, so cache the value.
+     * Construct a new Line from an existing text string, and highlight
+     * certain strings.
+     *
+     * @param str the text string
+     * @param defaultColor the color for unhighlighted text
+     * @param highlighter the highlighter to use
      */
-    private int displayLength = -1;
+    public Line(final String str, final CellAttributes defaultColor,
+        final Highlighter highlighter) {
+
+        this.defaultColor = defaultColor;
+        this.highlighter = highlighter;
+        this.rawText = new StringBuilder(str);
+
+        scanLine();
+    }
+
+    /**
+     * Construct a new Line from an existing text string.
+     *
+     * @param str the text string
+     * @param defaultColor the color for unhighlighted text
+     */
+    public Line(final String str, final CellAttributes defaultColor) {
+        this(str, defaultColor, null);
+    }
+
+    // ------------------------------------------------------------------------
+    // Line -------------------------------------------------------------------
+    // ------------------------------------------------------------------------
+
+    /**
+     * Get a (shallow) copy of the words in this line.
+     *
+     * @return a copy of the word list
+     */
+    public List<Word> getWords() {
+        return new ArrayList<Word>(words);
+    }
 
     /**
      * Get the current cursor position.
@@ -92,16 +138,6 @@ public class Line {
                 getDisplayLength() + ", requested position " + cursor);
         }
         this.cursor = cursor;
-        // TODO: set word
-    }
-
-    /**
-     * Get a (shallow) copy of the list of words.
-     *
-     * @return the list of words
-     */
-    public List<Word> getWords() {
-        return new ArrayList<Word>(words);
     }
 
     /**
@@ -110,62 +146,47 @@ public class Line {
      * @return the number of cells needed to display this line
      */
     public int getDisplayLength() {
-        if (displayLength != -1) {
-            return displayLength;
-        }
-        int n = 0;
-        for (Word word: words) {
-            n += word.getDisplayLength();
-        }
-        displayLength = n;
+        int n = StringUtils.width(rawText.toString());
 
-        // If we have any visible characters, add one to the display so that
-        // the cursor is immediately after the data.
-        if (displayLength > 0) {
-            displayLength++;
+        // For now just return the raw text length.
+        if (n > 0) {
+            // If we have any visible characters, add one to the display so
+            // that the cursor is immediately after the data.
+            return n + 1;
         }
-        return displayLength;
+        return n;
     }
 
     /**
-     * Construct a new Line from an existing text string, and highlight
-     * certain strings.
+     * Get the raw string that matches this line.
      *
-     * @param str the text string
-     * @param defaultColor the color for unhighlighted text
-     * @param highlighter the highlighter to use
+     * @return the string
      */
-    public Line(final String str, final CellAttributes defaultColor,
-        final Highlighter highlighter) {
-
-        this.defaultColor = defaultColor;
-        this.highlighter = highlighter;
+    public String getRawString() {
+        return rawText.toString();
+    }
 
-        currentWord = new Word(this.defaultColor, this.highlighter);
-        words.add(currentWord);
-        for (int i = 0; i < str.length(); i++) {
-            char ch = str.charAt(i);
-            Word newWord = currentWord.addChar(ch);
-            if (newWord != currentWord) {
+    /**
+     * Scan rawText and make words out of it.
+     */
+    private void scanLine() {
+        words.clear();
+        Word word = new Word(this.defaultColor, this.highlighter);
+        words.add(word);
+        for (int i = 0; i < rawText.length();) {
+            int ch = rawText.codePointAt(i);
+            i += Character.charCount(ch);
+            Word newWord = word.addChar(ch);
+            if (newWord != word) {
                 words.add(newWord);
-                currentWord = newWord;
+                word = newWord;
             }
         }
-        for (Word word: words) {
-            word.applyHighlight();
+        for (Word w: words) {
+            w.applyHighlight();
         }
     }
 
-    /**
-     * Construct a new Line from an existing text string.
-     *
-     * @param str the text string
-     * @param defaultColor the color for unhighlighted text
-     */
-    public Line(final String str, final CellAttributes defaultColor) {
-        this(str, defaultColor, null);
-    }
-
     /**
      * Decrement the cursor by one.  If at the first column, do nothing.
      *
@@ -175,8 +196,7 @@ public class Line {
         if (cursor == 0) {
             return false;
         }
-        // TODO: switch word
-        cursor--;
+        cursor -= StringUtils.width(rawText.codePointAt(cursor - 1));
         return true;
     }
 
@@ -192,8 +212,11 @@ public class Line {
         if (cursor == getDisplayLength() - 1) {
             return false;
         }
-        // TODO: switch word
-        cursor++;
+        if (cursor < getDisplayLength()) {
+            cursor += StringUtils.width(rawText.codePointAt(cursor));
+        } else {
+            cursor++;
+        }
         return true;
     }
 
@@ -205,7 +228,6 @@ public class Line {
     public boolean home() {
         if (cursor > 0) {
             cursor = 0;
-            currentWord = words.get(0);
             return true;
         }
         return false;
@@ -222,7 +244,6 @@ public class Line {
             if (cursor < 0) {
                 cursor = 0;
             }
-            currentWord = words.get(words.size() - 1);
             return true;
         }
         return false;
@@ -232,14 +253,25 @@ public class Line {
      * Delete the character under the cursor.
      */
     public void del() {
-        // TODO
+        assert (words.size() > 0);
+
+        if (cursor < getDisplayLength()) {
+            for (int i = 0; i < Character.charCount(rawText.charAt(cursor)); i++) {
+                rawText.deleteCharAt(cursor);
+            }
+        }
+
+        // Re-scan the line to determine the new word boundaries.
+        scanLine();
     }
 
     /**
      * Delete the character immediately preceeding the cursor.
      */
     public void backspace() {
-        // TODO
+        if (left()) {
+            del();
+        }
     }
 
     /**
@@ -247,8 +279,14 @@ public class Line {
      *
      * @param ch the character to insert
      */
-    public void addChar(final char ch) {
-        // TODO
+    public void addChar(final int ch) {
+        if (cursor < getDisplayLength() - 1) {
+            rawText.insert(cursor, Character.toChars(ch));
+        } else {
+            rawText.append(Character.toChars(ch));
+        }
+        scanLine();
+        cursor++;
     }
 
     /**
@@ -256,8 +294,17 @@ public class Line {
      *
      * @param ch the character to replace
      */
-    public void replaceChar(final char ch) {
-        // TODO
+    public void replaceChar(final int ch) {
+        if (cursor < getDisplayLength() - 1) {
+            for (int i = 0; i < Character.charCount(rawText.charAt(cursor)); i++) {
+                rawText.deleteCharAt(cursor);
+            }
+            rawText.insert(cursor, Character.toChars(ch));
+        } else {
+            rawText.append(Character.toChars(ch));
+        }
+        scanLine();
+        cursor++;
     }
 
 }