diff --git a/src/plugins/python/pythonformattoken.h b/src/plugins/python/pythonformattoken.h index 8a7465d769c..343d25d3b60 100644 --- a/src/plugins/python/pythonformattoken.h +++ b/src/plugins/python/pythonformattoken.h @@ -41,6 +41,8 @@ enum Format { Format_Identifier, Format_Whitespace, Format_ImportedModule, + Format_LParen, + Format_RParen, Format_FormatsAmount }; diff --git a/src/plugins/python/pythonhighlighter.cpp b/src/plugins/python/pythonhighlighter.cpp index c809e25b844..c1e7b68540f 100644 --- a/src/plugins/python/pythonhighlighter.cpp +++ b/src/plugins/python/pythonhighlighter.cpp @@ -79,6 +79,8 @@ static TextEditor::TextStyle styleForFormat(int format) case Format_Identifier: return C_TEXT; case Format_Whitespace: return C_VISUAL_WHITESPACE; case Format_ImportedModule: return C_STRING; + case Format_LParen: return C_OPERATOR; + case Format_RParen: return C_OPERATOR; case Format_FormatsAmount: QTC_CHECK(false); // should never get here return C_TEXT; @@ -164,6 +166,7 @@ int PythonHighlighter::highlightLine(const QString &text, int initialState) } FormatToken tk; + TextEditor::Parentheses parentheses; bool hasOnlyWhitespace = true; while (!(tk = scanner.read()).isEndOfBlock()) { Format format = tk.format(); @@ -175,11 +178,20 @@ int PythonHighlighter::highlightLine(const QString &text, int initialState) || format == Format_Doxygen) { setFormatWithSpaces(text, tk.begin(), tk.length(), formatForCategory(format)); } else { + if (format == Format_LParen) { + parentheses.append(TextEditor::Parenthesis(TextEditor::Parenthesis::Opened, + text.at(tk.begin()), tk.begin())); + } else if (format == Format_RParen) { + parentheses.append(TextEditor::Parenthesis(TextEditor::Parenthesis::Closed, + text.at(tk.begin()), tk.begin())); + } setFormat(tk.begin(), tk.length(), formatForCategory(format)); } + if (format != Format_Whitespace) hasOnlyWhitespace = false; } + TextEditor::TextDocumentLayout::setParentheses(currentBlock(), parentheses); return scanner.state(); } diff --git a/src/plugins/python/pythonscanner.cpp b/src/plugins/python/pythonscanner.cpp index abb6251e156..79c4fa30824 100644 --- a/src/plugins/python/pythonscanner.cpp +++ b/src/plugins/python/pythonscanner.cpp @@ -98,6 +98,11 @@ FormatToken Scanner::onDefaultState() return readComment(); } + if (first == '(' || first == '[' || first == '{') + return readBrace(true); + if (first == ')' || first == ']' || first == '}') + return readBrace(false); + if (first.isSpace()) return readWhiteSpace(); @@ -363,7 +368,7 @@ FormatToken Scanner::readWhiteSpace() */ FormatToken Scanner::readOperator() { - static const QString EXCLUDED_CHARS = "\'\"_#"; + static const QString EXCLUDED_CHARS = "\'\"_#([{}])"; QChar ch = peek(); while (ch.isPunct() && !EXCLUDED_CHARS.contains(ch)) { move(); @@ -372,6 +377,12 @@ FormatToken Scanner::readOperator() return FormatToken(Format_Operator, anchor(), length()); } +FormatToken Scanner::readBrace(bool isOpening) +{ + Format format = isOpening ? Format_LParen : Format_RParen; + return FormatToken(format, anchor(), length()); +} + void Scanner::clearState() { m_state = 0; diff --git a/src/plugins/python/pythonscanner.h b/src/plugins/python/pythonscanner.h index 5d37f3ba11c..dfa99ef0996 100644 --- a/src/plugins/python/pythonscanner.h +++ b/src/plugins/python/pythonscanner.h @@ -67,6 +67,7 @@ private: FormatToken readDoxygenComment(); FormatToken readWhiteSpace(); FormatToken readOperator(); + FormatToken readBrace(bool isOpening); void clearState(); void saveState(State state, QChar savedData);