forked from qt-creator/qt-creator
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:
@@ -849,7 +849,7 @@ clang::format::FormatStyle ClangFormatBaseIndenterPrivate::customSettingsStyle(
|
|||||||
return currentQtStyle(preferences);
|
return currentQtStyle(preferences);
|
||||||
|
|
||||||
clang::format::FormatStyle currentSettingsStyle;
|
clang::format::FormatStyle currentSettingsStyle;
|
||||||
bool success = parseConfigurationFile(filePath, currentSettingsStyle);
|
const Utils::expected_str<void> success = parseConfigurationFile(filePath, currentSettingsStyle);
|
||||||
QTC_ASSERT(success, return currentQtStyle(preferences));
|
QTC_ASSERT(success, return currentQtStyle(preferences));
|
||||||
|
|
||||||
return currentSettingsStyle;
|
return currentSettingsStyle;
|
||||||
|
@@ -37,6 +37,8 @@
|
|||||||
#include <utils/layoutbuilder.h>
|
#include <utils/layoutbuilder.h>
|
||||||
#include <utils/qtcassert.h>
|
#include <utils/qtcassert.h>
|
||||||
#include <utils/utilsicons.h>
|
#include <utils/utilsicons.h>
|
||||||
|
#include <utils/infolabel.h>
|
||||||
|
#include <utils/expected.h>
|
||||||
|
|
||||||
#include <QComboBox>
|
#include <QComboBox>
|
||||||
#include <QLabel>
|
#include <QLabel>
|
||||||
@@ -105,8 +107,7 @@ private:
|
|||||||
|
|
||||||
Guard m_ignoreChanges;
|
Guard m_ignoreChanges;
|
||||||
QLabel *m_clangVersion;
|
QLabel *m_clangVersion;
|
||||||
QLabel *m_clangFileIsCorrectText;
|
InfoLabel *m_clangFileIsCorrectText;
|
||||||
QLabel *m_clangFileIsCorrectIcon;
|
|
||||||
ClangFormatIndenter *m_indenter;
|
ClangFormatIndenter *m_indenter;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -137,7 +138,7 @@ ClangFormatConfigWidget::ClangFormatConfigWidget(TextEditor::ICodeStylePreferenc
|
|||||||
Column {
|
Column {
|
||||||
m_clangVersion,
|
m_clangVersion,
|
||||||
Row { m_editorScrollArea, m_preview },
|
Row { m_editorScrollArea, m_preview },
|
||||||
Row {m_clangFileIsCorrectIcon, m_clangFileIsCorrectText, st}
|
Row {m_clangFileIsCorrectText, st}
|
||||||
}.attachTo(this);
|
}.attachTo(this);
|
||||||
|
|
||||||
connect(codeStyle, &TextEditor::ICodeStylePreferences::currentPreferencesChanged,
|
connect(codeStyle, &TextEditor::ICodeStylePreferences::currentPreferencesChanged,
|
||||||
@@ -194,46 +195,28 @@ void ClangFormatConfigWidget::initEditor(TextEditor::ICodeStylePreferences *code
|
|||||||
m_editorScrollArea->setWidget(m_editor->widget());
|
m_editorScrollArea->setWidget(m_editor->widget());
|
||||||
m_editorScrollArea->setWidgetResizable(true);
|
m_editorScrollArea->setWidgetResizable(true);
|
||||||
|
|
||||||
m_clangFileIsCorrectText = new QLabel(Tr::tr("Clang-Format is configured correctly."));
|
m_clangFileIsCorrectText = new InfoLabel("", Utils::InfoLabel::Ok);
|
||||||
QPalette paletteCorrect = m_clangFileIsCorrectText->palette();
|
m_clangFileIsCorrectText->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
|
||||||
paletteCorrect.setColor(QPalette::WindowText, Qt::darkGreen);
|
m_clangFileIsCorrectText->hide();
|
||||||
m_clangFileIsCorrectText->setPalette(paletteCorrect);
|
|
||||||
|
|
||||||
m_clangFileIsCorrectIcon = new QLabel(this);
|
|
||||||
m_clangFileIsCorrectIcon->setPixmap(Icons::OK.icon().pixmap(16, 16));
|
|
||||||
|
|
||||||
m_clangVersion = new QLabel(Tr::tr("Current ClangFormat version: %1.").arg(LLVM_VERSION_STRING),
|
m_clangVersion = new QLabel(Tr::tr("Current ClangFormat version: %1.").arg(LLVM_VERSION_STRING),
|
||||||
this);
|
this);
|
||||||
|
|
||||||
connect(m_editor->document(), &TextEditor::TextDocument::contentsChanged, this, [this] {
|
connect(m_editor->document(), &TextEditor::TextDocument::contentsChanged, this, [this] {
|
||||||
clang::format::FormatStyle currentSettingsStyle;
|
clang::format::FormatStyle currentSettingsStyle;
|
||||||
const bool success
|
const Utils::expected_str<void> success
|
||||||
= parseConfigurationContent(m_editor->document()->contents().toStdString(),
|
= parseConfigurationContent(m_editor->document()->contents().toStdString(),
|
||||||
currentSettingsStyle);
|
currentSettingsStyle);
|
||||||
|
|
||||||
QString text;
|
|
||||||
Qt::GlobalColor currentColor;
|
|
||||||
QPixmap pixmap;
|
|
||||||
if (success) {
|
if (success) {
|
||||||
text = Tr::tr("Clang-Format is configured correctly.");
|
m_clangFileIsCorrectText->hide();
|
||||||
currentColor = Qt::darkGreen;
|
m_indenter->setOverriddenStyle(currentSettingsStyle);
|
||||||
pixmap = Icons::OK.icon().pixmap(16, 16);
|
updatePreview();
|
||||||
} 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)
|
|
||||||
return;
|
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);
|
QShortcut *completionSC = new QShortcut(QKeySequence("Ctrl+Space"), this);
|
||||||
@@ -347,8 +330,9 @@ void ClangFormatConfigWidget::apply()
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
clang::format::FormatStyle currentSettingsStyle;
|
clang::format::FormatStyle currentSettingsStyle;
|
||||||
const bool success = parseConfigurationContent(m_editor->document()->contents().toStdString(),
|
const Utils::expected_str<void> success
|
||||||
currentSettingsStyle);
|
= parseConfigurationContent(m_editor->document()->contents().toStdString(),
|
||||||
|
currentSettingsStyle);
|
||||||
|
|
||||||
auto saveSettings = [this] {
|
auto saveSettings = [this] {
|
||||||
QString errorString;
|
QString errorString;
|
||||||
|
@@ -19,6 +19,7 @@
|
|||||||
#include <projectexplorer/projectmanager.h>
|
#include <projectexplorer/projectmanager.h>
|
||||||
|
|
||||||
#include <utils/qtcassert.h>
|
#include <utils/qtcassert.h>
|
||||||
|
#include <utils/expected.h>
|
||||||
|
|
||||||
#include <QCryptographicHash>
|
#include <QCryptographicHash>
|
||||||
|
|
||||||
@@ -417,15 +418,30 @@ Utils::FilePath filePathToCurrentSettings(const TextEditor::ICodeStylePreference
|
|||||||
/ QLatin1String(Constants::SETTINGS_FILE_NAME);
|
/ 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;
|
auto diagHandler = [](const llvm::SMDiagnostic &diag, void * /*context*/) {
|
||||||
const std::error_code error = parseConfiguration(fileContent, &style);
|
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(),
|
return parseConfigurationContent(filePath.fileContents().value_or(QByteArray()).toStdString(),
|
||||||
style);
|
style);
|
||||||
|
@@ -48,7 +48,9 @@ clang::format::FormatStyle currentQtStyle(const TextEditor::ICodeStylePreference
|
|||||||
|
|
||||||
Utils::FilePath filePathToCurrentSettings(const TextEditor::ICodeStylePreferences *codeStyle);
|
Utils::FilePath filePathToCurrentSettings(const TextEditor::ICodeStylePreferences *codeStyle);
|
||||||
|
|
||||||
bool parseConfigurationContent(const std::string &fileContent, clang::format::FormatStyle &style);
|
Utils::expected_str<void> parseConfigurationContent(const std::string &fileContent,
|
||||||
bool parseConfigurationFile(const Utils::FilePath &filePath, clang::format::FormatStyle &style);
|
clang::format::FormatStyle &style);
|
||||||
|
Utils::expected_str<void> parseConfigurationFile(const Utils::FilePath &filePath,
|
||||||
|
clang::format::FormatStyle &style);
|
||||||
|
|
||||||
} // ClangFormat
|
} // ClangFormat
|
||||||
|
Reference in New Issue
Block a user