@@ -69,71 +69,79 @@ class EditorStore {
6969 ErrorHandler::validateParameter (!path.empty (), " Document path cannot be empty" );
7070 doc.validateDocument ();
7171
72- std::lock_guard<std::mutex> lock (mutex_);
72+ {
73+ std::lock_guard<std::mutex> lock (mutex_);
7374
74- if (documents_.size () >= MAX_OPEN_DOCUMENTS && documents_.find (path) == documents_.end ()) {
75- throw EditorException (ErrorCode::EDITOR_OPERATION_FAILED ,
76- " Too many open documents: " + std::to_string (documents_.size ()) +
77- " >= " + std::to_string (MAX_OPEN_DOCUMENTS ));
78- }
75+ if (documents_.size () >= MAX_OPEN_DOCUMENTS && documents_.find (path) == documents_.end ()) {
76+ throw EditorException (ErrorCode::EDITOR_OPERATION_FAILED ,
77+ " Too many open documents: " + std::to_string (documents_.size ()) +
78+ " >= " + std::to_string (MAX_OPEN_DOCUMENTS ));
79+ }
7980
80- documents_[path] = doc;
81+ documents_[path] = doc;
82+ }
8183 notifyListeners ();
8284 }
8385
8486 void updateScrollPosition (const std::string& path, int line, int character) {
8587 ErrorHandler::validateParameter (!path.empty (), " Document path cannot be empty" );
8688
87- std::lock_guard<std::mutex> lock (mutex_);
89+ {
90+ std::lock_guard<std::mutex> lock (mutex_);
8891
89- auto it = findDocumentOrThrowLocked (path, " Document not found: " );
92+ auto it = findDocumentOrThrowLocked (path, " Document not found: " );
9093
91- EditorDocument::ScrollPosition pos{line, character};
92- pos.validate ();
94+ EditorDocument::ScrollPosition pos{line, character};
95+ pos.validate ();
9396
94- it->second .scroll = pos;
97+ it->second .scroll = pos;
98+ }
9599 notifyListeners ();
96100 }
97101
98102 void updateFoldingRanges (const std::string& path, const std::vector<FoldRange>& ranges) {
99103 ErrorHandler::validateParameter (!path.empty (), " Document path cannot be empty" );
100104
101- std::lock_guard<std::mutex> lock (mutex_);
105+ {
106+ std::lock_guard<std::mutex> lock (mutex_);
102107
103- auto it = findDocumentOrThrowLocked (path, " Document not found: " );
108+ auto it = findDocumentOrThrowLocked (path, " Document not found: " );
104109
105- if (ranges.size () > MAX_FOLDING_RANGES ) {
106- throw EditorException (ErrorCode::EDITOR_FOLDING_ERROR ,
107- " Too many folding ranges: " + std::to_string (ranges.size ()) + " > " +
108- std::to_string (MAX_FOLDING_RANGES ));
109- }
110+ if (ranges.size () > MAX_FOLDING_RANGES ) {
111+ throw EditorException (ErrorCode::EDITOR_FOLDING_ERROR ,
112+ " Too many folding ranges: " + std::to_string (ranges.size ()) + " > " +
113+ std::to_string (MAX_FOLDING_RANGES ));
114+ }
110115
111- it->second .foldingRanges = ranges;
116+ it->second .foldingRanges = ranges;
117+ }
112118 notifyListeners ();
113119 }
114120
115121 void toggleFold (const std::string& path, size_t line) {
116122 ErrorHandler::validateParameter (!path.empty (), " Document path cannot be empty" );
117123
118- std::lock_guard<std::mutex> lock (mutex_);
124+ {
125+ std::lock_guard<std::mutex> lock (mutex_);
119126
120- auto it = findDocumentOrThrowLocked (path, " Document not found: " );
127+ auto it = findDocumentOrThrowLocked (path, " Document not found: " );
121128
122- auto & ranges = it->second .foldingRanges ;
123- bool found = false ;
124- for (auto & range : ranges) {
125- if (line >= range.startLine && line <= range.endLine ) {
126- range.isFolded = !range.isFolded ;
127- found = true ;
128- notifyListeners () ;
129- break ;
129+ auto & ranges = it->second .foldingRanges ;
130+ bool found = false ;
131+ for (auto & range : ranges) {
132+ if (line >= range.startLine && line <= range.endLine ) {
133+ range.isFolded = !range.isFolded ;
134+ found = true ;
135+ break ;
136+ }
130137 }
131- }
132138
133- if (!found) {
134- throw EditorException (ErrorCode::EDITOR_FOLDING_ERROR ,
135- " No foldable range found at line " + std::to_string (line) + " in " + path);
139+ if (!found) {
140+ throw EditorException (ErrorCode::EDITOR_FOLDING_ERROR ,
141+ " No foldable range found at line " + std::to_string (line) + " in " + path);
142+ }
136143 }
144+ notifyListeners ();
137145 }
138146
139147 std::vector<FoldRange> getFoldingRanges (const std::string& path) const {
@@ -148,6 +156,10 @@ class EditorStore {
148156 return {};
149157 }
150158
159+ // The returned pointer refers to an entry of the documents map and is not
160+ // protected by the lock once this returns: it is only valid until that
161+ // document is closed, and reading or writing through it is not
162+ // synchronised with other threads using the store.
151163 EditorDocument* getCurrentDocument () {
152164 std::lock_guard<std::mutex> lock (mutex_);
153165
@@ -163,12 +175,14 @@ class EditorStore {
163175 " File path too long: " + std::to_string (path.length ()));
164176 }
165177
166- std::lock_guard<std::mutex> lock (mutex_);
167- selectedFile_ = path;
178+ {
179+ std::lock_guard<std::mutex> lock (mutex_);
180+ selectedFile_ = path;
181+ }
168182 notifyListeners ();
169183 }
170184
171- const std::string& getSelectedFile () const {
185+ std::string getSelectedFile () const {
172186 std::lock_guard<std::mutex> lock (mutex_);
173187 return selectedFile_;
174188 }
@@ -201,15 +215,17 @@ class EditorStore {
201215 void closeDocument (const std::string& path) {
202216 ErrorHandler::validateParameter (!path.empty (), " Document path cannot be empty" );
203217
204- std::lock_guard<std::mutex> lock (mutex_);
218+ {
219+ std::lock_guard<std::mutex> lock (mutex_);
205220
206- auto it = findDocumentOrThrowLocked (path, " Cannot close document that is not open: " );
221+ auto it = findDocumentOrThrowLocked (path, " Cannot close document that is not open: " );
207222
208- documents_.erase (it);
223+ documents_.erase (it);
209224
210- // Clear selection if this was the selected file
211- if (selectedFile_ == path) {
212- selectedFile_.clear ();
225+ // Clear selection if this was the selected file
226+ if (selectedFile_ == path) {
227+ selectedFile_.clear ();
228+ }
213229 }
214230
215231 notifyListeners ();
@@ -229,9 +245,16 @@ class EditorStore {
229245 return it;
230246 }
231247
248+ // Must be called WITHOUT mutex_ held. The listeners are copied under the
249+ // lock and invoked after it is released, so a listener may call back into
250+ // the store (mutex_ is not recursive).
232251 void notifyListeners () {
233- // Note: mutex should already be locked when this is called
234- for (auto & listener : listeners_) {
252+ std::vector<std::function<void ()>> listeners;
253+ {
254+ std::lock_guard<std::mutex> lock (mutex_);
255+ listeners = listeners_;
256+ }
257+ for (auto & listener : listeners) {
235258 try {
236259 if (listener) {
237260 listener ();
0 commit comments