[frameworks/ktexteditor] /: vi-mode: Add the Date command
Ismael Asensio <[email protected]>
| Newsgroups | gmane.comp.kde.cvs |
|---|---|
| Message-ID | <[email protected]> |
Git commit 47c79f6d9fb55ec02360862c1e3d053201e1c584 by Ismael Asensio.
Committed on 20/07/2026 at 19:20.
Pushed by cullmann into branch 'master'.
vi-mode: Add the Date command
Make the already existing `date` command available to the vi command mode.
This allows to insert the current date and time by simply typing `:date`
or `:date <format>`
While this is not a pure vim command, it is a very low hanging fruit for
a very useful feature, and the "original" method requires launching and
parsing the `date` shell command, which gets much more involved
M +8 -0 autotests/src/vimode/modes.cpp
M +1 -0 src/vimode/emulatedcommandbar/commandmode.cpp
https://invent.kde.org/frameworks/ktexteditor/-/commit/47c79f6d9fb55ec02360862c1e3d053201e1c584
diff --git a/autotests/src/vimode/modes.cpp b/autotests/src/vimode/modes.cpp
index c4b4ca3a3..2a56b270f 100644
--- a/autotests/src/vimode/modes.cpp
+++ b/autotests/src/vimode/modes.cpp
@@ -18,6 +18,7 @@
#include <QMainWindow>
#include <QTest>
+using namespace Qt::StringLiterals;
using namespace KTextEditor;
QTEST_MAIN(ModesTest)
@@ -1549,6 +1550,13 @@ void ModesTest::CommandTests()
DoTest("1\n2\n3\n4", "2j\\:.,.-1d\\", "1\n4");
DoTest("1\n2\n3\n4", "\\:.+200-100-100+20-5-5-5-5+.-.,$-1+1-2+2-3+3-4+4-5+5-6+6-7+7-1000+1000+0-0-$+$-.+.-1d\\", "4");
DoTest("1\n2\n3\n4", "majmbjmcjmdgg\\:'a+'b+'d-'c,.d\\", "");
+
+ // Test date command
+ const QDateTime now = QDateTime::currentDateTime();
+ // Remove minutes and seconds after inserting the default format
+ // to highly reduce the probability of time-based test failures
+ DoTest("", "\\:date\\2F:D", now.toString(u"yyyy-MM-dd hh"_s).toLatin1().data());
+ DoTest("", "\\:date dd.MM.yyyy\\", now.toString(u"dd.MM.yyyy"_s).toLatin1().data());
}
void ModesTest::CommandSedTests()
diff --git a/src/vimode/emulatedcommandbar/commandmode.cpp b/src/vimode/emulatedcommandbar/commandmode.cpp
index 595913f7d..b24985921 100644
--- a/src/vimode/emulatedcommandbar/commandmode.cpp
+++ b/src/vimode/emulatedcommandbar/commandmode.cpp
@@ -50,6 +50,7 @@ CommandMode::CommandMode(EmulatedCommandBar *emulatedCommandBar,
cmds.push_back(SedReplace::self());
cmds.push_back(BufferCommands::self());
cmds.push_back(KateCommands::EditingCommands::self());
+ cmds.push_back(KateCommands::Date::self());
for (KateCommandLineScript *cmd : KateScriptManager::self()->commandLineScripts()) {
cmds.push_back(cmd);