forked from qt-creator/qt-creator
TextEditor: Simplify Utils::CommentDefinition structure
Change-Id: I8fc97ed61c47af2c3d9e5cc2bf81e97661204d4f Reviewed-by: David Schulz <david.schulz@digia.com>
This commit is contained in:
@@ -34,69 +34,32 @@
|
|||||||
using namespace Utils;
|
using namespace Utils;
|
||||||
|
|
||||||
CommentDefinition::CommentDefinition() :
|
CommentDefinition::CommentDefinition() :
|
||||||
m_afterWhiteSpaces(false),
|
isAfterWhiteSpaces(false),
|
||||||
m_singleLine(QLatin1String("//")),
|
singleLine(QLatin1String("//")),
|
||||||
m_multiLineStart(QLatin1String("/*")),
|
multiLineStart(QLatin1String("/*")),
|
||||||
m_multiLineEnd(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)
|
|
||||||
{
|
|
||||||
m_multiLineStart = multiLineStart;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
CommentDefinition &CommentDefinition::setMultiLineEnd(const QString &multiLineEnd)
|
|
||||||
{
|
|
||||||
m_multiLineEnd = multiLineEnd;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
bool CommentDefinition::hasSingleLineStyle() const
|
||||||
{ return !m_singleLine.isEmpty(); }
|
{
|
||||||
|
return !singleLine.isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
bool CommentDefinition::hasMultiLineStyle() const
|
bool CommentDefinition::hasMultiLineStyle() const
|
||||||
{ return !m_multiLineStart.isEmpty() && !m_multiLineEnd.isEmpty(); }
|
{
|
||||||
|
return !multiLineStart.isEmpty() && !multiLineEnd.isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
void CommentDefinition::clearCommentStyles()
|
void CommentDefinition::clearCommentStyles()
|
||||||
{
|
{
|
||||||
m_singleLine.clear();
|
singleLine.clear();
|
||||||
m_multiLineStart.clear();
|
multiLineStart.clear();
|
||||||
m_multiLineEnd.clear();
|
multiLineEnd.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace {
|
static bool isComment(const QString &text, int index,
|
||||||
|
const QString &commentType)
|
||||||
bool isComment(const QString &text,
|
|
||||||
int index,
|
|
||||||
const CommentDefinition &definition,
|
|
||||||
const QString & (CommentDefinition::* comment) () const)
|
|
||||||
{
|
{
|
||||||
const QString &commentType = ((definition).*(comment))();
|
|
||||||
const int length = commentType.length();
|
const int length = commentType.length();
|
||||||
|
|
||||||
Q_ASSERT(text.length() - index >= length);
|
Q_ASSERT(text.length() - index >= length);
|
||||||
@@ -110,8 +73,6 @@ bool isComment(const QString &text,
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace anynomous
|
|
||||||
|
|
||||||
|
|
||||||
void Utils::unCommentSelection(QPlainTextEdit *edit, const CommentDefinition &definition)
|
void Utils::unCommentSelection(QPlainTextEdit *edit, const CommentDefinition &definition)
|
||||||
{
|
{
|
||||||
@@ -146,42 +107,35 @@ void Utils::unCommentSelection(QPlainTextEdit *edit, const CommentDefinition &de
|
|||||||
|
|
||||||
QString startText = startBlock.text();
|
QString startText = startBlock.text();
|
||||||
int startPos = start - startBlock.position();
|
int startPos = start - startBlock.position();
|
||||||
const int multiLineStartLength = definition.multiLineStart().length();
|
const int multiLineStartLength = definition.multiLineStart.length();
|
||||||
bool hasLeadingCharacters = !startText.left(startPos).trimmed().isEmpty();
|
bool hasLeadingCharacters = !startText.left(startPos).trimmed().isEmpty();
|
||||||
|
|
||||||
if (startPos >= multiLineStartLength
|
if (startPos >= multiLineStartLength
|
||||||
&& isComment(startText,
|
&& isComment(startText,
|
||||||
startPos - multiLineStartLength,
|
startPos - multiLineStartLength,
|
||||||
definition,
|
definition.multiLineStart)) {
|
||||||
&CommentDefinition::multiLineStart)) {
|
|
||||||
startPos -= multiLineStartLength;
|
startPos -= multiLineStartLength;
|
||||||
start -= multiLineStartLength;
|
start -= multiLineStartLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool hasSelStart = (startPos <= startText.length() - multiLineStartLength
|
bool hasSelStart = startPos <= startText.length() - multiLineStartLength
|
||||||
&& isComment(startText,
|
&& isComment(startText, startPos, definition.multiLineStart);
|
||||||
startPos,
|
|
||||||
definition,
|
|
||||||
&CommentDefinition::multiLineStart));
|
|
||||||
|
|
||||||
QString endText = endBlock.text();
|
QString endText = endBlock.text();
|
||||||
int endPos = end - endBlock.position();
|
int endPos = end - endBlock.position();
|
||||||
const int multiLineEndLength = definition.multiLineEnd().length();
|
const int multiLineEndLength = definition.multiLineEnd.length();
|
||||||
bool hasTrailingCharacters =
|
bool hasTrailingCharacters =
|
||||||
!endText.left(endPos).remove(definition.singleLine()).trimmed().isEmpty()
|
!endText.left(endPos).remove(definition.singleLine).trimmed().isEmpty()
|
||||||
&& !endText.mid(endPos).trimmed().isEmpty();
|
&& !endText.mid(endPos).trimmed().isEmpty();
|
||||||
|
|
||||||
if (endPos <= endText.length() - multiLineEndLength
|
if (endPos <= endText.length() - multiLineEndLength
|
||||||
&& isComment(endText, endPos, definition, &CommentDefinition::multiLineEnd)) {
|
&& isComment(endText, endPos, definition.multiLineEnd)) {
|
||||||
endPos += multiLineEndLength;
|
endPos += multiLineEndLength;
|
||||||
end += multiLineEndLength;
|
end += multiLineEndLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool hasSelEnd = (endPos >= multiLineEndLength
|
bool hasSelEnd = endPos >= multiLineEndLength
|
||||||
&& isComment(endText,
|
&& isComment(endText, endPos - multiLineEndLength, definition.multiLineEnd);
|
||||||
endPos - multiLineEndLength,
|
|
||||||
definition,
|
|
||||||
&CommentDefinition::multiLineEnd));
|
|
||||||
|
|
||||||
doMultiLineStyleUncomment = hasSelStart && hasSelEnd;
|
doMultiLineStyleUncomment = hasSelStart && hasSelEnd;
|
||||||
doMultiLineStyleComment = !doMultiLineStyleUncomment
|
doMultiLineStyleComment = !doMultiLineStyleUncomment
|
||||||
@@ -191,8 +145,8 @@ void Utils::unCommentSelection(QPlainTextEdit *edit, const CommentDefinition &de
|
|||||||
} else if (!hasSelection && !definition.hasSingleLineStyle()) {
|
} else if (!hasSelection && !definition.hasSingleLineStyle()) {
|
||||||
|
|
||||||
QString text = startBlock.text().trimmed();
|
QString text = startBlock.text().trimmed();
|
||||||
doMultiLineStyleUncomment = text.startsWith(definition.multiLineStart())
|
doMultiLineStyleUncomment = text.startsWith(definition.multiLineStart)
|
||||||
&& text.endsWith(definition.multiLineEnd());
|
&& text.endsWith(definition.multiLineEnd);
|
||||||
doMultiLineStyleComment = !doMultiLineStyleUncomment && !text.isEmpty();
|
doMultiLineStyleComment = !doMultiLineStyleUncomment && !text.isEmpty();
|
||||||
|
|
||||||
start = startBlock.position();
|
start = startBlock.position();
|
||||||
@@ -212,36 +166,36 @@ void Utils::unCommentSelection(QPlainTextEdit *edit, const CommentDefinition &de
|
|||||||
cursor.setPosition(end);
|
cursor.setPosition(end);
|
||||||
cursor.movePosition(QTextCursor::PreviousCharacter,
|
cursor.movePosition(QTextCursor::PreviousCharacter,
|
||||||
QTextCursor::KeepAnchor,
|
QTextCursor::KeepAnchor,
|
||||||
definition.multiLineEnd().length());
|
definition.multiLineEnd.length());
|
||||||
cursor.removeSelectedText();
|
cursor.removeSelectedText();
|
||||||
cursor.setPosition(start);
|
cursor.setPosition(start);
|
||||||
cursor.movePosition(QTextCursor::NextCharacter,
|
cursor.movePosition(QTextCursor::NextCharacter,
|
||||||
QTextCursor::KeepAnchor,
|
QTextCursor::KeepAnchor,
|
||||||
definition.multiLineStart().length());
|
definition.multiLineStart.length());
|
||||||
cursor.removeSelectedText();
|
cursor.removeSelectedText();
|
||||||
} else if (doMultiLineStyleComment) {
|
} else if (doMultiLineStyleComment) {
|
||||||
cursor.setPosition(end);
|
cursor.setPosition(end);
|
||||||
cursor.insertText(definition.multiLineEnd());
|
cursor.insertText(definition.multiLineEnd);
|
||||||
cursor.setPosition(start);
|
cursor.setPosition(start);
|
||||||
cursor.insertText(definition.multiLineStart());
|
cursor.insertText(definition.multiLineStart);
|
||||||
} else {
|
} else {
|
||||||
endBlock = endBlock.next();
|
endBlock = endBlock.next();
|
||||||
doSingleLineStyleUncomment = true;
|
doSingleLineStyleUncomment = true;
|
||||||
for (QTextBlock block = startBlock; block != endBlock; block = block.next()) {
|
for (QTextBlock block = startBlock; block != endBlock; block = block.next()) {
|
||||||
QString text = block.text().trimmed();
|
QString text = block.text().trimmed();
|
||||||
if (!text.isEmpty() && !text.startsWith(definition.singleLine())) {
|
if (!text.isEmpty() && !text.startsWith(definition.singleLine)) {
|
||||||
doSingleLineStyleUncomment = false;
|
doSingleLineStyleUncomment = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const int singleLineLength = definition.singleLine().length();
|
const int singleLineLength = definition.singleLine.length();
|
||||||
for (QTextBlock block = startBlock; block != endBlock; block = block.next()) {
|
for (QTextBlock block = startBlock; block != endBlock; block = block.next()) {
|
||||||
if (doSingleLineStyleUncomment) {
|
if (doSingleLineStyleUncomment) {
|
||||||
QString text = block.text();
|
QString text = block.text();
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while (i <= text.size() - singleLineLength) {
|
while (i <= text.size() - singleLineLength) {
|
||||||
if (isComment(text, i, definition, &CommentDefinition::singleLine)) {
|
if (isComment(text, i, definition.singleLine)) {
|
||||||
cursor.setPosition(block.position() + i);
|
cursor.setPosition(block.position() + i);
|
||||||
cursor.movePosition(QTextCursor::NextCharacter,
|
cursor.movePosition(QTextCursor::NextCharacter,
|
||||||
QTextCursor::KeepAnchor,
|
QTextCursor::KeepAnchor,
|
||||||
@@ -254,14 +208,14 @@ void Utils::unCommentSelection(QPlainTextEdit *edit, const CommentDefinition &de
|
|||||||
++i;
|
++i;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
QString text = block.text();
|
const QString text = block.text();
|
||||||
foreach (QChar c, text) {
|
foreach (QChar c, text) {
|
||||||
if (!c.isSpace()) {
|
if (!c.isSpace()) {
|
||||||
if (definition.isAfterWhiteSpaces())
|
if (definition.isAfterWhiteSpaces)
|
||||||
cursor.setPosition(block.position() + text.indexOf(c));
|
cursor.setPosition(block.position() + text.indexOf(c));
|
||||||
else
|
else
|
||||||
cursor.setPosition(block.position());
|
cursor.setPosition(block.position());
|
||||||
cursor.insertText(definition.singleLine());
|
cursor.insertText(definition.singleLine);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,26 +45,16 @@ class QTCREATOR_UTILS_EXPORT CommentDefinition
|
|||||||
public:
|
public:
|
||||||
CommentDefinition();
|
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 hasSingleLineStyle() const;
|
||||||
bool hasMultiLineStyle() const;
|
bool hasMultiLineStyle() const;
|
||||||
|
|
||||||
void clearCommentStyles();
|
void clearCommentStyles();
|
||||||
|
|
||||||
private:
|
public:
|
||||||
bool m_afterWhiteSpaces;
|
bool isAfterWhiteSpaces;
|
||||||
QString m_singleLine;
|
QString singleLine;
|
||||||
QString m_multiLineStart;
|
QString multiLineStart;
|
||||||
QString m_multiLineEnd;
|
QString multiLineEnd;
|
||||||
};
|
};
|
||||||
|
|
||||||
QTCREATOR_UTILS_EXPORT
|
QTCREATOR_UTILS_EXPORT
|
||||||
|
|||||||
@@ -121,7 +121,7 @@ CMakeEditorWidget::CMakeEditorWidget(QWidget *parent, CMakeEditorFactory *factor
|
|||||||
baseTextDocument()->setSyntaxHighlighter(new CMakeHighlighter);
|
baseTextDocument()->setSyntaxHighlighter(new CMakeHighlighter);
|
||||||
|
|
||||||
m_commentDefinition.clearCommentStyles();
|
m_commentDefinition.clearCommentStyles();
|
||||||
m_commentDefinition.setSingleLine(QLatin1String("#"));
|
m_commentDefinition.singleLine = QLatin1Char('#');
|
||||||
|
|
||||||
ah->setupActions(this);
|
ah->setupActions(this);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,9 +49,9 @@ namespace PythonEditor {
|
|||||||
EditorWidget::EditorWidget(QWidget *parent)
|
EditorWidget::EditorWidget(QWidget *parent)
|
||||||
:TextEditor::BaseTextEditorWidget(parent)
|
:TextEditor::BaseTextEditorWidget(parent)
|
||||||
{
|
{
|
||||||
m_commentDefinition.setMultiLineStart(QString());
|
m_commentDefinition.multiLineStart.clear();
|
||||||
m_commentDefinition.setMultiLineEnd(QString());
|
m_commentDefinition.multiLineEnd.clear();
|
||||||
m_commentDefinition.setSingleLine(QLatin1String("#"));
|
m_commentDefinition.singleLine = QLatin1Char('#');
|
||||||
|
|
||||||
setParenthesesMatchingEnabled(true);
|
setParenthesesMatchingEnabled(true);
|
||||||
setMarksVisible(true);
|
setMarksVisible(true);
|
||||||
|
|||||||
@@ -85,7 +85,7 @@ ProFileEditorWidget::ProFileEditorWidget(QWidget *parent, ProFileEditorFactory *
|
|||||||
|
|
||||||
baseTextDocument()->setSyntaxHighlighter(new ProFileHighlighter);
|
baseTextDocument()->setSyntaxHighlighter(new ProFileHighlighter);
|
||||||
m_commentDefinition.clearCommentStyles();
|
m_commentDefinition.clearCommentStyles();
|
||||||
m_commentDefinition.setSingleLine(QString(QLatin1Char('#')));
|
m_commentDefinition.singleLine = QLatin1Char('#');
|
||||||
}
|
}
|
||||||
|
|
||||||
void ProFileEditorWidget::unCommentSelection()
|
void ProFileEditorWidget::unCommentSelection()
|
||||||
|
|||||||
@@ -1178,13 +1178,13 @@ void BaseTextEditorWidget::moveLineUpDown(bool up)
|
|||||||
QString trimmedText(text.trimmed());
|
QString trimmedText(text.trimmed());
|
||||||
|
|
||||||
if (commentDefinition->hasSingleLineStyle()) {
|
if (commentDefinition->hasSingleLineStyle()) {
|
||||||
if (trimmedText.startsWith(commentDefinition->singleLine()))
|
if (trimmedText.startsWith(commentDefinition->singleLine))
|
||||||
shouldReindent = false;
|
shouldReindent = false;
|
||||||
}
|
}
|
||||||
if (shouldReindent && commentDefinition->hasMultiLineStyle()) {
|
if (shouldReindent && commentDefinition->hasMultiLineStyle()) {
|
||||||
// Don't have any single line comments; try multi line.
|
// Don't have any single line comments; try multi line.
|
||||||
if (trimmedText.startsWith(commentDefinition->multiLineStart())
|
if (trimmedText.startsWith(commentDefinition->multiLineStart)
|
||||||
&& trimmedText.endsWith(commentDefinition->multiLineEnd())) {
|
&& trimmedText.endsWith(commentDefinition->multiLineEnd)) {
|
||||||
shouldReindent = false;
|
shouldReindent = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -167,10 +167,10 @@ void PlainTextEditorWidget::configure(const Core::MimeType &mimeType)
|
|||||||
if (!definition.isNull() && definition->isValid()) {
|
if (!definition.isNull() && definition->isValid()) {
|
||||||
highlighter->setDefaultContext(definition->initialContext());
|
highlighter->setDefaultContext(definition->initialContext());
|
||||||
|
|
||||||
m_commentDefinition.setAfterWhiteSpaces(definition->isCommentAfterWhiteSpaces());
|
m_commentDefinition.isAfterWhiteSpaces = definition->isCommentAfterWhiteSpaces();
|
||||||
m_commentDefinition.setSingleLine(definition->singleLineComment());
|
m_commentDefinition.singleLine = definition->singleLineComment();
|
||||||
m_commentDefinition.setMultiLineStart(definition->multiLineCommentStart());
|
m_commentDefinition.multiLineStart = definition->multiLineCommentStart();
|
||||||
m_commentDefinition.setMultiLineEnd(definition->multiLineCommentEnd());
|
m_commentDefinition.multiLineEnd = definition->multiLineCommentEnd();
|
||||||
|
|
||||||
setCodeFoldingSupported(true);
|
setCodeFoldingSupported(true);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user