mirror of
https://github.com/juce-framework/JUCE.git
synced 2026-01-10 23:44:24 +00:00
Introjucer: code indent tweaks.
This commit is contained in:
parent
7ef8246cae
commit
d78b10edb3
3 changed files with 52 additions and 13 deletions
|
|
@ -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;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
|
|
|
|||
|
|
@ -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!
|
||||
*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue