Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
251 changes: 119 additions & 132 deletions include/bolt/collaboration/collaboration_protocol.hpp

Large diffs are not rendered by default.

233 changes: 118 additions & 115 deletions include/bolt/collaboration/collaborative_session.hpp

Large diffs are not rendered by default.

161 changes: 78 additions & 83 deletions include/bolt/core/editor_store.hpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#ifndef BOLT_CORE_EDITOR_STORE_HPP_
#define BOLT_CORE_EDITOR_STORE_HPP_

#ifndef EDITOR_STORE_HPP
#define EDITOR_STORE_HPP

#include <string>
#include <functional>
#include <map>
#include <memory>
#include <mutex>
#include <optional>
#include <functional>
#include <string>
#include <vector>
#include <mutex>
#include "bolt/gui_components.hpp"
#include "bolt/editor/code_folding.hpp"

#include "bolt/core/error_handling.hpp"
#include "bolt/editor/code_folding.hpp"
#include "bolt/gui_components.hpp"

namespace bolt {

Expand All @@ -21,12 +21,12 @@ struct EditorDocument {
struct ScrollPosition {
int line = 0;
int character = 0;

void validate() const {
if (line < 0 || character < 0) {
throw EditorException(ErrorCode::INVALID_PARAMETER,
"Scroll position cannot be negative: line=" + std::to_string(line) +
", character=" + std::to_string(character));
"Scroll position cannot be negative: line=" + std::to_string(line) +
", character=" + std::to_string(character));
}
}
} scroll;
Expand All @@ -36,30 +36,30 @@ struct EditorDocument {
} cursor;
// Code folding state
std::vector<FoldRange> foldingRanges;

void validateDocument() const {
if (filePath.empty()) {
throw EditorException(ErrorCode::INVALID_PARAMETER, "Document file path cannot be empty");
}
if (filePath.length() > MAX_FILE_PATH_LENGTH) {
throw EditorException(ErrorCode::INVALID_PARAMETER,
"File path too long: " + std::to_string(filePath.length()) +
" > " + std::to_string(MAX_FILE_PATH_LENGTH));
"File path too long: " + std::to_string(filePath.length()) + " > " +
std::to_string(MAX_FILE_PATH_LENGTH));
}
if (value.length() > MAX_DOCUMENT_SIZE) {
throw EditorException(ErrorCode::INVALID_PARAMETER,
"Document too large: " + std::to_string(value.length()) +
" > " + std::to_string(MAX_DOCUMENT_SIZE));
throw EditorException(
ErrorCode::INVALID_PARAMETER,
"Document too large: " + std::to_string(value.length()) + " > " + std::to_string(MAX_DOCUMENT_SIZE));
}
scroll.validate();
}

static constexpr size_t MAX_FILE_PATH_LENGTH = 2048;
static constexpr size_t MAX_DOCUMENT_SIZE = 50 * 1024 * 1024; // 50MB
static constexpr size_t MAX_DOCUMENT_SIZE = 50 * 1024 * 1024; // 50MB
};

class EditorStore {
public:
public:
static EditorStore& getInstance() {
static EditorStore instance;
return instance;
Expand All @@ -68,69 +68,57 @@ class EditorStore {
void setDocument(const std::string& path, const EditorDocument& doc) {
ErrorHandler::validateParameter(!path.empty(), "Document path cannot be empty");
doc.validateDocument();

std::lock_guard<std::mutex> lock(mutex_);

if (documents_.size() >= MAX_OPEN_DOCUMENTS && documents_.find(path) == documents_.end()) {
throw EditorException(ErrorCode::EDITOR_OPERATION_FAILED,
"Too many open documents: " + std::to_string(documents_.size()) +
" >= " + std::to_string(MAX_OPEN_DOCUMENTS));
"Too many open documents: " + std::to_string(documents_.size()) +
" >= " + std::to_string(MAX_OPEN_DOCUMENTS));
}

documents_[path] = doc;
notifyListeners();
}

void updateScrollPosition(const std::string& path, int line, int character) {
ErrorHandler::validateParameter(!path.empty(), "Document path cannot be empty");

std::lock_guard<std::mutex> lock(mutex_);

auto it = documents_.find(path);
if (it == documents_.end()) {
throw EditorException(ErrorCode::EDITOR_DOCUMENT_INVALID,
"Document not found: " + path);
}


auto it = findDocumentOrThrowLocked(path, "Document not found: ");

EditorDocument::ScrollPosition pos{line, character};
pos.validate();

it->second.scroll = pos;
notifyListeners();
}

void updateFoldingRanges(const std::string& path, const std::vector<FoldRange>& ranges) {
ErrorHandler::validateParameter(!path.empty(), "Document path cannot be empty");

std::lock_guard<std::mutex> lock(mutex_);

auto it = documents_.find(path);
if (it == documents_.end()) {
throw EditorException(ErrorCode::EDITOR_DOCUMENT_INVALID,
"Document not found: " + path);
}


auto it = findDocumentOrThrowLocked(path, "Document not found: ");

if (ranges.size() > MAX_FOLDING_RANGES) {
throw EditorException(ErrorCode::EDITOR_FOLDING_ERROR,
"Too many folding ranges: " + std::to_string(ranges.size()) +
" > " + std::to_string(MAX_FOLDING_RANGES));
"Too many folding ranges: " + std::to_string(ranges.size()) + " > " +
std::to_string(MAX_FOLDING_RANGES));
}

it->second.foldingRanges = ranges;
notifyListeners();
}

void toggleFold(const std::string& path, size_t line) {
ErrorHandler::validateParameter(!path.empty(), "Document path cannot be empty");

std::lock_guard<std::mutex> lock(mutex_);

auto it = documents_.find(path);
if (it == documents_.end()) {
throw EditorException(ErrorCode::EDITOR_DOCUMENT_INVALID,
"Document not found: " + path);
}


auto it = findDocumentOrThrowLocked(path, "Document not found: ");

auto& ranges = it->second.foldingRanges;
bool found = false;
for (auto& range : ranges) {
Expand All @@ -141,18 +129,18 @@ class EditorStore {
break;
}
}

if (!found) {
throw EditorException(ErrorCode::EDITOR_FOLDING_ERROR,
"No foldable range found at line " + std::to_string(line) + " in " + path);
"No foldable range found at line " + std::to_string(line) + " in " + path);
}
}

std::vector<FoldRange> getFoldingRanges(const std::string& path) const {
ErrorHandler::validateParameter(!path.empty(), "Document path cannot be empty");

std::lock_guard<std::mutex> lock(mutex_);

auto it = documents_.find(path);
if (it != documents_.end()) {
return it->second.foldingRanges;
Expand All @@ -162,7 +150,7 @@ class EditorStore {

EditorDocument* getCurrentDocument() {
std::lock_guard<std::mutex> lock(mutex_);

if (!selectedFile_.empty() && documents_.count(selectedFile_)) {
return &documents_[selectedFile_];
}
Expand All @@ -172,9 +160,9 @@ class EditorStore {
void setSelectedFile(const std::string& path) {
if (!path.empty()) {
ErrorHandler::validateParameter(path.length() <= EditorDocument::MAX_FILE_PATH_LENGTH,
"File path too long: " + std::to_string(path.length()));
"File path too long: " + std::to_string(path.length()));
}

std::lock_guard<std::mutex> lock(mutex_);
selectedFile_ = path;
notifyListeners();
Expand All @@ -187,53 +175,60 @@ class EditorStore {

void addListener(std::function<void()> listener) {
ErrorHandler::validateParameter(listener != nullptr, "Listener cannot be null");

std::lock_guard<std::mutex> lock(mutex_);

if (listeners_.size() >= MAX_LISTENERS) {
throw EditorException(ErrorCode::STORE_STATE_INVALID,
"Too many listeners: " + std::to_string(listeners_.size()) +
" >= " + std::to_string(MAX_LISTENERS));
throw EditorException(
ErrorCode::STORE_STATE_INVALID,
"Too many listeners: " + std::to_string(listeners_.size()) + " >= " + std::to_string(MAX_LISTENERS));
}

listeners_.push_back(listener);
}

size_t getDocumentCount() const {
std::lock_guard<std::mutex> lock(mutex_);
return documents_.size();
}

bool hasDocument(const std::string& path) const {
ErrorHandler::validateParameter(!path.empty(), "Document path cannot be empty");
std::lock_guard<std::mutex> lock(mutex_);
return documents_.find(path) != documents_.end();
}

void closeDocument(const std::string& path) {
ErrorHandler::validateParameter(!path.empty(), "Document path cannot be empty");

std::lock_guard<std::mutex> lock(mutex_);

auto it = documents_.find(path);
if (it == documents_.end()) {
throw EditorException(ErrorCode::EDITOR_DOCUMENT_INVALID,
"Cannot close document that is not open: " + path);
}


auto it = findDocumentOrThrowLocked(path, "Cannot close document that is not open: ");

documents_.erase(it);

// Clear selection if this was the selected file
if (selectedFile_ == path) {
selectedFile_.clear();
}

notifyListeners();
}

private:
private:
EditorStore() {}

// Finds the open document at path, or throws EDITOR_DOCUMENT_INVALID with notFoundPrefix + path.
// The caller must already hold mutex_.
std::map<std::string, EditorDocument>::iterator findDocumentOrThrowLocked(const std::string& path,
const char* notFoundPrefix) {
auto it = documents_.find(path);
if (it == documents_.end()) {
throw EditorException(ErrorCode::EDITOR_DOCUMENT_INVALID, notFoundPrefix + path);
}
return it;
}

void notifyListeners() {
// Note: mutex should already be locked when this is called
for (auto& listener : listeners_) {
Expand All @@ -252,12 +247,12 @@ class EditorStore {
std::map<std::string, EditorDocument> documents_;
std::vector<std::function<void()>> listeners_;
mutable std::mutex mutex_;

static constexpr size_t MAX_OPEN_DOCUMENTS = 50;
static constexpr size_t MAX_FOLDING_RANGES = 10000;
static constexpr size_t MAX_LISTENERS = 100;
};

} // namespace bolt
} // namespace bolt

#endif
#endif // BOLT_CORE_EDITOR_STORE_HPP_
Loading
Loading