瀏覽代碼

fix(lua): Fix Buffer usertype bindings and EditorCore properties

- Added missing Buffer usertype registration to LuaApi.
- Changed 'editor.buffer' property to return 'Buffer*' to fix sol2 nil error with non-copyable Buffer class.
- Fixed method name mismatches ('file_path', 'on_buffer_event').
- Added manual conversion for std::optional to sol::optional where needed.
- Added 'config' and 'message' to EditorCore bindings.
- Ensures LUA_DEFAULTS and init.lua can successfully access 'editor.buffer'.
Bernardo Magri 1 月之前
父節點
當前提交
8943d77678
共有 1 個文件被更改,包括 80 次插入0 次删除
  1. 80 0
      src/lua_api.cpp

+ 80 - 0
src/lua_api.cpp

@@ -586,6 +586,75 @@ void LuaApi::register_types() {
         {"y_or_n_p", MinibufferMode::y_or_n_p}
     });
 
+    // Buffer type
+    lua_.new_usertype<Buffer>("Buffer",
+        sol::no_constructor,
+        "name", &Buffer::name,
+        "set_name", &Buffer::set_name,
+        "filepath", [](const Buffer& b) -> sol::optional<std::string> {
+            auto path = b.file_path();
+            if (!path.has_value()) return sol::nullopt;
+            return path->string();
+        },
+        "line_count", &Buffer::line_count,
+        "is_modified", &Buffer::is_modified,
+        "line", [](Buffer& b, size_t idx) {
+            if (idx >= b.line_count()) return std::string("");
+            return b.line(idx);
+        },
+        "content", &Buffer::content,
+        "insert", &Buffer::insert,
+        "insert_newline", &Buffer::insert_newline,
+        "erase", &Buffer::erase,
+        "erase_char", &Buffer::erase_char,
+        "clear", &Buffer::clear,
+        "save", &Buffer::save,
+        
+        // Mark & Region
+        "set_mark", &Buffer::set_mark,
+        "deactivate_mark", &Buffer::deactivate_mark,
+        "has_mark", [](const Buffer& b) { return b.mark().has_value(); },
+        "mark", sol::property([](const Buffer& b) -> sol::optional<Position> {
+            auto m = b.mark();
+            if (m.has_value()) return *m;
+            return sol::nullopt;
+        }),
+        "get_region", [](Buffer& b, Position p) -> sol::optional<Range> {
+            auto r = b.get_region(p);
+            if (r.has_value()) return *r;
+            return sol::nullopt;
+        },
+        "get_text_in_range", &Buffer::get_text_in_range,
+        "get_all_text", &Buffer::content, // Alias
+        "replace", [](Buffer& b, Range r, const std::string& text) {
+            b.erase(r);
+            b.insert(r.start, text);
+        },
+
+        // Styling
+        "set_style", &Buffer::set_style,
+        "clear_styles", &Buffer::clear_styles,
+        "get_line_styles", &Buffer::get_line_styles,
+
+        // Events
+        "on_buffer_event", [](Buffer& b, sol::function callback) {
+            b.on_buffer_event([callback](const BufferEventData& data) {
+                try {
+                    callback(data);
+                } catch (const sol::error& e) {
+                    spdlog::error("Lua error in buffer event listener: {}", e.what());
+                }
+            });
+        },
+        
+        // Search
+        "find", [](Buffer& b, const std::string& query, sol::optional<Position> start_pos) -> sol::optional<Range> {
+            auto r = b.find(query, start_pos.value_or(Position{0,0}));
+            if (r.has_value()) return *r;
+            return sol::nullopt;
+        }
+    );
+
     // EditorCore type
     lua_.new_usertype<EditorCore>("EditorCore",
         sol::no_constructor,
@@ -637,13 +706,24 @@ void LuaApi::register_types() {
         // Buffer management
         "get_buffer_names", &EditorCore::get_buffer_names,
 
+        "buffer", sol::property([](EditorCore& e) -> Buffer* {
+            try {
+                return &e.buffer();
+            } catch (const std::exception& ex) {
+                spdlog::error("Exception in editor.buffer: {}", ex.what());
+                return nullptr;
+            }
+        }),
+
         "switch_buffer_in_window", &EditorCore::switch_buffer_in_window,
         "close_buffer", &EditorCore::close_buffer,
         "get_all_buffer_info", &EditorCore::get_all_buffer_info,
         // Theme management
         "theme_manager", sol::property([](EditorCore& e) -> ThemeManager& { return e.theme_manager(); }),
+        "config", sol::property([](EditorCore& e) -> Config& { return e.config(); }), // Added config property
         "set_theme", [](EditorCore& e, const std::string& theme_name) { e.theme_manager().set_active_theme(theme_name); }, // Corrected
         "active_theme", sol::property([](EditorCore& e) -> const Theme& { return *e.theme_manager().active_theme(); }), // Corrected
+        "message", &EditorCore::set_message, // Added message alias for set_message
 
         
         // Key binding (method on EditorCore)