|
|
@@ -13,16 +13,43 @@ namespace lumacs {
|
|
|
// Helper class to adapt a lambda/function to ICompletionSource
|
|
|
class LambdaCompletionSource : public ICompletionSource {
|
|
|
public:
|
|
|
- using ProviderFunc = std::function<std::vector<CompletionCandidate>(const std::string&)>;
|
|
|
-
|
|
|
- explicit LambdaCompletionSource(ProviderFunc provider) : provider_(std::move(provider)) {}
|
|
|
+ explicit LambdaCompletionSource(sol::function provider) : provider_(std::move(provider)) {}
|
|
|
|
|
|
std::vector<CompletionCandidate> get_candidates(const std::string& input) override {
|
|
|
- return provider_(input);
|
|
|
+ sol::protected_function_result lua_result = provider_(input); // Get the raw Lua result
|
|
|
+ std::vector<CompletionCandidate> candidates;
|
|
|
+
|
|
|
+ if (lua_result.valid()) {
|
|
|
+ sol::type result_type = lua_result.get_type();
|
|
|
+ if (result_type == sol::type::table) {
|
|
|
+ sol::table result_table = lua_result; // Convert to sol::table
|
|
|
+ for (auto& pair : result_table) {
|
|
|
+ if (pair.second.get_type() == sol::type::table) {
|
|
|
+ sol::table candidate_table = pair.second;
|
|
|
+ std::string text = "";
|
|
|
+ int score = 50;
|
|
|
+ std::string desc = "";
|
|
|
+
|
|
|
+ if (candidate_table["text"].valid()) {
|
|
|
+ text = candidate_table["text"];
|
|
|
+ }
|
|
|
+ if (candidate_table["score"].valid()) {
|
|
|
+ score = candidate_table["score"];
|
|
|
+ }
|
|
|
+ if (candidate_table["description"].valid()) {
|
|
|
+ desc = candidate_table["description"].get<std::string>(); // Get the string
|
|
|
+ }
|
|
|
+
|
|
|
+ candidates.emplace_back(text, score, "", desc); // Fix: Explicitly pass empty string for display_text
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return candidates;
|
|
|
}
|
|
|
|
|
|
private:
|
|
|
- ProviderFunc provider_;
|
|
|
+ sol::function provider_;
|
|
|
};
|
|
|
|
|
|
LuaApi::LuaApi() {
|
|
|
@@ -170,6 +197,39 @@ bool LuaApi::execute_key_binding(const std::string& key) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+// Manual C functions implementations
|
|
|
+int LuaApi::get_active_buffer_line_count_lua() {
|
|
|
+ if (!core_ || !core_->active_window() || !core_->active_window()->buffer_ptr()) {
|
|
|
+ std::cerr << "[DEBUG_MANUAL] get_active_buffer_line_count_lua: Core, active window or buffer is null!" << std::endl;
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ return static_cast<int>(core_->buffer().line_count());
|
|
|
+}
|
|
|
+
|
|
|
+void LuaApi::lua_editor_move_right() {
|
|
|
+ if (!core_) {
|
|
|
+ std::cerr << "[DEBUG_MANUAL] lua_editor_move_right: Core is null!" << std::endl;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ core_->move_right();
|
|
|
+}
|
|
|
+
|
|
|
+void LuaApi::lua_config_set_string(const std::string& key, const std::string& value) {
|
|
|
+ if (!core_) {
|
|
|
+ std::cerr << "[DEBUG_MANUAL] lua_config_set_string: Core is null!" << std::endl;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ core_->config().set(key, value);
|
|
|
+}
|
|
|
+
|
|
|
+std::string LuaApi::lua_config_get_string(const std::string& key, const std::string& default_val) {
|
|
|
+ if (!core_) {
|
|
|
+ std::cerr << "[DEBUG_MANUAL] lua_config_get_string: Core is null!" << std::endl;
|
|
|
+ return default_val;
|
|
|
+ }
|
|
|
+ return core_->config().get<std::string>(key, default_val);
|
|
|
+}
|
|
|
+
|
|
|
void LuaApi::setup_api() {
|
|
|
register_types();
|
|
|
register_functions();
|
|
|
@@ -394,6 +454,8 @@ void LuaApi::register_types() {
|
|
|
"get_text_in_range", &Buffer::get_text_in_range
|
|
|
);
|
|
|
|
|
|
+
|
|
|
+
|
|
|
// Color type
|
|
|
lua_.new_usertype<Color>("Color",
|
|
|
sol::constructors<Color(), Color(int, int, int)>(),
|
|
|
@@ -726,42 +788,10 @@ void LuaApi::register_functions() {
|
|
|
return result;
|
|
|
};
|
|
|
|
|
|
- lua_["register_completion_provider"] = [this](int mode_int, sol::function provider_func) { // Added 'this' back to capture
|
|
|
+ lua_["register_completion_provider"] = [this](int mode_int, sol::function provider_func) {
|
|
|
MinibufferMode mode = static_cast<MinibufferMode>(mode_int);
|
|
|
- auto provider = [provider_func](const std::string& input) -> std::vector<CompletionCandidate> { // Removed 'this' from capture
|
|
|
- try {
|
|
|
- auto result = provider_func(input);
|
|
|
- std::vector<CompletionCandidate> candidates;
|
|
|
- if (result.valid() && result.get_type() == sol::type::table) {
|
|
|
- sol::table result_table = result;
|
|
|
- for (auto& pair : result_table) {
|
|
|
- if (pair.second.get_type() == sol::type::table) {
|
|
|
- sol::table candidate = pair.second;
|
|
|
- std::string text = "";
|
|
|
- int score = 50;
|
|
|
- std::string desc = "";
|
|
|
-
|
|
|
- if (candidate["text"].valid()) {
|
|
|
- text = candidate["text"];
|
|
|
- }
|
|
|
- if (candidate["score"].valid()) {
|
|
|
- score = candidate["score"];
|
|
|
- }
|
|
|
- if (candidate["description"].valid()) {
|
|
|
- desc = candidate["description"];
|
|
|
- }
|
|
|
-
|
|
|
- candidates.emplace_back(text, score, desc);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- return candidates;
|
|
|
- } catch (const sol::error& e) {
|
|
|
- std::cerr << "Lua error in completion provider: " << e.what() << std::endl;
|
|
|
- return {};
|
|
|
- }
|
|
|
- };
|
|
|
- (*core_).completion_system().register_source(mode, std::make_unique<LambdaCompletionSource>(provider));
|
|
|
+ // Pass the sol::function directly to the LambdaCompletionSource constructor
|
|
|
+ (*core_).completion_system().register_source(mode, std::make_unique<LambdaCompletionSource>(provider_func));
|
|
|
};
|
|
|
|
|
|
// Plugin Manager functions
|