// PSimpleIndenter has such a method but it's not exposed in the base class. private boolean isBlockBegin(CharSequence lineToTheLeft) { if (lineToTheLeft.length() == 0) { return false; } char lastChar = lineToTheLeft.charAt(lineToTheLeft.length() - 1); if (PBracketUtilities.isOpenBracket(lastChar) == false) { return false; } char closeBracket = PBracketUtilities.getPartnerForBracket(lastChar); if (textArea.getIndenter().isElectric(closeBracket) == false) { return false; } return true; }
public void insertNewline(boolean fixIndentation) { textArea.getTextBuffer().getUndoBuffer().startCompoundEdit(); try { final int startPosition = textArea.getSelectionStart(); CharSequence chars = textArea.getTextBuffer(); int startLineIndex = textArea.getLineOfOffset(startPosition); int startLineStartOffset = textArea.getLineStartOffset(startLineIndex); CharSequence lineToTheLeft = chars.subSequence(startLineStartOffset, startPosition); if (isBlockBegin(lineToTheLeft) && insertMatchingBrackets()) { return; } if (isUnclosedComment(chars, startPosition, lineToTheLeft)) { insertMatchingCloseComment(); } else { textArea.replaceSelection( "\n" + textArea.getIndenter().getCurrentIndentationOfLine(startLineIndex)); if (textArea.getIndenter().canOnlyAutoIndent()) { // The other indenters all get a chance to fix the current line's indentation before // suggesting an auto-indent level for this line. // It's possible that not differentiating between these two cases when calling the // indenter is a design error. // It's also possible that allowing this code to modify lines other than the // newly-inserted line is a design error. // We certainly make it annoyingly hard for the user to override us even when they want // to, though maybe if our indenters were better no-one would care? textArea.getIndenter().fixIndentation(); } else if (fixIndentation) { textArea.getIndenter().fixIndentationOnLine(startLineIndex); textArea.getIndenter().fixIndentation(); } else { new PCopyingIndenter(textArea).fixIndentation(); } } } finally { textArea.getTextBuffer().getUndoBuffer().finishCompoundEdit(); } }