| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- #pragma once
- #include <string>
- #include <functional>
- #include <memory>
- #include <vector>
- #include <unordered_map>
- #include <filesystem>
- namespace lumacs {
- class EditorCore; // Forward declaration
- /// Result of command execution
- struct CommandResult {
- bool success;
- std::string message;
-
- CommandResult(bool s = true, std::string msg = "")
- : success(s), message(std::move(msg)) {}
- };
- /// Command function type
- using CommandFunction = std::function<CommandResult(const std::vector<std::string>&)>;
- /// Command metadata
- struct Command {
- std::string name;
- std::string description;
- CommandFunction function;
- std::vector<std::string> aliases;
- bool interactive; // Whether this command should be available via M-x
-
- Command(std::string n, std::string desc, CommandFunction func,
- std::vector<std::string> alias = {}, bool inter = true)
- : name(std::move(n)), description(std::move(desc)),
- function(std::move(func)), aliases(std::move(alias)), interactive(inter) {}
- };
- /// Completion candidate with ranking
- struct CompletionCandidate {
- std::string text;
- int score;
- std::string description;
-
- CompletionCandidate(std::string t, int s = 0, std::string desc = "")
- : text(std::move(t)), score(s), description(std::move(desc)) {}
-
- bool operator<(const CompletionCandidate& other) const {
- if (score != other.score) return score > other.score; // Higher score first
- return text < other.text; // Alphabetical for same score
- }
- };
- /// Completion provider function type
- using CompletionProvider = std::function<std::vector<CompletionCandidate>(const std::string& input)>;
- /// File system aware completion provider
- class FileSystemCompletionProvider {
- public:
- std::vector<CompletionCandidate> complete_path(const std::string& input) const;
- std::vector<CompletionCandidate> complete_directory(const std::string& input) const;
- std::vector<CompletionCandidate> complete_file(const std::string& input, const std::vector<std::string>& extensions = {}) const;
-
- private:
- bool is_absolute_path(const std::string& path) const;
- std::string normalize_path(const std::string& path) const;
- int calculate_path_score(const std::string& candidate, const std::string& input) const;
- };
- /// Command system for Lumacs editor
- class CommandSystem {
- public:
- explicit CommandSystem(EditorCore& core);
- ~CommandSystem() = default;
- // Command registration
- void register_command(std::unique_ptr<Command> command);
- void register_command(const std::string& name, const std::string& description,
- CommandFunction function, const std::vector<std::string>& aliases = {},
- bool interactive = true);
-
- // Command execution
- CommandResult execute(const std::string& command_name, const std::vector<std::string>& args = {});
- CommandResult execute_string(const std::string& command_string);
-
- // Command lookup
- bool has_command(const std::string& name) const;
- std::shared_ptr<Command> get_command(const std::string& name) const;
- std::vector<std::string> get_all_command_names() const;
- std::vector<std::string> get_interactive_command_names() const;
-
- // Completion system
- std::vector<CompletionCandidate> complete_command(const std::string& input) const;
- std::vector<CompletionCandidate> complete_buffer_name(const std::string& input) const;
- std::vector<CompletionCandidate> complete_file_path(const std::string& input) const;
- std::vector<CompletionCandidate> complete_theme_name(const std::string& input) const;
-
- // Register completion providers for custom commands
- void register_completion_provider(const std::string& command_name, CompletionProvider provider);
- std::vector<CompletionCandidate> get_completions(const std::string& command_name, const std::string& input) const;
-
- // Fuzzy matching utilities
- static int fuzzy_match_score(const std::string& candidate, const std::string& input);
- static bool fuzzy_match(const std::string& candidate, const std::string& input);
-
- private:
- EditorCore& core_;
- std::unordered_map<std::string, std::shared_ptr<Command>> commands_;
- std::unordered_map<std::string, CompletionProvider> completion_providers_;
- FileSystemCompletionProvider fs_provider_;
-
- void register_builtin_commands();
- std::vector<std::string> parse_command_string(const std::string& command_string) const;
- };
- } // namespace lumacs
|