X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2Fteditor%2FDocument.java;h=e94950371af02507ad1abaad4f305983e8260274;hb=ed76cd41d2764d6c5d3ef03449723579af9a2197;hp=bd8e91f7a48fa575747fca4e7a0cfb8e22c4248a;hpb=615a0d99fd0aa4437116dd083147f9150d5e6527;p=fanfix.git diff --git a/src/jexer/teditor/Document.java b/src/jexer/teditor/Document.java index bd8e91f..e949503 100644 --- a/src/jexer/teditor/Document.java +++ b/src/jexer/teditor/Document.java @@ -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"), @@ -89,7 +89,8 @@ public class Document { public Document(final String str, final CellAttributes defaultColor) { this.defaultColor = defaultColor; - // TODO: set different colors based on file extension + // Set colors to resemble the Borland IDE colors, but for Java + // language keywords. highlighter.setJavaColors(); String [] rawLines = str.split("\n"); @@ -120,6 +121,13 @@ public class Document { return dirty; } + /** + * Unset the dirty flag. + */ + public void setNotDirty() { + dirty = false; + } + /** * Save contents to file. * @@ -209,6 +217,15 @@ public class Document { return lines.get(lineNumber).getCursor(); } + /** + * Get the character at the current cursor position in the text. + * + * @return the character, or -1 if the cursor is at the end of the line + */ + public int getChar() { + return lines.get(lineNumber).getChar(); + } + /** * Set the current cursor position of the editing line. 0-based. * @@ -309,7 +326,8 @@ public class Document { } /** - * Decrement the cursor by one. If at the first column, do nothing. + * Decrement the cursor by one. If at the first column on the first + * line, do nothing. * * @return true if the cursor position changed */ @@ -326,7 +344,8 @@ public class Document { } /** - * Increment the cursor by one. If at the last column, do nothing. + * Increment the cursor by one. If at the last column on the last line, + * do nothing. * * @return true if the cursor position changed */ @@ -342,6 +361,151 @@ public class Document { return true; } + /** + * Go back to the beginning of this word if in the middle, or the + * beginning of the previous word. + */ + public void backwardsWord() { + + // If at the beginning of a word already, push past it. + if ((getChar() != -1) + && (getRawLine().length() > 0) + && !Character.isWhitespace((char) getChar()) + ) { + left(); + } + + // int line = lineNumber; + while ((getChar() == -1) + || (getRawLine().length() == 0) + || Character.isWhitespace((char) getChar()) + ) { + if (left() == false) { + return; + } + } + + + assert (getChar() != -1); + + if (!Character.isWhitespace((char) getChar()) + && (getRawLine().length() > 0) + ) { + // Advance until at the beginning of the document or a whitespace + // is encountered. + while (!Character.isWhitespace((char) getChar())) { + int line = lineNumber; + if (left() == false) { + // End of document, bail out. + return; + } + if (lineNumber != line) { + // We wrapped a line. Here that counts as whitespace. + right(); + return; + } + } + } + + // We went one past the word, push back to the first character of + // that word. + right(); + return; + } + + /** + * Go to the beginning of the next word. + */ + public void forwardsWord() { + int line = lineNumber; + while ((getChar() == -1) + || (getRawLine().length() == 0) + ) { + if (right() == false) { + return; + } + if (lineNumber != line) { + // We wrapped a line. Here that counts as whitespace. + if (!Character.isWhitespace((char) getChar())) { + // We found a character immediately after the line. + // Done! + return; + } + // Still looking... + line = lineNumber; + } + } + assert (getChar() != -1); + + if (!Character.isWhitespace((char) getChar()) + && (getRawLine().length() > 0) + ) { + // Advance until at the end of the document or a whitespace is + // encountered. + while (!Character.isWhitespace((char) getChar())) { + line = lineNumber; + if (right() == false) { + // End of document, bail out. + return; + } + if (lineNumber != line) { + // We wrapped a line. Here that counts as whitespace. + if (!Character.isWhitespace((char) getChar()) + && (getRawLine().length() > 0) + ) { + // We found a character immediately after the line. + // Done! + return; + } + break; + } + } + } + + while ((getChar() == -1) + || (getRawLine().length() == 0) + ) { + if (right() == false) { + return; + } + if (lineNumber != line) { + // We wrapped a line. Here that counts as whitespace. + if (!Character.isWhitespace((char) getChar())) { + // We found a character immediately after the line. + // Done! + return; + } + // Still looking... + line = lineNumber; + } + } + assert (getChar() != -1); + + if (Character.isWhitespace((char) getChar())) { + // Advance until at the end of the document or a non-whitespace + // is encountered. + while (Character.isWhitespace((char) getChar())) { + if (right() == false) { + // End of document, bail out. + return; + } + } + return; + } + + // We wrapped the line to get here. + return; + } + + /** + * Get the raw string that matches this line. + * + * @return the string + */ + public String getRawLine() { + return lines.get(lineNumber).getRawString(); + } + /** * Go to the first column of this line. * @@ -413,7 +577,7 @@ public class Document { */ public void enter() { dirty = true; - int cursor = lines.get(lineNumber).getCursor(); + int cursor = lines.get(lineNumber).getRawCursor(); String original = lines.get(lineNumber).getRawString(); String firstLine = original.substring(0, cursor); String secondLine = original.substring(cursor); @@ -430,7 +594,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); @@ -481,4 +645,18 @@ public class Document { return lines.get(lineNumber).getDisplayLength(); } + /** + * Get the entire contents of the document as one string. + * + * @return the document contents + */ + public String getText() { + StringBuilder sb = new StringBuilder(); + for (Line line: getLines()) { + sb.append(line.getRawString()); + sb.append("\n"); + } + return sb.toString(); + } + }