Skip to content

Commit

Permalink
[Impl] Use StringVectorModel to provide code completions
Browse files Browse the repository at this point in the history
StringVectorModel:
- Like QStringListModel, but utilize QVector<QString> instead.
  • Loading branch information
IoeCmcomc committed Jul 28, 2023
1 parent 65dd51f commit b4851c4
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 27 deletions.
34 changes: 18 additions & 16 deletions src/codeeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "parsers/command/minecraftparser.h"
#include "parsers/command/schema/schemaliteralnode.h"
#include "parsers/command/schema/schemaargumentnode.h"
#include "stringvectormodel.h"
#include "game.h"

#include <QPainter>
Expand All @@ -24,13 +25,12 @@
#include <QScroller>
#include <QScrollBar>
#include <QCompleter>
#include <QStringListModel>
#include <QElapsedTimer>
#include <QAbstractItemView>
#include <QMenu>


QStringList getMinecraftInfoKeys(const QString &key) {
QVector<QString> getMinecraftInfoKeys(const QString &key) {
// QElapsedTimer timer;
// timer.start();

Expand All @@ -40,17 +40,17 @@ QStringList getMinecraftInfoKeys(const QString &key) {
// qDebug() << "getMinecraftInfoKeys() exec time:" <<
// timer.nsecsElapsed() / 1e6;

return infoMap.keys();
return infoMap.keys().toVector();
}

QStringList loadMinecraftCommandLiterals(
QVector<QString> loadMinecraftCommandLiterals(
const Command::Schema::Node * const node, ushort depth = 0) {
QElapsedTimer timer;

if (depth == 0)
timer.start();

QStringList ret;
QVector<QString> ret;

const auto &&literalChidrens = node->literalChildren();
for (auto it = literalChidrens.cbegin();
Expand All @@ -76,8 +76,8 @@ QStringList loadMinecraftCommandLiterals(
return ret;
}

QStringList loadMinecraftCompletionInfo() {
QStringList &&ret = loadMinecraftCommandLiterals(
QVector<QString> loadMinecraftCompletionInfo() {
QVector<QString> &&ret = loadMinecraftCommandLiterals(
Command::MinecraftParser::schema());

ret += getMinecraftInfoKeys(QStringLiteral("attribute"));
Expand All @@ -100,8 +100,8 @@ QStringList loadMinecraftCompletionInfo() {
ret += Game::getRegistry(QStringLiteral("particle_type"));
ret += Game::getRegistry(QStringLiteral("sound_event"));

ret.sort(Qt::CaseInsensitive);
ret.removeDuplicates();
std::sort(ret.begin(), ret.end());
ret.erase(std::unique(ret.begin(), ret.end()), ret.end());
return ret;
}

Expand Down Expand Up @@ -130,7 +130,7 @@ qreal perceivedLightness(const QColor &color) {
}
}

QStringList CodeEditor::minecraftCompletionInfo = {};
QVector<QString> CodeEditor::minecraftCompletionInfo = {};

CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent) {
m_gutter = new CodeGutter(this);
Expand Down Expand Up @@ -193,7 +193,7 @@ void CodeEditor::initCompleter() {
minecraftCompletionInfo.size() << "items)";
}
}
completer->setModel(new QStringListModel(minecraftCompletionInfo, this));
completer->setModel(new StringVectorModel(minecraftCompletionInfo, this));
completer->setModelSorting(QCompleter::CaseInsensitivelySortedModel);
completer->setCaseSensitivity(Qt::CaseInsensitive);
completer->setWrapAround(false);
Expand Down Expand Up @@ -489,20 +489,22 @@ void CodeEditor::keyPressEvent(QKeyEvent *e) {
if (completionPrefix != m_completer->completionPrefix()) {
if (m_completer->popup()->isHidden()) {
qDebug() << "Combining final completions";
QStringList completionInfo = minecraftCompletionInfo;
QVector<QString> completionInfo = minecraftCompletionInfo;

const QVector<QString> &&idList = Glhp::fileIdList(
QDir::currentPath(), QString(), QString(), false);
for (const auto &item: idList) {
completionInfo << item;
}

completionInfo.sort(Qt::CaseInsensitive);
completionInfo.removeDuplicates();
std::sort(completionInfo.begin(), completionInfo.end());
completionInfo.erase(std::unique(completionInfo.begin(),
completionInfo.end()),
completionInfo.end());

if (auto *model =
qobject_cast<QStringListModel *>(m_completer->model())) {
model->setStringList(completionInfo);
qobject_cast<StringVectorModel *>(m_completer->model())) {
model->setVector(completionInfo);
}
// qDebug() << minecraftCompletionInfo.size() <<
// m_completer->model()->rowCount() <<
Expand Down
3 changes: 1 addition & 2 deletions src/codeeditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <QPlainTextEdit>
#include <QPointer>
#include <QSettings>
#include <QStringList>

#include "codefile.h"
#include "parsers/parser.h"
Expand Down Expand Up @@ -94,7 +93,7 @@ private /*slots*/ :
void onTextChanged();

private:
static QStringList minecraftCompletionInfo;
static QVector<QString> minecraftCompletionInfo;

QTextCharFormat bracketSeclectFmt;
QTextCharFormat errorHighlightRule;
Expand Down
15 changes: 9 additions & 6 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ QString Game::versionString() {
return value;
}

QStringList Game::getRegistry(const QString &type) {
QVector<QString> Game::getRegistry(const QString &type) {
return getRegistry(type, Game::versionString());
}

QStringList Game::getRegistry(const QString &type, const QString &version) {
static LRU::Cache<std::tuple<QString, QString>, QStringList> cache{ 20 };
QVector<QString> Game::getRegistry(const QString &type,
const QString &version) {
static LRU::Cache<std::tuple<QString, QString>,
QVector<QString> > cache{ 20 };

const std::tuple inputPair{ type, version };

Expand All @@ -40,7 +42,8 @@ QStringList Game::getRegistry(const QString &type, const QString &version) {
return result;
}

QStringList Game::loadRegistry(const QString &type, const QString &version) {
QVector<QString> Game::loadRegistry(const QString &type,
const QString &version) {
QFileInfo finfo(":minecraft/" + version + "/registries/" + type +
"/data.min.json");

Expand All @@ -60,8 +63,8 @@ QStringList Game::loadRegistry(const QString &type, const QString &version) {
return {};
}

QStringList values;
const auto &&array = doc.array();
QVector<QString> values;
const auto &&array = doc.array();
for (const auto &value : array) {
values << value.toString();
}
Expand Down
6 changes: 3 additions & 3 deletions src/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ namespace Game {
const int depth = 0);
QVariantMap loadInfo(const QString &type, const QString &version,
const int depth = 0);
QStringList getRegistry(const QString &type);
QStringList getRegistry(const QString &type, const QString &version);
QStringList loadRegistry(const QString &type, const QString &version);
QVector<QString> getRegistry(const QString &type);
QVector<QString> getRegistry(const QString &type, const QString &version);
QVector<QString> loadRegistry(const QString &type, const QString &version);
}

#endif // GAME_H
2 changes: 2 additions & 0 deletions src/src.pro
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ SOURCES += \
stackedwidget.cpp \
statisticsdialog.cpp \
statusbar.cpp \
stringvectormodel.cpp \
stripedscrollbar.cpp \
# stylesheetreapplier.cpp \
tabbeddocumentinterface.cpp \
Expand Down Expand Up @@ -223,6 +224,7 @@ HEADERS += \
stackedwidget.h \
statisticsdialog.h \
statusbar.h \
stringvectormodel.h \
stripedscrollbar.h \
# stylesheetreapplier.h \ # Already added in mcdatapackerwidgets.pri
tabbeddocumentinterface.h \
Expand Down
35 changes: 35 additions & 0 deletions src/stringvectormodel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "stringvectormodel.h"

StringVectorModel::StringVectorModel(QObject *parent)
: QAbstractListModel{parent} {
}

StringVectorModel::StringVectorModel(const StringVector &vec, QObject *parent)
: QAbstractListModel{parent}, m_vector{vec} {
}

int StringVectorModel::rowCount(const QModelIndex &parent) const {
return m_vector.size();
}

int StringVectorModel::columnCount(const QModelIndex &parent) const {
return 1;
}

QVariant StringVectorModel::data(const QModelIndex &index, int role) const {
if (role == Qt::DisplayRole || role == Qt::EditRole) {
return m_vector.value(index.row(), QString());
} else {
return {};
}
}

StringVector StringVectorModel::vector() const {
return m_vector;
}

void StringVectorModel::setVector(const StringVector &newVector) {
beginResetModel();
m_vector = newVector;
endResetModel();
}
28 changes: 28 additions & 0 deletions src/stringvectormodel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef STRINGVECTORMODEL_H
#define STRINGVECTORMODEL_H

#include <QAbstractListModel>

using StringVector = QVector<QString>;

class StringVectorModel : public QAbstractListModel {
Q_OBJECT
public:
explicit StringVectorModel(
QObject *parent = nullptr);
explicit StringVectorModel(const StringVector &vec,
QObject *parent = nullptr);

int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index,
int role = Qt::DisplayRole) const override;

StringVector vector() const;
void setVector(const StringVector &newVector);

private:
StringVector m_vector;
};

#endif // STRINGVECTORMODEL_H

0 comments on commit b4851c4

Please sign in to comment.