From e108e73acfaf6dfa8298bb934402846d387fdab8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mattias=20Lasersk=C3=B6ld?= Date: Fri, 26 Jul 2024 18:53:52 +0200 Subject: [PATCH] More new command registering --- src/core/profiler.cpp | 10 +++++++ src/modes/normalmode.cpp | 4 +-- src/modes/parentmode.cpp | 6 ++-- src/script/standardcommands.cpp | 42 ++++++++++++++-------------- src/script/standardcommands.h | 9 ++---- src/script/staticcommandregister.cpp | 7 +++++ src/script/staticcommandregister.h | 1 + src/views/mainwindow.cpp | 15 ++++++++++ 8 files changed, 61 insertions(+), 33 deletions(-) diff --git a/src/core/profiler.cpp b/src/core/profiler.cpp index f8d0d9d..64acda5 100644 --- a/src/core/profiler.cpp +++ b/src/core/profiler.cpp @@ -1,5 +1,6 @@ #include "profiler.h" #include "files/config.h" +#include "script/staticcommandregister.h" #include #include #include @@ -233,3 +234,12 @@ void setProfilerThreadName(std::string name) { } localProfilingThreadData.data->name = std::move(name); } + +namespace { + +StaticCommandRegister profilerReg{{ + {"start_profiling", [](auto env) { enableProfiling(); }}, + {"stop_profiling", [](auto env) { disableProfiling(); }}, +}}; + +} diff --git a/src/modes/normalmode.cpp b/src/modes/normalmode.cpp index ee870f4..7017da7 100644 --- a/src/modes/normalmode.cpp +++ b/src/modes/normalmode.cpp @@ -36,7 +36,7 @@ std::shared_ptr createNormalMode() { {{"X"}, sc.erase}, {{Key::Delete}, sc.combine(sc.right, sc.erase)}, {{"x"}, sc.erase_after}, - {{Key::Escape}, {[](Ptr env) { env->mainWindow().escape(); }}}, + {{Key::Escape}, {sc.f("escape")}}, {{"\n"}, {sc.down}}, {{Key::Space}, {sc.right}}, {{"i"}, sc.insert_mode}, @@ -77,7 +77,7 @@ std::shared_ptr createNormalMode() { {{"dd"}, sc.delete_line}, {{"dw"}, sc.delete_word}, // Does not use normal w-motion on first - {{"cc"}, sc.combine(sc.clear_line, sc.copy, sc.copy_indentation)}, + {{"cc"}, sc.combine(sc.clear_line, sc.f("copy"), sc.copy_indentation)}, // {{"cw"}, sc.combine(sc.select_word, sc.erase, // sc.insert_mode)}, {{"ciw"}, sc.combine(sc.select_inner_word, sc.erase, sc.insert_mode)}, diff --git a/src/modes/parentmode.cpp b/src/modes/parentmode.cpp index 29af4c3..5fea63a 100644 --- a/src/modes/parentmode.cpp +++ b/src/modes/parentmode.cpp @@ -35,8 +35,8 @@ std::shared_ptr createParentMode() { sc.f("browse_files")}, {KeyEvent{Key::KeyCombination, 'S', Modifiers::Ctrl}, sc.combine(sc.format, sc.save)}, - {KeyEvent{Key::KeyCombination, 'C', Modifiers::Ctrl}, sc.copy}, - {KeyEvent{Key::KeyCombination, 'X', Modifiers::Ctrl}, sc.cut}, + {KeyEvent{Key::KeyCombination, 'C', Modifiers::Ctrl}, sc.f("copy")}, + {KeyEvent{Key::KeyCombination, 'X', Modifiers::Ctrl}, sc.f("cut")}, {KeyEvent{Key::KeyCombination, 'Z', Modifiers::Ctrl}, sc.undo}, {KeyEvent{Key::KeyCombination, 'Y', Modifiers::Ctrl}, sc.redo}, {KeyEvent{Key::KeyCombination, 'B', Modifiers::Ctrl}, @@ -50,7 +50,7 @@ std::shared_ptr createParentMode() { sc.f("rename_symbol")}, {KeyEvent{Key::KeyCombination, 'A', Modifiers::Ctrl}, sc.select_all}, {KeyEvent{Key::KeyCombination, 'P', Modifiers::Ctrl}, - sc.command_palette}, + sc.f("command_palette")}, {KeyEvent{Key::KeyCombination, 'N', Modifiers::Ctrl}, sc.new_file}, {KeyEvent{Key::Left, {}, Modifiers::Alt}, sc.back}, {KeyEvent{Key::Right, {}, Modifiers::Alt}, sc.forward}, diff --git a/src/script/standardcommands.cpp b/src/script/standardcommands.cpp index 1faae00..f9f28c1 100644 --- a/src/script/standardcommands.cpp +++ b/src/script/standardcommands.cpp @@ -410,12 +410,12 @@ StandardCommands create() { env->editor().mode(createNormalMode()); saveInteraction(env); }; - DEF(copy) { - env->mainWindow().copy(false); - }; - DEF(cut) { - env->mainWindow().copy(true); - }; + // DEF(copy) { + // env->mainWindow().copy(false); + // }; + // DEF(cut) { + // env->mainWindow().copy(true); + // }; DEF(copy_indentation) { auto &e = env->editor(); e.cursor(copyIndentation(e.cursor())); @@ -558,25 +558,25 @@ StandardCommands create() { e.selection(range); }; - DEF(command_palette) { - env->mainWindow().showCommandPalette(); - }; + // DEF(command_palette) { + // env->mainWindow().showCommandPalette(); + // }; - DEF(split_editor) { - env->mainWindow().splitEditor(); - }; + // DEF(split_editor) { + // env->mainWindow().splitEditor(); + // }; - DEF(close_editor) { - env->mainWindow().closeEditor(); - }; + // DEF(close_editor) { + // env->mainWindow().closeEditor(); + // }; - DEF(start_profiling) { - enableProfiling(); - }; + // DEF(start_profiling) { + // enableProfiling(); + // }; - DEF(stop_profiling) { - disableProfiling(); - }; + // DEF(stop_profiling) { + // disableProfiling(); + // }; DEF(back) { for (bool retry = true; retry;) { diff --git a/src/script/standardcommands.h b/src/script/standardcommands.h index 95902f3..9de0ce4 100644 --- a/src/script/standardcommands.h +++ b/src/script/standardcommands.h @@ -8,6 +8,8 @@ #include #include +// This list should contain only core commands, the rest is registered through +// StaticCommandRegister or some other method #define STANDARD_COMMAND_LIST \ STD_DEF(left) \ STD_DEF(right) \ @@ -42,8 +44,6 @@ STD_DEF(toggle_case) \ STD_DEF(format) \ STD_DEF(save) \ - STD_DEF(copy) \ - STD_DEF(cut) \ STD_DEF(copy_indentation) \ STD_DEF(close_brace) \ STD_DEF(undo) \ @@ -64,16 +64,11 @@ STD_DEF(select_inner_paren) \ STD_DEF(select_around_paren) \ STD_DEF(select_all) \ - STD_DEF(command_palette) \ STD_DEF(new_file) \ STD_DEF(indent) \ STD_DEF(deindent) \ STD_DEF(back) \ STD_DEF(forward) \ - STD_DEF(split_editor) \ - STD_DEF(close_editor) \ - STD_DEF(start_profiling) \ - STD_DEF(stop_profiling) \ STD_DEF(debug_run) \ STD_DEF(debug_pause) \ STD_DEF(debug_stop) \ diff --git a/src/script/staticcommandregister.cpp b/src/script/staticcommandregister.cpp index a8adee1..f756e65 100644 --- a/src/script/staticcommandregister.cpp +++ b/src/script/staticcommandregister.cpp @@ -5,3 +5,10 @@ StaticCommandRegister::StaticCommandRegister(std::string name, FunctionType function) { StandardCommands::get().addCommand(name, function, nullptr); } + +StaticCommandRegister::StaticCommandRegister(std::vector entities) { + for (auto &entity : entities) { + StandardCommands::get().addCommand( + entity.name, entity.function, nullptr); + } +} diff --git a/src/script/staticcommandregister.h b/src/script/staticcommandregister.h index 5316be9..524734b 100644 --- a/src/script/staticcommandregister.h +++ b/src/script/staticcommandregister.h @@ -14,4 +14,5 @@ class StaticCommandRegister { }; StaticCommandRegister(std::string name, FunctionType function); + StaticCommandRegister(std::vector entities); }; diff --git a/src/views/mainwindow.cpp b/src/views/mainwindow.cpp index b48313f..d457cf3 100644 --- a/src/views/mainwindow.cpp +++ b/src/views/mainwindow.cpp @@ -13,6 +13,7 @@ #include "navigation/inavigation.h" #include "screen/iscreen.h" #include "script/localenvironment.h" +#include "script/staticcommandregister.h" #include "syntax/basichighligting.h" #include "syntax/iformat.h" #include "syntax/palette.h" @@ -28,6 +29,20 @@ #include #include +namespace { + +StaticCommandRegister MainWindowReg{{ + {"command_palette", + [](auto &&env) { env->mainWindow().showCommandPalette(); }}, + {"split_editor", [](auto &&env) { env->mainWindow().splitEditor(); }}, + {"close_editor", [](auto &&env) { env->mainWindow().closeEditor(); }}, + {"cut", [](auto &&env) { env->mainWindow().copy(true); }}, + {"copy", [](auto &&env) { env->mainWindow().copy(false); }}, + {"escape", [](auto &&env) { env->mainWindow().escape(); }}, +}}; + +} + MainWindow::MainWindow(CoreEnvironment &core, IScreen &screen, ThreadContext &context)