Add "indent blocks twice" option for GNU indenting

Reviewed-by: mae
Merge-request: 133
This commit is contained in:
Alexey Semenko
2010-04-01 04:25:32 +04:00
committed by mae
parent 0fd5e4e9c4
commit 538921e8dc
7 changed files with 58 additions and 7 deletions

View File

@@ -1669,6 +1669,7 @@ static void indentCPPBlock(const CPPEditor::TabSettings &ts,
indenter.setIndentSize(ts.m_indentSize); indenter.setIndentSize(ts.m_indentSize);
indenter.setTabSize(ts.m_tabSize); indenter.setTabSize(ts.m_tabSize);
indenter.setIndentBraces(ts.m_indentBraces); indenter.setIndentBraces(ts.m_indentBraces);
indenter.setDoubleIndentBlocks(ts.m_doubleIndentBlocks);
const TextEditor::TextBlockIterator current(block); const TextEditor::TextBlockIterator current(block);
const int indent = indenter.indentForBottomLine(current, programBegin, programEnd, typedChar); const int indent = indenter.indentForBottomLine(current, programBegin, programEnd, typedChar);

View File

@@ -170,6 +170,8 @@ void BehaviorSettingsPage::settingsFromUI(TabSettings &tabSettings,
tabSettings.m_tabSize = m_d->m_page.tabSize->value(); tabSettings.m_tabSize = m_d->m_page.tabSize->value();
tabSettings.m_indentSize = m_d->m_page.indentSize->value(); tabSettings.m_indentSize = m_d->m_page.indentSize->value();
tabSettings.m_indentBraces = m_d->m_page.indentBraces->isChecked(); tabSettings.m_indentBraces = m_d->m_page.indentBraces->isChecked();
tabSettings.m_doubleIndentBlocks = m_d->m_page.doubleIndentBlocks->isChecked();
tabSettings.m_tabKeyBehavior = (TabSettings::TabKeyBehavior)m_d->m_page.tabKeyBehavior->currentIndex(); tabSettings.m_tabKeyBehavior = (TabSettings::TabKeyBehavior)m_d->m_page.tabKeyBehavior->currentIndex();
storageSettings.m_cleanWhitespace = m_d->m_page.cleanWhitespace->isChecked(); storageSettings.m_cleanWhitespace = m_d->m_page.cleanWhitespace->isChecked();
@@ -191,6 +193,7 @@ void BehaviorSettingsPage::settingsToUI()
m_d->m_page.tabSize->setValue(tabSettings.m_tabSize); m_d->m_page.tabSize->setValue(tabSettings.m_tabSize);
m_d->m_page.indentSize->setValue(tabSettings.m_indentSize); m_d->m_page.indentSize->setValue(tabSettings.m_indentSize);
m_d->m_page.indentBraces->setChecked(tabSettings.m_indentBraces); m_d->m_page.indentBraces->setChecked(tabSettings.m_indentBraces);
m_d->m_page.doubleIndentBlocks->setChecked(tabSettings.m_doubleIndentBlocks);
m_d->m_page.tabKeyBehavior->setCurrentIndex(tabSettings.m_tabKeyBehavior); m_d->m_page.tabKeyBehavior->setCurrentIndex(tabSettings.m_tabKeyBehavior);
const StorageSettings &storageSettings = m_d->m_storageSettings; const StorageSettings &storageSettings = m_d->m_storageSettings;

View File

@@ -176,6 +176,16 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="5" column="0">
<widget class="QCheckBox" name="doubleIndentBlocks">
<property name="enabled">
<bool>true</bool>
</property>
<property name="text">
<string>Indent blocks t&amp;wice</string>
</property>
</widget>
</item>
</layout> </layout>
</item> </item>
<item> <item>

View File

@@ -42,6 +42,7 @@ static const char *autoIndentKey = "AutoIndent";
static const char *tabSizeKey = "TabSize"; static const char *tabSizeKey = "TabSize";
static const char *indentSizeKey = "IndentSize"; static const char *indentSizeKey = "IndentSize";
static const char *indentBracesKey = "IndentBraces"; static const char *indentBracesKey = "IndentBraces";
static const char *doubleIndentBlocksKey = "DoubleIndentBlocks";
static const char *tabKeyBehaviorKey = "TabKeyBehavior"; static const char *tabKeyBehaviorKey = "TabKeyBehavior";
static const char *groupPostfix = "TabSettings"; static const char *groupPostfix = "TabSettings";
@@ -55,6 +56,7 @@ TabSettings::TabSettings() :
m_tabSize(8), m_tabSize(8),
m_indentSize(4), m_indentSize(4),
m_indentBraces(false), m_indentBraces(false),
m_doubleIndentBlocks(false),
m_tabKeyBehavior(TabNeverIndents) m_tabKeyBehavior(TabNeverIndents)
{ {
} }
@@ -72,6 +74,7 @@ void TabSettings::toSettings(const QString &category, QSettings *s) const
s->setValue(QLatin1String(tabSizeKey), m_tabSize); s->setValue(QLatin1String(tabSizeKey), m_tabSize);
s->setValue(QLatin1String(indentSizeKey), m_indentSize); s->setValue(QLatin1String(indentSizeKey), m_indentSize);
s->setValue(QLatin1String(indentBracesKey), m_indentBraces); s->setValue(QLatin1String(indentBracesKey), m_indentBraces);
s->setValue(QLatin1String(doubleIndentBlocksKey), m_doubleIndentBlocks);
s->setValue(QLatin1String(tabKeyBehaviorKey), m_tabKeyBehavior); s->setValue(QLatin1String(tabKeyBehaviorKey), m_tabKeyBehavior);
s->endGroup(); s->endGroup();
} }
@@ -92,6 +95,9 @@ void TabSettings::fromSettings(const QString &category, const QSettings *s)
m_tabSize = s->value(group + QLatin1String(tabSizeKey), m_tabSize).toInt(); m_tabSize = s->value(group + QLatin1String(tabSizeKey), m_tabSize).toInt();
m_indentSize = s->value(group + QLatin1String(indentSizeKey), m_indentSize).toInt(); m_indentSize = s->value(group + QLatin1String(indentSizeKey), m_indentSize).toInt();
m_indentBraces = s->value(group + QLatin1String(indentBracesKey), m_indentBraces).toBool(); m_indentBraces = s->value(group + QLatin1String(indentBracesKey), m_indentBraces).toBool();
m_doubleIndentBlocks
= s->value(group + QLatin1String(doubleIndentBlocksKey), m_doubleIndentBlocks).toBool();
m_tabKeyBehavior = (TabKeyBehavior)s->value(group + QLatin1String(tabKeyBehaviorKey), m_tabKeyBehavior).toInt(); m_tabKeyBehavior = (TabKeyBehavior)s->value(group + QLatin1String(tabKeyBehaviorKey), m_tabKeyBehavior).toInt();
} }
@@ -339,6 +345,7 @@ bool TabSettings::equals(const TabSettings &ts) const
&& m_tabSize == ts.m_tabSize && m_tabSize == ts.m_tabSize
&& m_indentSize == ts.m_indentSize && m_indentSize == ts.m_indentSize
&& m_indentBraces == ts.m_indentBraces && m_indentBraces == ts.m_indentBraces
&& m_doubleIndentBlocks == ts.m_doubleIndentBlocks
&& m_tabKeyBehavior == ts.m_tabKeyBehavior; && m_tabKeyBehavior == ts.m_tabKeyBehavior;
} }

View File

@@ -83,6 +83,7 @@ struct TEXTEDITOR_EXPORT TabSettings
int m_tabSize; int m_tabSize;
int m_indentSize; int m_indentSize;
bool m_indentBraces; bool m_indentBraces;
bool m_doubleIndentBlocks;
TabKeyBehavior m_tabKeyBehavior; TabKeyBehavior m_tabKeyBehavior;
bool equals(const TabSettings &ts) const; bool equals(const TabSettings &ts) const;

View File

@@ -94,7 +94,7 @@ public:
void setIndentSize(int size); void setIndentSize(int size);
void setTabSize(int size ); void setTabSize(int size );
void setIndentBraces(bool indent); void setIndentBraces(bool indent);
void setDoubleIndentBlocks(bool indent);
/* Return indentation for the last line of the sequence /* Return indentation for the last line of the sequence
* based on the previous lines. */ * based on the previous lines. */
int indentForBottomLine(const Iterator &current, int indentForBottomLine(const Iterator &current,
@@ -126,6 +126,7 @@ private:
int ppIndentSize; int ppIndentSize;
bool ppIndentBraces; bool ppIndentBraces;
int ppContinuationIndentSize; int ppContinuationIndentSize;
bool ppDoubleIndentBlocks;
Iterator yyProgramBegin; Iterator yyProgramBegin;
Iterator yyProgramEnd; Iterator yyProgramEnd;

View File

@@ -97,6 +97,8 @@ namespace {
when it cannot be picked up. when it cannot be picked up.
* ppIndentBraces will indent braces flush with an indented code * ppIndentBraces will indent braces flush with an indented code
block. block.
* ppDoubleIndentBlocks do double indent of blocks so as together
with ppIndentBraces enabled it form some sort of GNU indenting style
*/ */
@@ -109,6 +111,7 @@ Indenter<Iterator>::Indenter() :
ppIndentSize(4), ppIndentSize(4),
ppIndentBraces(false), ppIndentBraces(false),
ppContinuationIndentSize(8), ppContinuationIndentSize(8),
ppDoubleIndentBlocks(false),
yyLinizerState(new LinizerState), yyLinizerState(new LinizerState),
yyLine(0), yyLine(0),
yyBraceDepth(0), yyBraceDepth(0),
@@ -147,6 +150,13 @@ void Indenter<Iterator>::setIndentBraces(bool indent)
{ {
ppIndentBraces = indent; ppIndentBraces = indent;
} }
template <class Iterator>
void Indenter<Iterator>::setDoubleIndentBlocks(bool indent)
{
ppDoubleIndentBlocks = indent;
}
/* /*
Returns the first non-space character in the string t, or Returns the first non-space character in the string t, or
QChar::null if the string is made only of white space. QChar::null if the string is made only of white space.
@@ -846,8 +856,8 @@ int Indenter<Iterator>::indentForContinuationLine()
The "{" should be flush left. The "{" should be flush left.
*/ */
if ( !isContinuationLine() ) if ( !isContinuationLine() )
return indentOfLine( *yyLine ); return indentOfLine( *yyLine );
} else if ( isContinuationLine() || yyLine->endsWith(comma)) { } else if ( isContinuationLine() || yyLine->endsWith(comma)) {
/* /*
We have We have
@@ -1012,8 +1022,15 @@ int Indenter<Iterator>::indentForStandaloneLine()
Never trust lines containing only '{' or '}', as some Never trust lines containing only '{' or '}', as some
people (Richard M. Stallman) format them weirdly. people (Richard M. Stallman) format them weirdly.
*/ */
if ( yyLine->trimmed().length() > 1 ) if ( yyLine->trimmed().length() > 1 ) {
return indentOfLine( *yyLine ) - *yyBraceDepth * ppIndentSize; if (!ppDoubleIndentBlocks)
return indentOfLine( *yyLine ) - *yyBraceDepth * ppIndentSize;
else {
if (*yyBraceDepth == -1 && indentOfLine( *yyLine ) == 0)
return ppIndentSize; // don't do double indent for upper level blocks
return indentOfLine( *yyLine ) - *yyBraceDepth * ppIndentSize * 2;
}
}
} }
if ( !readLine() ) if ( !readLine() )
@@ -1082,12 +1099,23 @@ int Indenter<Iterator>::indentForBottomLine(const Iterator &current,
if ( ppIndentBraces && firstCh == openingBrace ) { if ( ppIndentBraces && firstCh == openingBrace ) {
indent += ppIndentSize; indent += ppIndentSize;
} else if ( !ppIndentBraces && firstCh == closingBrace ) { } else if ( firstCh == closingBrace ) {
/* /*
A closing brace is one level more to the left than the A closing brace is one level more to the left than the
code it follows. code it follows.
*/ */
indent -= ppIndentSize; indent -= ppIndentSize;
/*
But if braces indenting is enabled the shift exists
*/
if (ppIndentBraces)
indent += ppIndentSize;
/*
Double indenting of code blocks makes block righter, so move our brace back
*/
if (ppDoubleIndentBlocks)
indent -= ppIndentSize;
} else if ( okay(typedIn, colon) ) { } else if ( okay(typedIn, colon) ) {
if ( m_constants.m_caseLabel.indexIn(bottomLine) != -1 ) { if ( m_constants.m_caseLabel.indexIn(bottomLine) != -1 ) {
/* /*