on master help sign in
lsp: Refactor code that could use document_by_path_mut
dc7799bBlaž Hrastnik
helix-term/src/application.rs+3−8
@@ -276,15 +276,10 @@ impl Application {
276
276
277 match notification {
277 match notification {
278 Notification::PublishDiagnostics(params) => {
278 Notification::PublishDiagnostics(params) => {
279− let path = Some(params.uri.to_file_path().unwrap());
279+ let path = params.uri.to_file_path().unwrap();
280+ let doc = self.editor.document_by_path_mut(&path);
280
281
281− let doc = self
282− .editor
283− .documents
284− .iter_mut()
285− .find(|(_, doc)| doc.path() == path.as_ref());
286−
287− if let Some((_, doc)) = doc {
282+ if let Some(doc) = doc {
288 let text = doc.text();
283 let text = doc.text();
289
284
290 let diagnostics = params
285 let diagnostics = params
helix-view/src/editor.rs+5
@@ -340,6 +340,11 @@ impl Editor {
340 .find(|doc| doc.path().map(|p| p == path.as_ref()).unwrap_or(false))
340 .find(|doc| doc.path().map(|p| p == path.as_ref()).unwrap_or(false))
341 }
341 }
342
342
343+ pub fn document_by_path_mut<P: AsRef<Path>>(&mut self, path: P) -> Option<&mut Document> {
344+ self.documents_mut()
345+ .find(|doc| doc.path().map(|p| p == path.as_ref()).unwrap_or(false))
346+ }
347+
343 pub fn cursor(&self) -> (Option<Position>, CursorKind) {
348 pub fn cursor(&self) -> (Option<Position>, CursorKind) {
344 let view = view!(self);
349 let view = view!(self);
345 let doc = &self.documents[view.doc];
350 let doc = &self.documents[view.doc];