File indexing completed on 2024-04-21 03:58:06
0001 /* 0002 SPDX-FileCopyrightText: 2008 Erlend Hamberg <ehamberg@gmail.com> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include <vimode/command.h> 0008 #include <vimode/keyparser.h> 0009 0010 using namespace KateVi; 0011 0012 Command::Command(const QString &pattern, bool (NormalViMode::*commandMethod)(), unsigned int flags) 0013 : m_pattern(KeyParser::self()->encodeKeySequence(pattern)) 0014 , m_flags(flags) 0015 , m_ptr2commandMethod(commandMethod) 0016 { 0017 } 0018 0019 Command::~Command() = default; 0020 0021 bool Command::execute(NormalViMode *mode) const 0022 { 0023 return (mode->*m_ptr2commandMethod)(); 0024 } 0025 0026 bool Command::matches(const QString &pattern) const 0027 { 0028 if (!(m_flags & REGEX_PATTERN)) { 0029 return m_pattern.startsWith(pattern); 0030 } else { 0031 // compile once, void costly isValid check, good enough to have set the pattern. 0032 if (m_patternRegex.pattern().isEmpty()) { 0033 m_patternRegex = QRegularExpression(m_pattern, QRegularExpression::UseUnicodePropertiesOption); 0034 } 0035 const auto match = m_patternRegex.match(pattern, 0, QRegularExpression::PartialPreferFirstMatch); 0036 // Partial matching could lead to a complete match, in that case hasPartialMatch() will return false, and hasMatch() will return true 0037 return match.hasPartialMatch() || match.hasMatch(); 0038 } 0039 } 0040 0041 bool Command::matchesExact(const QString &pattern) const 0042 { 0043 if (!(m_flags & REGEX_PATTERN)) { 0044 return (m_pattern == pattern); 0045 } else { 0046 // compile once, void costly isValid check, good enough to have set the pattern. 0047 if (m_patternAnchoredRegex.pattern().isEmpty()) { 0048 m_patternAnchoredRegex = QRegularExpression(QRegularExpression::anchoredPattern(m_pattern), QRegularExpression::UseUnicodePropertiesOption); 0049 } 0050 return m_patternAnchoredRegex.match(pattern).hasMatch(); 0051 } 0052 }