KTextEditor

command.cpp
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(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(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(pattern, 0, 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(m_pattern), QRegularExpression::UseUnicodePropertiesOption);
49 }
50 return m_patternAnchoredRegex.match(pattern).hasMatch();
51 }
52}
for encoding keypresses w/ modifiers into an internal QChar representation and back again to a descri...
Definition keyparser.h:28
Commands for the vi normal mode.
KCOREADDONS_EXPORT Result match(QStringView pattern, QStringView str)
QString anchoredPattern(QStringView expression)
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 24 2025 11:55:25 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.