Skip to content

Commit

Permalink
Fix #469: setClearWhitespaceLinesEnabled doesn't work if auto-indent …
Browse files Browse the repository at this point in the history
…is enabled
  • Loading branch information
bobbylight committed Dec 10, 2022
1 parent f3f91b3 commit 875dbab
Show file tree
Hide file tree
Showing 5 changed files with 797 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ private boolean appearsNested(RSyntaxTextArea textArea,

while (line<textArea.getLineCount()) {
Token t = textArea.getTokenListForLine(line);
int i = 0;
int i;
// If examining the first line, start at offs.
if (line++==firstLine) {
t = RSyntaxUtilities.getTokenAtOffset(t, offs);
Expand Down Expand Up @@ -265,7 +265,7 @@ private void insertBreakInMLC(ActionEvent e, RSyntaxTextArea textArea,
textArea.replaceSelection("\n" + header);
if (nested) {
dot = textArea.getCaretPosition(); // Has changed
textArea.insert("\n" + leadingWS + " */", dot);
textArea.insert("\n" + leadingWS + (firstMlcLine ? " " : "") + "*/", dot);
textArea.setCaretPosition(dot);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1533,77 +1533,71 @@ else if (ch=='}') {
*/
protected void handleInsertBreak(RSyntaxTextArea textArea,
boolean noSelection) {
// If we're auto-indenting...
if (noSelection && textArea.isAutoIndentEnabled()) {
insertNewlineWithAutoIndent(textArea);

if (noSelection) {
try {
handleInsertBreakWithoutSelection(textArea);
} catch (BadLocationException ble) { // Never happens
textArea.replaceSelection("\n");
ble.printStackTrace();
}
}
else {
textArea.replaceSelection("\n");
if (noSelection) {
possiblyCloseCurlyBrace(textArea, null);
}
}
}

private void insertNewlineWithAutoIndent(RSyntaxTextArea sta) {

try {
private void handleInsertBreakWithoutSelection(RSyntaxTextArea textArea)
throws BadLocationException {

int caretPos = sta.getCaretPosition();
Document doc = sta.getDocument();
Element map = doc.getDefaultRootElement();
int lineNum = map.getElementIndex(caretPos);
Element line = map.getElement(lineNum);
int start = line.getStartOffset();
int end = line.getEndOffset()-1; // Why always "-1"?
int len = end-start;
String s = doc.getText(start, len);

// endWS is the end of the leading whitespace of the
// current line.
String leadingWS = RSyntaxUtilities.getLeadingWhitespace(s);
StringBuilder sb = new StringBuilder("\n");
int caretPos = textArea.getCaretPosition();
Document doc = textArea.getDocument();
Element map = doc.getDefaultRootElement();
int lineNum = map.getElementIndex(caretPos);
Element line = map.getElement(lineNum);
int start = line.getStartOffset();
int end = line.getEndOffset()-1; // Why always "-1"?
int len = end-start;
String s = doc.getText(start, len);
int caretOffsInLine = caretPos - start;

StringBuilder sb = new StringBuilder("\n");
String leadingWS = null;
if (textArea.isAutoIndentEnabled()) {
leadingWS = RSyntaxUtilities.getLeadingWhitespace(s, caretOffsInLine);
sb.append(leadingWS);
}

// If there is only whitespace between the caret and
// the EOL, pressing Enter auto-indents the new line to
// the same place as the previous line.
int nonWhitespacePos = atEndOfLine(caretPos-start, s, len);
if (nonWhitespacePos==-1) {
if (leadingWS.length()==len &&
sta.isClearWhitespaceLinesEnabled()) {
// If the line was nothing but whitespace, select it
// so its contents get removed.
sta.setSelectionStart(start);
sta.setSelectionEnd(end);
}
sta.replaceSelection(sb.toString());
}
// If the text remaining on the line would be all whitespace,
// remove it if necessary
if (textArea.isClearWhitespaceLinesEnabled() && isAllWhitespace(s, 0, caretOffsInLine)) {
// Select all text on the line before the caret so it gets removed
textArea.setCaretPosition(start);
}

// If there is non-whitespace between the caret and the
// EOL, pressing Enter takes that text to the next line
// and auto-indents it to the same place as the last
// line.
else {
sb.append(s.substring(nonWhitespacePos));
sta.replaceRange(sb.toString(), caretPos, end);
sta.setCaretPosition(caretPos + leadingWS.length()+1);
}
// Find any non-whitespace text after the caret. If there is any, it gets put
// onto the next line. Whitespace between the caret and that text gets removed.
int nonWhitespacePos = atEndOfLine(caretPos-start, s, len);
textArea.moveCaretPosition(nonWhitespacePos > -1 ? nonWhitespacePos : end);
textArea.replaceSelection(sb.toString());

// Must do it after everything else, as the "smart indent"
// calculation depends on the previous line's state
// AFTER the Enter press (stuff may have been moved down).
if (sta.getShouldIndentNextLine(lineNum)) {
sta.replaceSelection("\t");
}
// Must do it after everything else, as the "smart indent"
// calculation depends on the previous line's state
// AFTER the Enter press (stuff may have been moved down).
if (textArea.getShouldIndentNextLine(lineNum)) {
textArea.replaceSelection("\t");
}

possiblyCloseCurlyBrace(sta, leadingWS);
possiblyCloseCurlyBrace(textArea, leadingWS);
}

} catch (BadLocationException ble) { // Never happens
sta.replaceSelection("\n");
ble.printStackTrace();
private static boolean isAllWhitespace(String str, int from, int to) {
for (int i = from; i < to; i++) {
if (!Character.isWhitespace(str.charAt(i))) {
return false;
}
}

return true;
}

private void possiblyCloseCurlyBrace(RSyntaxTextArea textArea,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,25 @@ public static Color getHyperlinkForeground() {
*
* @param text The String to check.
* @return The leading whitespace.
* @see #getLeadingWhitespace(String, int)
* @see #getLeadingWhitespace(Document, int)
*/
public static String getLeadingWhitespace(String text) {
return getLeadingWhitespace(text, Integer.MAX_VALUE);
}


/**
* Returns the leading whitespace of a string.
*
* @param text The String to check.
* @param upTo Stops checking at the specified offset.
* @return The leading whitespace.
* @see #getLeadingWhitespace(String)
*/
public static String getLeadingWhitespace(String text, int upTo) {
int count = 0;
int len = text.length();
int len = Math.min(text.length(), upTo);
while (count<len && RSyntaxUtilities.isWhitespace(text.charAt(count))) {
count++;
}
Expand Down
Loading

0 comments on commit 875dbab

Please sign in to comment.