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
This commit is contained in:
mae
2010-07-12 11:16:10 +02:00
parent c1d93f4a45
commit ab8a8c018d
5 changed files with 47 additions and 41 deletions

View File

@@ -857,7 +857,7 @@ int CodeFormatter::tokenizeBlock(const QTextBlock &block, bool *endedJoined)
*endedJoined = tokenize.endedJoined(); *endedJoined = tokenize.endedJoined();
const int lexerState = tokenize.state(); const int lexerState = tokenize.state();
TextBlockUserData::setLexerState(block, lexerState); BaseTextDocumentLayout::setLexerState(block, lexerState);
return lexerState; return lexerState;
} }
@@ -942,12 +942,12 @@ bool QtStyleCodeFormatter::loadBlockData(const QTextBlock &block, BlockData *dat
void QtStyleCodeFormatter::saveLexerState(QTextBlock *block, int state) const void QtStyleCodeFormatter::saveLexerState(QTextBlock *block, int state) const
{ {
TextBlockUserData::setLexerState(*block, state); BaseTextDocumentLayout::setLexerState(*block, state);
} }
int QtStyleCodeFormatter::loadLexerState(const QTextBlock &block) const 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 void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedIndentDepth) const

View File

@@ -366,28 +366,6 @@ TextBlockUserData::MatchType TextBlockUserData::matchCursorForward(QTextCursor *
return NoMatch; 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) void TextBlockUserData::setCodeFormatterData(CodeFormatterData *data)
{ {
if (m_codeFormatterData) if (m_codeFormatterData)
@@ -480,6 +458,23 @@ void BaseTextDocumentLayout::changeBraceDepth(QTextBlock &block, int delta)
setBraceDepth(block, braceDepth(block) + 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) void BaseTextDocumentLayout::setFoldingIndent(const QTextBlock &block, int indent)
{ {
if (indent == 0) { if (indent == 0) {

View File

@@ -68,6 +68,7 @@ public:
: m_folded(false), : m_folded(false),
m_ifdefedOut(false), m_ifdefedOut(false),
m_foldingIndent(0), m_foldingIndent(0),
m_lexerState(0),
m_foldingStartIncluded(false), m_foldingStartIncluded(false),
m_foldingEndIncluded(false), m_foldingEndIncluded(false),
m_codeFormatterData(0) m_codeFormatterData(0)
@@ -106,15 +107,15 @@ public:
static bool findPreviousBlockOpenParenthesis(QTextCursor *cursor, bool checkStartPosition = false); static bool findPreviousBlockOpenParenthesis(QTextCursor *cursor, bool checkStartPosition = false);
static bool findNextBlockClosingParenthesis(QTextCursor *cursor); static bool findNextBlockClosingParenthesis(QTextCursor *cursor);
int foldingIndent() const { return m_foldingIndent; } inline int foldingIndent() const { return m_foldingIndent; }
void setFoldingIndent(int indent) { m_foldingIndent = indent; } inline void setFoldingIndent(int indent) { m_foldingIndent = indent; }
void setFoldingStartIncluded(bool included) { m_foldingStartIncluded = included; } inline void setFoldingStartIncluded(bool included) { m_foldingStartIncluded = included; }
bool foldingStartIncluded() const { return m_foldingStartIncluded; } inline bool foldingStartIncluded() const { return m_foldingStartIncluded; }
void setFoldingEndIncluded(bool included) { m_foldingEndIncluded = included; } inline void setFoldingEndIncluded(bool included) { m_foldingEndIncluded = included; }
bool foldingEndIncluded() const { return m_foldingEndIncluded; } 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; } CodeFormatterData *codeFormatterData() const { return m_codeFormatterData; }
void setCodeFormatterData(CodeFormatterData *data); void setCodeFormatterData(CodeFormatterData *data);
@@ -124,6 +125,7 @@ private:
uint m_folded : 1; uint m_folded : 1;
uint m_ifdefedOut : 1; uint m_ifdefedOut : 1;
uint m_foldingIndent : 16; uint m_foldingIndent : 16;
uint m_lexerState : 4;
uint m_foldingStartIncluded : 1; uint m_foldingStartIncluded : 1;
uint m_foldingEndIncluded : 1; uint m_foldingEndIncluded : 1;
Parentheses m_parentheses; Parentheses m_parentheses;
@@ -152,6 +154,8 @@ public:
static void changeBraceDepth(QTextBlock &block, int delta); static void changeBraceDepth(QTextBlock &block, int delta);
static void setFoldingIndent(const QTextBlock &block, int indent); static void setFoldingIndent(const QTextBlock &block, int indent);
static int foldingIndent(const QTextBlock &block); 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 void changeFoldingIndent(QTextBlock &block, int delta);
static bool canFold(const QTextBlock &block); static bool canFold(const QTextBlock &block);
static void doFoldOrUnfold(const QTextBlock& block, bool unfold); static void doFoldOrUnfold(const QTextBlock& block, bool unfold);
@@ -177,6 +181,7 @@ public:
void setRequiredWidth(int width); void setRequiredWidth(int width);
QSizeF documentSize() const; QSizeF documentSize() const;
}; };
} // namespace TextEditor } // namespace TextEditor

View File

@@ -4028,11 +4028,7 @@ int BaseTextEditor::paragraphSeparatorAboutToBeInserted(QTextCursor &cursor)
return 0; return 0;
// verify that we indeed do have an extra opening brace in the document // verify that we indeed do have an extra opening brace in the document
int braceDepth = document()->lastBlock().userState(); int braceDepth = BaseTextDocumentLayout::braceDepth(document()->lastBlock());
if (braceDepth >= 0)
braceDepth >>= 8;
else
braceDepth= 0;
if (braceDepth <= 0) if (braceDepth <= 0)
return 0; // braces are all balanced or worse, no need to do anything 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(); const TabSettings &ts = tabSettings();
QTextBlock block = cursor.block(); QTextBlock block = cursor.block();
int indentation = ts.indentationColumn(block.text()); int indentation = ts.indentationColumn(block.text());
if (block.next().isValid()
&& ts.indentationColumn(block.next().text()) > indentation) 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; return 0;
}
}
int pos = cursor.position(); int pos = cursor.position();

View File

@@ -59,6 +59,7 @@ struct TEXTEDITOR_EXPORT TabSettings
int lineIndentPosition(const QString &text) const; int lineIndentPosition(const QString &text) const;
int firstNonSpace(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 columnAt(const QString &text, int position) const;
int spacesLeftFromPosition(const QString &text, int position) const; int spacesLeftFromPosition(const QString &text, int position) const;
int indentedColumn(int column, bool doIndent = true) const; int indentedColumn(int column, bool doIndent = true) const;