Fixed mention results order bug.
Re-added fix for mentioning roles.
MODIFY
ui/window.go
ui/window.go
@@ -1036,9 +1036,10 @@ func (window *Window) PopulateMentionWindowFromCurrentGuild(mentionWindow *tview
}
}
+ roleMap := make(map[string]*discordgo.Role)
for _, role := range guild.Roles {
- role := role.Name
- memberNames = append(memberNames, role)
+ roleMap[role.Name] = role
+ memberNames = append(memberNames, role.Name)
}
searchResults := fuzzy.ScoreSearch(namePart, memberNames)
@@ -1046,30 +1047,34 @@ func (window *Window) PopulateMentionWindowFromCurrentGuild(mentionWindow *tview
userWithNickSet := make(map[string]struct{})
for _, result := range sortedResults {
- userOrNickName := result.Key
- userAndNickName := nameMap[userOrNickName]
- var userNodeText string
- var userName string
- if len(userAndNickName) > 0 {
- _, containsStr := userWithNickSet[userAndNickName]
+ // Check if result was a role.
+ if role, ok := roleMap[result.Key]; ok {
+ window.addNodeToMentionWindow(mentionWindow, role.Name, role)
+ continue
+ }
+
+ var displayName string = result.Key
+ var userMentionReference string = result.Key
+ if combinedUserAndNickName, ok := nameMap[result.Key]; ok {
// If the combined string has been added, skip this entry.
- if containsStr {
+ if _, containsStr := userWithNickSet[combinedUserAndNickName]; containsStr {
continue
}
- userWithNickSet[userAndNickName] = struct{}{}
- userNodeText = userAndNickName
- userName = nameMap[userAndNickName]
- } else {
- userNodeText = userOrNickName
- userName = userOrNickName
+ userWithNickSet[combinedUserAndNickName] = struct{}{}
+ displayName = combinedUserAndNickName
+ userMentionReference = nameMap[combinedUserAndNickName]
}
- userNode := tview.NewTreeNode(userNodeText)
- userNode.SetReference(userName)
- mentionWindow.GetRoot().AddChild(userNode)
+ window.addNodeToMentionWindow(mentionWindow, displayName, userMentionReference)
}
}
+func (window *Window) addNodeToMentionWindow(mentionWindow *tview.TreeView, name string, reference interface{}) {
+ userNode := tview.NewTreeNode(name)
+ userNode.SetReference(reference)
+ mentionWindow.GetRoot().AddChild(userNode)
+}
+
func (window *Window) PopulateMentionWindowFromCurrentChannel(mentionWindow *tview.TreeView, namePart string) {
var memberNames []string
for _, user := range window.selectedChannel.Recipients {

MODIFY
util/fuzzy/fuzzy.go
util/fuzzy/fuzzy.go
@@ -58,11 +58,11 @@ func Score(needle, haystack string) float64 {
lowerNeedle := strings.ToLower(needle)
lowerHaystack := strings.ToLower(haystack)
score := 0.0
- for i, j := 0, 0; i < needleLength; i, j = i+1, j+1 {
+ for i, j := 0, 0; i < needleLength && j < haystackLength; i, j = i+1, j+1 {
letter := lowerNeedle[i]
- letterIndex := strings.IndexByte(lowerHaystack[j:], letter)
- if letterIndex < 0 {
+ letterIndex := strings.IndexByte(lowerHaystack[j:], letter) + j
+ if (letterIndex - j) < 0 {
return -1
}

MODIFY
util/fuzzy/fuzzy_test.go
util/fuzzy/fuzzy_test.go
@@ -49,4 +49,11 @@ func TestFuzzyScore(t *testing.T) {
t.Errorf("Expected score to be > 0\n")
}
+ scoreA = Score("sout", "lexjusto#4214")
+ scoreB = Score("sout", "Southclaws#4153")
+
+ if scoreA >= scoreB {
+ t.Errorf("Unexpected score difference.\n")
+ }
+
}

Fixed mention results order bug.

-Re-added fix for mentioning roles.