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

View File

@ -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<BMessage> ChatPopUpItems();

View File

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

View File

@ -9,6 +9,9 @@
#include <Window.h>
#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());
}
}

View File

@ -15,10 +15,12 @@ class SendTextView : public BTextView {
public:
SendTextView(const char* name, ConversationView* convView);
void KeyDown(const char* bytes, int32 numBytes);
virtual void KeyDown(const char* bytes, int32 numBytes);
private:
void _AutoComplete();
const char* _CommandAutoComplete();
const char* _UserAutoComplete();
void _AppendHistory();
void _UpHistory();
@ -26,9 +28,11 @@ private:
ConversationView* fConversationView;
// Used for auto-completion
int32 fCurrentIndex;
BString fCurrentWord;
// Used for history
BStringList fHistory;
int32 fHistoryIndex;
};