From 82f920798fc37c0b01956875b9c78cc9ca679fad Mon Sep 17 00:00:00 2001 From: Jaidyn Ann Date: Sat, 31 Jul 2021 20:18:36 -0500 Subject: [PATCH] Auto-completion for commands --- application/Server.cpp | 11 +++++ application/Server.h | 1 + application/views/ConversationView.cpp | 1 + application/views/SendTextView.cpp | 62 ++++++++++++++++++++++---- application/views/SendTextView.h | 16 ++++--- 5 files changed, 76 insertions(+), 15 deletions(-) diff --git a/application/Server.cpp b/application/Server.cpp index 16526a0..0973c4a 100644 --- a/application/Server.cpp +++ b/application/Server.cpp @@ -831,6 +831,17 @@ Server::AddConversation(Conversation* chat, int64 instance) } +CommandMap +Server::Commands(int64 instance) +{ + ProtocolLooper* looper = fLoopers.ValueFor(instance); + CommandMap cmds = fCommands; + if (looper != NULL) + cmds.AddList(looper->Commands()); + return cmds; +} + + ChatCommand* Server::CommandById(BString id, int64 instance) { diff --git a/application/Server.h b/application/Server.h index 2ce1be1..74839c7 100644 --- a/application/Server.h +++ b/application/Server.h @@ -63,6 +63,7 @@ public: Conversation* ConversationById(BString id, int64 instance); void AddConversation(Conversation* chat, int64 instance); + CommandMap Commands(int64 instance); ChatCommand* CommandById(BString id, int64 instance); BObjectList ChatPopUpItems(); diff --git a/application/views/ConversationView.cpp b/application/views/ConversationView.cpp index 4880a2c..d33d92b 100644 --- a/application/views/ConversationView.cpp +++ b/application/views/ConversationView.cpp @@ -104,6 +104,7 @@ ConversationView::MessageReceived(BMessage* message) fConversation->ImMessage(&msg); fSendView->SetText(""); + fSendView->ScrollToOffset(0); break; } case IM_MESSAGE: diff --git a/application/views/SendTextView.cpp b/application/views/SendTextView.cpp index abc10ae..67f4ca2 100644 --- a/application/views/SendTextView.cpp +++ b/application/views/SendTextView.cpp @@ -9,6 +9,9 @@ #include #include "AppMessages.h" +#include "MainWindow.h" +#include "Server.h" +#include "TheApp.h" SendTextView::SendTextView(const char* name, ConversationView* convView) @@ -57,6 +60,9 @@ SendTextView::KeyDown(const char* bytes, int32 numBytes) void SendTextView::_AutoComplete() { + if (fConversationView == NULL || fConversationView->GetConversation() == NULL) + return; + BStringList words; BString text = Text(); text.Split(" ", true, words); @@ -67,10 +73,51 @@ SendTextView::_AutoComplete() if (fCurrentWord.IsEmpty() == true) fCurrentWord = lastWord; - // No command auto-completion (yet) + // Now to find the substitutes + const char* substitution = NULL; if (fCurrentWord.StartsWith("/") == true) - return; + substitution = _CommandAutoComplete(); + else + substitution = _UserAutoComplete(); + if (substitution == NULL) + fCurrentIndex = 0; + else { + text.ReplaceLast(lastWord.String(), substitution); + SetText(text.String()); + Select(TextLength(), TextLength()); + } +} + + +const char* +SendTextView::_CommandAutoComplete() +{ + int64 instance = + fConversationView->GetConversation()->GetProtocolLooper()->GetInstance(); + CommandMap commands = + ((TheApp*)be_app)->GetMainWindow()->GetServer()->Commands(instance); + + BString command; + BString cmdWord = BString(fCurrentWord).Remove(0, 1); + for (int i, j = 0; i < commands.CountItems(); i++) + if (commands.KeyAt(i).StartsWith(cmdWord)) { + if (j == fCurrentIndex) { + command = commands.KeyAt(i); + fCurrentIndex++; + break; + } + j++; + } + if (command.IsEmpty() == true) + return NULL; + return command.Prepend("/").String(); +} + + +const char* +SendTextView::_UserAutoComplete() +{ BString user; UserMap users = fConversationView->GetConversation()->Users(); for (int i, j = 0; i < users.CountItems(); i++) @@ -82,14 +129,9 @@ SendTextView::_AutoComplete() } j++; } - if (user.IsEmpty() == true) - fCurrentIndex = 0; - else { - text.ReplaceLast(lastWord.String(), user.String()); - SetText(text.String()); - Select(TextLength(), TextLength()); - } + return NULL; + return user.String(); } @@ -112,6 +154,7 @@ SendTextView::_UpHistory() if (fHistoryIndex < fHistory.CountStrings()) { fHistoryIndex++; SetText(fHistory.StringAt(fHistoryIndex - 1)); + Select(TextLength(), TextLength()); } } @@ -122,5 +165,6 @@ SendTextView::_DownHistory() if (fHistoryIndex > 1) { fHistoryIndex--; SetText(fHistory.StringAt(fHistoryIndex - 1)); + Select(TextLength(), TextLength()); } } diff --git a/application/views/SendTextView.h b/application/views/SendTextView.h index 7d9524f..7c0e43c 100644 --- a/application/views/SendTextView.h +++ b/application/views/SendTextView.h @@ -13,22 +13,26 @@ class SendTextView : public BTextView { public: - SendTextView(const char* name, ConversationView* convView); + SendTextView(const char* name, ConversationView* convView); - void KeyDown(const char* bytes, int32 numBytes); + virtual void KeyDown(const char* bytes, int32 numBytes); private: - void _AutoComplete(); + void _AutoComplete(); + const char* _CommandAutoComplete(); + const char* _UserAutoComplete(); - void _AppendHistory(); - void _UpHistory(); - void _DownHistory(); + void _AppendHistory(); + void _UpHistory(); + void _DownHistory(); ConversationView* fConversationView; + // Used for auto-completion int32 fCurrentIndex; BString fCurrentWord; + // Used for history BStringList fHistory; int32 fHistoryIndex; };