Skip to content

Commit

Permalink
New way to register non core standard commands
Browse files Browse the repository at this point in the history
  • Loading branch information
mls-m5 committed Jul 26, 2024
1 parent 3c3d0ec commit e9f9d73
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 30 deletions.
3 changes: 2 additions & 1 deletion src/modes/parentmode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ std::shared_ptr<IMode> 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},
Expand Down
32 changes: 26 additions & 6 deletions src/plugin/gitcommitinteraction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ void handleGitCommitResponse(std::shared_ptr<IEnvironment> env,
message += line + "\n";
}

auto files = std::vector<std::string>{};

// 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<std::string>{};

// Parse files
for (std::string line; std::getline(ss, line);) {
if (line.starts_with("===")) {
Expand All @@ -49,16 +51,32 @@ void handleGitCommitResponse(std::shared_ptr<IEnvironment> 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);
Expand All @@ -80,6 +98,8 @@ void handleGitCommitResponse(std::shared_ptr<IEnvironment> env,

runCommandAndCapture("git commit --file \"" + messagePath.string() + "\" " +
(isAmend ? "--amend" : ""));

env->statusMessage({"files commited to git"});
}

void beginGitCommitImpl(std::shared_ptr<IEnvironment> env, bool isGitAmend) {
Expand All @@ -100,7 +120,7 @@ void beginGitCommitImpl(std::shared_ptr<IEnvironment> 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{
Expand Down
1 change: 1 addition & 0 deletions src/script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
7 changes: 5 additions & 2 deletions src/script/browsefileinteraction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -143,8 +144,10 @@ void internalBeginFileViewInteraction(std::shared_ptr<IEnvironment> env,
handleFileViewResponse);
}

} // namespace

void beginBrowseFileInteraction(std::shared_ptr<IEnvironment> env) {
internalBeginFileViewInteraction(env, {});
}

StaticCommandRegister browseReg{"browse_files", beginBrowseFileInteraction};

} // namespace
8 changes: 5 additions & 3 deletions src/script/renamefileinteraction.cpp
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -38,8 +38,6 @@ void handleUserResponse(std::shared_ptr<IEnvironment> env,
}
}

} // namespace

void beginRenameFileInteraction(std::shared_ptr<IEnvironment> env) {
auto &e = env->editor();
auto file = e.file();
Expand All @@ -61,3 +59,7 @@ void beginRenameFileInteraction(std::shared_ptr<IEnvironment> env) {
Interaction{si.serialize(), {3 + pathStr.size(), 2}, "rename file"},
handleUserResponse);
}

StaticCommandRegister renameReg{"rename_file", beginRenameFileInteraction};

} // namespace
6 changes: 0 additions & 6 deletions src/script/renamefileinteraction.h

This file was deleted.

22 changes: 12 additions & 10 deletions src/script/standardcommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
};

Expand All @@ -681,3 +675,11 @@ StandardCommands &StandardCommands::get() {
static auto sc = create();
return sc;
}

void StandardCommands::addCommand(std::string_view name,
std::function<void(EnvPtrT)> f,
void *ref) {
auto &fs = namedCommands[std::string(name)];
fs.f = f;
fs.ref = ref;
}
6 changes: 4 additions & 2 deletions src/script/standardcommands.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
Expand All @@ -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) \
Expand Down Expand Up @@ -117,6 +115,10 @@ struct StandardCommands {
/// A reference can be obtained from IEnvironment for plugins
static StandardCommands &get();

const std::function<void(EnvPtrT)> &f(const std::string &name) {
return namedCommands.at(name).f;
}

/// Combine several standard commands into one single function
template <typename... Args>
static std::function<void(EnvPtrT)> combine(Args... args) {
Expand Down
7 changes: 7 additions & 0 deletions src/script/staticcommandregister.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "staticcommandregister.h"
#include "standardcommands.h"

StaticCommandRegister::StaticCommandRegister(std::string name,
FunctionType function) {
StandardCommands::get().addCommand(name, function, nullptr);
}
17 changes: 17 additions & 0 deletions src/script/staticcommandregister.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include "script/ienvironment.h"
#include <functional>
#include <string>

class StaticCommandRegister {
public:
using FunctionType = std::function<void(std::shared_ptr<IEnvironment>)>;

struct Entity {
std::string name;
FunctionType function;
};

StaticCommandRegister(std::string name, FunctionType function);
};

0 comments on commit e9f9d73

Please sign in to comment.