From 3090efff97b71e050118a5d9f81182db98b7528c Mon Sep 17 00:00:00 2001 From: scztt Date: Sun, 24 May 2015 12:19:11 -0700 Subject: [PATCH] ide: better newline behavior within brackets This adds two additional conditions for creating a double-newline when hitting return w/in brackets. We should NOT add a double-newline if we're in between two closing brackets - i.e. ]], ]), etc. And, we should NOT add a double-newline if the closing bracket to the right of the cursor is the first thing on the line. This avoids several spurious double-newlines that are common in SC code (see bug #814, for example). Note that this still allows a double-newline if you're adding function arguments to a function over multiple lines. Hitting return in this case: ([1, 2], [3, 4],) (i.e. with a comma before the return) will create a double-newline. --- editors/sc-ide/widgets/code_editor/sc_editor.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/editors/sc-ide/widgets/code_editor/sc_editor.cpp b/editors/sc-ide/widgets/code_editor/sc_editor.cpp index 69be568061a..eeeb7a7494a 100644 --- a/editors/sc-ide/widgets/code_editor/sc_editor.cpp +++ b/editors/sc-ide/widgets/code_editor/sc_editor.cpp @@ -174,8 +174,15 @@ void ScCodeEditor::keyPressEvent( QKeyEvent *e ) { QTextBlock cursorBlock = cursor.block(); int cursorPosInBlock = cursor.position() - cursorBlock.position(); + + TokenIterator prevToken = TokenIterator::leftOf(cursorBlock, cursorPosInBlock); TokenIterator nextToken = TokenIterator::rightOf(cursorBlock, cursorPosInBlock); - if ( nextToken.block() == cursorBlock && nextToken.type() == Token::ClosingBracket ) + + if ( nextToken.block() == cursorBlock + && nextToken.type() == Token::ClosingBracket + && prevToken.type() != Token::ClosingBracket // no double-newline if cursor is between closing brackets, i.e. ]) + && !(prevToken.block().firstLineNumber() < nextToken.block().firstLineNumber()) // no double-nl if only whitespace to the left + ) { cursor.beginEditBlock(); cursor.insertBlock();