@@ -69,51 +69,103 @@ public class Win32 : IProgram, IEquatable<Win32>
69
69
Enabled = false
70
70
} ;
71
71
72
+ private static MatchResult Match ( string query , List < string > candidates )
73
+ {
74
+ if ( candidates . Count == 0 )
75
+ return null ;
76
+
77
+ List < MatchResult > matches = new List < MatchResult > ( ) ;
78
+ foreach ( var candidate in candidates )
79
+ {
80
+ var match = StringMatcher . FuzzySearch ( query , candidate ) ;
81
+ if ( match . IsSearchPrecisionScoreMet ( ) )
82
+ {
83
+ matches . Add ( match ) ;
84
+ }
85
+ }
86
+ if ( matches . Count == 0 )
87
+ {
88
+ return null ;
89
+ }
90
+ else
91
+ {
92
+ return matches . MaxBy ( match => match . Score ) ;
93
+ }
94
+ }
72
95
73
96
public Result Result ( string query , IPublicAPI api )
74
97
{
75
98
string title ;
76
99
MatchResult matchResult ;
77
100
78
101
// Name of the result
79
- string resultName = string . IsNullOrEmpty ( LocalizedName ) ? Name : LocalizedName ;
102
+ // Check equality to avoid matching again in candidates
103
+ bool useLocalizedName = ! string . IsNullOrEmpty ( LocalizedName ) && ! Name . Equals ( LocalizedName ) ;
104
+ string resultName = useLocalizedName ? LocalizedName : Name ;
80
105
81
- // We suppose Name won't be null
82
- if ( ! Main . _settings . EnableDescription || Description == null || resultName . StartsWith ( Description ) )
106
+ if ( ! Main . _settings . EnableDescription )
83
107
{
84
108
title = resultName ;
85
- matchResult = StringMatcher . FuzzySearch ( query , title ) ;
86
- }
87
- else if ( Description . StartsWith ( resultName ) )
88
- {
89
- title = Description ;
90
- matchResult = StringMatcher . FuzzySearch ( query , Description ) ;
109
+ matchResult = StringMatcher . FuzzySearch ( query , resultName ) ;
91
110
}
92
111
else
93
112
{
94
- title = $ "{ resultName } : { Description } ";
95
- var nameMatch = StringMatcher . FuzzySearch ( query , resultName ) ;
96
- var desciptionMatch = StringMatcher . FuzzySearch ( query , Description ) ;
97
- if ( desciptionMatch . Score > nameMatch . Score )
113
+ if ( string . IsNullOrEmpty ( Description ) || resultName . StartsWith ( Description ) )
98
114
{
99
- for ( int i = 0 ; i < desciptionMatch . MatchData . Count ; i ++ )
115
+ // Description is invalid or included in resultName
116
+ // Description is always localized, so Name.StartsWith(Description) is generally useless
117
+ title = resultName ;
118
+ matchResult = StringMatcher . FuzzySearch ( query , resultName ) ;
119
+ }
120
+ else if ( Description . StartsWith ( resultName ) )
121
+ {
122
+ // resultName included in Description
123
+ title = Description ;
124
+ matchResult = StringMatcher . FuzzySearch ( query , Description ) ;
125
+ }
126
+ else
127
+ {
128
+ // Search in both
129
+ title = $ "{ resultName } : { Description } ";
130
+ var nameMatch = StringMatcher . FuzzySearch ( query , resultName ) ;
131
+ var descriptionMatch = StringMatcher . FuzzySearch ( query , Description ) ;
132
+ if ( descriptionMatch . Score > nameMatch . Score )
133
+ {
134
+ for ( int i = 0 ; i < descriptionMatch . MatchData . Count ; i ++ )
135
+ {
136
+ descriptionMatch . MatchData [ i ] += resultName . Length + 2 ; // 2 is ": "
137
+ }
138
+ matchResult = descriptionMatch ;
139
+ }
140
+ else
100
141
{
101
- desciptionMatch . MatchData [ i ] += resultName . Length + 2 ; // 2 is ": "
142
+ matchResult = nameMatch ;
102
143
}
103
- matchResult = desciptionMatch ;
104
144
}
105
- else matchResult = nameMatch ;
106
145
}
107
146
147
+ List < string > candidates = new List < string > ( ) ;
148
+
108
149
if ( ! matchResult . IsSearchPrecisionScoreMet ( ) )
109
150
{
110
151
if ( ExecutableName != null ) // only lnk program will need this one
111
- matchResult = StringMatcher . FuzzySearch ( query , ExecutableName ) ;
112
-
113
- if ( ! matchResult . IsSearchPrecisionScoreMet ( ) )
152
+ {
153
+ candidates . Add ( ExecutableName ) ;
154
+ }
155
+ if ( useLocalizedName )
156
+ {
157
+ candidates . Add ( Name ) ;
158
+ }
159
+ matchResult = Match ( query , candidates ) ;
160
+ if ( matchResult == null )
161
+ {
114
162
return null ;
115
-
116
- matchResult . MatchData = new List < int > ( ) ;
163
+ }
164
+ else
165
+ {
166
+ // Nothing to highlight in title in this case
167
+ matchResult . MatchData . Clear ( ) ;
168
+ }
117
169
}
118
170
119
171
string subtitle = string . Empty ;
0 commit comments