Skip to content
Snippets Groups Projects
Commit bc0d5e38 authored by mae's avatar mae
Browse files

Prepared padding support (as opposed to indent)

This will be hooked up with the new indenter.
If you use tabs for spaces, and have tabSize==indentSize, we
will be able to preserve spaces for padding nonetheless.
parent df6fcd36
No related branches found
No related tags found
No related merge requests found
...@@ -143,6 +143,18 @@ int TabSettings::indentationColumn(const QString &text) const ...@@ -143,6 +143,18 @@ int TabSettings::indentationColumn(const QString &text) const
return columnAt(text, firstNonSpace(text)); return columnAt(text, firstNonSpace(text));
} }
int TabSettings::maximumPadding(const QString &text) const
{
int fns = columnAt(text, firstNonSpace(text));
int i = fns;
while (i > 0) {
if (text.at(i-1) != QLatin1Char(' '))
break;
--i;
}
return fns - i;
}
int TabSettings::trailingWhitespaces(const QString &text) const int TabSettings::trailingWhitespaces(const QString &text) const
{ {
...@@ -286,7 +298,7 @@ QString TabSettings::indentationString(int startColumn, int targetColumn, const ...@@ -286,7 +298,7 @@ QString TabSettings::indentationString(int startColumn, int targetColumn, const
return s; return s;
} }
void TabSettings::indentLine(QTextBlock block, int newIndent) const void TabSettings::indentLine(QTextBlock block, int newIndent, int padding) const
{ {
const QString text = block.text(); const QString text = block.text();
const int oldBlockLength = text.size(); const int oldBlockLength = text.size();
...@@ -295,8 +307,15 @@ void TabSettings::indentLine(QTextBlock block, int newIndent) const ...@@ -295,8 +307,15 @@ void TabSettings::indentLine(QTextBlock block, int newIndent) const
if (indentationColumn(text) == newIndent) if (indentationColumn(text) == newIndent)
return; return;
const QString indentString = indentationString(0, newIndent, block); QString indentString;
newIndent = indentString.length();
if (!m_spacesForTabs && m_tabSize == m_indentSize) {
// user likes tabs for spaces and uses tabs for indentation, preserve padding
indentString = indentationString(0, newIndent - padding, block);
indentString += QString(padding, QLatin1Char(' '));
} else {
indentString = indentationString(0, newIndent, block);
}
if (oldBlockLength == indentString.length() && text == indentString) if (oldBlockLength == indentString.length() && text == indentString)
return; return;
...@@ -321,8 +340,15 @@ void TabSettings::reindentLine(QTextBlock block, int delta) const ...@@ -321,8 +340,15 @@ void TabSettings::reindentLine(QTextBlock block, int delta) const
if (oldIndent == newIndent) if (oldIndent == newIndent)
return; return;
const QString indentString = indentationString(0, newIndent, block); QString indentString;
newIndent = indentString.length(); if (!m_spacesForTabs && m_tabSize == m_indentSize) {
// user likes tabs for spaces and uses tabs for indentation, preserve padding
int padding = qMin(maximumPadding(text), newIndent);
indentString = indentationString(0, newIndent - padding, block);
indentString += QString(padding, QLatin1Char(' '));
} else {
indentString = indentationString(0, newIndent, block);
}
if (oldBlockLength == indentString.length() && text == indentString) if (oldBlockLength == indentString.length() && text == indentString)
return; return;
......
...@@ -65,10 +65,11 @@ struct TEXTEDITOR_EXPORT TabSettings ...@@ -65,10 +65,11 @@ struct TEXTEDITOR_EXPORT TabSettings
QString indentationString(int startColumn, int targetColumn, const QTextBlock &currentBlock = QTextBlock()) const; QString indentationString(int startColumn, int targetColumn, const QTextBlock &currentBlock = QTextBlock()) const;
QString indentationString(const QString &text) const; QString indentationString(const QString &text) const;
int indentationColumn(const QString &text) const; int indentationColumn(const QString &text) const;
int maximumPadding(const QString &text) const;
bool cursorIsAtBeginningOfLine(const QTextCursor &cursor) const; bool cursorIsAtBeginningOfLine(const QTextCursor &cursor) const;
void indentLine(QTextBlock block, int newIndent) const; void indentLine(QTextBlock block, int newIndent, int padding = 0) const;
void reindentLine(QTextBlock block, int delta) const; void reindentLine(QTextBlock block, int delta) const;
int trailingWhitespaces(const QString &text) const; int trailingWhitespaces(const QString &text) const;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment