forked from qt-creator/qt-creator
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:
@@ -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
|
||||
|
@@ -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);
|
||||
|
@@ -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();
|
||||
|
@@ -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;
|
||||
|
Reference in New Issue
Block a user