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>
This commit is contained in:
Nikolai Kosjar
2017-09-05 09:59:25 +02:00
parent edab564cf0
commit 89fa23a916
11 changed files with 42 additions and 54 deletions

View File

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

View File

@@ -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}.
*/

View File

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

View File

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

View File

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

View File

@@ -54,7 +54,7 @@ public:
private:
void formatFile();
void formatSelectedText();
void formatAtCursor();
void disableFormattingSelectedText();
BeautifierPlugin *m_beautifierPlugin;
QAction *m_formatFile = nullptr;

View File

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

View File

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

View File

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

View File

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

View File

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