From 545f20b9051caad8d0223bd332a094c2ffbe8435 Mon Sep 17 00:00:00 2001
From: Kevin Smith <git@kismith.co.uk>
Date: Sat, 24 Jul 2010 20:51:52 +0100
Subject: Improvements and further testing to tab completion.

Resolves: #506
Resolves: #498

diff --git a/SwifTools/TabComplete.cpp b/SwifTools/TabComplete.cpp
index 2fd0547..1e15595 100644
--- a/SwifTools/TabComplete.cpp
+++ b/SwifTools/TabComplete.cpp
@@ -13,9 +13,10 @@
 namespace Swift {
 
 void TabComplete::addWord(const String& word) {
-	words_.push_back(word);
+	words_.erase(std::remove(words_.begin(), words_.end(), word), words_.end());
+	words_.insert(words_.begin(), word);
 	if (word.getLowerCase().beginsWith(lastShort_)) {
-		lastCompletionCandidates_.push_back(word);
+		lastCompletionCandidates_.insert(lastCompletionCandidates_.begin(), word);
 	}
 }
 
diff --git a/SwifTools/UnitTest/TabCompleteTest.cpp b/SwifTools/UnitTest/TabCompleteTest.cpp
index 4d152f6..b7b643f 100644
--- a/SwifTools/UnitTest/TabCompleteTest.cpp
+++ b/SwifTools/UnitTest/TabCompleteTest.cpp
@@ -20,6 +20,7 @@ class TabCompleteTest : public CppUnit::TestFixture {
 	CPPUNIT_TEST(testChangeMatch);
 	CPPUNIT_TEST(testRemoveDuringComplete);
 	CPPUNIT_TEST(testAddDuringComplete);
+	CPPUNIT_TEST(testSwiftRoomSample);
 	CPPUNIT_TEST_SUITE_END();
 	
 public:
@@ -71,14 +72,14 @@ public:
 		completer_.addWord(long2);
 		completer_.addWord("Bleh");
 		CPPUNIT_ASSERT_EQUAL(
-			long1,
-			completer_.completeWord(short1));
-		CPPUNIT_ASSERT_EQUAL(
 			long2,
-			completer_.completeWord(long1));
+			completer_.completeWord(short1));
 		CPPUNIT_ASSERT_EQUAL(
 			long1,
 			completer_.completeWord(long2));
+		CPPUNIT_ASSERT_EQUAL(
+			long2,
+			completer_.completeWord(long1));
 	}
 
 	void testChangeMatch() {
@@ -113,15 +114,15 @@ public:
 		completer_.addWord(long2);
 		completer_.addWord("Bleh");
 		CPPUNIT_ASSERT_EQUAL(
-			long1,
-			completer_.completeWord(short1));
-		completer_.removeWord(long1);
-		CPPUNIT_ASSERT_EQUAL(
 			long2,
-			completer_.completeWord(long1));
+			completer_.completeWord(short1));
+		completer_.removeWord(long2);
 		CPPUNIT_ASSERT_EQUAL(
-			long2,
+			long1,
 			completer_.completeWord(long2));
+		CPPUNIT_ASSERT_EQUAL(
+			long1,
+			completer_.completeWord(long1));
 	}
 
 	void testAddDuringComplete() {
@@ -134,17 +135,129 @@ public:
 		completer_.addWord(long2);
 		completer_.addWord("Bleh");
 		CPPUNIT_ASSERT_EQUAL(
-			long1,
+			long2,
 			completer_.completeWord(short1));
 		completer_.addWord(long3);
 		CPPUNIT_ASSERT_EQUAL(
-			long2,
-			completer_.completeWord(long1));
+			long1,
+			completer_.completeWord(long2));
 		CPPUNIT_ASSERT_EQUAL(
 			long3,
-			completer_.completeWord(long2));
+			completer_.completeWord(long1));
+	}
+
+	void testSwiftRoomSample() {
+		String t("t");
+		String Anpan("Anpan");
+		String cdubouloz("cdubouloz");
+		String Tobias("Tobias");
+		String Zash("Zash");
+		String lastsky("lastsky");
+		String Steve("Steve Kille");
+		String Flo("Flo");
+		String Test("Test");
+		String test("test");
+		completer_.addWord(Anpan);
+		completer_.addWord(cdubouloz);
+		completer_.addWord(Tobias);
+		completer_.addWord(lastsky);
+		completer_.addWord(Steve);
+		completer_.addWord(Flo);
+		completer_.addWord(Zash);
+
+		CPPUNIT_ASSERT_EQUAL(
+			Tobias,
+			completer_.completeWord(t));
+		CPPUNIT_ASSERT_EQUAL(
+			Tobias,
+			completer_.completeWord(Tobias));
+
+		completer_.addWord(Test);
+
+		CPPUNIT_ASSERT_EQUAL(
+			Test,
+			completer_.completeWord(t));
+		CPPUNIT_ASSERT_EQUAL(
+			Tobias,
+			completer_.completeWord(Test));
+		CPPUNIT_ASSERT_EQUAL(
+			Test,
+			completer_.completeWord(Tobias));
+		CPPUNIT_ASSERT_EQUAL(
+			Tobias,
+			completer_.completeWord(Test));
+
+		completer_.addWord(Zash);
+		completer_.addWord(Zash);
+		completer_.addWord(Zash);
+		completer_.addWord(Zash);
+
+		completer_.removeWord(Test);
+
+		CPPUNIT_ASSERT_EQUAL(
+			Tobias,
+			completer_.completeWord(t));
+		CPPUNIT_ASSERT_EQUAL(
+			Tobias,
+			completer_.completeWord(Tobias));
+
+		completer_.addWord(test);
+
+		completer_.addWord(Zash);
+		completer_.addWord(Zash);
+		completer_.addWord(Zash);
+		completer_.addWord(Zash);
+
+
+		CPPUNIT_ASSERT_EQUAL(
+			test,
+			completer_.completeWord(t));
+		CPPUNIT_ASSERT_EQUAL(
+			Tobias,
+			completer_.completeWord(test));
+		CPPUNIT_ASSERT_EQUAL(
+			test,
+			completer_.completeWord(Tobias));
+		CPPUNIT_ASSERT_EQUAL(
+			Tobias,
+			completer_.completeWord(test));
+
+		completer_.removeWord(test);
+
+		CPPUNIT_ASSERT_EQUAL(
+			Tobias,
+			completer_.completeWord(t));
+		CPPUNIT_ASSERT_EQUAL(
+			Tobias,
+			completer_.completeWord(Tobias));
+
+		completer_.removeWord(Tobias);
+		CPPUNIT_ASSERT_EQUAL(
+			t,
+			completer_.completeWord(t));
+		CPPUNIT_ASSERT_EQUAL(
+			t,
+			completer_.completeWord(t));
+		CPPUNIT_ASSERT_EQUAL(
+			t,
+			completer_.completeWord(t));
+		CPPUNIT_ASSERT_EQUAL(
+			t,
+			completer_.completeWord(t));
+
+		completer_.addWord(Tobias);
+
+		CPPUNIT_ASSERT_EQUAL(
+			Tobias,
+			completer_.completeWord(t));
+		CPPUNIT_ASSERT_EQUAL(
+			Tobias,
+			completer_.completeWord(Tobias));
+
 	}
 
+
+
 private:
 	TabComplete completer_;
 };
diff --git a/Swift/Controllers/Chat/MUCController.cpp b/Swift/Controllers/Chat/MUCController.cpp
index 92ff7a9..4abab99 100644
--- a/Swift/Controllers/Chat/MUCController.cpp
+++ b/Swift/Controllers/Chat/MUCController.cpp
@@ -207,10 +207,11 @@ void MUCController::preHandleIncomingMessage(boost::shared_ptr<MessageEvent> mes
 	if (joined_ && messageTargetsMe(message) && !message->getPayload<Delay>()) {
 		eventController_->handleIncomingEvent(messageEvent);
 	}
-	String nick = message->getFrom().getResource();
-	if (nick != nick_) {
-		completer_->removeWord(nick);
-		completer_->addWord(nick);
+	if (joined_) {
+		String nick = message->getFrom().getResource();
+		if (nick != nick_) {
+			completer_->addWord(nick);
+		}
 	}
 	/*Buggy implementations never send the status code, so use an incoming message as a hint that joining's done (e.g. the old ejabberd on psi-im.org).*/
 	receivedActivity();
-- 
cgit v0.10.2-6-g49f6