#35 editor wip
[fanfix.git] / src / jexer / teditor / Line.java
index d6016c22ee701c478bf86bd18597f87117b7260a..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.
      */
@@ -65,6 +70,42 @@ public class Line {
      */
     private StringBuilder rawText;
 
+    // ------------------------------------------------------------------------
+    // Constructors -----------------------------------------------------------
+    // ------------------------------------------------------------------------
+
+    /**
+     * 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
+     */
+    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.
      *
@@ -105,7 +146,7 @@ public class Line {
      * @return the number of cells needed to display this line
      */
     public int getDisplayLength() {
-        int n = rawText.length();
+        int n = StringUtils.width(rawText.toString());
 
         // For now just return the raw text length.
         if (n > 0) {
@@ -132,8 +173,9 @@ public class Line {
         words.clear();
         Word word = new Word(this.defaultColor, this.highlighter);
         words.add(word);
-        for (int i = 0; i < rawText.length(); i++) {
-            char ch = rawText.charAt(i);
+        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);
@@ -145,34 +187,6 @@ public class Line {
         }
     }
 
-    /**
-     * 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
-     */
-    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);
-    }
-
     /**
      * Decrement the cursor by one.  If at the first column, do nothing.
      *
@@ -182,7 +196,7 @@ public class Line {
         if (cursor == 0) {
             return false;
         }
-        cursor--;
+        cursor -= StringUtils.width(rawText.codePointAt(cursor - 1));
         return true;
     }
 
@@ -198,7 +212,11 @@ public class Line {
         if (cursor == getDisplayLength() - 1) {
             return false;
         }
-        cursor++;
+        if (cursor < getDisplayLength()) {
+            cursor += StringUtils.width(rawText.codePointAt(cursor));
+        } else {
+            cursor++;
+        }
         return true;
     }
 
@@ -238,7 +256,9 @@ public class Line {
         assert (words.size() > 0);
 
         if (cursor < getDisplayLength()) {
-            rawText.deleteCharAt(cursor);
+            for (int i = 0; i < Character.charCount(rawText.charAt(cursor)); i++) {
+                rawText.deleteCharAt(cursor);
+            }
         }
 
         // Re-scan the line to determine the new word boundaries.
@@ -259,11 +279,11 @@ public class Line {
      *
      * @param ch the character to insert
      */
-    public void addChar(final char ch) {
+    public void addChar(final int ch) {
         if (cursor < getDisplayLength() - 1) {
-            rawText.insert(cursor, ch);
+            rawText.insert(cursor, Character.toChars(ch));
         } else {
-            rawText.append(ch);
+            rawText.append(Character.toChars(ch));
         }
         scanLine();
         cursor++;
@@ -274,11 +294,14 @@ public class Line {
      *
      * @param ch the character to replace
      */
-    public void replaceChar(final char ch) {
+    public void replaceChar(final int ch) {
         if (cursor < getDisplayLength() - 1) {
-            rawText.setCharAt(cursor, ch);
+            for (int i = 0; i < Character.charCount(rawText.charAt(cursor)); i++) {
+                rawText.deleteCharAt(cursor);
+            }
+            rawText.insert(cursor, Character.toChars(ch));
         } else {
-            rawText.append(ch);
+            rawText.append(Character.toChars(ch));
         }
         scanLine();
         cursor++;