| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- #pragma once
- #include <string>
- #include <vector>
- #include <functional>
- #include <unordered_map>
- #include <optional>
- #include <variant>
- namespace lumacs {
- // Forward declaration for EditorCore, as CommandSystem will interact with it
- class EditorCore;
- /// @brief Represents the result of a command execution.
- struct CommandResult {
- bool success;
- std::string message;
- // Potentially add more fields, like return value, error code, etc.
- };
- /// @brief Type for command functions.
- /// Commands take a vector of string arguments and return a CommandResult.
- using CommandFunction = std::function<CommandResult(const std::vector<std::string>&)>;
- /// @brief Represents a single command.
- struct Command {
- std::string name;
- CommandFunction function;
- std::string doc_string; // Documentation for the command
- bool interactive; // Whether the command can be called interactively (e.g., via M-x)
- std::string interactive_spec; // Emacs-like interactive spec for argument gathering
- Command(std::string name, CommandFunction func, std::string doc = "", bool interactive = false, std::string i_spec = "")
- : name(std::move(name)), function(std::move(func)), doc_string(std::move(doc)), interactive(interactive), interactive_spec(std::move(i_spec)) {}
- };
- /// @brief Manages the registration and execution of editor commands.
- class CommandSystem {
- public:
- explicit CommandSystem(EditorCore* core = nullptr);
- /// @brief Registers a command with the system.
- void register_command(const std::string& name, CommandFunction function,
- const std::string& doc_string = "", bool interactive = false);
- /// @brief Executes a command by name with given arguments.
- /// @param name The name of the command to execute.
- /// @param args Arguments for the command.
- /// @return CommandResult indicating success/failure and a message.
- CommandResult execute(const std::string& name, const std::vector<std::string>& args);
- /// @brief Get a list of all registered command names.
- std::vector<std::string> get_command_names() const;
- /// @brief Get documentation for a specific command.
- std::optional<std::string> get_command_doc_string(const std::string& name) const;
- private:
- EditorCore* core_; // Pointer to EditorCore for commands to interact with editor state
- std::unordered_map<std::string, Command> commands_;
- };
- } // namespace lumacs
|