ClangFormat: Move clangformat output to infolabel

Move clangformat output to infolabel in clangformatconfigwidget
to reduce noiziness in cli and to enhance the convenience of editing
the .clang-format file.

Fixes: QTCREATORBUG-30269
Change-Id: I78053bde9791122172f1d3d4ba712a9954ea9c4e
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
Artem Sokolovskii
2024-02-05 16:05:55 +01:00
parent 8ee97b1e02
commit 44c9074c0b
4 changed files with 44 additions and 42 deletions

View File

@@ -849,7 +849,7 @@ clang::format::FormatStyle ClangFormatBaseIndenterPrivate::customSettingsStyle(
return currentQtStyle(preferences);
clang::format::FormatStyle currentSettingsStyle;
bool success = parseConfigurationFile(filePath, currentSettingsStyle);
const Utils::expected_str<void> success = parseConfigurationFile(filePath, currentSettingsStyle);
QTC_ASSERT(success, return currentQtStyle(preferences));
return currentSettingsStyle;

View File

@@ -37,6 +37,8 @@
#include <utils/layoutbuilder.h>
#include <utils/qtcassert.h>
#include <utils/utilsicons.h>
#include <utils/infolabel.h>
#include <utils/expected.h>
#include <QComboBox>
#include <QLabel>
@@ -105,8 +107,7 @@ private:
Guard m_ignoreChanges;
QLabel *m_clangVersion;
QLabel *m_clangFileIsCorrectText;
QLabel *m_clangFileIsCorrectIcon;
InfoLabel *m_clangFileIsCorrectText;
ClangFormatIndenter *m_indenter;
};
@@ -137,7 +138,7 @@ ClangFormatConfigWidget::ClangFormatConfigWidget(TextEditor::ICodeStylePreferenc
Column {
m_clangVersion,
Row { m_editorScrollArea, m_preview },
Row {m_clangFileIsCorrectIcon, m_clangFileIsCorrectText, st}
Row {m_clangFileIsCorrectText, st}
}.attachTo(this);
connect(codeStyle, &TextEditor::ICodeStylePreferences::currentPreferencesChanged,
@@ -194,46 +195,28 @@ void ClangFormatConfigWidget::initEditor(TextEditor::ICodeStylePreferences *code
m_editorScrollArea->setWidget(m_editor->widget());
m_editorScrollArea->setWidgetResizable(true);
m_clangFileIsCorrectText = new QLabel(Tr::tr("Clang-Format is configured correctly."));
QPalette paletteCorrect = m_clangFileIsCorrectText->palette();
paletteCorrect.setColor(QPalette::WindowText, Qt::darkGreen);
m_clangFileIsCorrectText->setPalette(paletteCorrect);
m_clangFileIsCorrectIcon = new QLabel(this);
m_clangFileIsCorrectIcon->setPixmap(Icons::OK.icon().pixmap(16, 16));
m_clangFileIsCorrectText = new InfoLabel("", Utils::InfoLabel::Ok);
m_clangFileIsCorrectText->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
m_clangFileIsCorrectText->hide();
m_clangVersion = new QLabel(Tr::tr("Current ClangFormat version: %1.").arg(LLVM_VERSION_STRING),
this);
connect(m_editor->document(), &TextEditor::TextDocument::contentsChanged, this, [this] {
clang::format::FormatStyle currentSettingsStyle;
const bool success
const Utils::expected_str<void> success
= parseConfigurationContent(m_editor->document()->contents().toStdString(),
currentSettingsStyle);
QString text;
Qt::GlobalColor currentColor;
QPixmap pixmap;
if (success) {
text = Tr::tr("Clang-Format is configured correctly.");
currentColor = Qt::darkGreen;
pixmap = Icons::OK.icon().pixmap(16, 16);
} else {
text = Tr::tr("Clang-Format is not configured correctly.");
currentColor = Qt::red;
pixmap = Icons::WARNING.icon().pixmap(16, 16);
}
m_clangFileIsCorrectText->setText(text);
QPalette paletteCorrect = m_clangFileIsCorrectText->palette();
paletteCorrect.setColor(QPalette::WindowText, currentColor);
m_clangFileIsCorrectText->setPalette(paletteCorrect);
m_clangFileIsCorrectIcon->setPixmap(pixmap);
if (!success)
m_clangFileIsCorrectText->hide();
m_indenter->setOverriddenStyle(currentSettingsStyle);
updatePreview();
return;
m_indenter->setOverriddenStyle(currentSettingsStyle);
updatePreview();
}
m_clangFileIsCorrectText->show();
m_clangFileIsCorrectText->setText(Tr::tr("Warning: ") + success.error());
m_clangFileIsCorrectText->setType(Utils::InfoLabel::Warning);
});
QShortcut *completionSC = new QShortcut(QKeySequence("Ctrl+Space"), this);
@@ -347,8 +330,9 @@ void ClangFormatConfigWidget::apply()
return;
clang::format::FormatStyle currentSettingsStyle;
const bool success = parseConfigurationContent(m_editor->document()->contents().toStdString(),
currentSettingsStyle);
const Utils::expected_str<void> success
= parseConfigurationContent(m_editor->document()->contents().toStdString(),
currentSettingsStyle);
auto saveSettings = [this] {
QString errorString;

View File

@@ -19,6 +19,7 @@
#include <projectexplorer/projectmanager.h>
#include <utils/qtcassert.h>
#include <utils/expected.h>
#include <QCryptographicHash>
@@ -417,15 +418,30 @@ Utils::FilePath filePathToCurrentSettings(const TextEditor::ICodeStylePreference
/ QLatin1String(Constants::SETTINGS_FILE_NAME);
}
bool parseConfigurationContent(const std::string &fileContent, clang::format::FormatStyle &style)
static QString s_errorMessage;
Utils::expected_str<void> parseConfigurationContent(const std::string &fileContent,
clang::format::FormatStyle &style)
{
style.Language = clang::format::FormatStyle::LK_Cpp;
const std::error_code error = parseConfiguration(fileContent, &style);
auto diagHandler = [](const llvm::SMDiagnostic &diag, void * /*context*/) {
s_errorMessage = QString::fromStdString(diag.getMessage().str()) + " "
+ QString::number(diag.getLineNo()) + ":"
+ QString::number(diag.getColumnNo());
};
return error.value() == static_cast<int>(ParseError::Success);
style.Language = clang::format::FormatStyle::LK_Cpp;
const std::error_code error = parseConfiguration(llvm::MemoryBufferRef(fileContent, "YAML"),
&style,
false,
diagHandler,
nullptr);
if (error)
return make_unexpected(s_errorMessage);
return {};
}
bool parseConfigurationFile(const Utils::FilePath &filePath, clang::format::FormatStyle &style)
Utils::expected_str<void> parseConfigurationFile(const Utils::FilePath &filePath,
clang::format::FormatStyle &style)
{
return parseConfigurationContent(filePath.fileContents().value_or(QByteArray()).toStdString(),
style);

View File

@@ -48,7 +48,9 @@ clang::format::FormatStyle currentQtStyle(const TextEditor::ICodeStylePreference
Utils::FilePath filePathToCurrentSettings(const TextEditor::ICodeStylePreferences *codeStyle);
bool parseConfigurationContent(const std::string &fileContent, clang::format::FormatStyle &style);
bool parseConfigurationFile(const Utils::FilePath &filePath, clang::format::FormatStyle &style);
Utils::expected_str<void> parseConfigurationContent(const std::string &fileContent,
clang::format::FormatStyle &style);
Utils::expected_str<void> parseConfigurationFile(const Utils::FilePath &filePath,
clang::format::FormatStyle &style);
} // ClangFormat