forked from qt-creator/qt-creator
ClangFormat: Show .clang-format file as text instead of the table
Change-Id: I8b912ec6f29dfe7059ac6ab3888a6c3698769fc1 Reviewed-by: Marco Bubke <marco.bubke@qt.io>
This commit is contained in:
@@ -23,7 +23,6 @@
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
|
||||
#include "clangformatconfigwidget.h"
|
||||
|
||||
#include "clangformatconstants.h"
|
||||
@@ -46,73 +45,7 @@ using namespace ProjectExplorer;
|
||||
|
||||
namespace ClangFormat {
|
||||
|
||||
static void readTable(QTableWidget *table, std::istringstream &stream)
|
||||
{
|
||||
table->horizontalHeader()->hide();
|
||||
table->verticalHeader()->hide();
|
||||
|
||||
table->setColumnCount(2);
|
||||
table->setRowCount(0);
|
||||
|
||||
std::string line;
|
||||
while (std::getline(stream, line)) {
|
||||
if (line == "---" || line == "...")
|
||||
continue;
|
||||
|
||||
const size_t firstLetter = line.find_first_not_of(' ');
|
||||
if (firstLetter == std::string::npos || line.at(firstLetter) == '#')
|
||||
continue;
|
||||
|
||||
table->insertRow(table->rowCount());
|
||||
const size_t colonPos = line.find_first_of(':');
|
||||
auto *keyItem = new QTableWidgetItem;
|
||||
auto *valueItem = new QTableWidgetItem;
|
||||
|
||||
keyItem->setFlags(keyItem->flags() & ~Qt::ItemFlags(Qt::ItemIsEditable));
|
||||
table->setItem(table->rowCount() - 1, 0, keyItem);
|
||||
table->setItem(table->rowCount() - 1, 1, valueItem);
|
||||
|
||||
if (colonPos == std::string::npos) {
|
||||
keyItem->setText(QString::fromStdString(line));
|
||||
valueItem->setFlags(valueItem->flags() & ~Qt::ItemFlags(Qt::ItemIsEditable));
|
||||
continue;
|
||||
}
|
||||
|
||||
keyItem->setText(QString::fromStdString(line.substr(0, colonPos)));
|
||||
|
||||
const size_t optionValueStart = line.find_first_not_of(' ', colonPos + 1);
|
||||
if (optionValueStart == std::string::npos)
|
||||
valueItem->setFlags(valueItem->flags() & ~Qt::ItemFlags(Qt::ItemIsEditable));
|
||||
else
|
||||
valueItem->setText(QString::fromStdString(line.substr(optionValueStart)));
|
||||
}
|
||||
|
||||
table->resizeColumnToContents(0);
|
||||
table->resizeColumnToContents(1);
|
||||
}
|
||||
|
||||
static QByteArray tableToYAML(QTableWidget *table)
|
||||
{
|
||||
QByteArray text;
|
||||
text += "---\n";
|
||||
for (int i = 0; i < table->rowCount(); ++i) {
|
||||
auto *keyItem = table->item(i, 0);
|
||||
auto *valueItem = table->item(i, 1);
|
||||
|
||||
QByteArray itemText = keyItem->text().toUtf8();
|
||||
if (!valueItem->text().isEmpty() || !itemText.trimmed().startsWith('-'))
|
||||
itemText += ": ";
|
||||
itemText += valueItem->text().toUtf8() + '\n';
|
||||
|
||||
text += itemText;
|
||||
}
|
||||
text += "...\n";
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
ClangFormatConfigWidget::ClangFormatConfigWidget(ProjectExplorer::Project *project,
|
||||
QWidget *parent)
|
||||
ClangFormatConfigWidget::ClangFormatConfigWidget(ProjectExplorer::Project *project, QWidget *parent)
|
||||
: CodeStyleEditorWidget(parent)
|
||||
, m_project(project)
|
||||
, m_ui(std::make_unique<Ui::ClangFormatConfigWidget>())
|
||||
@@ -159,8 +92,7 @@ void ClangFormatConfigWidget::initialize()
|
||||
m_ui->applyButton->hide();
|
||||
m_ui->verticalLayout->addStretch(1);
|
||||
|
||||
connect(m_ui->createFileButton, &QPushButton::clicked,
|
||||
this, [this]() {
|
||||
connect(m_ui->createFileButton, &QPushButton::clicked, this, [this]() {
|
||||
createStyleFileIfNeeded(false);
|
||||
initialize();
|
||||
});
|
||||
@@ -181,8 +113,8 @@ void ClangFormatConfigWidget::initialize()
|
||||
m_ui->projectHasClangFormat->hide();
|
||||
} else {
|
||||
m_ui->projectHasClangFormat->setText(
|
||||
tr("Current project has its own .clang-format file "
|
||||
"and can be configured in Projects > Code Style > C++."));
|
||||
tr("Current project has its own .clang-format file "
|
||||
"and can be configured in Projects > Code Style > C++."));
|
||||
}
|
||||
createStyleFileIfNeeded(true);
|
||||
showGlobalCheckboxes();
|
||||
@@ -197,8 +129,7 @@ void ClangFormatConfigWidget::fillTable()
|
||||
clang::format::FormatStyle style = m_project ? currentProjectStyle() : currentGlobalStyle();
|
||||
|
||||
std::string configText = clang::format::configurationAsText(style);
|
||||
std::istringstream stream(configText);
|
||||
readTable(m_ui->clangFormatOptionsTable, stream);
|
||||
m_ui->clangFormatOptionsTable->setPlainText(QString::fromStdString(configText));
|
||||
}
|
||||
|
||||
ClangFormatConfigWidget::~ClangFormatConfigWidget() = default;
|
||||
@@ -213,10 +144,10 @@ void ClangFormatConfigWidget::apply()
|
||||
settings.write();
|
||||
}
|
||||
|
||||
const QByteArray text = tableToYAML(m_ui->clangFormatOptionsTable);
|
||||
const QString text = m_ui->clangFormatOptionsTable->toPlainText();
|
||||
clang::format::FormatStyle style;
|
||||
style.Language = clang::format::FormatStyle::LK_Cpp;
|
||||
const std::error_code error = clang::format::parseConfiguration(text.data(), &style);
|
||||
const std::error_code error = clang::format::parseConfiguration(text.toStdString(), &style);
|
||||
if (error.value() != static_cast<int>(clang::format::ParseError::Success)) {
|
||||
QMessageBox::warning(this,
|
||||
tr("Error in ClangFormat configuration"),
|
||||
@@ -234,7 +165,7 @@ void ClangFormatConfigWidget::apply()
|
||||
if (!file.open(QFile::WriteOnly))
|
||||
return;
|
||||
|
||||
file.write(text);
|
||||
file.write(text.toUtf8());
|
||||
file.close();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user