1/*
2 SPDX-FileCopyrightText: 2008 Erlend Hamberg <ehamberg@gmail.com>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include <vimode/command.h>
8#include <vimode/keyparser.h>
9
10using namespace KateVi;
11
12Command::Command(const QString &pattern, bool (NormalViMode::*commandMethod)(), unsigned int flags)
13 : m_pattern(KeyParser::self()->encodeKeySequence(keys: pattern))
14 , m_flags(flags)
15 , m_ptr2commandMethod(commandMethod)
16{
17}
18
19Command::~Command() = default;
20
21bool Command::execute(NormalViMode *mode) const
22{
23 return (mode->*m_ptr2commandMethod)();
24}
25
26bool Command::matches(const QString &pattern) const
27{
28 if (!(m_flags & REGEX_PATTERN)) {
29 return m_pattern.startsWith(s: pattern);
30 } else {
31 // compile once, void costly isValid check, good enough to have set the pattern.
32 if (m_patternRegex.pattern().isEmpty()) {
33 m_patternRegex = QRegularExpression(m_pattern, QRegularExpression::UseUnicodePropertiesOption);
34 }
35 const auto match = m_patternRegex.match(subject: pattern, offset: 0, matchType: QRegularExpression::PartialPreferFirstMatch);
36 // Partial matching could lead to a complete match, in that case hasPartialMatch() will return false, and hasMatch() will return true
37 return match.hasPartialMatch() || match.hasMatch();
38 }
39}
40
41bool Command::matchesExact(const QString &pattern) const
42{
43 if (!(m_flags & REGEX_PATTERN)) {
44 return (m_pattern == pattern);
45 } else {
46 // compile once, void costly isValid check, good enough to have set the pattern.
47 if (m_patternAnchoredRegex.pattern().isEmpty()) {
48 m_patternAnchoredRegex = QRegularExpression(QRegularExpression::anchoredPattern(expression: m_pattern), QRegularExpression::UseUnicodePropertiesOption);
49 }
50 return m_patternAnchoredRegex.match(subject: pattern).hasMatch();
51 }
52}
53

source code of ktexteditor/src/vimode/command.cpp