From e9f9d7367effafbae5cd292c09857b09fe6eb1e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mattias=20Lasersk=C3=B6ld?= Date: Fri, 26 Jul 2024 18:26:33 +0200 Subject: [PATCH] New way to register non core standard commands --- src/modes/parentmode.cpp | 3 ++- src/plugin/gitcommitinteraction.cpp | 32 ++++++++++++++++++++++------ src/script.cpp | 1 + src/script/browsefileinteraction.cpp | 7 ++++-- src/script/renamefileinteraction.cpp | 8 ++++--- src/script/renamefileinteraction.h | 6 ------ src/script/standardcommands.cpp | 22 ++++++++++--------- src/script/standardcommands.h | 6 ++++-- src/script/staticcommandregister.cpp | 7 ++++++ src/script/staticcommandregister.h | 17 +++++++++++++++ 10 files changed, 79 insertions(+), 30 deletions(-) delete mode 100644 src/script/renamefileinteraction.h create mode 100644 src/script/staticcommandregister.cpp create mode 100644 src/script/staticcommandregister.h diff --git a/src/modes/parentmode.cpp b/src/modes/parentmode.cpp index 3862d4ab..2f97e8f6 100644 --- a/src/modes/parentmode.cpp +++ b/src/modes/parentmode.cpp @@ -31,7 +31,8 @@ std::shared_ptr createParentMode() { sc.toggle_comment}, // {KeyEvent{Key::KeyCombination, 'O', Modifiers::Ctrl}, // {[](Ptr env) { env->mainWindow().showOpen(); }}}, - {KeyEvent{Key::KeyCombination, 'O', Modifiers::Ctrl}, sc.browse_files}, + {KeyEvent{Key::KeyCombination, 'O', Modifiers::Ctrl}, + sc.f("browse_files")}, {KeyEvent{Key::KeyCombination, 'S', Modifiers::Ctrl}, sc.combine(sc.format, sc.save)}, {KeyEvent{Key::KeyCombination, 'C', Modifiers::Ctrl}, sc.copy}, diff --git a/src/plugin/gitcommitinteraction.cpp b/src/plugin/gitcommitinteraction.cpp index 18bb105a..ba83851b 100644 --- a/src/plugin/gitcommitinteraction.cpp +++ b/src/plugin/gitcommitinteraction.cpp @@ -34,12 +34,14 @@ void handleGitCommitResponse(std::shared_ptr env, message += line + "\n"; } - auto files = std::vector{}; - + // Clear the cache so that only the files selected in the editor is selected + // by git if (runCommandAndCapture("git reset")) { return; } + auto files = std::vector{}; + // Parse files for (std::string line; std::getline(ss, line);) { if (line.starts_with("===")) { @@ -49,16 +51,32 @@ void handleGitCommitResponse(std::shared_ptr env, continue; } + auto isDelete = line.at(0) == 'D' || line.at(1) == 'D'; line = line.substr(3); if (line.empty()) { continue; } - if (runCommand(("git add \"" + line + "\"").c_str())) { - logError("Failed to add ", line, " to git"); - return; + if (isDelete) { + if (runCommand(("git rm --cached \"" + line + "\"").c_str())) { + logError("Failed to cache removed file ", line, " to git"); + return; + } + } + else { + if (runCommand(("git add \"" + line + "\"").c_str())) { + logError("Failed to add ", line, " to git"); + return; + } } + + files.push_back(line); + } + + if (files.empty()) { + env->statusMessage({"No files selected for git commit. Abort..."}); + return; } auto gitDir = localConfigDirectory("git", true); @@ -80,6 +98,8 @@ void handleGitCommitResponse(std::shared_ptr env, runCommandAndCapture("git commit --file \"" + messagePath.string() + "\" " + (isAmend ? "--amend" : "")); + + env->statusMessage({"files commited to git"}); } void beginGitCommitImpl(std::shared_ptr env, bool isGitAmend) { @@ -100,7 +120,7 @@ void beginGitCommitImpl(std::shared_ptr env, bool isGitAmend) { "\n"; ss << "# Press return when done\n\n"; ss << "==============\n\n"; - ss << "============== move files below, or remove this line\n"; + ss << "============== move files up, or remove this line to select files\n"; ss << stream.rdbuf(); auto interaction = Interaction{ diff --git a/src/script.cpp b/src/script.cpp index 9a31b97a..9e2fc91f 100644 --- a/src/script.cpp +++ b/src/script.cpp @@ -9,5 +9,6 @@ #include "script/saveinteraction.cpp" #include "script/simpleinteraction.cpp" #include "script/standardcommands.cpp" +#include "script/staticcommandregister.cpp" #include "script/togglecomments.cpp" #include "script/vimcommands.cpp" diff --git a/src/script/browsefileinteraction.cpp b/src/script/browsefileinteraction.cpp index a85fbbaa..561f7d60 100644 --- a/src/script/browsefileinteraction.cpp +++ b/src/script/browsefileinteraction.cpp @@ -2,6 +2,7 @@ #include "files/project.h" #include "script/ienvironment.h" #include "script/interaction.h" +#include "script/staticcommandregister.h" #include "syntax/palette.h" #include "text/formattype.h" #include "text/fstring.h" @@ -143,8 +144,10 @@ void internalBeginFileViewInteraction(std::shared_ptr env, handleFileViewResponse); } -} // namespace - void beginBrowseFileInteraction(std::shared_ptr env) { internalBeginFileViewInteraction(env, {}); } + +StaticCommandRegister browseReg{"browse_files", beginBrowseFileInteraction}; + +} // namespace diff --git a/src/script/renamefileinteraction.cpp b/src/script/renamefileinteraction.cpp index a92f4ab0..26bbcebb 100644 --- a/src/script/renamefileinteraction.cpp +++ b/src/script/renamefileinteraction.cpp @@ -1,9 +1,9 @@ -#include "renamefileinteraction.h" #include "core/coreenvironment.h" #include "core/debugoutput.h" #include "script/ienvironment.h" #include "script/interaction.h" #include "script/simpleinteraction.h" +#include "script/staticcommandregister.h" #include "text/fstring.h" #include "views/editor.h" #include "views/mainwindow.h" @@ -38,8 +38,6 @@ void handleUserResponse(std::shared_ptr env, } } -} // namespace - void beginRenameFileInteraction(std::shared_ptr env) { auto &e = env->editor(); auto file = e.file(); @@ -61,3 +59,7 @@ void beginRenameFileInteraction(std::shared_ptr env) { Interaction{si.serialize(), {3 + pathStr.size(), 2}, "rename file"}, handleUserResponse); } + +StaticCommandRegister renameReg{"rename_file", beginRenameFileInteraction}; + +} // namespace diff --git a/src/script/renamefileinteraction.h b/src/script/renamefileinteraction.h deleted file mode 100644 index fa4f26d2..00000000 --- a/src/script/renamefileinteraction.h +++ /dev/null @@ -1,6 +0,0 @@ -#pragma once - -#include "ienvironment.h" -#include - -void beginRenameFileInteraction(std::shared_ptr); diff --git a/src/script/standardcommands.cpp b/src/script/standardcommands.cpp index 8cf93823..eb2f6182 100644 --- a/src/script/standardcommands.cpp +++ b/src/script/standardcommands.cpp @@ -14,10 +14,8 @@ #include "plugin/rundebug.h" #include "renameinteraction.h" #include "saveinteraction.h" -#include "script/browsefileinteraction.h" #include "script/ienvironment.h" #include "script/indent.h" -#include "script/renamefileinteraction.h" #include "text/cursorops.h" #include "text/cursorrange.h" #include "text/cursorrangeops.h" @@ -502,9 +500,9 @@ StandardCommands create() { quitMedit(env->context()); }; - DEF(browse_files) { - beginBrowseFileInteraction(env); - }; + // DEF(browse_files) { + // beginBrowseFileInteraction(env); + // }; DEF(close_buffer) { auto &buffer = env->editor(); @@ -544,10 +542,6 @@ StandardCommands create() { env->editor().buffer(env->core().files().create()); }; - DEF(rename_file) { - beginRenameFileInteraction(env); - }; - DEF(select_inner_word) { auto &e = env->editor(); auto cursor = e.cursor(); @@ -660,7 +654,7 @@ StandardCommands create() { auto currentFileLocation = [](IEnvironment &env) { auto source = SourceLocation{}; source.path = env.editor().path(); - source.position = env.editor().cursor(); + source.position = env.editor().cursor().pos(); return source; }; @@ -681,3 +675,11 @@ StandardCommands &StandardCommands::get() { static auto sc = create(); return sc; } + +void StandardCommands::addCommand(std::string_view name, + std::function f, + void *ref) { + auto &fs = namedCommands[std::string(name)]; + fs.f = f; + fs.ref = ref; +} diff --git a/src/script/standardcommands.h b/src/script/standardcommands.h index aca190fe..5f5fc7da 100644 --- a/src/script/standardcommands.h +++ b/src/script/standardcommands.h @@ -52,7 +52,6 @@ STD_DEF(build) \ STD_DEF(run) \ STD_DEF(quit) \ - STD_DEF(browse_files) \ STD_DEF(close_buffer) \ STD_DEF(insert_mode) \ STD_DEF(normal_mode) \ @@ -68,7 +67,6 @@ STD_DEF(select_all) \ STD_DEF(command_palette) \ STD_DEF(new_file) \ - STD_DEF(rename_file) \ STD_DEF(indent) \ STD_DEF(deindent) \ STD_DEF(back) \ @@ -117,6 +115,10 @@ struct StandardCommands { /// A reference can be obtained from IEnvironment for plugins static StandardCommands &get(); + const std::function &f(const std::string &name) { + return namedCommands.at(name).f; + } + /// Combine several standard commands into one single function template static std::function combine(Args... args) { diff --git a/src/script/staticcommandregister.cpp b/src/script/staticcommandregister.cpp new file mode 100644 index 00000000..a8adee13 --- /dev/null +++ b/src/script/staticcommandregister.cpp @@ -0,0 +1,7 @@ +#include "staticcommandregister.h" +#include "standardcommands.h" + +StaticCommandRegister::StaticCommandRegister(std::string name, + FunctionType function) { + StandardCommands::get().addCommand(name, function, nullptr); +} diff --git a/src/script/staticcommandregister.h b/src/script/staticcommandregister.h new file mode 100644 index 00000000..5316be99 --- /dev/null +++ b/src/script/staticcommandregister.h @@ -0,0 +1,17 @@ +#pragma once + +#include "script/ienvironment.h" +#include +#include + +class StaticCommandRegister { +public: + using FunctionType = std::function)>; + + struct Entity { + std::string name; + FunctionType function; + }; + + StaticCommandRegister(std::string name, FunctionType function); +};