1
/* This file is part of the Baloo query parser
2
Copyright (c) 2013 Denis Steckelmacher <steckdenis@yahoo.fr>
4
This library is free software; you can redistribute it and/or
5
modify it under the terms of the GNU Library General Public
6
License version 2.1 as published by the Free Software Foundation,
9
This library is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
Library General Public License for more details.
14
You should have received a copy of the GNU Library General Public License
15
along with this library; see the file COPYING.LIB. If not, write to
16
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17
Boston, MA 02110-1301, USA.
20
#ifndef __PATTERNMATCHER_H__
21
#define __PATTERNMATCHER_H__
23
#include "completionproposal.h"
28
#include <QtCore/QStringList>
30
namespace Baloo { class QueryParser; }
35
PatternMatcher(Baloo::QueryParser *parser,
36
QList<Baloo::Term> &terms,
38
const QStringList &pattern,
39
Baloo::CompletionProposal::Type completion_type,
40
const KLocalizedString &completion_description);
43
void runPass(const T &pass)
45
QList<Baloo::Term> matched_terms;
47
for (int i=0; i<capture_count; ++i) {
48
matched_terms.append(Baloo::Term());
51
// Try to start to match the pattern at every position in the term list
52
for (int index=0; index<terms.count(); ++index) {
55
int matched_length = matchPattern(index, matched_terms, start_position, end_position);
57
if (matched_length > 0) {
58
// The pattern matched, run the pass on the matching terms
59
QList<Baloo::Term> replacement = pass.run(matched_terms);
61
if (replacement.count() > 0) {
62
// Replace terms first_match_index..i with replacement
63
for (int i=0; i<matched_length; ++i) {
64
terms.removeAt(index);
67
for (int i=replacement.count()-1; i>=0; --i) {
68
terms.insert(index, replacement.at(i));
71
// If the pass returned only one replacement term, set
72
// its position. If more terms are returned, the pass
73
// must handle positions itself
74
if (replacement.count() == 1) {
75
setTermRange(terms[index], start_position, end_position);
78
// Re-explore the terms vector as indexes have changed
82
// If the pattern contains "%%", terms are appended to the end
83
// of matched_terms. Remove them now that they are not needed anymore
84
while (matched_terms.count() > capture_count) {
85
matched_terms.removeLast();
92
int captureCount() const;
93
int matchPattern(int first_term_index,
94
QList<Baloo::Term> &matched_terms,
96
int &end_position) const;
97
bool matchTerm(const Baloo::Term &term, const QString &pattern, int &capture_index) const;
98
void addCompletionProposal(int first_pattern_index_not_matching,
99
int first_term_index_matching,
100
int first_term_index_not_matching) const;
103
Baloo::QueryParser *parser;
104
QList<Baloo::Term> &terms;
107
Baloo::CompletionProposal::Type completion_type;
108
KLocalizedString completion_description;
b'\\ No newline at end of file'