From ab8a8c018d32f4a8905f52f7feedc75360225a69 Mon Sep 17 00:00:00 2001 From: mae <qt-info@nokia.com> Date: Mon, 12 Jul 2010 11:16:10 +0200 Subject: [PATCH] Fix brace states The lexer needs its own lexerState, before it was using the syntax highlighter user state. This breaks the brace depth calculation of the highlighter. Reviewed-by: Roberto Raggi --- src/plugins/cpptools/cppcodeformatter.cpp | 6 +-- .../texteditor/basetextdocumentlayout.cpp | 39 ++++++++----------- .../texteditor/basetextdocumentlayout.h | 21 ++++++---- src/plugins/texteditor/basetexteditor.cpp | 21 ++++++---- src/plugins/texteditor/tabsettings.h | 1 + 5 files changed, 47 insertions(+), 41 deletions(-) diff --git a/src/plugins/cpptools/cppcodeformatter.cpp b/src/plugins/cpptools/cppcodeformatter.cpp index 3c24648e96b..75fe53fda81 100644 --- a/src/plugins/cpptools/cppcodeformatter.cpp +++ b/src/plugins/cpptools/cppcodeformatter.cpp @@ -857,7 +857,7 @@ int CodeFormatter::tokenizeBlock(const QTextBlock &block, bool *endedJoined) *endedJoined = tokenize.endedJoined(); const int lexerState = tokenize.state(); - TextBlockUserData::setLexerState(block, lexerState); + BaseTextDocumentLayout::setLexerState(block, lexerState); return lexerState; } @@ -942,12 +942,12 @@ bool QtStyleCodeFormatter::loadBlockData(const QTextBlock &block, BlockData *dat void QtStyleCodeFormatter::saveLexerState(QTextBlock *block, int state) const { - TextBlockUserData::setLexerState(*block, state); + BaseTextDocumentLayout::setLexerState(*block, state); } int QtStyleCodeFormatter::loadLexerState(const QTextBlock &block) const { - return TextBlockUserData::lexerState(block); + return BaseTextDocumentLayout::lexerState(block); } void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedIndentDepth) const diff --git a/src/plugins/texteditor/basetextdocumentlayout.cpp b/src/plugins/texteditor/basetextdocumentlayout.cpp index 18a41919f88..c69808f8de8 100644 --- a/src/plugins/texteditor/basetextdocumentlayout.cpp +++ b/src/plugins/texteditor/basetextdocumentlayout.cpp @@ -366,28 +366,6 @@ TextBlockUserData::MatchType TextBlockUserData::matchCursorForward(QTextCursor * return NoMatch; } -int TextBlockUserData::lexerState(const QTextBlock &block) -{ - if (!block.isValid()) - return -1; - - int data = block.userState(); - if (data == -1) - return -1; - return data & 0xFF; -} - -void TextBlockUserData::setLexerState(QTextBlock block, int state) -{ - if (!block.isValid()) - return; - - int data = block.userState(); - if (data == -1) - data = 0; - block.setUserState((data & ~0xFF) | (state & 0xFF)); -} - void TextBlockUserData::setCodeFormatterData(CodeFormatterData *data) { if (m_codeFormatterData) @@ -480,6 +458,23 @@ void BaseTextDocumentLayout::changeBraceDepth(QTextBlock &block, int delta) setBraceDepth(block, braceDepth(block) + delta); } +void BaseTextDocumentLayout::setLexerState(const QTextBlock &block, int state) +{ + if (state == 0) { + if (TextBlockUserData *userData = testUserData(block)) + userData->setLexerState(0); + } else { + userData(block)->setLexerState(qMax(0,state)); + } +} + +int BaseTextDocumentLayout::lexerState(const QTextBlock &block) +{ + if (TextBlockUserData *userData = testUserData(block)) + return userData->lexerState(); + return 0; +} + void BaseTextDocumentLayout::setFoldingIndent(const QTextBlock &block, int indent) { if (indent == 0) { diff --git a/src/plugins/texteditor/basetextdocumentlayout.h b/src/plugins/texteditor/basetextdocumentlayout.h index d6fa481ec74..e70802d0098 100644 --- a/src/plugins/texteditor/basetextdocumentlayout.h +++ b/src/plugins/texteditor/basetextdocumentlayout.h @@ -68,6 +68,7 @@ public: : m_folded(false), m_ifdefedOut(false), m_foldingIndent(0), + m_lexerState(0), m_foldingStartIncluded(false), m_foldingEndIncluded(false), m_codeFormatterData(0) @@ -106,15 +107,15 @@ public: static bool findPreviousBlockOpenParenthesis(QTextCursor *cursor, bool checkStartPosition = false); static bool findNextBlockClosingParenthesis(QTextCursor *cursor); - int foldingIndent() const { return m_foldingIndent; } - void setFoldingIndent(int indent) { m_foldingIndent = indent; } - void setFoldingStartIncluded(bool included) { m_foldingStartIncluded = included; } - bool foldingStartIncluded() const { return m_foldingStartIncluded; } - void setFoldingEndIncluded(bool included) { m_foldingEndIncluded = included; } - bool foldingEndIncluded() const { return m_foldingEndIncluded; } + inline int foldingIndent() const { return m_foldingIndent; } + inline void setFoldingIndent(int indent) { m_foldingIndent = indent; } + inline void setFoldingStartIncluded(bool included) { m_foldingStartIncluded = included; } + inline bool foldingStartIncluded() const { return m_foldingStartIncluded; } + inline void setFoldingEndIncluded(bool included) { m_foldingEndIncluded = included; } + inline bool foldingEndIncluded() const { return m_foldingEndIncluded; } + inline int lexerState() const { return m_lexerState; } + inline void setLexerState(int state) {m_lexerState = state; } - static int lexerState(const QTextBlock &block); - static void setLexerState(QTextBlock block, int state); CodeFormatterData *codeFormatterData() const { return m_codeFormatterData; } void setCodeFormatterData(CodeFormatterData *data); @@ -124,6 +125,7 @@ private: uint m_folded : 1; uint m_ifdefedOut : 1; uint m_foldingIndent : 16; + uint m_lexerState : 4; uint m_foldingStartIncluded : 1; uint m_foldingEndIncluded : 1; Parentheses m_parentheses; @@ -152,6 +154,8 @@ public: static void changeBraceDepth(QTextBlock &block, int delta); static void setFoldingIndent(const QTextBlock &block, int indent); static int foldingIndent(const QTextBlock &block); + static void setLexerState(const QTextBlock &block, int state); + static int lexerState(const QTextBlock &block); static void changeFoldingIndent(QTextBlock &block, int delta); static bool canFold(const QTextBlock &block); static void doFoldOrUnfold(const QTextBlock& block, bool unfold); @@ -177,6 +181,7 @@ public: void setRequiredWidth(int width); QSizeF documentSize() const; + }; } // namespace TextEditor diff --git a/src/plugins/texteditor/basetexteditor.cpp b/src/plugins/texteditor/basetexteditor.cpp index 22d69c88728..e82160232b3 100644 --- a/src/plugins/texteditor/basetexteditor.cpp +++ b/src/plugins/texteditor/basetexteditor.cpp @@ -4028,11 +4028,7 @@ int BaseTextEditor::paragraphSeparatorAboutToBeInserted(QTextCursor &cursor) return 0; // verify that we indeed do have an extra opening brace in the document - int braceDepth = document()->lastBlock().userState(); - if (braceDepth >= 0) - braceDepth >>= 8; - else - braceDepth= 0; + int braceDepth = BaseTextDocumentLayout::braceDepth(document()->lastBlock()); if (braceDepth <= 0) return 0; // braces are all balanced or worse, no need to do anything @@ -4049,9 +4045,18 @@ int BaseTextEditor::paragraphSeparatorAboutToBeInserted(QTextCursor &cursor) const TabSettings &ts = tabSettings(); QTextBlock block = cursor.block(); int indentation = ts.indentationColumn(block.text()); - if (block.next().isValid() - && ts.indentationColumn(block.next().text()) > indentation) - return 0; + + if (block.next().isValid()) { // not the last block + block = block.next(); + //skip all empty blocks + while (block.isValid() && ts.onlySpace(block.text())) + block = block.next(); + if (block.isValid() + && ts.indentationColumn(block.text()) > indentation) { + qDebug() << "indentation check failed" << indentation << ts.indentationColumn(block.next().text()); + return 0; + } + } int pos = cursor.position(); diff --git a/src/plugins/texteditor/tabsettings.h b/src/plugins/texteditor/tabsettings.h index 3a15787b098..ae63e116a81 100644 --- a/src/plugins/texteditor/tabsettings.h +++ b/src/plugins/texteditor/tabsettings.h @@ -59,6 +59,7 @@ struct TEXTEDITOR_EXPORT TabSettings int lineIndentPosition(const QString &text) const; int firstNonSpace(const QString &text) const; + inline bool onlySpace(const QString &text) const { return firstNonSpace(text) == text.length(); } int columnAt(const QString &text, int position) const; int spacesLeftFromPosition(const QString &text, int position) const; int indentedColumn(int column, bool doIndent = true) const; -- GitLab