Skip to content

Commit

Permalink
More new command registering
Browse files Browse the repository at this point in the history
  • Loading branch information
mls-m5 committed Jul 26, 2024
1 parent 1179372 commit e108e73
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 33 deletions.
10 changes: 10 additions & 0 deletions src/core/profiler.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "profiler.h"
#include "files/config.h"
#include "script/staticcommandregister.h"
#include <chrono>
#include <cstdint>
#include <fstream>
Expand Down Expand Up @@ -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(); }},
}};

}
4 changes: 2 additions & 2 deletions src/modes/normalmode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ std::shared_ptr<IMode> 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},
Expand Down Expand Up @@ -77,7 +77,7 @@ std::shared_ptr<IMode> 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)},
Expand Down
6 changes: 3 additions & 3 deletions src/modes/parentmode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ std::shared_ptr<IMode> 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},
Expand All @@ -50,7 +50,7 @@ std::shared_ptr<IMode> 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},
Expand Down
42 changes: 21 additions & 21 deletions src/script/standardcommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down Expand Up @@ -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;) {
Expand Down
9 changes: 2 additions & 7 deletions src/script/standardcommands.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <string>
#include <unordered_map>

// 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) \
Expand Down Expand Up @@ -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) \
Expand All @@ -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) \
Expand Down
7 changes: 7 additions & 0 deletions src/script/staticcommandregister.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,10 @@ StaticCommandRegister::StaticCommandRegister(std::string name,
FunctionType function) {
StandardCommands::get().addCommand(name, function, nullptr);
}

StaticCommandRegister::StaticCommandRegister(std::vector<Entity> entities) {
for (auto &entity : entities) {
StandardCommands::get().addCommand(
entity.name, entity.function, nullptr);
}
}
1 change: 1 addition & 0 deletions src/script/staticcommandregister.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ class StaticCommandRegister {
};

StaticCommandRegister(std::string name, FunctionType function);
StaticCommandRegister(std::vector<Entity> entities);
};
15 changes: 15 additions & 0 deletions src/views/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -28,6 +29,20 @@
#include <memory>
#include <string_view>

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)
Expand Down

0 comments on commit e108e73

Please sign in to comment.