From 1989061231fc2bbca9cb04f1c8bb0c4fb6d28e9b Mon Sep 17 00:00:00 2001 From: Roberto Raggi Date: Tue, 12 Jan 2010 14:13:43 +0100 Subject: [PATCH] Initial support of auto-brace completion in QML --- src/plugins/qmleditor/qmleditor.cpp | 93 +++++++++++++++++++++++++++++ src/plugins/qmleditor/qmleditor.h | 6 ++ 2 files changed, 99 insertions(+) diff --git a/src/plugins/qmleditor/qmleditor.cpp b/src/plugins/qmleditor/qmleditor.cpp index 2413840b0d6..2278b34caa5 100644 --- a/src/plugins/qmleditor/qmleditor.cpp +++ b/src/plugins/qmleditor/qmleditor.cpp @@ -719,5 +719,98 @@ void QmlTextEditor::unCommentSelection() Utils::unCommentSelection(this); } +bool QmlTextEditor::contextAllowsAutoParentheses(const QTextCursor &cursor, const QString &textToInsert) const +{ + if (isInComment(cursor)) + return false; + + QChar ch; + + if (! textToInsert.isEmpty()) + ch = textToInsert.at(0); + + switch (ch.unicode()) { +#if 0 // ### enable me + case '\'': + case '"': +#endif + + case '(': + case '[': + case '{': + + case ')': + case ']': + case '}': + + case ';': + return true; + + default: + if (ch.isNull()) + return true; + } // end of switch + + return false; +} + +bool QmlTextEditor::isInComment(const QTextCursor &) const +{ + // ### implement me + return false; +} + +QString QmlTextEditor::insertMatchingBrace(const QTextCursor &tc, const QString &text, const QChar &, int *skippedChars) const +{ + if (text.length() != 1) + return QString(); + + const QChar la = characterAt(tc.position()); + + const QChar ch = text.at(0); + switch (ch.unicode()) { +#if 0 // ### enable me + case '\'': + if (la != ch) + return QString(ch); + ++*skippedChars; + break; + + case '"': + if (la != ch) + return QString(ch); + ++*skippedChars; + break; +#endif + + case '(': + return QString(QLatin1Char(')')); + + case '[': + return QString(QLatin1Char(']')); + + case '{': + return QString(); // nothing to do. + + case ')': + case ']': + case '}': + case ';': + if (la == ch) + ++*skippedChars; + break; + + default: + break; + } // end of switch + + return QString(); +} + +QString QmlTextEditor::insertParagraphSeparator(const QTextCursor &) const +{ + return QLatin1String("}\n"); +} + } // namespace Internal } // namespace QmlEditor diff --git a/src/plugins/qmleditor/qmleditor.h b/src/plugins/qmleditor/qmleditor.h index 75221ec77f1..9a5b2ef6c57 100644 --- a/src/plugins/qmleditor/qmleditor.h +++ b/src/plugins/qmleditor/qmleditor.h @@ -132,6 +132,12 @@ protected: void createToolBar(QmlEditorEditable *editable); TextEditor::BaseTextEditor::Link findLinkAt(const QTextCursor &cursor, bool resolveTarget = true); + //// brace matching + virtual bool contextAllowsAutoParentheses(const QTextCursor &cursor, const QString &textToInsert = QString()) const; + virtual bool isInComment(const QTextCursor &cursor) const; + virtual QString insertMatchingBrace(const QTextCursor &tc, const QString &text, const QChar &la, int *skippedChars) const; + virtual QString insertParagraphSeparator(const QTextCursor &tc) const; + private: virtual bool isElectricCharacter(const QChar &ch) const; virtual void indentBlock(QTextDocument *doc, QTextBlock block, QChar typedChar);