From 4b0152c606d70d6402e45976ed6365662ce533ca Mon Sep 17 00:00:00 2001 From: Le Tan Date: Wed, 22 Aug 2018 20:08:10 +0800 Subject: [PATCH] VSearch: fix tag search "abc def" to search file with tag "abc" and "def". --- src/vsearch.cpp | 52 +++++++++++++++++++++++++++++++++++++++------ src/vsearchconfig.h | 6 +++--- 2 files changed, 48 insertions(+), 10 deletions(-) diff --git a/src/vsearch.cpp b/src/vsearch.cpp index d7b719bc..c3d3519e 100644 --- a/src/vsearch.cpp +++ b/src/vsearch.cpp @@ -505,20 +505,58 @@ VSearchResultItem *VSearch::searchForTag(const VFile *p_file) const const VNoteFile *file = static_cast(p_file); 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()) { continue; } - if (matchNonContent(tag)) { - return new VSearchResultItem(VSearchResultItem::Note, - VSearchResultItem::LineNumber, - file->getName(), - file->fetchPath()); + bool matched = false; + if (singleToken) { + matched = contentToken.matched(tag); + } else { + matched = contentToken.matchBatchMode(tag); + } + + if (matched) { + if (!item) { + item = new VSearchResultItem(VSearchResultItem::Note, + VSearchResultItem::LineNumber, + file->getName(), + file->fetchPath()); + } + + VSearchResultSubItem sitem(i, tag); + item->m_matches.append(sitem); + } + + if (!singleToken && contentToken.readyToEndBatchMode(allMatched)) { + break; } } - return NULL; + 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 diff --git a/src/vsearchconfig.h b/src/vsearchconfig.h index 2afa3780..23cc0f19 100644 --- a/src/vsearchconfig.h +++ b/src/vsearchconfig.h @@ -429,10 +429,10 @@ struct VSearchConfig // Wildcard pattern to filter file. QString m_pattern; - // Token for name, outline, and tag. + // Token for name, outline. VSearchToken m_token; - // Token for content. + // Token for content and tag. VSearchToken m_contentToken; }; @@ -538,7 +538,7 @@ struct VSearchResult { friend class VSearch; - VSearchResult(VSearch *p_search) + explicit VSearchResult(VSearch *p_search) : m_state(VSearchState::Idle), m_search(p_search) {