Commit 4e75fc8c authored by hjk's avatar hjk Committed by David Schulz
Browse files

TextEditor: Simplify Utils::CommentDefinition structure



Change-Id: I8fc97ed61c47af2c3d9e5cc2bf81e97661204d4f
Reviewed-by: default avatarDavid Schulz <david.schulz@digia.com>
parent dfc0ee64
......@@ -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;
}
}
......
......@@ -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
......
......@@ -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);
}
......
......@@ -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);
......
......@@ -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()
......
......@@ -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;
}
}
......
......@@ -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);
}
......
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