#35 editor wip
[fanfix.git] / src / jexer / teditor / Document.java
index c8ab746096f20971871fe8da99efbe67dd4fcade..159a95f6d95252cc4a01f7903d476ba784ce149a 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"),
@@ -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);
@@ -414,7 +430,7 @@ public class Document {
      *
      * @param ch the character to replace or insert
      */
-    public void addChar(final char ch) {
+    public void addChar(final int ch) {
         dirty = true;
         if (overwrite) {
             lines.get(lineNumber).replaceChar(ch);
@@ -456,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();
+    }
+
 }