@@ -8,6 +8,7 @@ use crate::{
9use futures_util::future;
9use futures_util::future;
11+ collections::BTreeMap,
11 path::{Path, PathBuf},
12 path::{Path, PathBuf},
13 sync::Arc,
14 sync::Arc,
@@ -15,8 +16,6 @@ use std::{
16use tokio::time::{sleep, Duration, Instant, Sleep};
17use tokio::time::{sleep, Duration, Instant, Sleep};
20use anyhow::Error;
19use anyhow::Error;
22pub use helix_core::diagnostic::Severity;
21pub use helix_core::diagnostic::Severity;
@@ -108,7 +107,8 @@ impl std::fmt::Debug for Motion {
108#[derive(Debug)]
107#[derive(Debug)]
109pub struct Editor {
108pub struct Editor {
110 pub tree: Tree,
109 pub tree: Tree,
110+ pub next_document_id: usize,
111− pub documents: SlotMap<DocumentId, Document>,
111+ pub documents: BTreeMap<DocumentId, Document>,
112 pub count: Option<std::num::NonZeroUsize>,
112 pub count: Option<std::num::NonZeroUsize>,
113 pub selected_register: Option<char>,
113 pub selected_register: Option<char>,
114 pub registers: Registers,
114 pub registers: Registers,
@@ -149,7 +149,8 @@ impl Editor {
151 tree: Tree::new(area),
151 tree: Tree::new(area),
152+ next_document_id: 0,
152− documents: SlotMap::with_key(),
153+ documents: BTreeMap::new(),
153 count: None,
154 count: None,
154 selected_register: None,
155 selected_register: None,
155 theme: themes.default(),
156 theme: themes.default(),
@@ -216,7 +217,7 @@ impl Editor {
217 fn _refresh(&mut self) {
218 fn _refresh(&mut self) {
218 for (view, _) in self.tree.views_mut() {
219 for (view, _) in self.tree.views_mut() {
219− let doc = &self.documents[view.doc];
220+ let doc = &self.documents[&view.doc];
220 view.ensure_cursor_in_view(doc, self.config.scrolloff)
221 view.ensure_cursor_in_view(doc, self.config.scrolloff)
@@ -225,7 +226,7 @@ impl Editor {
225 use crate::tree::Layout;
226 use crate::tree::Layout;
226 use helix_core::Selection;
227 use helix_core::Selection;
228− if !self.documents.contains_key(id) {
229+ if !self.documents.contains_key(&id) {
229 log::error!("cannot switch to document that does not exist (anymore)");
230 log::error!("cannot switch to document that does not exist (anymore)");
@@ -249,7 +250,7 @@ impl Editor {
249 // Copy `doc.id` into a variable before calling `self.documents.remove`, which requires a mutable
250 // Copy `doc.id` into a variable before calling `self.documents.remove`, which requires a mutable
250 // borrow, invalidating direct access to `doc.id`.
251 // borrow, invalidating direct access to `doc.id`.
251 let id = doc.id;
252 let id = doc.id;
252− self.documents.remove(id);
253+ self.documents.remove(&id);
254 let jump = (view.doc, doc.selection(view.id).clone());
255 let jump = (view.doc, doc.selection(view.id).clone());
255 view.jumps.push(jump);
256 view.jumps.push(jump);
@@ -281,14 +282,14 @@ impl Editor {
281 let view = View::new(id);
282 let view = View::new(id);
282 let view_id = self.tree.split(view, Layout::Horizontal);
283 let view_id = self.tree.split(view, Layout::Horizontal);
283 // initialize selection for view
284 // initialize selection for view
284− let doc = &mut self.documents[id];
285+ let doc = self.documents.get_mut(&id).unwrap();
285 doc.selections.insert(view_id, Selection::point(0));
286 doc.selections.insert(view_id, Selection::point(0));
287 Action::VerticalSplit => {
288 Action::VerticalSplit => {
288 let view = View::new(id);
289 let view = View::new(id);
289 let view_id = self.tree.split(view, Layout::Vertical);
290 let view_id = self.tree.split(view, Layout::Vertical);
290 // initialize selection for view
291 // initialize selection for view
291− let doc = &mut self.documents[id];
292+ let doc = self.documents.get_mut(&id).unwrap();
292 doc.selections.insert(view_id, Selection::point(0));
293 doc.selections.insert(view_id, Selection::point(0));
@@ -297,9 +298,11 @@ impl Editor {
299 pub fn new_file(&mut self, action: Action) -> DocumentId {
300 pub fn new_file(&mut self, action: Action) -> DocumentId {
301+ let id = DocumentId(self.next_document_id);
302+ self.next_document_id += 1;
300− let doc = Document::default();
303+ let mut doc = Document::default();
301− let id = self.documents.insert(doc);
305+ self.documents.insert(id, doc);
302− self.documents[id].id = id;
303 self.switch(id, action);
306 self.switch(id, action);
@@ -349,8 +352,10 @@ impl Editor {
349 doc.set_language_server(Some(language_server));
352 doc.set_language_server(Some(language_server));
355+ let id = DocumentId(self.next_document_id);
356+ self.next_document_id += 1;
352− let id = self.documents.insert(doc);
358+ self.documents.insert(id, doc);
353− self.documents[id].id = id;
@@ -361,16 +366,20 @@ impl Editor {
361 pub fn close(&mut self, id: ViewId, close_buffer: bool) {
366 pub fn close(&mut self, id: ViewId, close_buffer: bool) {
362 let view = self.tree.get(self.tree.focus);
367 let view = self.tree.get(self.tree.focus);
363 // remove selection
368 // remove selection
364− self.documents[view.doc].selections.remove(&id);
369+ self.documents
366 if close_buffer {
375 if close_buffer {
367 // get around borrowck issues
376 // get around borrowck issues
368− let doc = &self.documents[view.doc];
377+ let doc = &self.documents[&view.doc];
370 if let Some(language_server) = doc.language_server() {
379 if let Some(language_server) = doc.language_server() {
371 tokio::spawn(language_server.text_document_did_close(doc.identifier()));
380 tokio::spawn(language_server.text_document_did_close(doc.identifier()));
373− self.documents.remove(view.doc);
382+ self.documents.remove(&view.doc);
376 self.tree.remove(id);
385 self.tree.remove(id);
@@ -409,18 +418,18 @@ impl Editor {
410 pub fn ensure_cursor_in_view(&mut self, id: ViewId) {
419 pub fn ensure_cursor_in_view(&mut self, id: ViewId) {
411 let view = self.tree.get_mut(id);
420 let view = self.tree.get_mut(id);
412− let doc = &self.documents[view.doc];
421+ let doc = &self.documents[&view.doc];
413 view.ensure_cursor_in_view(doc, self.config.scrolloff)
422 view.ensure_cursor_in_view(doc, self.config.scrolloff)
416 #[inline]
425 #[inline]
417 pub fn document(&self, id: DocumentId) -> Option<&Document> {
426 pub fn document(&self, id: DocumentId) -> Option<&Document> {
418− self.documents.get(id)
427+ self.documents.get(&id)
421 #[inline]
430 #[inline]
422 pub fn document_mut(&mut self, id: DocumentId) -> Option<&mut Document> {
431 pub fn document_mut(&mut self, id: DocumentId) -> Option<&mut Document> {
423− self.documents.get_mut(id)
432+ self.documents.get_mut(&id)
426 #[inline]
435 #[inline]
@@ -445,7 +454,7 @@ impl Editor {
446 pub fn cursor(&self) -> (Option<Position>, CursorKind) {
455 pub fn cursor(&self) -> (Option<Position>, CursorKind) {
447 let view = view!(self);
456 let view = view!(self);
448− let doc = &self.documents[view.doc];
457+ let doc = &self.documents[&view.doc];
449 let cursor = doc
458 let cursor = doc
450 .selection(view.id)
459 .selection(view.id)
451 .primary()
460 .primary()