Editor: Fix whitespace cleaning.

Task-number: QTCREATORBUG-7994
Change-Id: I6c197ccc3a148555018e8f8184d116c88d7ea400
Reviewed-by: Christian Stenger <christian.stenger@theqtcompany.com>
Reviewed-by: Eike Ziller <eike.ziller@theqtcompany.com>
This commit is contained in:
David Schulz
2016-01-13 14:32:23 +01:00
parent bc921b46a2
commit 9aa51d4857
22 changed files with 346 additions and 200 deletions

View File

@@ -32,9 +32,6 @@
namespace PythonEditor {
// Tab size hardcoded as PEP8 style guide requires, but can be moved to settings
static const int TAB_SIZE = 4;
PythonIndenter::PythonIndenter()
{
m_jumpKeywords << QLatin1String("return")
@@ -59,39 +56,21 @@ bool PythonIndenter::isElectricCharacter(const QChar &ch) const
return (ch == QLatin1Char(':'));
}
/**
* @brief Indents one block (i.e. one line) of code
* @param doc Unused
* @param block Block that represents line
* @param typedChar Unused
* @param tabSettings An IDE tabulation settings
*
* Usually this function called once when you begin new line of code by pressing
* Enter. If Indenter reimplements indent() function, than indentBlock() may be
* called in other cases.
*/
void PythonIndenter::indentBlock(QTextDocument *document,
const QTextBlock &block,
const QChar &typedChar,
const TextEditor::TabSettings &settings)
int PythonIndenter::indentFor(const QTextBlock &block, const TextEditor::TabSettings &tabSettings)
{
Q_UNUSED(document);
Q_UNUSED(typedChar);
QTextBlock previousBlock = block.previous();
if (previousBlock.isValid()) {
QString previousLine = previousBlock.text();
int indentation = settings.indentationColumn(previousLine);
if (!previousBlock.isValid())
return 0;
if (isElectricLine(previousLine))
indentation += TAB_SIZE;
else
indentation = qMax<int>(0, indentation + getIndentDiff(previousLine));
QString previousLine = previousBlock.text();
int indentation = tabSettings.indentationColumn(previousLine);
settings.indentLine(block, indentation);
} else {
// First line in whole document
settings.indentLine(block, 0);
}
if (isElectricLine(previousLine))
indentation += tabSettings.m_indentSize;
else
indentation = qMax<int>(0, indentation + getIndentDiff(previousLine, tabSettings));
return indentation;
}
/// @return True if electric character is last non-space character at given string
@@ -109,13 +88,14 @@ bool PythonIndenter::isElectricLine(const QString &line) const
}
/// @return negative indent diff if previous line breaks control flow branch
int PythonIndenter::getIndentDiff(const QString &previousLine) const
int PythonIndenter::getIndentDiff(const QString &previousLine,
const TextEditor::TabSettings &tabSettings) const
{
Internal::Scanner sc(previousLine.constData(), previousLine.length());
forever {
Internal::FormatToken tk = sc.read();
if ((tk.format() == Internal::Format_Keyword) && m_jumpKeywords.contains(sc.value(tk)))
return -TAB_SIZE;
return -tabSettings.m_indentSize;
if (tk.format() != Internal::Format_Whitespace)
break;
}

View File

@@ -35,17 +35,16 @@ class PythonIndenter : public TextEditor::Indenter
{
public:
PythonIndenter();
virtual ~PythonIndenter();
virtual ~PythonIndenter() override;
bool isElectricCharacter(const QChar &ch) const;
void indentBlock(QTextDocument *document,
const QTextBlock &block,
const QChar &typedChar,
const TextEditor::TabSettings &settings);
bool isElectricCharacter(const QChar &ch) const override;
int indentFor(const QTextBlock &block, const TextEditor::TabSettings &tabSettings) override;
protected:
bool isElectricLine(const QString &line) const;
int getIndentDiff(const QString &previousLine) const;
int getIndentDiff(const QString &previousLine,
const TextEditor::TabSettings &tabSettings) const;
private:
QStringList m_jumpKeywords;