Commit e53ae818 authored by Mitch Curtis's avatar Mitch Curtis

CPP, QmlJs editors: don't indent when moving commented lines up/down.

Currently, pressing Ctrl + Shift + Up/Down with a commented block of
text selected will indent that block if the line two lines above the
block is indented. This is undesirable, because the indenting for that
block is incorrect when it is uncommented. See the following example:

Step 1:
void f()
{
    int x;

//    int y;
}

Step 2 - After Ctrl + Shift + Up:
void f()
{
    int x;
    //    int y;

}

Step 3 - After uncommenting the block:
void f()
{
    int x;
        int y;

}

This patch tells the CPP and QmlJs editors not to indent commented
blocks when moving them. Blocks that are not entirely within comments
(excluding whitespace) are not affected.

Tested with (C++ and JavaScript, respectively):
http://paste.kde.org/688778/
http://paste.kde.org/688784/

Change-Id: I35414e6dfd5a1084fd997594e711ea9932231981
Reviewed-by: default avatarDavid Schulz <david.schulz@digia.com>
parent b682adae
......@@ -80,7 +80,6 @@
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/mimedatabase.h>
#include <utils/qtcassert.h>
#include <utils/uncommentselection.h>
#include <extensionsystem/pluginmanager.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <texteditor/basetextdocument.h>
......@@ -1964,6 +1963,11 @@ bool CPPEditor::open(QString *errorString, const QString &fileName, const QStrin
return b;
}
const Utils::CommentDefinition *CPPEditor::commentDefinition() const
{
return &m_commentDefinition;
}
void CPPEditorWidget::setFontSettings(const TextEditor::FontSettings &fs)
{
TextEditor::BaseTextEditorWidget::setFontSettings(fs);
......
......@@ -36,6 +36,7 @@
#include <cpptools/ModelManagerInterface.h>
#include <cplusplus/CppDocument.h>
#include <cplusplus/LookupContext.h>
#include <utils/uncommentselection.h>
#include <texteditor/basetexteditor.h>
#include <texteditor/quickfix.h>
#include <texteditor/texteditorconstants.h>
......@@ -155,6 +156,10 @@ public:
bool isTemporary() const { return false; }
bool open(QString *errorString, const QString &fileName, const QString &realFileName);
const Utils::CommentDefinition *commentDefinition() const;
private:
Utils::CommentDefinition m_commentDefinition;
};
class CPPEditorWidget : public TextEditor::BaseTextEditorWidget
......
......@@ -96,4 +96,9 @@ void QmlJSEditorEditable::setTextCodec(QTextCodec *codec, TextCodecReason reason
editorWidget()->setTextCodec(codec);
}
const Utils::CommentDefinition *QmlJSEditorEditable::commentDefinition() const
{
return &m_commentDefinition;
}
} // namespace QmlJSEditor
......@@ -31,6 +31,7 @@
#define QMLJSEDITOREDITABLE_H
#include "qmljseditor_global.h"
#include <utils/uncommentselection.h>
#include <texteditor/basetexteditor.h>
namespace QmlJSEditor {
......@@ -50,6 +51,10 @@ public:
bool open(QString *errorString, const QString &fileName, const QString &realFileName);
Core::Id preferredModeType() const;
void setTextCodec(QTextCodec *codec, TextCodecReason = TextCodecOtherReason);
const Utils::CommentDefinition *commentDefinition() const;
private:
Utils::CommentDefinition m_commentDefinition;
};
} // namespace QmlJSEditor
......
......@@ -70,6 +70,7 @@
#include <utils/stylehelper.h>
#include <utils/tooltip/tooltip.h>
#include <utils/tooltip/tipcontents.h>
#include <utils/uncommentselection.h>
#include <QCoreApplication>
#include <QTextCodec>
......@@ -1171,7 +1172,28 @@ void BaseTextEditorWidget::moveLineUpDown(bool up)
}
d->m_refactorOverlay->setMarkers(nonAffectedMarkers + affectedMarkers);
reindent(document(), move);
bool shouldReindent = true;
const Utils::CommentDefinition* commentDefinition(editor()->commentDefinition());
if (commentDefinition) {
QString trimmedText(text.trimmed());
if (commentDefinition->hasSingleLineStyle()) {
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())) {
shouldReindent = false;
}
}
}
if (shouldReindent) {
// The text was not commented at all; re-indent.
reindent(document(), move);
}
move.endEditBlock();
setTextCursor(move);
......@@ -6347,6 +6369,11 @@ void BaseTextEditor::select(int toPos)
e->setTextCursor(tc);
}
const CommentDefinition *BaseTextEditor::commentDefinition() const
{
return 0;
}
void BaseTextEditor::updateCursorPosition()
{
const QTextCursor cursor = e->textCursor();
......
......@@ -663,7 +663,7 @@ public:
void replace(int length, const QString &string);
void setCursorPosition(int pos);
void select(int toPos);
const Utils::CommentDefinition* commentDefinition() const;
private slots:
void updateCursorPosition();
......
......@@ -50,6 +50,10 @@ class QRect;
class QTextBlock;
QT_END_NAMESPACE
namespace Utils {
class CommentDefinition;
}
namespace TextEditor {
class TEXTEDITOR_EXPORT ITextEditor : public Core::IEditor
......@@ -100,6 +104,8 @@ public:
virtual void setTextCodec(QTextCodec *, TextCodecReason reason = TextCodecOtherReason) = 0;
virtual QTextCodec *textCodec() const = 0;
virtual const Utils::CommentDefinition* commentDefinition() const = 0;
static QMap<QString, QString> openedTextEditorsContents();
static QMap<QString, QTextCodec *> openedTextEditorsEncodings();
......
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