|
|
@@ -39,11 +39,30 @@ void BufferManager::new_buffer(std::string name) {
|
|
|
}
|
|
|
|
|
|
bool BufferManager::load_file(const std::filesystem::path& path) {
|
|
|
- std::filesystem::path abs_path = std::filesystem::absolute(path);
|
|
|
+ std::error_code ec;
|
|
|
+ std::filesystem::path abs_path = std::filesystem::absolute(path, ec);
|
|
|
+ if (ec) {
|
|
|
+ spdlog::error("Failed to resolve absolute path: {}", ec.message());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ bool path_exists = std::filesystem::exists(abs_path, ec);
|
|
|
|
|
|
// Check if already open
|
|
|
for (const auto& buf : buffers_) {
|
|
|
- if (buf->file_path() && std::filesystem::equivalent(*buf->file_path(), abs_path)) {
|
|
|
+ if (!buf->file_path()) continue;
|
|
|
+ const auto& buf_path = *buf->file_path();
|
|
|
+
|
|
|
+ bool is_same = false;
|
|
|
+ // Only use equivalent if BOTH exist on disk to avoid exceptions/errors
|
|
|
+ if (path_exists && std::filesystem::exists(buf_path, ec)) {
|
|
|
+ is_same = std::filesystem::equivalent(buf_path, abs_path, ec);
|
|
|
+ } else {
|
|
|
+ // Fallback: string comparison (handles new files or one missing)
|
|
|
+ is_same = (buf_path == abs_path);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (is_same) {
|
|
|
if (window_manager_.active_window()) {
|
|
|
window_manager_.active_window()->set_buffer(buf);
|
|
|
}
|
|
|
@@ -54,16 +73,29 @@ bool BufferManager::load_file(const std::filesystem::path& path) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- auto new_buffer_opt = Buffer::from_file(abs_path);
|
|
|
- if (!new_buffer_opt) {
|
|
|
- return false;
|
|
|
+ // Not open, try to load or create
|
|
|
+ std::shared_ptr<Buffer> new_buffer_ptr;
|
|
|
+
|
|
|
+ if (path_exists) {
|
|
|
+ auto buf_opt = Buffer::from_file(abs_path);
|
|
|
+ if (buf_opt) {
|
|
|
+ new_buffer_ptr = std::make_shared<Buffer>(std::move(*buf_opt));
|
|
|
+ } else {
|
|
|
+ // File exists but load failed (e.g. permissions)
|
|
|
+ spdlog::error("Failed to load file: {}", abs_path.string());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // File does not exist -> Create new empty buffer for it
|
|
|
+ new_buffer_ptr = std::make_shared<Buffer>(abs_path.filename().string());
|
|
|
+ new_buffer_ptr->set_file_path(abs_path);
|
|
|
+ spdlog::info("New file buffer created: {}", abs_path.string());
|
|
|
}
|
|
|
-
|
|
|
- auto new_buffer = std::make_shared<Buffer>(std::move(*new_buffer_opt));
|
|
|
- buffers_.push_back(new_buffer);
|
|
|
+
|
|
|
+ buffers_.push_back(new_buffer_ptr);
|
|
|
|
|
|
if (window_manager_.active_window()) {
|
|
|
- window_manager_.active_window()->set_buffer(new_buffer);
|
|
|
+ window_manager_.active_window()->set_buffer(new_buffer_ptr);
|
|
|
}
|
|
|
|
|
|
notifier_.emit_event(EditorEvent::BufferModified);
|