From 971854bfefad4c644ac3d5f5d6003f24d551bf1d Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Mon, 10 Mar 2025 22:04:24 +0530 Subject: Add completer + simplify command parsing + class generator --- include/CommandParser.hpp | 9 +++------ include/LuaRuntime.hpp | 2 +- include/completion/CommandsModel.hpp | 32 ++++++++++++++++++++++++++++++++ include/completion/Completer.hpp | 14 ++++++++++++++ include/completion/CompleterDelegate.hpp | 15 +++++++++++++++ include/widgets/CommandInput.hpp | 3 ++- 6 files changed, 67 insertions(+), 8 deletions(-) create mode 100644 include/completion/CommandsModel.hpp create mode 100644 include/completion/Completer.hpp create mode 100644 include/completion/CompleterDelegate.hpp (limited to 'include') diff --git a/include/CommandParser.hpp b/include/CommandParser.hpp index 128b5f4..dea04c5 100644 --- a/include/CommandParser.hpp +++ b/include/CommandParser.hpp @@ -12,13 +12,10 @@ enum CommandType { TabPrev, }; -class Cmd { -public: +struct Cmd { CommandType command; - QStringList args; - - Cmd(CommandType command = Noop, QStringList args = QStringList()) - : command(command), args(args) {} + QString argsString; + QString rawInput; }; class CommandParser { diff --git a/include/LuaRuntime.hpp b/include/LuaRuntime.hpp index 85bb1ae..96c0004 100644 --- a/include/LuaRuntime.hpp +++ b/include/LuaRuntime.hpp @@ -13,7 +13,7 @@ public: return &inst; } - void evaluate(const char *code); + void evaluate(QString code); protected: LuaRuntime(); diff --git a/include/completion/CommandsModel.hpp b/include/completion/CommandsModel.hpp new file mode 100644 index 0000000..6cb25f9 --- /dev/null +++ b/include/completion/CommandsModel.hpp @@ -0,0 +1,32 @@ +#pragma once + +#include +#include +#include + +struct Command { + QString name; + QString description; +}; + +const QList commands = { + {.name = "open", .description = "Open a url in the current tab"}, + {.name = "tabopen", .description = "Open a url in a new tab"}, + {.name = "bnext", .description = "Go to next tab"}, + {.name = "bprev", .description = "Go to previous tab"}, +}; + +class CommandsModel : public QAbstractListModel { + Q_OBJECT + +public: + CommandsModel(QObject *parent = nullptr); + int rowCount(const QModelIndex &parent = QModelIndex()) const override; + int columnCount(const QModelIndex &parent = QModelIndex()) const override; + QHash roleNames() const override; + QVariant data(const QModelIndex &index, + int role = Qt::DisplayRole) const override; + +private: + QList items; +}; diff --git a/include/completion/Completer.hpp b/include/completion/Completer.hpp new file mode 100644 index 0000000..b67de61 --- /dev/null +++ b/include/completion/Completer.hpp @@ -0,0 +1,14 @@ +#pragma once + +#include +#include +#include + +class Completer : public QCompleter { + Q_OBJECT + +public: + Completer(QObject *parent = nullptr); + + // bool eventFilter(QObject *watched, QEvent *event) override; +}; diff --git a/include/completion/CompleterDelegate.hpp b/include/completion/CompleterDelegate.hpp new file mode 100644 index 0000000..fc7a321 --- /dev/null +++ b/include/completion/CompleterDelegate.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include +#include +#include + +class CompleterDelegate : public QStyledItemDelegate { + Q_OBJECT + +public: + explicit CompleterDelegate(QObject *parent = nullptr); + + void paint(QPainter *painter, const QStyleOptionViewItem &option, + const QModelIndex &index) const override; +}; diff --git a/include/widgets/CommandInput.hpp b/include/widgets/CommandInput.hpp index a6ca0ed..3ce9500 100644 --- a/include/widgets/CommandInput.hpp +++ b/include/widgets/CommandInput.hpp @@ -1,8 +1,9 @@ #pragma once #include -#include #include +#include +#include class CommandInput : public QWidget { Q_OBJECT -- cgit v1.3.1