#25 fix OOB on mouse down
authorKevin Lamonte <kevin.lamonte@gmail.com>
Wed, 27 Dec 2017 23:36:33 +0000 (18:36 -0500)
committerKevin Lamonte <kevin.lamonte@gmail.com>
Wed, 27 Dec 2017 23:36:33 +0000 (18:36 -0500)
src/jexer/TEditorWidget.java
src/jexer/teditor/Document.java

index 5ab7a239f76721fac9b2bd91ac3125a743dc4154..dcf5e9e6d267380d3b541bf275d4cdef67f8dcea 100644 (file)
@@ -164,7 +164,7 @@ public class TEditorWidget extends TWidget {
 
             document.setLineNumber(newLine);
             setCursorY(mouse.getY());
-            if (newX > document.getCurrentLine().getDisplayLength()) {
+            if (newX >= document.getCurrentLine().getDisplayLength()) {
                 document.end();
                 alignCursor();
             } else {
index c84c20787e6453e987db5fcec7f01911b356e959..e1a28a0d37f39504c482da627c880110e3d1a9d8 100644 (file)
@@ -185,7 +185,11 @@ public class Document {
      * @param cursor the new cursor position
      */
     public void setCursor(final int cursor) {
-        lines.get(lineNumber).setCursor(cursor);
+        if (cursor >= lines.get(lineNumber).getDisplayLength()) {
+            lines.get(lineNumber).end();
+        } else {
+            lines.get(lineNumber).setCursor(cursor);
+        }
     }
 
     /**
@@ -215,7 +219,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 +243,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 +262,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 +286,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);