aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorXiaofeng Wang <wasphin@gmail.com>2020-09-19 01:01:47 +0800
committerXiaofeng Wang <wasphin@gmail.com>2020-10-01 15:04:54 +0000
commitb6b6482c72748b42f2ca0993826d3ff859dc64eb (patch)
treeb26057e2eaf0b2c851d6862463834348b6449925
parentb819f3fd5210a6edc1fc208a4b9375b395349cdd (diff)
Add an action to format code using clang-format by lines
When the file contians non-ascii characters, the position calculated by QTextBlock may not the same as clang-format. Format at Cursor will not work in this case, formatting by lines as a workaround. Fixes: QTCREATORBUG-21812 Change-Id: I4906130111ef2a2f15ffa698a14bec9430cbb3d5 Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
-rw-r--r--src/plugins/beautifier/beautifierplugin.cpp6
-rw-r--r--src/plugins/beautifier/beautifierplugin.h1
-rw-r--r--src/plugins/beautifier/clangformat/clangformat.cpp30
-rw-r--r--src/plugins/beautifier/clangformat/clangformat.h2
4 files changed, 39 insertions, 0 deletions
diff --git a/src/plugins/beautifier/beautifierplugin.cpp b/src/plugins/beautifier/beautifierplugin.cpp
index 661b41d016..58e934098e 100644
--- a/src/plugins/beautifier/beautifierplugin.cpp
+++ b/src/plugins/beautifier/beautifierplugin.cpp
@@ -218,6 +218,12 @@ QString BeautifierPlugin::msgFormatAtCursor()
return tr("&Format at Cursor");
}
+QString BeautifierPlugin::msgFormatLines()
+{
+ //: Menu entry
+ return tr("Format &Line(s)");
+}
+
QString BeautifierPlugin::msgDisableFormattingSelectedText()
{
//: Menu entry
diff --git a/src/plugins/beautifier/beautifierplugin.h b/src/plugins/beautifier/beautifierplugin.h
index c35ef2ab9a..b3538820ec 100644
--- a/src/plugins/beautifier/beautifierplugin.h
+++ b/src/plugins/beautifier/beautifierplugin.h
@@ -41,6 +41,7 @@ public:
static QString msgFormatCurrentFile();
static QString msgFormatSelectedText();
static QString msgFormatAtCursor();
+ static QString msgFormatLines();
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 a0e3a2b3c4..f7a289945f 100644
--- a/src/plugins/beautifier/clangformat/clangformat.cpp
+++ b/src/plugins/beautifier/clangformat/clangformat.cpp
@@ -65,6 +65,11 @@ ClangFormat::ClangFormat()
menu->addAction(cmd);
connect(m_formatFile, &QAction::triggered, this, &ClangFormat::formatFile);
+ m_formatLines = new QAction(BeautifierPlugin::msgFormatLines(), this);
+ cmd = Core::ActionManager::registerAction(m_formatLines, "ClangFormat.FormatLines");
+ menu->addAction(cmd);
+ connect(m_formatLines, &QAction::triggered, this, &ClangFormat::formatLines);
+
m_formatRange = new QAction(BeautifierPlugin::msgFormatAtCursor(), this);
cmd = Core::ActionManager::registerAction(m_formatRange, "ClangFormat.FormatAtCursor");
menu->addAction(cmd);
@@ -123,6 +128,31 @@ void ClangFormat::formatAtCursor()
}
}
+void ClangFormat::formatLines()
+{
+ const TextEditorWidget *widget = TextEditorWidget::currentTextEditorWidget();
+ if (!widget)
+ return;
+
+ const QTextCursor tc = widget->textCursor();
+ // Current line by default
+ int lineStart = tc.blockNumber() + 1;
+ int lineEnd = lineStart;
+
+ // Note that clang-format will extend the range to the next bigger
+ // syntactic construct if needed.
+ if (tc.hasSelection()) {
+ const QTextBlock start = tc.document()->findBlock(tc.selectionStart());
+ const QTextBlock end = tc.document()->findBlock(tc.selectionEnd());
+ lineStart = start.blockNumber() + 1;
+ lineEnd = end.blockNumber() + 1;
+ }
+
+ auto cmd = command();
+ cmd.addOption(QString("-lines=%1:%2").arg(QString::number(lineStart)).arg(QString::number(lineEnd)));
+ formatCurrentFile(cmd);
+}
+
void ClangFormat::disableFormattingSelectedText()
{
TextEditorWidget *widget = TextEditorWidget::currentTextEditorWidget();
diff --git a/src/plugins/beautifier/clangformat/clangformat.h b/src/plugins/beautifier/clangformat/clangformat.h
index 3207f04349..24ce566861 100644
--- a/src/plugins/beautifier/clangformat/clangformat.h
+++ b/src/plugins/beautifier/clangformat/clangformat.h
@@ -48,10 +48,12 @@ public:
private:
void formatFile();
void formatAtCursor();
+ void formatLines();
void disableFormattingSelectedText();
TextEditor::Command command(int offset, int length) const;
QAction *m_formatFile = nullptr;
+ QAction *m_formatLines = nullptr;
QAction *m_formatRange = nullptr;
QAction *m_disableFormattingSelectedText = nullptr;
ClangFormatSettings m_settings;