diff --git a/src/plugins/pythoneditor/pythonhighlighter.cpp b/src/plugins/pythoneditor/pythonhighlighter.cpp index 389beee4b87..b485c2a450e 100644 --- a/src/plugins/pythoneditor/pythonhighlighter.cpp +++ b/src/plugins/pythoneditor/pythonhighlighter.cpp @@ -37,6 +37,7 @@ #include "pythonscanner.h" #include +#include #include #include @@ -116,6 +117,24 @@ static bool isImportKeyword(const QString &keyword) return keyword == "import" || keyword == "from"; } +static int indent(const QString &line) +{ + for (int i = 0, size = line.size(); i < size; ++i) { + if (!line.at(i).isSpace()) + return i; + } + return -1; +} + +static void setFoldingIndent(const QTextBlock &block, int indent) +{ + if (TextEditor::TextBlockUserData *userData = TextEditor::TextDocumentLayout::userData(block)) { + userData->setFoldingIndent(indent); + userData->setFoldingStartIncluded(false); + userData->setFoldingEndIncluded(false); + } +} + /** * @brief Highlight line of code, returns new block state * @param text Source code to highlight @@ -127,6 +146,23 @@ int PythonHighlighter::highlightLine(const QString &text, int initialState) Scanner scanner(text.constData(), text.size()); scanner.setState(initialState); + const int pos = indent(text); + if (pos < 0) { + // Empty lines do not change folding indent + setFoldingIndent(currentBlock(), m_lastIndent); + } else { + m_lastIndent = pos; + if (pos == 0 && text.startsWith('#') && !text.startsWith("#!")) { + // A comment block at indentation 0. Fold on first line. + setFoldingIndent(currentBlock(), withinLicenseHeader ? 1 : 0); + withinLicenseHeader = true; + } else { + // Normal Python code. Line indentation can be used as folding indent. + setFoldingIndent(currentBlock(), m_lastIndent); + withinLicenseHeader = false; + } + } + FormatToken tk; bool hasOnlyWhitespace = true; while (!(tk = scanner.read()).isEndOfBlock()) { diff --git a/src/plugins/pythoneditor/pythonhighlighter.h b/src/plugins/pythoneditor/pythonhighlighter.h index 778d3727b29..63c35dfaf34 100644 --- a/src/plugins/pythoneditor/pythonhighlighter.h +++ b/src/plugins/pythoneditor/pythonhighlighter.h @@ -41,6 +41,9 @@ private: void highlightBlock(const QString &text) override; int highlightLine(const QString &text, int initialState); void highlightImport(Internal::Scanner &scanner); + + int m_lastIndent = 0; + bool withinLicenseHeader = false; }; } // namespace Internal