diff options
-rw-r--r-- | src/libs/utils/uncommentselection.cpp | 114 | ||||
-rw-r--r-- | src/libs/utils/uncommentselection.h | 20 | ||||
-rw-r--r-- | src/plugins/cmakeprojectmanager/cmakeeditor.cpp | 2 | ||||
-rw-r--r-- | src/plugins/pythoneditor/pythoneditorwidget.cpp | 6 | ||||
-rw-r--r-- | src/plugins/qt4projectmanager/profileeditor.cpp | 2 | ||||
-rw-r--r-- | src/plugins/texteditor/basetexteditor.cpp | 6 | ||||
-rw-r--r-- | src/plugins/texteditor/plaintexteditor.cpp | 8 |
7 files changed, 51 insertions, 107 deletions
diff --git a/src/libs/utils/uncommentselection.cpp b/src/libs/utils/uncommentselection.cpp index 26159c043f..dae53e3cd4 100644 --- a/src/libs/utils/uncommentselection.cpp +++ b/src/libs/utils/uncommentselection.cpp @@ -34,69 +34,32 @@ using namespace Utils; CommentDefinition::CommentDefinition() : - m_afterWhiteSpaces(false), - m_singleLine(QLatin1String("//")), - m_multiLineStart(QLatin1String("/*")), - m_multiLineEnd(QLatin1String("*/")) + isAfterWhiteSpaces(false), + singleLine(QLatin1String("//")), + multiLineStart(QLatin1String("/*")), + multiLineEnd(QLatin1String("*/")) {} -CommentDefinition &CommentDefinition::setAfterWhiteSpaces(const bool afterWhiteSpaces) -{ - m_afterWhiteSpaces = afterWhiteSpaces; - return *this; -} - -CommentDefinition &CommentDefinition::setSingleLine(const QString &singleLine) -{ - m_singleLine = singleLine; - return *this; -} - -CommentDefinition &CommentDefinition::setMultiLineStart(const QString &multiLineStart) +bool CommentDefinition::hasSingleLineStyle() const { - m_multiLineStart = multiLineStart; - return *this; + return !singleLine.isEmpty(); } -CommentDefinition &CommentDefinition::setMultiLineEnd(const QString &multiLineEnd) +bool CommentDefinition::hasMultiLineStyle() const { - m_multiLineEnd = multiLineEnd; - return *this; + return !multiLineStart.isEmpty() && !multiLineEnd.isEmpty(); } -bool CommentDefinition::isAfterWhiteSpaces() const -{ return m_afterWhiteSpaces; } - -const QString &CommentDefinition::singleLine() const -{ return m_singleLine; } - -const QString &CommentDefinition::multiLineStart() const -{ return m_multiLineStart; } - -const QString &CommentDefinition::multiLineEnd() const -{ return m_multiLineEnd; } - -bool CommentDefinition::hasSingleLineStyle() const -{ return !m_singleLine.isEmpty(); } - -bool CommentDefinition::hasMultiLineStyle() const -{ return !m_multiLineStart.isEmpty() && !m_multiLineEnd.isEmpty(); } - void CommentDefinition::clearCommentStyles() { - m_singleLine.clear(); - m_multiLineStart.clear(); - m_multiLineEnd.clear(); + singleLine.clear(); + multiLineStart.clear(); + multiLineEnd.clear(); } -namespace { - -bool isComment(const QString &text, - int index, - const CommentDefinition &definition, - const QString & (CommentDefinition::* comment) () const) +static bool isComment(const QString &text, int index, + const QString &commentType) { - const QString &commentType = ((definition).*(comment))(); const int length = commentType.length(); Q_ASSERT(text.length() - index >= length); @@ -110,8 +73,6 @@ bool isComment(const QString &text, return true; } -} // namespace anynomous - void Utils::unCommentSelection(QPlainTextEdit *edit, const CommentDefinition &definition) { @@ -146,42 +107,35 @@ void Utils::unCommentSelection(QPlainTextEdit *edit, const CommentDefinition &de QString startText = startBlock.text(); int startPos = start - startBlock.position(); - const int multiLineStartLength = definition.multiLineStart().length(); + const int multiLineStartLength = definition.multiLineStart.length(); bool hasLeadingCharacters = !startText.left(startPos).trimmed().isEmpty(); if (startPos >= multiLineStartLength && isComment(startText, startPos - multiLineStartLength, - definition, - &CommentDefinition::multiLineStart)) { + definition.multiLineStart)) { startPos -= multiLineStartLength; start -= multiLineStartLength; } - bool hasSelStart = (startPos <= startText.length() - multiLineStartLength - && isComment(startText, - startPos, - definition, - &CommentDefinition::multiLineStart)); + bool hasSelStart = startPos <= startText.length() - multiLineStartLength + && isComment(startText, startPos, definition.multiLineStart); QString endText = endBlock.text(); int endPos = end - endBlock.position(); - const int multiLineEndLength = definition.multiLineEnd().length(); + const int multiLineEndLength = definition.multiLineEnd.length(); bool hasTrailingCharacters = - !endText.left(endPos).remove(definition.singleLine()).trimmed().isEmpty() + !endText.left(endPos).remove(definition.singleLine).trimmed().isEmpty() && !endText.mid(endPos).trimmed().isEmpty(); if (endPos <= endText.length() - multiLineEndLength - && isComment(endText, endPos, definition, &CommentDefinition::multiLineEnd)) { + && isComment(endText, endPos, definition.multiLineEnd)) { endPos += multiLineEndLength; end += multiLineEndLength; } - bool hasSelEnd = (endPos >= multiLineEndLength - && isComment(endText, - endPos - multiLineEndLength, - definition, - &CommentDefinition::multiLineEnd)); + bool hasSelEnd = endPos >= multiLineEndLength + && isComment(endText, endPos - multiLineEndLength, definition.multiLineEnd); doMultiLineStyleUncomment = hasSelStart && hasSelEnd; doMultiLineStyleComment = !doMultiLineStyleUncomment @@ -191,8 +145,8 @@ void Utils::unCommentSelection(QPlainTextEdit *edit, const CommentDefinition &de } else if (!hasSelection && !definition.hasSingleLineStyle()) { QString text = startBlock.text().trimmed(); - doMultiLineStyleUncomment = text.startsWith(definition.multiLineStart()) - && text.endsWith(definition.multiLineEnd()); + doMultiLineStyleUncomment = text.startsWith(definition.multiLineStart) + && text.endsWith(definition.multiLineEnd); doMultiLineStyleComment = !doMultiLineStyleUncomment && !text.isEmpty(); start = startBlock.position(); @@ -212,36 +166,36 @@ void Utils::unCommentSelection(QPlainTextEdit *edit, const CommentDefinition &de cursor.setPosition(end); cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor, - definition.multiLineEnd().length()); + definition.multiLineEnd.length()); cursor.removeSelectedText(); cursor.setPosition(start); cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, - definition.multiLineStart().length()); + definition.multiLineStart.length()); cursor.removeSelectedText(); } else if (doMultiLineStyleComment) { cursor.setPosition(end); - cursor.insertText(definition.multiLineEnd()); + cursor.insertText(definition.multiLineEnd); cursor.setPosition(start); - cursor.insertText(definition.multiLineStart()); + cursor.insertText(definition.multiLineStart); } else { endBlock = endBlock.next(); doSingleLineStyleUncomment = true; for (QTextBlock block = startBlock; block != endBlock; block = block.next()) { QString text = block.text().trimmed(); - if (!text.isEmpty() && !text.startsWith(definition.singleLine())) { + if (!text.isEmpty() && !text.startsWith(definition.singleLine)) { doSingleLineStyleUncomment = false; break; } } - const int singleLineLength = definition.singleLine().length(); + const int singleLineLength = definition.singleLine.length(); for (QTextBlock block = startBlock; block != endBlock; block = block.next()) { if (doSingleLineStyleUncomment) { QString text = block.text(); int i = 0; while (i <= text.size() - singleLineLength) { - if (isComment(text, i, definition, &CommentDefinition::singleLine)) { + if (isComment(text, i, definition.singleLine)) { cursor.setPosition(block.position() + i); cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, @@ -254,14 +208,14 @@ void Utils::unCommentSelection(QPlainTextEdit *edit, const CommentDefinition &de ++i; } } else { - QString text = block.text(); + const QString text = block.text(); foreach (QChar c, text) { if (!c.isSpace()) { - if (definition.isAfterWhiteSpaces()) + if (definition.isAfterWhiteSpaces) cursor.setPosition(block.position() + text.indexOf(c)); else cursor.setPosition(block.position()); - cursor.insertText(definition.singleLine()); + cursor.insertText(definition.singleLine); break; } } diff --git a/src/libs/utils/uncommentselection.h b/src/libs/utils/uncommentselection.h index 511878bd41..a0cad994df 100644 --- a/src/libs/utils/uncommentselection.h +++ b/src/libs/utils/uncommentselection.h @@ -45,26 +45,16 @@ class QTCREATOR_UTILS_EXPORT CommentDefinition public: CommentDefinition(); - CommentDefinition &setAfterWhiteSpaces(const bool); - CommentDefinition &setSingleLine(const QString &singleLine); - CommentDefinition &setMultiLineStart(const QString &multiLineStart); - CommentDefinition &setMultiLineEnd(const QString &multiLineEnd); - - bool isAfterWhiteSpaces() const; - const QString &singleLine() const; - const QString &multiLineStart() const; - const QString &multiLineEnd() const; - bool hasSingleLineStyle() const; bool hasMultiLineStyle() const; void clearCommentStyles(); -private: - bool m_afterWhiteSpaces; - QString m_singleLine; - QString m_multiLineStart; - QString m_multiLineEnd; +public: + bool isAfterWhiteSpaces; + QString singleLine; + QString multiLineStart; + QString multiLineEnd; }; QTCREATOR_UTILS_EXPORT diff --git a/src/plugins/cmakeprojectmanager/cmakeeditor.cpp b/src/plugins/cmakeprojectmanager/cmakeeditor.cpp index d7aae9ba32..f91450d541 100644 --- a/src/plugins/cmakeprojectmanager/cmakeeditor.cpp +++ b/src/plugins/cmakeprojectmanager/cmakeeditor.cpp @@ -121,7 +121,7 @@ CMakeEditorWidget::CMakeEditorWidget(QWidget *parent, CMakeEditorFactory *factor baseTextDocument()->setSyntaxHighlighter(new CMakeHighlighter); m_commentDefinition.clearCommentStyles(); - m_commentDefinition.setSingleLine(QLatin1String("#")); + m_commentDefinition.singleLine = QLatin1Char('#'); ah->setupActions(this); } diff --git a/src/plugins/pythoneditor/pythoneditorwidget.cpp b/src/plugins/pythoneditor/pythoneditorwidget.cpp index ddd6fe9da4..15010ffa52 100644 --- a/src/plugins/pythoneditor/pythoneditorwidget.cpp +++ b/src/plugins/pythoneditor/pythoneditorwidget.cpp @@ -49,9 +49,9 @@ namespace PythonEditor { EditorWidget::EditorWidget(QWidget *parent) :TextEditor::BaseTextEditorWidget(parent) { - m_commentDefinition.setMultiLineStart(QString()); - m_commentDefinition.setMultiLineEnd(QString()); - m_commentDefinition.setSingleLine(QLatin1String("#")); + m_commentDefinition.multiLineStart.clear(); + m_commentDefinition.multiLineEnd.clear(); + m_commentDefinition.singleLine = QLatin1Char('#'); setParenthesesMatchingEnabled(true); setMarksVisible(true); diff --git a/src/plugins/qt4projectmanager/profileeditor.cpp b/src/plugins/qt4projectmanager/profileeditor.cpp index c9d3f0a599..0ed797d372 100644 --- a/src/plugins/qt4projectmanager/profileeditor.cpp +++ b/src/plugins/qt4projectmanager/profileeditor.cpp @@ -85,7 +85,7 @@ ProFileEditorWidget::ProFileEditorWidget(QWidget *parent, ProFileEditorFactory * baseTextDocument()->setSyntaxHighlighter(new ProFileHighlighter); m_commentDefinition.clearCommentStyles(); - m_commentDefinition.setSingleLine(QString(QLatin1Char('#'))); + m_commentDefinition.singleLine = QLatin1Char('#'); } void ProFileEditorWidget::unCommentSelection() diff --git a/src/plugins/texteditor/basetexteditor.cpp b/src/plugins/texteditor/basetexteditor.cpp index 3e3da710ac..db08b1df87 100644 --- a/src/plugins/texteditor/basetexteditor.cpp +++ b/src/plugins/texteditor/basetexteditor.cpp @@ -1178,13 +1178,13 @@ void BaseTextEditorWidget::moveLineUpDown(bool up) QString trimmedText(text.trimmed()); if (commentDefinition->hasSingleLineStyle()) { - if (trimmedText.startsWith(commentDefinition->singleLine())) + if (trimmedText.startsWith(commentDefinition->singleLine)) shouldReindent = false; } if (shouldReindent && commentDefinition->hasMultiLineStyle()) { // Don't have any single line comments; try multi line. - if (trimmedText.startsWith(commentDefinition->multiLineStart()) - && trimmedText.endsWith(commentDefinition->multiLineEnd())) { + if (trimmedText.startsWith(commentDefinition->multiLineStart) + && trimmedText.endsWith(commentDefinition->multiLineEnd)) { shouldReindent = false; } } diff --git a/src/plugins/texteditor/plaintexteditor.cpp b/src/plugins/texteditor/plaintexteditor.cpp index c0a8982208..322113738f 100644 --- a/src/plugins/texteditor/plaintexteditor.cpp +++ b/src/plugins/texteditor/plaintexteditor.cpp @@ -167,10 +167,10 @@ void PlainTextEditorWidget::configure(const Core::MimeType &mimeType) if (!definition.isNull() && definition->isValid()) { highlighter->setDefaultContext(definition->initialContext()); - m_commentDefinition.setAfterWhiteSpaces(definition->isCommentAfterWhiteSpaces()); - m_commentDefinition.setSingleLine(definition->singleLineComment()); - m_commentDefinition.setMultiLineStart(definition->multiLineCommentStart()); - m_commentDefinition.setMultiLineEnd(definition->multiLineCommentEnd()); + m_commentDefinition.isAfterWhiteSpaces = definition->isCommentAfterWhiteSpaces(); + m_commentDefinition.singleLine = definition->singleLineComment(); + m_commentDefinition.multiLineStart = definition->multiLineCommentStart(); + m_commentDefinition.multiLineEnd = definition->multiLineCommentEnd(); setCodeFoldingSupported(true); } |