Meson: Inline ArrayOptionLineEdit

Change-Id: I28da4e5edd6a74274c17ad6b5fe91d161c574388
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Jarek Kobus
2024-07-25 13:29:31 +02:00
parent 4f31d77e6e
commit 943efa0535
5 changed files with 79 additions and 112 deletions

View File

@@ -3,8 +3,6 @@ add_qtc_plugin(MesonProjectManager
DEPENDS QmlJS DEPENDS QmlJS
PLUGIN_DEPENDS Core ProjectExplorer TextEditor QtSupport PLUGIN_DEPENDS Core ProjectExplorer TextEditor QtSupport
SOURCES SOURCES
arrayoptionlineedit.cpp
arrayoptionlineedit.h
buildoptions.h buildoptions.h
buildoptionsmodel.cpp buildoptionsmodel.cpp
buildoptionsmodel.h buildoptionsmodel.h

View File

@@ -1,67 +0,0 @@
// Copyright (C) 2020 Alexis Jeandet.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "arrayoptionlineedit.h"
namespace MesonProjectManager::Internal {
ArrayOptionLineEdit::ArrayOptionLineEdit(QWidget *parent)
: QPlainTextEdit(parent)
{
m_highLighter = new RegexHighlighter(this);
m_highLighter->setDocument(this->document());
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setLineWrapMode(QPlainTextEdit::NoWrap);
QFontMetrics metrics(this->font());
int lineHeight = metrics.lineSpacing();
this->setFixedHeight(lineHeight * 1.5);
}
QStringList ArrayOptionLineEdit::options()
{
return m_highLighter->options(this->toPlainText());
}
void ArrayOptionLineEdit::keyPressEvent(QKeyEvent *e)
{
if (e->key() != Qt::Key_Return)
return QPlainTextEdit::keyPressEvent(e);
e->accept();
}
RegexHighlighter::RegexHighlighter(QWidget *parent)
: QSyntaxHighlighter(parent)
{
m_format.setUnderlineStyle(QTextCharFormat::SingleUnderline);
m_format.setUnderlineColor(QColor(180, 180, 180));
m_format.setBackground(QBrush(QColor(180, 180, 230, 80)));
}
QStringList RegexHighlighter::options(const QString &text)
{
QRegularExpressionMatchIterator i = m_regex.globalMatch(text);
QStringList op;
while (i.hasNext()) {
QRegularExpressionMatch match = i.next();
for (int j = 1; j <= match.lastCapturedIndex(); j++) {
auto str = match.captured(j);
if (!str.isEmpty())
op.push_back(str);
}
}
return op;
}
void RegexHighlighter::highlightBlock(const QString &text)
{
QRegularExpressionMatchIterator i = m_regex.globalMatch(text);
while (i.hasNext()) {
QRegularExpressionMatch match = i.next();
for (int j = 1; j <= match.lastCapturedIndex(); j++) {
setFormat(match.capturedStart(j), match.capturedLength(j), m_format);
}
}
}
} // namespace MesonProjectManager::Internal

View File

@@ -1,37 +0,0 @@
// Copyright (C) 2020 Alexis Jeandet.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#pragma once
#include <QColor>
#include <QPlainTextEdit>
#include <QRegularExpression>
#include <QSyntaxHighlighter>
namespace MesonProjectManager::Internal {
class RegexHighlighter : public QSyntaxHighlighter
{
const QRegularExpression m_regex{R"('([^']+)'+|([^', ]+)[, ]*)"};
QTextCharFormat m_format;
public:
RegexHighlighter(QWidget *parent);
void highlightBlock(const QString &text) override;
QStringList options(const QString &text);
};
class ArrayOptionLineEdit : public QPlainTextEdit
{
Q_OBJECT
RegexHighlighter *m_highLighter;
public:
ArrayOptionLineEdit(QWidget *parent = nullptr);
QStringList options();
protected:
void keyPressEvent(QKeyEvent *e) override;
};
} // namespace MesonProjectManager::Internal

View File

@@ -3,18 +3,91 @@
#include "buildoptionsmodel.h" #include "buildoptionsmodel.h"
#include "arrayoptionlineedit.h"
#include "mesonprojectmanagertr.h" #include "mesonprojectmanagertr.h"
#include <QColor>
#include <QComboBox> #include <QComboBox>
#include <QLabel> #include <QLabel>
#include <QLineEdit> #include <QLineEdit>
#include <QMap> #include <QPlainTextEdit>
#include <QStyledItemDelegate> #include <QRegularExpression>
#include <QTextEdit> #include <QSyntaxHighlighter>
namespace MesonProjectManager::Internal { namespace MesonProjectManager::Internal {
static QRegularExpression &regExp()
{
static QRegularExpression s_regexp{R"('([^']+)'+|([^', ]+)[, ]*)"};
return s_regexp;
}
class RegexHighlighter final : public QSyntaxHighlighter
{
public:
RegexHighlighter(QWidget *parent) : QSyntaxHighlighter(parent)
{
m_format.setUnderlineStyle(QTextCharFormat::SingleUnderline);
m_format.setUnderlineColor(QColor(180, 180, 180));
m_format.setBackground(QBrush(QColor(180, 180, 230, 80)));
}
void highlightBlock(const QString &text) final
{
QRegularExpressionMatchIterator i = regExp().globalMatch(text);
while (i.hasNext()) {
const QRegularExpressionMatch match = i.next();
for (int j = 1; j <= match.lastCapturedIndex(); j++)
setFormat(match.capturedStart(j), match.capturedLength(j), m_format);
}
}
QStringList options(const QString &text) const
{
QRegularExpressionMatchIterator i = regExp().globalMatch(text);
QStringList op;
while (i.hasNext()) {
const QRegularExpressionMatch match = i.next();
for (int j = 1; j <= match.lastCapturedIndex(); j++) {
auto str = match.captured(j);
if (!str.isEmpty())
op.push_back(str);
}
}
return op;
}
private:
QTextCharFormat m_format;
};
class ArrayOptionLineEdit final : public QPlainTextEdit
{
Q_OBJECT
public:
ArrayOptionLineEdit(QWidget *parent = nullptr) : QPlainTextEdit(parent)
{
m_highLighter = new RegexHighlighter(this);
m_highLighter->setDocument(document());
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setLineWrapMode(QPlainTextEdit::NoWrap);
setFixedHeight(QFontMetrics(font()).lineSpacing() * 1.5);
}
QStringList options() const { return m_highLighter->options(toPlainText()); }
protected:
void keyPressEvent(QKeyEvent *e) final
{
if (e->key() != Qt::Key_Return)
return QPlainTextEdit::keyPressEvent(e);
e->accept();
}
private:
RegexHighlighter *m_highLighter = nullptr;
};
// this could be relaxed once we have something able to link reliably meson build type // this could be relaxed once we have something able to link reliably meson build type
// to QTC build type and update it, this must not break any features like tests/debug/profiling... // to QTC build type and update it, this must not break any features like tests/debug/profiling...
static const QStringList lockedOptions = {"buildtype", "debug", "backend", "optimization"}; static const QStringList lockedOptions = {"buildtype", "debug", "backend", "optimization"};
@@ -192,3 +265,5 @@ void BuildOptionDelegate::setModelData(QWidget *editor,
} }
} // namespace MesonProjectManager::Internal } // namespace MesonProjectManager::Internal
#include "buildoptionsmodel.moc"

View File

@@ -30,8 +30,6 @@ Project {
"target.h", "target.h",
"mesonpluginconstants.h", "mesonpluginconstants.h",
"mesonprojectplugin.cpp", "mesonprojectplugin.cpp",
"arrayoptionlineedit.cpp",
"arrayoptionlineedit.h",
"buildoptionsmodel.cpp", "buildoptionsmodel.cpp",
"buildoptionsmodel.h", "buildoptionsmodel.h",
"mesonbuildconfiguration.cpp", "mesonbuildconfiguration.cpp",