diff options
author | Nikolai Kosjar <nikolai.kosjar@qt.io> | 2017-09-05 09:59:25 +0200 |
---|---|---|
committer | Nikolai Kosjar <nikolai.kosjar@qt.io> | 2017-09-21 07:15:12 +0000 |
commit | 89fa23a916b55bd06b0b0756d6d874b4b03e5ba1 (patch) | |
tree | dd0507586e081df9849c90c545b868f2bd15d9f9 | |
parent | edab564cf0c051ad0774dc5a59751d3c5781b263 (diff) |
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 <riitta-leena.miettinen@qt.io>
Reviewed-by: Ivan Donchevskii <ivan.donchevskii@qt.io>
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
Reviewed-by: Lorenz Haas <lorenz.haas@histomatics.de>
-rw-r--r-- | dist/clangformat/README.md | 2 | ||||
-rw-r--r-- | doc/src/editors/creator-beautifier.qdoc | 12 | ||||
-rw-r--r-- | src/plugins/beautifier/beautifierplugin.cpp | 6 | ||||
-rw-r--r-- | src/plugins/beautifier/beautifierplugin.h | 1 | ||||
-rw-r--r-- | src/plugins/beautifier/clangformat/clangformat.cpp | 18 | ||||
-rw-r--r-- | src/plugins/beautifier/clangformat/clangformat.h | 2 | ||||
-rw-r--r-- | src/plugins/beautifier/clangformat/clangformatconstants.h | 2 | ||||
-rw-r--r-- | src/plugins/beautifier/clangformat/clangformatoptionspage.cpp | 2 | ||||
-rw-r--r-- | src/plugins/beautifier/clangformat/clangformatoptionspage.ui | 36 | ||||
-rw-r--r-- | src/plugins/beautifier/clangformat/clangformatsettings.cpp | 12 | ||||
-rw-r--r-- | src/plugins/beautifier/clangformat/clangformatsettings.h | 3 |
11 files changed, 42 insertions, 54 deletions
diff --git a/dist/clangformat/README.md b/dist/clangformat/README.md index aa3584e462..05fd065c01 100644 --- a/dist/clangformat/README.md +++ b/dist/clangformat/README.md @@ -57,7 +57,7 @@ For Windows: 3. Set shortcuts for convenience: In Menu: Tools > Options > Environment > Keyboard * ClangFormat / FormatFile - e.g. Alt+C, F - * ClangFormat / FormatSelectedText - e.g. Alt+C, S + * ClangFormat / FormatAtCursor - e.g. Alt+C, C * ClangFormat / DisableFormattingSelectedText - e.g. Alt+C, D Due to several issues outlined below the FormatFile action might be of limited diff --git a/doc/src/editors/creator-beautifier.qdoc b/doc/src/editors/creator-beautifier.qdoc index e93d222594..52662b53c9 100644 --- a/doc/src/editors/creator-beautifier.qdoc +++ b/doc/src/editors/creator-beautifier.qdoc @@ -174,12 +174,14 @@ \endlist In addition to the \uicontrol {Format Current File} command, ClangFormat - and Uncrustify provide the \uicontrol {Format Selected Text} command. If you + and Uncrustify provide additional commands. + ClangFormat provides the \uicontrol {Format at Cursor} command. If you + select it when no text is selected, the syntactic entity under the cursor + is formatted. The \uicontrol {Disable Formatting for Selected Text} command + wraps selected lines within \c {// clang-format off} and + \c {// clang-format on}. + Uncrustify provides the \uicontrol {Format Selected Text} command. If you select it when no text is selected, the whole file is formatted by default. To disable this behavior, deselect the \uicontrol {Format entire file if no text was selected} check box. - - ClangFormat provides additionally the \uicontrol {Disable Formatting for - Selected Text} command. If you select it, the selected lines will be - wrapped within \c {// clang-format off} and \c {// clang-format on}. */ diff --git a/src/plugins/beautifier/beautifierplugin.cpp b/src/plugins/beautifier/beautifierplugin.cpp index 947233606c..e2715af146 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 32189bcb19..f298063f0a 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 b677a59fbc..97298ffc39 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 bb5e5bf788..34cccb5fbc 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 51f3981fc5..2c94b347a5 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 ddac14461f..d9f881dc41 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 c40b28c041..7d060611b4 100644 --- a/src/plugins/beautifier/clangformat/clangformatoptionspage.ui +++ b/src/plugins/beautifier/clangformat/clangformatoptionspage.ui @@ -20,6 +20,19 @@ <string>Options</string> </property> <layout class="QGridLayout" name="gridLayout"> + <item row="2" column="0"> + <widget class="QRadioButton" name="useCustomizedStyle"> + <property name="text"> + <string>Use customized style:</string> + </property> + <property name="autoExclusive"> + <bool>true</bool> + </property> + </widget> + </item> + <item row="2" column="1" colspan="2"> + <widget class="Beautifier::Internal::ConfigurationPanel" name="configurations" native="true"/> + </item> <item row="0" column="0"> <widget class="QRadioButton" name="usePredefinedStyle"> <property name="sizePolicy"> @@ -62,29 +75,6 @@ </property> </widget> </item> - <item row="2" column="0"> - <widget class="QRadioButton" name="useCustomizedStyle"> - <property name="text"> - <string>Use customized style:</string> - </property> - <property name="autoExclusive"> - <bool>true</bool> - </property> - </widget> - </item> - <item row="2" column="1" colspan="2"> - <widget class="Beautifier::Internal::ConfigurationPanel" name="configurations" native="true"/> - </item> - <item row="3" column="0" colspan="2"> - <widget class="QCheckBox" name="formatEntireFileFallback"> - <property name="toolTip"> - <string>For action Format Selected Text.</string> - </property> - <property name="text"> - <string>Format entire file if no text was selected</string> - </property> - </widget> - </item> </layout> </widget> </item> diff --git a/src/plugins/beautifier/clangformat/clangformatsettings.cpp b/src/plugins/beautifier/clangformat/clangformatsettings.cpp index f4c2189181..6369c60510 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 b8a62be6bd..4fca7ddc24 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; |