From 89fa23a916b55bd06b0b0756d6d874b4b03e5ba1 Mon Sep 17 00:00:00 2001 From: Nikolai Kosjar Date: Tue, 5 Sep 2017 09:59:25 +0200 Subject: Beautifier: ClangFormat: Format current syntactic entity for no selection In case there was no selection, the action "Format Selected Text" could format the whole file (option) as a fallback. However, there is also the use case of formatting the syntactic entity under the cursor. Introducing another separate action for this feels wrong, so remove the fallback instead since there is already an action handling this. Change-Id: Ia73f6074433e706bb4c2d375ad5b84dd59bc93a3 Reviewed-by: Leena Miettinen Reviewed-by: Ivan Donchevskii Reviewed-by: Tim Jenssen Reviewed-by: Lorenz Haas --- src/plugins/beautifier/beautifierplugin.cpp | 6 ++++ src/plugins/beautifier/beautifierplugin.h | 1 + src/plugins/beautifier/clangformat/clangformat.cpp | 18 +++++++---- src/plugins/beautifier/clangformat/clangformat.h | 2 +- .../beautifier/clangformat/clangformatconstants.h | 2 +- .../clangformat/clangformatoptionspage.cpp | 2 -- .../clangformat/clangformatoptionspage.ui | 36 ++++++++-------------- .../beautifier/clangformat/clangformatsettings.cpp | 12 -------- .../beautifier/clangformat/clangformatsettings.h | 3 -- 9 files changed, 34 insertions(+), 48 deletions(-) (limited to 'src/plugins/beautifier') diff --git a/src/plugins/beautifier/beautifierplugin.cpp b/src/plugins/beautifier/beautifierplugin.cpp index 947233606c1..e2715af1464 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::msgFormatAtCursor() +{ + //: Menu entry + return tr("&Format at Cursor"); +} + QString BeautifierPlugin::msgDisableFormattingSelectedText() { //: Menu entry diff --git a/src/plugins/beautifier/beautifierplugin.h b/src/plugins/beautifier/beautifierplugin.h index 32189bcb19d..f298063f0ae 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 msgFormatAtCursor(); 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 b677a59fbcc..97298ffc39e 100644 --- a/src/plugins/beautifier/clangformat/clangformat.cpp +++ b/src/plugins/beautifier/clangformat/clangformat.cpp @@ -83,11 +83,11 @@ bool ClangFormat::initialize() menu->addAction(cmd); connect(m_formatFile, &QAction::triggered, this, &ClangFormat::formatFile); - m_formatRange = new QAction(BeautifierPlugin::msgFormatSelectedText(), this); + m_formatRange = new QAction(BeautifierPlugin::msgFormatAtCursor(), this); cmd = Core::ActionManager::registerAction(m_formatRange, - Constants::ClangFormat::ACTION_FORMATSELECTED); + Constants::ClangFormat::ACTION_FORMATATCURSOR); menu->addAction(cmd); - connect(m_formatRange, &QAction::triggered, this, &ClangFormat::formatSelectedText); + connect(m_formatRange, &QAction::triggered, this, &ClangFormat::formatAtCursor); m_disableFormattingSelectedText = new QAction(BeautifierPlugin::msgDisableFormattingSelectedText(), this); @@ -122,7 +122,7 @@ void ClangFormat::formatFile() m_beautifierPlugin->formatCurrentFile(command()); } -void ClangFormat::formatSelectedText() +void ClangFormat::formatAtCursor() { const TextEditor::TextEditorWidget *widget = TextEditor::TextEditorWidget::currentTextEditorWidget(); @@ -134,8 +134,14 @@ void ClangFormat::formatSelectedText() const int offset = tc.selectionStart(); const int length = tc.selectionEnd() - offset; m_beautifierPlugin->formatCurrentFile(command(offset, length)); - } else if (m_settings->formatEntireFileFallback()) { - formatFile(); + } else { + // Pretend that the current line was selected. + // Note that clang-format will extend the range to the next bigger + // syntactic construct if needed. + const QTextBlock block = tc.block(); + const int offset = block.position(); + const int length = block.length(); + m_beautifierPlugin->formatCurrentFile(command(offset, length)); } } diff --git a/src/plugins/beautifier/clangformat/clangformat.h b/src/plugins/beautifier/clangformat/clangformat.h index bb5e5bf7881..34cccb5fbcc 100644 --- a/src/plugins/beautifier/clangformat/clangformat.h +++ b/src/plugins/beautifier/clangformat/clangformat.h @@ -54,7 +54,7 @@ public: private: void formatFile(); - void formatSelectedText(); + void formatAtCursor(); void disableFormattingSelectedText(); BeautifierPlugin *m_beautifierPlugin; QAction *m_formatFile = nullptr; diff --git a/src/plugins/beautifier/clangformat/clangformatconstants.h b/src/plugins/beautifier/clangformat/clangformatconstants.h index 51f3981fc5d..2c94b347a51 100644 --- a/src/plugins/beautifier/clangformat/clangformatconstants.h +++ b/src/plugins/beautifier/clangformat/clangformatconstants.h @@ -33,7 +33,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_FORMATATCURSOR[] = "ClangFormat.FormatAtCursor"; const char ACTION_DISABLEFORMATTINGSELECTED[] = "ClangFormat.DisableFormattingSelectedText"; const char MENU_ID[] = "ClangFormat.Menu"; const char OPTION_ID[] = "ClangFormat"; diff --git a/src/plugins/beautifier/clangformat/clangformatoptionspage.cpp b/src/plugins/beautifier/clangformat/clangformatoptionspage.cpp index ddac14461f1..d9f881dc415 100644 --- a/src/plugins/beautifier/clangformat/clangformatoptionspage.cpp +++ b/src/plugins/beautifier/clangformat/clangformatoptionspage.cpp @@ -78,7 +78,6 @@ void ClangFormatOptionsPageWidget::restore() const int fallbackStyleIndex = ui->fallbackStyle->findText(m_settings->fallbackStyle()); if (fallbackStyleIndex != -1) ui->fallbackStyle->setCurrentIndex(fallbackStyleIndex); - ui->formatEntireFileFallback->setChecked(m_settings->formatEntireFileFallback()); ui->configurations->setSettings(m_settings); ui->configurations->setCurrentConfiguration(m_settings->customStyle()); @@ -96,7 +95,6 @@ void ClangFormatOptionsPageWidget::apply() m_settings->setPredefinedStyle(ui->predefinedStyle->currentText()); m_settings->setFallbackStyle(ui->fallbackStyle->currentText()); m_settings->setCustomStyle(ui->configurations->currentConfiguration()); - m_settings->setFormatEntireFileFallback(ui->formatEntireFileFallback->isChecked()); m_settings->save(); // update since not all MIME types are accepted (invalids or duplicates) diff --git a/src/plugins/beautifier/clangformat/clangformatoptionspage.ui b/src/plugins/beautifier/clangformat/clangformatoptionspage.ui index c40b28c0417..7d060611b4b 100644 --- a/src/plugins/beautifier/clangformat/clangformatoptionspage.ui +++ b/src/plugins/beautifier/clangformat/clangformatoptionspage.ui @@ -20,6 +20,19 @@ Options + + + + Use customized style: + + + true + + + + + + @@ -62,29 +75,6 @@ - - - - Use customized style: - - - true - - - - - - - - - - For action Format Selected Text. - - - Format entire file if no text was selected - - - diff --git a/src/plugins/beautifier/clangformat/clangformatsettings.cpp b/src/plugins/beautifier/clangformat/clangformatsettings.cpp index f4c2189181b..6369c60510f 100644 --- a/src/plugins/beautifier/clangformat/clangformatsettings.cpp +++ b/src/plugins/beautifier/clangformat/clangformatsettings.cpp @@ -43,7 +43,6 @@ const char USE_PREDEFINED_STYLE[] = "usePredefinedStyle"; const char PREDEFINED_STYLE[] = "predefinedStyle"; const char FALLBACK_STYLE[] = "fallbackStyle"; const char CUSTOM_STYLE[] = "customStyle"; -const char FORMAT_ENTIRE_FILE_FALLBACK[] = "formatEntireFileFallback"; } ClangFormatSettings::ClangFormatSettings() : @@ -54,7 +53,6 @@ ClangFormatSettings::ClangFormatSettings() : m_settings.insert(PREDEFINED_STYLE, "LLVM"); m_settings.insert(FALLBACK_STYLE, "Default"); m_settings.insert(CUSTOM_STYLE, QVariant()); - m_settings.insert(FORMAT_ENTIRE_FILE_FALLBACK, QVariant(true)); read(); } @@ -215,16 +213,6 @@ void ClangFormatSettings::setCustomStyle(const QString &customStyle) m_settings.insert(CUSTOM_STYLE, QVariant(customStyle)); } -bool ClangFormatSettings::formatEntireFileFallback() const -{ - return m_settings.value(FORMAT_ENTIRE_FILE_FALLBACK).toBool(); -} - -void ClangFormatSettings::setFormatEntireFileFallback(bool formatEntireFileFallback) -{ - m_settings.insert(FORMAT_ENTIRE_FILE_FALLBACK, QVariant(formatEntireFileFallback)); -} - QStringList ClangFormatSettings::predefinedStyles() const { return {"LLVM", "Google", "Chromium", "Mozilla", "WebKit", "File"}; diff --git a/src/plugins/beautifier/clangformat/clangformatsettings.h b/src/plugins/beautifier/clangformat/clangformatsettings.h index b8a62be6bdc..4fca7ddc248 100644 --- a/src/plugins/beautifier/clangformat/clangformatsettings.h +++ b/src/plugins/beautifier/clangformat/clangformatsettings.h @@ -54,9 +54,6 @@ public: QString customStyle() const; void setCustomStyle(const QString &customStyle); - bool formatEntireFileFallback() const; - void setFormatEntireFileFallback(bool formatEntireFileFallback); - QStringList predefinedStyles() const; QStringList fallbackStyles() const; -- cgit v1.2.3