summaryrefslogtreecommitdiff
path: root/src/plugins/beautifier
diff options
context:
space:
mode:
authorNikolai Kosjar <nikolai.kosjar@qt.io>2017-09-04 15:18:42 +0200
committerNikolai Kosjar <nikolai.kosjar@qt.io>2017-09-21 07:14:27 +0000
commitedab564cf0c051ad0774dc5a59751d3c5781b263 (patch)
treeff1a3618f8f3b81b647188d24c231d065d56714c /src/plugins/beautifier
parent189dbc585ea8edba0e87c06dc58240dfbcaba97c (diff)
downloadqt-creator-edab564cf0c051ad0774dc5a59751d3c5781b263.tar.gz
Beautifier: ClangFormat: Add action "Disable Formatting for Selected Text"
Change-Id: I0786dfdc0679bbdf1cf1157067bd7f572ac7d108 Reviewed-by: Ivan Donchevskii <ivan.donchevskii@qt.io> Reviewed-by: Leena Miettinen <riitta-leena.miettinen@qt.io> Reviewed-by: Tim Jenssen <tim.jenssen@qt.io> Reviewed-by: Lorenz Haas <lorenz.haas@histomatics.de>
Diffstat (limited to 'src/plugins/beautifier')
-rw-r--r--src/plugins/beautifier/beautifierplugin.cpp6
-rw-r--r--src/plugins/beautifier/beautifierplugin.h1
-rw-r--r--src/plugins/beautifier/clangformat/clangformat.cpp45
-rw-r--r--src/plugins/beautifier/clangformat/clangformat.h2
-rw-r--r--src/plugins/beautifier/clangformat/clangformatconstants.h1
5 files changed, 55 insertions, 0 deletions
diff --git a/src/plugins/beautifier/beautifierplugin.cpp b/src/plugins/beautifier/beautifierplugin.cpp
index 0dcfbf7c0e..947233606c 100644
--- a/src/plugins/beautifier/beautifierplugin.cpp
+++ b/src/plugins/beautifier/beautifierplugin.cpp
@@ -496,6 +496,12 @@ QString BeautifierPlugin::msgFormatSelectedText()
return tr("Format &Selected Text");
}
+QString BeautifierPlugin::msgDisableFormattingSelectedText()
+{
+ //: Menu entry
+ return tr("&Disable Formatting for Selected Text");
+}
+
QString BeautifierPlugin::msgCommandPromptDialogTitle(const QString &command)
{
//: File dialog title for path chooser when choosing binary
diff --git a/src/plugins/beautifier/beautifierplugin.h b/src/plugins/beautifier/beautifierplugin.h
index 14d6674ab8..32189bcb19 100644
--- a/src/plugins/beautifier/beautifierplugin.h
+++ b/src/plugins/beautifier/beautifierplugin.h
@@ -80,6 +80,7 @@ public:
static QString msgCannotGetConfigurationFile(const QString &command);
static QString msgFormatCurrentFile();
static QString msgFormatSelectedText();
+ static QString msgDisableFormattingSelectedText();
static QString msgCommandPromptDialogTitle(const QString &command);
static void showError(const QString &error);
diff --git a/src/plugins/beautifier/clangformat/clangformat.cpp b/src/plugins/beautifier/clangformat/clangformat.cpp
index 13f7caab24..b677a59fbc 100644
--- a/src/plugins/beautifier/clangformat/clangformat.cpp
+++ b/src/plugins/beautifier/clangformat/clangformat.cpp
@@ -48,6 +48,7 @@
#include <QAction>
#include <QMenu>
+#include <QTextBlock>
namespace Beautifier {
namespace Internal {
@@ -88,6 +89,14 @@ bool ClangFormat::initialize()
menu->addAction(cmd);
connect(m_formatRange, &QAction::triggered, this, &ClangFormat::formatSelectedText);
+ m_disableFormattingSelectedText
+ = new QAction(BeautifierPlugin::msgDisableFormattingSelectedText(), this);
+ cmd = Core::ActionManager::registerAction(
+ m_disableFormattingSelectedText, Constants::ClangFormat::ACTION_DISABLEFORMATTINGSELECTED);
+ menu->addAction(cmd);
+ connect(m_disableFormattingSelectedText, &QAction::triggered,
+ this, &ClangFormat::disableFormattingSelectedText);
+
Core::ActionManager::actionContainer(Constants::MENU_ID)->addMenu(menu);
connect(m_settings, &ClangFormatSettings::supportedMimeTypesChanged,
@@ -130,6 +139,42 @@ void ClangFormat::formatSelectedText()
}
}
+void ClangFormat::disableFormattingSelectedText()
+{
+ TextEditor::TextEditorWidget *widget = TextEditor::TextEditorWidget::currentTextEditorWidget();
+ if (!widget)
+ return;
+
+ const QTextCursor tc = widget->textCursor();
+ if (!tc.hasSelection())
+ return;
+
+ // Insert start marker
+ const QTextBlock selectionStartBlock = tc.document()->findBlock(tc.selectionStart());
+ QTextCursor insertCursor(tc.document());
+ insertCursor.beginEditBlock();
+ insertCursor.setPosition(selectionStartBlock.position());
+ insertCursor.insertText("// clang-format off\n");
+ const int positionToRestore = tc.position();
+
+ // Insert end marker
+ QTextBlock selectionEndBlock = tc.document()->findBlock(tc.selectionEnd());
+ insertCursor.setPosition(selectionEndBlock.position() + selectionEndBlock.length() - 1);
+ insertCursor.insertText("\n// clang-format on");
+ insertCursor.endEditBlock();
+
+ // Reset the cursor position in order to clear the selection.
+ QTextCursor restoreCursor(tc.document());
+ restoreCursor.setPosition(positionToRestore);
+ widget->setTextCursor(restoreCursor);
+
+ // The indentation of these markers might be undesired, so reformat.
+ // This is not optimal because two undo steps will be needed to remove the markers.
+ const int reformatTextLength = insertCursor.position() - selectionStartBlock.position();
+ m_beautifierPlugin->formatCurrentFile(command(selectionStartBlock.position(),
+ reformatTextLength));
+}
+
Command ClangFormat::command() const
{
Command command;
diff --git a/src/plugins/beautifier/clangformat/clangformat.h b/src/plugins/beautifier/clangformat/clangformat.h
index 86037fac0c..bb5e5bf788 100644
--- a/src/plugins/beautifier/clangformat/clangformat.h
+++ b/src/plugins/beautifier/clangformat/clangformat.h
@@ -55,9 +55,11 @@ public:
private:
void formatFile();
void formatSelectedText();
+ void disableFormattingSelectedText();
BeautifierPlugin *m_beautifierPlugin;
QAction *m_formatFile = nullptr;
QAction *m_formatRange = nullptr;
+ QAction *m_disableFormattingSelectedText = nullptr;
ClangFormatSettings *m_settings;
Command command(int offset, int length) const;
};
diff --git a/src/plugins/beautifier/clangformat/clangformatconstants.h b/src/plugins/beautifier/clangformat/clangformatconstants.h
index 837d332306..51f3981fc5 100644
--- a/src/plugins/beautifier/clangformat/clangformatconstants.h
+++ b/src/plugins/beautifier/clangformat/clangformatconstants.h
@@ -34,6 +34,7 @@ namespace ClangFormat {
const char DISPLAY_NAME[] = QT_TRANSLATE_NOOP("Beautifier::Internal::ClangFormat::ClangFormat", "ClangFormat");
const char ACTION_FORMATFILE[] = "ClangFormat.FormatFile";
const char ACTION_FORMATSELECTED[] = "ClangFormat.FormatSelectedText";
+const char ACTION_DISABLEFORMATTINGSELECTED[] = "ClangFormat.DisableFormattingSelectedText";
const char MENU_ID[] = "ClangFormat.Menu";
const char OPTION_ID[] = "ClangFormat";
const char SETTINGS_NAME[] = "clangformat";