From 2b32036ab38a3ed1c1f227b107f9814196218f4c Mon Sep 17 00:00:00 2001 From: jules Date: Tue, 4 Sep 2012 10:39:29 +0100 Subject: [PATCH] Introjucer: code indenting changes. --- .../Code Editor/jucer_SourceCodeEditor.cpp | 136 ++++-------------- .../Source/Utility/jucer_CodeHelpers.cpp | 55 +++++++ .../Source/Utility/jucer_CodeHelpers.h | 5 + 3 files changed, 90 insertions(+), 106 deletions(-) diff --git a/extras/Introjucer/Source/Code Editor/jucer_SourceCodeEditor.cpp b/extras/Introjucer/Source/Code Editor/jucer_SourceCodeEditor.cpp index b169fb115e..0aa586b5e1 100644 --- a/extras/Introjucer/Source/Code Editor/jucer_SourceCodeEditor.cpp +++ b/extras/Introjucer/Source/Code Editor/jucer_SourceCodeEditor.cpp @@ -159,65 +159,10 @@ void SourceCodeEditor::valueTreeRedirected (ValueTree&) //============================================================================== -namespace CppUtils -{ - static CPlusPlusCodeTokeniser* getCppTokeniser() - { - static CPlusPlusCodeTokeniser cppTokeniser; - return &cppTokeniser; - } - - static String getLeadingWhitespace (String line) - { - line = line.removeCharacters ("\r\n"); - const String::CharPointerType endOfLeadingWS (line.getCharPointer().findEndOfWhitespace()); - return String (line.getCharPointer(), endOfLeadingWS); - } - - static int getBraceCount (String::CharPointerType line) - { - int braces = 0; - - for (;;) - { - const juce_wchar c = line.getAndAdvance(); - - if (c == 0) break; - else if (c == '{') ++braces; - else if (c == '}') --braces; - else if (c == '/') { if (*line == '/') break; } - else if (c == '"' || c == '\'') { while (! (line.isEmpty() || line.getAndAdvance() == c)) {} } - } - - return braces; - } - - static bool getIndentForCurrentBlock (CodeDocument::Position pos, String& whitespace) - { - int braceCount = 0; - - while (pos.getLineNumber() > 0) - { - pos = pos.movedByLines (-1); - - const String line (pos.getLineText()); - const String trimmedLine (line.trimStart()); - - braceCount += getBraceCount (trimmedLine.getCharPointer()); - - if (braceCount > 0) - { - whitespace = getLeadingWhitespace (line); - return true; - } - } - - return false; - } -} +static CPlusPlusCodeTokeniser cppTokeniser; CppCodeEditorComponent::CppCodeEditorComponent (CodeDocument& codeDocument) - : CodeEditorComponent (codeDocument, CppUtils::getCppTokeniser()) + : CodeEditorComponent (codeDocument, &cppTokeniser) { } @@ -227,34 +172,32 @@ void CppCodeEditorComponent::handleReturnKey() CodeDocument::Position pos (getCaretPos()); - if (pos.getLineNumber() > 0 && pos.getLineText().trim().isEmpty()) - { - const String previousLine (pos.movedByLines (-1).getLineText()); - const String trimmedPreviousLine (previousLine.trim()); - - if (trimmedPreviousLine.endsWithChar ('{') - || ((trimmedPreviousLine.startsWith ("if ") - || trimmedPreviousLine.startsWith ("for ") - || trimmedPreviousLine.startsWith ("while ")) - && trimmedPreviousLine.endsWithChar (')'))) - { - const String leadingWhitespace (CppUtils::getLeadingWhitespace (previousLine)); - insertTextAtCaret (leadingWhitespace); - insertTabAtCaret(); - } - else - { - while (pos.getLineNumber() > 0) - { - pos = pos.movedByLines (-1); + String blockIndent, lastLineIndent; + CodeHelpers::getIndentForCurrentBlock (pos, getTabString (getTabSize()), blockIndent, lastLineIndent); - if (pos.getLineText().trimStart().isNotEmpty()) - { - insertTextAtCaret (CppUtils::getLeadingWhitespace (pos.getLineText())); - break; - } - } - } + const String remainderOfBrokenLine (pos.getLineText()); + const int numLeadingWSChars = CodeHelpers::getLeadingWhitespace (remainderOfBrokenLine).length(); + + if (numLeadingWSChars > 0) + getDocument().deleteSection (pos, pos.movedBy (numLeadingWSChars)); + + if (remainderOfBrokenLine.trimStart().startsWithChar ('}')) + insertTextAtCaret (blockIndent); + else + insertTextAtCaret (lastLineIndent); + + const String previousLine (pos.movedByLines (-1).getLineText()); + const String trimmedPreviousLine (previousLine.trim()); + + if ((trimmedPreviousLine.startsWith ("if ") + || trimmedPreviousLine.startsWith ("if(") + || trimmedPreviousLine.startsWith ("for ") + || trimmedPreviousLine.startsWith ("for(") + || trimmedPreviousLine.startsWith ("while(") + || trimmedPreviousLine.startsWith ("while ")) + && trimmedPreviousLine.endsWithChar (')')) + { + insertTabAtCaret(); } } @@ -270,34 +213,15 @@ void CppCodeEditorComponent::insertTextAtCaret (const String& newText) { moveCaretToStartOfLine (true); - String whitespace; - if (CppUtils::getIndentForCurrentBlock (pos, whitespace)) + String blockIndent, lastLineIndent; + if (CodeHelpers::getIndentForCurrentBlock (pos, getTabString (getTabSize()), blockIndent, lastLineIndent)) { - CodeEditorComponent::insertTextAtCaret (whitespace); + CodeEditorComponent::insertTextAtCaret (blockIndent); 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 (CppUtils::getIndentForCurrentBlock (pos, whitespace)) - { - CodeEditorComponent::insertTextAtCaret (newText + whitespace); - - if (remainderOfLine.startsWithChar ('{')) - insertTabAtCaret(); - - return; - } - } - } } CodeEditorComponent::insertTextAtCaret (newText); diff --git a/extras/Introjucer/Source/Utility/jucer_CodeHelpers.cpp b/extras/Introjucer/Source/Utility/jucer_CodeHelpers.cpp index c849394b34..c56ccd1012 100644 --- a/extras/Introjucer/Source/Utility/jucer_CodeHelpers.cpp +++ b/extras/Introjucer/Source/Utility/jucer_CodeHelpers.cpp @@ -417,4 +417,59 @@ namespace CodeHelpers out << indent << " default: break;" << newLine << indent << "}" << newLine << newLine; } + + String getLeadingWhitespace (String line) + { + line = line.removeCharacters ("\r\n"); + const String::CharPointerType endOfLeadingWS (line.getCharPointer().findEndOfWhitespace()); + return String (line.getCharPointer(), endOfLeadingWS); + } + + int getBraceCount (String::CharPointerType line) + { + int braces = 0; + + for (;;) + { + const juce_wchar c = line.getAndAdvance(); + + if (c == 0) break; + else if (c == '{') ++braces; + else if (c == '}') --braces; + else if (c == '/') { if (*line == '/') break; } + else if (c == '"' || c == '\'') { while (! (line.isEmpty() || line.getAndAdvance() == c)) {} } + } + + return braces; + } + + bool getIndentForCurrentBlock (CodeDocument::Position pos, const String& tab, + String& blockIndent, String& lastLineIndent) + { + int braceCount = 0; + + while (pos.getLineNumber() > 0) + { + pos = pos.movedByLines (-1); + + const String line (pos.getLineText()); + const String trimmedLine (line.trimStart()); + + braceCount += getBraceCount (trimmedLine.getCharPointer()); + + if (braceCount > 0) + { + blockIndent = getLeadingWhitespace (line); + if (lastLineIndent.isEmpty()) + lastLineIndent = blockIndent + tab; + + return true; + } + + if (lastLineIndent.isEmpty() && trimmedLine.isNotEmpty()) + lastLineIndent = getLeadingWhitespace (line); + } + + return false; + } } diff --git a/extras/Introjucer/Source/Utility/jucer_CodeHelpers.h b/extras/Introjucer/Source/Utility/jucer_CodeHelpers.h index 3c7bf909f3..b7503368f0 100644 --- a/extras/Introjucer/Source/Utility/jucer_CodeHelpers.h +++ b/extras/Introjucer/Source/Utility/jucer_CodeHelpers.h @@ -48,6 +48,11 @@ namespace CodeHelpers void createStringMatcher (OutputStream& out, const String& utf8PointerVariable, const StringArray& strings, const StringArray& codeToExecute, const int indentLevel); + + String getLeadingWhitespace (String line); + int getBraceCount (String::CharPointerType line); + bool getIndentForCurrentBlock (CodeDocument::Position pos, const String& tab, + String& blockIndent, String& lastLineIndent); }