Changeset View
Changeset View
Standalone View
Standalone View
kdevplatform/language/interfaces/quickopenfilter.h
Show First 20 Lines • Show All 188 Lines • ▼ Show 20 Line(s) | 187 | } else if (m_oldFilterText.mid(0, m_oldFilterText.count() - 1) == text.mid(0, text.count() - 1) | |||
---|---|---|---|---|---|
189 | //Good, the prefix is the same, and the last item has been extended | 189 | //Good, the prefix is the same, and the last item has been extended | ||
190 | } else if (m_oldFilterText.size() == text.size() - 1 && m_oldFilterText == text.mid(0, text.size() - 1)) { | 190 | } else if (m_oldFilterText.size() == text.size() - 1 && m_oldFilterText == text.mid(0, text.size() - 1)) { | ||
191 | //Good, an item has been added | 191 | //Good, an item has been added | ||
192 | } else { | 192 | } else { | ||
193 | //Start filtering based on the whole data, there was a big change to the filter | 193 | //Start filtering based on the whole data, there was a big change to the filter | ||
194 | filterBase = m_items; | 194 | filterBase = m_items; | ||
195 | } | 195 | } | ||
196 | 196 | | |||
197 | // filterBase is correctly sorted, to keep it that way we add | 197 | QVector<QPair<int, int>> matches; | ||
198 | // exact matches to this list in sorted way and then prepend the whole list in one go. | 198 | for (int i = 0, c = filterBase.size(); i < c; ++i) { | ||
199 | QVector<Item> exactMatches; | 199 | const auto& data = filterBase.at(i); | ||
200 | // similar for starting matches | | |||
201 | QVector<Item> startMatches; | | |||
202 | // all other matches are sorted by where they match, we prefer matches at the end | | |||
203 | QVector<Item> otherMatches; | | |||
204 | foreach( const Item& data, filterBase ) { | | |||
205 | const auto matchQuality = matchPathFilter(static_cast<Parent*>(this)->itemPath(data), text); | 200 | const auto matchQuality = matchPathFilter(static_cast<Parent*>(this)->itemPath(data), text); | ||
206 | switch (matchQuality) { | 201 | if (matchQuality == -1) { | ||
207 | case PathFilterMatchQuality::NoMatch: | 202 | continue; | ||
208 | break; | | |||
209 | case PathFilterMatchQuality::ExactMatch: | | |||
210 | exactMatches << data; | | |||
211 | break; | | |||
212 | case PathFilterMatchQuality::StartMatch: | | |||
213 | startMatches << data; | | |||
214 | break; | | |||
215 | case PathFilterMatchQuality::OtherMatch: | | |||
216 | otherMatches << data; | | |||
217 | break; | | |||
218 | } | 203 | } | ||
204 | matches.push_back({matchQuality, i}); | ||||
219 | } | 205 | } | ||
220 | 206 | std::sort(matches.begin(), matches.end(), | |||
221 | m_filtered = exactMatches + startMatches + otherMatches; | 207 | [](const QPair<int, int>& lhs, const QPair<int, int>& rhs) | ||
208 | { | ||||
209 | return lhs.first < rhs.first; | ||||
210 | }); | ||||
211 | m_filtered.resize(matches.size()); | ||||
212 | std::transform(matches.begin(), matches.end(), m_filtered.begin(), | ||||
213 | [&filterBase](const QPair<int, int>& match) { | ||||
214 | return filterBase.at(match.second); | ||||
215 | }); | ||||
222 | m_oldFilterText = text; | 216 | m_oldFilterText = text; | ||
223 | } | 217 | } | ||
224 | 218 | | |||
225 | private: | 219 | private: | ||
226 | QStringList m_oldFilterText; | 220 | QStringList m_oldFilterText; | ||
227 | QVector<Item> m_filtered; | 221 | QVector<Item> m_filtered; | ||
228 | QVector<Item> m_items; | 222 | QVector<Item> m_items; | ||
229 | }; | 223 | }; | ||
230 | 224 | | |||
231 | } | 225 | } | ||
232 | 226 | | |||
233 | #endif | 227 | #endif |