+ /**
+ * 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();
+ }
+
+ /**
+ * Trim trailing whitespace from lines and trailing empty
+ * lines from the document.
+ */
+ public void cleanWhitespace() {
+ for (Line line: getLines()) {
+ line.trimRight();
+ }
+ if (lines.size() == 0) {
+ return;
+ }
+ while (lines.get(lines.size() - 1).length() == 0) {
+ lines.remove(lines.size() - 1);
+ }
+ if (lineNumber > lines.size() - 1) {
+ lineNumber = lines.size() - 1;
+ }
+ }
+
+ /**
+ * Set keyword highlighting.
+ *
+ * @param enabled if true, enable keyword highlighting
+ */
+ public void setHighlighting(final boolean enabled) {
+ highlighter.setEnabled(enabled);
+ for (Line line: getLines()) {
+ line.scanLine();
+ }
+ }
+
+ /**
+ * Convert a string with leading spaces to a mix of tabs and spaces.
+ *
+ * @param string the string to convert
+ */
+ private String convertSpacesToTabs(final String string) {
+ if (string.length() == 0) {
+ return string;
+ }
+
+ int start = 0;
+ while (string.charAt(start) == ' ') {
+ start++;
+ }
+ int tabCount = start / 8;
+ if (tabCount == 0) {
+ return string;
+ }
+
+ StringBuilder sb = new StringBuilder(string.length());
+
+ for (int i = 0; i < tabCount; i++) {
+ sb.append('\t');
+ }
+ sb.append(string.substring(tabCount * 8));
+ return sb.toString();
+ }
+