diff --git a/extras/Introjucer/Source/Code Editor/jucer_SourceCodeEditor.h b/extras/Introjucer/Source/Code Editor/jucer_SourceCodeEditor.h index d5476efe34..4470d63044 100644 --- a/extras/Introjucer/Source/Code Editor/jucer_SourceCodeEditor.h +++ b/extras/Introjucer/Source/Code Editor/jucer_SourceCodeEditor.h @@ -127,7 +127,9 @@ public: if (pos.getLineNumber() > 0 && pos.getLineText().trim().isEmpty()) { - String indent (getIndentForCurrentBlock (pos)); + String indent; + getIndentForCurrentBlock (pos, indent); + const String previousLine (pos.movedByLines (-1).getLineText()); const String trimmedPreviousLine (previousLine.trim()); const String leadingWhitespace (getLeadingWhitespace (previousLine)); @@ -154,10 +156,34 @@ public: && pos.getLineText().trim().isEmpty()) { moveCaretToStartOfLine (true); - CodeEditorComponent::insertTextAtCaret (getIndentForCurrentBlock (pos)); - if (newText == "{") - insertTabAtCaret(); + String whitespace; + if (getIndentForCurrentBlock (pos, whitespace)) + { + CodeEditorComponent::insertTextAtCaret (whitespace); + + if (newText == "{") + insertTabAtCaret(); + } + } + else if (newText == getDocument().getNewLineCharacters() + && pos.getLineNumber() > 0) + { + const String remainderOfLine (pos.getLineText().substring (pos.getIndexInLine())); + + if (remainderOfLine.startsWithChar ('{') || remainderOfLine.startsWithChar ('}')) + { + String whitespace; + if (getIndentForCurrentBlock (pos, whitespace)) + { + CodeEditorComponent::insertTextAtCaret (newText + whitespace); + + if (remainderOfLine.startsWithChar ('{')) + insertTabAtCaret(); + + return; + } + } } } @@ -178,7 +204,7 @@ private: return String (line.getCharPointer(), endOfLeadingWS); } - static String getIndentForCurrentBlock (CodeDocument::Position pos) + static bool getIndentForCurrentBlock (CodeDocument::Position pos, String& whitespace) { int braceCount = 0; @@ -198,12 +224,17 @@ private: ++braceCount; if (tokens[i] == "{") + { if (--braceCount < 0) - return getLeadingWhitespace (line); + { + whitespace = getLeadingWhitespace (line); + return true; + } + } } } - return String::empty; + return false; } }; diff --git a/modules/juce_gui_extra/code_editor/juce_CodeEditorComponent.cpp b/modules/juce_gui_extra/code_editor/juce_CodeEditorComponent.cpp index d34f95b62d..04f9292b1c 100644 --- a/modules/juce_gui_extra/code_editor/juce_CodeEditorComponent.cpp +++ b/modules/juce_gui_extra/code_editor/juce_CodeEditorComponent.cpp @@ -697,11 +697,11 @@ void CodeEditorComponent::insertTabAtCaret() { const int caretCol = indexToColumn (caretPos.getLineNumber(), caretPos.getIndexInLine()); const int spacesNeeded = spacesPerTab - (caretCol % spacesPerTab); - insertText (String::repeatedString (" ", spacesNeeded)); + insertTextAtCaret (String::repeatedString (" ", spacesNeeded)); } else { - insertText ("\t"); + insertTextAtCaret ("\t"); } } @@ -765,9 +765,7 @@ void CodeEditorComponent::indentSelectedLines (const int spacesToAdd) if (newNumLeadingSpaces != numLeadingSpaces) { document.deleteSection (wsStart, wsEnd); - document.insertText (wsStart, String::repeatedString (useSpacesForTabs ? " " : "\t", - useSpacesForTabs ? newNumLeadingSpaces - : (newNumLeadingSpaces / spacesPerTab))); + document.insertText (wsStart, getTabString (newNumLeadingSpaces)); } } } @@ -1077,7 +1075,7 @@ bool CodeEditorComponent::keyPressed (const KeyPress& key) void CodeEditorComponent::handleReturnKey() { - insertText (document.getNewLineCharacters()); + insertTextAtCaret (document.getNewLineCharacters()); } void CodeEditorComponent::handleTabKey() @@ -1248,6 +1246,13 @@ void CodeEditorComponent::setTabSize (const int numSpaces, const bool insertSpac } } +String CodeEditorComponent::getTabString (const int numSpaces) const +{ + return String::repeatedString (useSpacesForTabs ? " " : "\t", + useSpacesForTabs ? numSpaces + : (numSpaces / spacesPerTab)); +} + int CodeEditorComponent::indexToColumn (int lineNum, int index) const noexcept { String::CharPointerType t (document.getLine (lineNum).getCharPointer()); diff --git a/modules/juce_gui_extra/code_editor/juce_CodeEditorComponent.h b/modules/juce_gui_extra/code_editor/juce_CodeEditorComponent.h index 6d109b006c..f328fbc2c5 100644 --- a/modules/juce_gui_extra/code_editor/juce_CodeEditorComponent.h +++ b/modules/juce_gui_extra/code_editor/juce_CodeEditorComponent.h @@ -194,6 +194,9 @@ public: */ bool areSpacesInsertedForTabs() const { return useSpacesForTabs; } + /** Returns a string containing spaces or tab characters to generate the given number of spaces. */ + String getTabString (int numSpaces) const; + /** Changes the font. Make sure you only use a fixed-width font, or this component will look pretty nasty! */