VSearch: fix tag search

"abc def" to search file with tag "abc" and "def".
This commit is contained in:
Le Tan 2018-08-22 20:08:10 +08:00
parent e1091e0d80
commit 4b0152c606
2 changed files with 48 additions and 10 deletions

View File

@ -505,20 +505,58 @@ VSearchResultItem *VSearch::searchForTag(const VFile *p_file) const
const VNoteFile *file = static_cast<const VNoteFile *>(p_file); const VNoteFile *file = static_cast<const VNoteFile *>(p_file);
const QStringList &tags = file->getTags(); const QStringList &tags = file->getTags();
for (auto const & tag: tags) {
VSearchToken &contentToken = m_config->m_contentToken;
bool singleToken = contentToken.tokenSize() == 1;
if (!singleToken) {
contentToken.startBatchMode();
}
VSearchResultItem *item = NULL;
bool allMatched = false;
for (int i = 0; i < tags.size(); ++i) {
const QString &tag = tags[i];
if (tag.isEmpty()) { if (tag.isEmpty()) {
continue; continue;
} }
if (matchNonContent(tag)) { bool matched = false;
return new VSearchResultItem(VSearchResultItem::Note, if (singleToken) {
matched = contentToken.matched(tag);
} else {
matched = contentToken.matchBatchMode(tag);
}
if (matched) {
if (!item) {
item = new VSearchResultItem(VSearchResultItem::Note,
VSearchResultItem::LineNumber, VSearchResultItem::LineNumber,
file->getName(), file->getName(),
file->fetchPath()); file->fetchPath());
} }
VSearchResultSubItem sitem(i, tag);
item->m_matches.append(sitem);
} }
return NULL; if (!singleToken && contentToken.readyToEndBatchMode(allMatched)) {
break;
}
}
if (!singleToken) {
contentToken.readyToEndBatchMode(allMatched);
contentToken.endBatchMode();
if (!allMatched && item) {
// This file does not meet all the tokens.
delete item;
item = NULL;
}
}
return item;
} }
VSearchResultItem *VSearch::searchForContent(const VFile *p_file) const VSearchResultItem *VSearch::searchForContent(const VFile *p_file) const

View File

@ -429,10 +429,10 @@ struct VSearchConfig
// Wildcard pattern to filter file. // Wildcard pattern to filter file.
QString m_pattern; QString m_pattern;
// Token for name, outline, and tag. // Token for name, outline.
VSearchToken m_token; VSearchToken m_token;
// Token for content. // Token for content and tag.
VSearchToken m_contentToken; VSearchToken m_contentToken;
}; };
@ -538,7 +538,7 @@ struct VSearchResult
{ {
friend class VSearch; friend class VSearch;
VSearchResult(VSearch *p_search) explicit VSearchResult(VSearch *p_search)
: m_state(VSearchState::Idle), : m_state(VSearchState::Idle),
m_search(p_search) m_search(p_search)
{ {