aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--dist/clangformat/README.md2
-rw-r--r--doc/src/editors/creator-beautifier.qdoc12
-rw-r--r--src/plugins/beautifier/beautifierplugin.cpp6
-rw-r--r--src/plugins/beautifier/beautifierplugin.h1
-rw-r--r--src/plugins/beautifier/clangformat/clangformat.cpp18
-rw-r--r--src/plugins/beautifier/clangformat/clangformat.h2
-rw-r--r--src/plugins/beautifier/clangformat/clangformatconstants.h2
-rw-r--r--src/plugins/beautifier/clangformat/clangformatoptionspage.cpp2
-rw-r--r--src/plugins/beautifier/clangformat/clangformatoptionspage.ui36
-rw-r--r--src/plugins/beautifier/clangformat/clangformatsettings.cpp12
-rw-r--r--src/plugins/beautifier/clangformat/clangformatsettings.h3
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;