diff --git a/src/plugins/cpptools/cppcodeformatter.cpp b/src/plugins/cpptools/cppcodeformatter.cpp
index 3c24648e96bec2948683fa47ef362a05e90772b6..75fe53fda81be2bcfe8e863ab1167a7eae06dbc9 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 18a41919f8888ceb06796c4a9bfd03e6de3e14e7..c69808f8de8838de3b518ddac59fbb0322607768 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 d6fa481ec74d928350f3289927c2027cd2695648..e70802d0098ce2d4cfb60323bf5bc88c3064681f 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 22d69c8872885144e966b77ce8f093a47ef0bf74..e82160232b36726268d8c2773bea1d6c7a04b4ea 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 3a15787b0986560d169dc111f4c046baf93ae198..ae63e116a81070b7dc36dcc7d72a12eb37a817e3 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;