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>
This commit is contained in:
Nikolai Kosjar
2017-09-04 15:18:42 +02:00
parent 189dbc585e
commit edab564cf0
7 changed files with 60 additions and 0 deletions

View File

@@ -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;