Commit ab8a8c01 authored by mae's avatar mae
Browse files

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
parent c1d93f4a
......@@ -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
......
......@@ -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) {
......
......@@ -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
......
......@@ -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();
......
......@@ -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;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment