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>
This commit is contained in:
Xiaofeng Wang
2020-09-19 01:01:47 +08:00
parent b819f3fd52
commit b6b6482c72
4 changed files with 39 additions and 0 deletions

View File

@@ -218,6 +218,12 @@ QString BeautifierPlugin::msgFormatAtCursor()
return tr("&Format at Cursor"); return tr("&Format at Cursor");
} }
QString BeautifierPlugin::msgFormatLines()
{
//: Menu entry
return tr("Format &Line(s)");
}
QString BeautifierPlugin::msgDisableFormattingSelectedText() QString BeautifierPlugin::msgDisableFormattingSelectedText()
{ {
//: Menu entry //: Menu entry

View File

@@ -41,6 +41,7 @@ public:
static QString msgFormatCurrentFile(); static QString msgFormatCurrentFile();
static QString msgFormatSelectedText(); static QString msgFormatSelectedText();
static QString msgFormatAtCursor(); static QString msgFormatAtCursor();
static QString msgFormatLines();
static QString msgDisableFormattingSelectedText(); static QString msgDisableFormattingSelectedText();
static QString msgCommandPromptDialogTitle(const QString &command); static QString msgCommandPromptDialogTitle(const QString &command);
static void showError(const QString &error); static void showError(const QString &error);

View File

@@ -65,6 +65,11 @@ ClangFormat::ClangFormat()
menu->addAction(cmd); menu->addAction(cmd);
connect(m_formatFile, &QAction::triggered, this, &ClangFormat::formatFile); 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); m_formatRange = new QAction(BeautifierPlugin::msgFormatAtCursor(), this);
cmd = Core::ActionManager::registerAction(m_formatRange, "ClangFormat.FormatAtCursor"); cmd = Core::ActionManager::registerAction(m_formatRange, "ClangFormat.FormatAtCursor");
menu->addAction(cmd); 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() void ClangFormat::disableFormattingSelectedText()
{ {
TextEditorWidget *widget = TextEditorWidget::currentTextEditorWidget(); TextEditorWidget *widget = TextEditorWidget::currentTextEditorWidget();

View File

@@ -48,10 +48,12 @@ public:
private: private:
void formatFile(); void formatFile();
void formatAtCursor(); void formatAtCursor();
void formatLines();
void disableFormattingSelectedText(); void disableFormattingSelectedText();
TextEditor::Command command(int offset, int length) const; TextEditor::Command command(int offset, int length) const;
QAction *m_formatFile = nullptr; QAction *m_formatFile = nullptr;
QAction *m_formatLines = nullptr;
QAction *m_formatRange = nullptr; QAction *m_formatRange = nullptr;
QAction *m_disableFormattingSelectedText = nullptr; QAction *m_disableFormattingSelectedText = nullptr;
ClangFormatSettings m_settings; ClangFormatSettings m_settings;