Commit 538921e8 authored by Alexey Semenko's avatar Alexey Semenko Committed by mae
Browse files

Add "indent blocks twice" option for GNU indenting

Reviewed-by: mae
Merge-request: 133
parent 0fd5e4e9
......@@ -1669,6 +1669,7 @@ static void indentCPPBlock(const CPPEditor::TabSettings &ts,
indenter.setIndentSize(ts.m_indentSize);
indenter.setTabSize(ts.m_tabSize);
indenter.setIndentBraces(ts.m_indentBraces);
indenter.setDoubleIndentBlocks(ts.m_doubleIndentBlocks);
const TextEditor::TextBlockIterator current(block);
const int indent = indenter.indentForBottomLine(current, programBegin, programEnd, typedChar);
......
......@@ -170,6 +170,8 @@ void BehaviorSettingsPage::settingsFromUI(TabSettings &tabSettings,
tabSettings.m_tabSize = m_d->m_page.tabSize->value();
tabSettings.m_indentSize = m_d->m_page.indentSize->value();
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();
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.indentSize->setValue(tabSettings.m_indentSize);
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);
const StorageSettings &storageSettings = m_d->m_storageSettings;
......
......@@ -176,6 +176,16 @@
</property>
</widget>
</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>
</item>
<item>
......
......@@ -42,6 +42,7 @@ static const char *autoIndentKey = "AutoIndent";
static const char *tabSizeKey = "TabSize";
static const char *indentSizeKey = "IndentSize";
static const char *indentBracesKey = "IndentBraces";
static const char *doubleIndentBlocksKey = "DoubleIndentBlocks";
static const char *tabKeyBehaviorKey = "TabKeyBehavior";
static const char *groupPostfix = "TabSettings";
......@@ -55,6 +56,7 @@ TabSettings::TabSettings() :
m_tabSize(8),
m_indentSize(4),
m_indentBraces(false),
m_doubleIndentBlocks(false),
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(indentSizeKey), m_indentSize);
s->setValue(QLatin1String(indentBracesKey), m_indentBraces);
s->setValue(QLatin1String(doubleIndentBlocksKey), m_doubleIndentBlocks);
s->setValue(QLatin1String(tabKeyBehaviorKey), m_tabKeyBehavior);
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_indentSize = s->value(group + QLatin1String(indentSizeKey), m_indentSize).toInt();
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();
}
......@@ -339,6 +345,7 @@ bool TabSettings::equals(const TabSettings &ts) const
&& m_tabSize == ts.m_tabSize
&& m_indentSize == ts.m_indentSize
&& m_indentBraces == ts.m_indentBraces
&& m_doubleIndentBlocks == ts.m_doubleIndentBlocks
&& m_tabKeyBehavior == ts.m_tabKeyBehavior;
}
......
......@@ -83,6 +83,7 @@ struct TEXTEDITOR_EXPORT TabSettings
int m_tabSize;
int m_indentSize;
bool m_indentBraces;
bool m_doubleIndentBlocks;
TabKeyBehavior m_tabKeyBehavior;
bool equals(const TabSettings &ts) const;
......
......@@ -94,7 +94,7 @@ public:
void setIndentSize(int size);
void setTabSize(int size );
void setIndentBraces(bool indent);
void setDoubleIndentBlocks(bool indent);
/* Return indentation for the last line of the sequence
* based on the previous lines. */
int indentForBottomLine(const Iterator &current,
......@@ -126,6 +126,7 @@ private:
int ppIndentSize;
bool ppIndentBraces;
int ppContinuationIndentSize;
bool ppDoubleIndentBlocks;
Iterator yyProgramBegin;
Iterator yyProgramEnd;
......
......@@ -97,6 +97,8 @@ namespace {
when it cannot be picked up.
* ppIndentBraces will indent braces flush with an indented code
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),
ppIndentBraces(false),
ppContinuationIndentSize(8),
ppDoubleIndentBlocks(false),
yyLinizerState(new LinizerState),
yyLine(0),
yyBraceDepth(0),
......@@ -147,6 +150,13 @@ void Indenter<Iterator>::setIndentBraces(bool 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
QChar::null if the string is made only of white space.
......@@ -846,8 +856,8 @@ int Indenter<Iterator>::indentForContinuationLine()
The "{" should be flush left.
*/
if ( !isContinuationLine() )
return indentOfLine( *yyLine );
} else if ( isContinuationLine() || yyLine->endsWith(comma)) {
return indentOfLine( *yyLine );
} else if ( isContinuationLine() || yyLine->endsWith(comma)) {
/*
We have
......@@ -1012,8 +1022,15 @@ int Indenter<Iterator>::indentForStandaloneLine()
Never trust lines containing only '{' or '}', as some
people (Richard M. Stallman) format them weirdly.
*/
if ( yyLine->trimmed().length() > 1 )
return indentOfLine( *yyLine ) - *yyBraceDepth * ppIndentSize;
if ( yyLine->trimmed().length() > 1 ) {
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() )
......@@ -1082,12 +1099,23 @@ int Indenter<Iterator>::indentForBottomLine(const Iterator &current,
if ( ppIndentBraces && firstCh == openingBrace ) {
indent += ppIndentSize;
} else if ( !ppIndentBraces && firstCh == closingBrace ) {
} else if ( firstCh == closingBrace ) {
/*
A closing brace is one level more to the left than the
code it follows.
*/
*/
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) ) {
if ( m_constants.m_caseLabel.indexIn(bottomLine) != -1 ) {
/*
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment