Auto-completion for commands

This commit is contained in:
Jaidyn Ann 2021-07-31 20:18:36 -05:00
parent 2e3742d4e1
commit 82f920798f
5 changed files with 76 additions and 15 deletions

View File

@ -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* ChatCommand*
Server::CommandById(BString id, int64 instance) Server::CommandById(BString id, int64 instance)
{ {

View File

@ -63,6 +63,7 @@ public:
Conversation* ConversationById(BString id, int64 instance); Conversation* ConversationById(BString id, int64 instance);
void AddConversation(Conversation* chat, int64 instance); void AddConversation(Conversation* chat, int64 instance);
CommandMap Commands(int64 instance);
ChatCommand* CommandById(BString id, int64 instance); ChatCommand* CommandById(BString id, int64 instance);
BObjectList<BMessage> ChatPopUpItems(); BObjectList<BMessage> ChatPopUpItems();

View File

@ -104,6 +104,7 @@ ConversationView::MessageReceived(BMessage* message)
fConversation->ImMessage(&msg); fConversation->ImMessage(&msg);
fSendView->SetText(""); fSendView->SetText("");
fSendView->ScrollToOffset(0);
break; break;
} }
case IM_MESSAGE: case IM_MESSAGE:

View File

@ -9,6 +9,9 @@
#include <Window.h> #include <Window.h>
#include "AppMessages.h" #include "AppMessages.h"
#include "MainWindow.h"
#include "Server.h"
#include "TheApp.h"
SendTextView::SendTextView(const char* name, ConversationView* convView) SendTextView::SendTextView(const char* name, ConversationView* convView)
@ -57,6 +60,9 @@ SendTextView::KeyDown(const char* bytes, int32 numBytes)
void void
SendTextView::_AutoComplete() SendTextView::_AutoComplete()
{ {
if (fConversationView == NULL || fConversationView->GetConversation() == NULL)
return;
BStringList words; BStringList words;
BString text = Text(); BString text = Text();
text.Split(" ", true, words); text.Split(" ", true, words);
@ -67,10 +73,51 @@ SendTextView::_AutoComplete()
if (fCurrentWord.IsEmpty() == true) if (fCurrentWord.IsEmpty() == true)
fCurrentWord = lastWord; fCurrentWord = lastWord;
// No command auto-completion (yet) // Now to find the substitutes
const char* substitution = NULL;
if (fCurrentWord.StartsWith("/") == true) 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; BString user;
UserMap users = fConversationView->GetConversation()->Users(); UserMap users = fConversationView->GetConversation()->Users();
for (int i, j = 0; i < users.CountItems(); i++) for (int i, j = 0; i < users.CountItems(); i++)
@ -82,14 +129,9 @@ SendTextView::_AutoComplete()
} }
j++; j++;
} }
if (user.IsEmpty() == true) if (user.IsEmpty() == true)
fCurrentIndex = 0; return NULL;
else { return user.String();
text.ReplaceLast(lastWord.String(), user.String());
SetText(text.String());
Select(TextLength(), TextLength());
}
} }
@ -112,6 +154,7 @@ SendTextView::_UpHistory()
if (fHistoryIndex < fHistory.CountStrings()) { if (fHistoryIndex < fHistory.CountStrings()) {
fHistoryIndex++; fHistoryIndex++;
SetText(fHistory.StringAt(fHistoryIndex - 1)); SetText(fHistory.StringAt(fHistoryIndex - 1));
Select(TextLength(), TextLength());
} }
} }
@ -122,5 +165,6 @@ SendTextView::_DownHistory()
if (fHistoryIndex > 1) { if (fHistoryIndex > 1) {
fHistoryIndex--; fHistoryIndex--;
SetText(fHistory.StringAt(fHistoryIndex - 1)); SetText(fHistory.StringAt(fHistoryIndex - 1));
Select(TextLength(), TextLength());
} }
} }

View File

@ -13,22 +13,26 @@
class SendTextView : public BTextView { class SendTextView : public BTextView {
public: 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: private:
void _AutoComplete(); void _AutoComplete();
const char* _CommandAutoComplete();
const char* _UserAutoComplete();
void _AppendHistory(); void _AppendHistory();
void _UpHistory(); void _UpHistory();
void _DownHistory(); void _DownHistory();
ConversationView* fConversationView; ConversationView* fConversationView;
// Used for auto-completion
int32 fCurrentIndex; int32 fCurrentIndex;
BString fCurrentWord; BString fCurrentWord;
// Used for history
BStringList fHistory; BStringList fHistory;
int32 fHistoryIndex; int32 fHistoryIndex;
}; };