// NamesPrefix lists all members' names with a given prefix, used to query
// for autocompletion purposes. Sorted by which user was last active.
-func (r *Room) NamesPrefix(prefix string, current_user *message.User) []string {
+func (r *Room) NamesPrefix(prefix string) []string {
items := r.Members.ListPrefix(prefix)
// Sort results by recently active
users = append(users, item.Value().(*Member).User)
}
sort.Sort(message.RecentActiveUsers(users))
- for i, user := range users {
- if user.Name() == current_user.Name() {
- // move it to the end. one user in the list?
- save := users[0]
- copy(users[i:], users[i+1:])
- users[len(users)-1] = save
- }
- }
// Pull out names
names := make([]string, 0, len(items))
members[3].HandleMsg(message.NewMsg("hi")) // foo
members[1].HandleMsg(message.NewMsg("hi")) // aab
- if got, want := r.NamesPrefix("a", members[3].User), []string{"aab", "aaa", "aac"}; !reflect.DeepEqual(got, want) {
+ if got, want := r.NamesPrefix("a"), []string{"aab", "aaa", "aac"}; !reflect.DeepEqual(got, want) {
t.Errorf("got: %q; want: %q", got, want)
}
members[2].HandleMsg(message.NewMsg("hi")) // aac
- if got, want := r.NamesPrefix("a", members[3].User), []string{"aac", "aab", "aaa"}; !reflect.DeepEqual(got, want) {
+ if got, want := r.NamesPrefix("a"), []string{"aac", "aab", "aaa"}; !reflect.DeepEqual(got, want) {
t.Errorf("got: %q; want: %q", got, want)
}
- if got, want := r.NamesPrefix("f", members[0].User), []string{"foo"}; !reflect.DeepEqual(got, want) {
+ if got, want := r.NamesPrefix("f"), []string{"foo"}; !reflect.DeepEqual(got, want) {
t.Errorf("got: %q; want: %q", got, want)
}
- if got, want := r.NamesPrefix("bar", members[3].User), []string{}; !reflect.DeepEqual(got, want) {
+ if got, want := r.NamesPrefix("bar"), []string{}; !reflect.DeepEqual(got, want) {
t.Errorf("got: %q; want: %q", got, want)
}
}
h.listener.Serve()
}
-func (h *Host) completeName(partial string, current_user *message.User) string {
- names := h.NamesPrefix(partial, current_user)
+func (h *Host) completeName(partial string, skipName string) string {
+ names := h.NamesPrefix(partial)
if len(names) == 0 {
// Didn't find anything
return ""
+ } else if name := names[0]; name != skipName {
+ // First name is not the skipName, great
+ return name
+ } else if len(names) > 1 {
+ // Next candidate
+ return names[1]
}
-
- return names[len(names)-1]
+ return ""
}
func (h *Host) completeCommand(partial string) string {
}
} else {
// Name
- completed = h.completeName(partial, u)
+ completed = h.completeName(partial, u.Name())
if completed == "" {
return
}