From b6b6482c72748b42f2ca0993826d3ff859dc64eb Mon Sep 17 00:00:00 2001 From: Xiaofeng Wang Date: Sat, 19 Sep 2020 01:01:47 +0800 Subject: [PATCH] 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 --- src/plugins/beautifier/beautifierplugin.cpp | 6 ++++ src/plugins/beautifier/beautifierplugin.h | 1 + .../beautifier/clangformat/clangformat.cpp | 30 +++++++++++++++++++ .../beautifier/clangformat/clangformat.h | 2 ++ 4 files changed, 39 insertions(+) diff --git a/src/plugins/beautifier/beautifierplugin.cpp b/src/plugins/beautifier/beautifierplugin.cpp index 661b41d0165..58e934098e1 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 c35ef2ab9a3..b3538820ec9 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 a0e3a2b3c46..f7a289945fd 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 3207f043490..24ce566861d 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;