@@ -8,6 +8,7 @@ use crate::{
88
99use futures_util::future;
1010use std::{
11+ collections::BTreeMap,
1112 path::{Path, PathBuf},
1213 pin::Pin,
1314 sync::Arc,
@@ -15,8 +16,6 @@ use std::{
1516
1617use tokio::time::{sleep, Duration, Instant, Sleep};
1718
18−use slotmap::SlotMap;
19−
2019use anyhow::Error;
2120
2221pub use helix_core::diagnostic::Severity;
@@ -108,7 +107,8 @@ impl std::fmt::Debug for Motion {
108107#[derive(Debug)]
109108pub struct Editor {
110109 pub tree: Tree,
111− pub documents: SlotMap<DocumentId, Document>,
110+ pub next_document_id: usize,
111+ pub documents: BTreeMap<DocumentId, Document>,
112112 pub count: Option<std::num::NonZeroUsize>,
113113 pub selected_register: Option<char>,
114114 pub registers: Registers,
@@ -149,7 +149,8 @@ impl Editor {
149149
150150 Self {
151151 tree: Tree::new(area),
152− documents: SlotMap::with_key(),
152+ next_document_id: 0,
153+ documents: BTreeMap::new(),
153154 count: None,
154155 selected_register: None,
155156 theme: themes.default(),
@@ -216,7 +217,7 @@ impl Editor {
216217
217218 fn _refresh(&mut self) {
218219 for (view, _) in self.tree.views_mut() {
219− let doc = &self.documents[view.doc];
220+ let doc = &self.documents[&view.doc];
220221 view.ensure_cursor_in_view(doc, self.config.scrolloff)
221222 }
222223 }
@@ -225,7 +226,7 @@ impl Editor {
225226 use crate::tree::Layout;
226227 use helix_core::Selection;
227228
228− if !self.documents.contains_key(id) {
229+ if !self.documents.contains_key(&id) {
229230 log::error!("cannot switch to document that does not exist (anymore)");
230231 return;
231232 }
@@ -249,7 +250,7 @@ impl Editor {
249250 // Copy `doc.id` into a variable before calling `self.documents.remove`, which requires a mutable
250251 // borrow, invalidating direct access to `doc.id`.
251252 let id = doc.id;
252− self.documents.remove(id);
253+ self.documents.remove(&id);
253254 } else {
254255 let jump = (view.doc, doc.selection(view.id).clone());
255256 view.jumps.push(jump);
@@ -281,14 +282,14 @@ impl Editor {
281282 let view = View::new(id);
282283 let view_id = self.tree.split(view, Layout::Horizontal);
283284 // initialize selection for view
284− let doc = &mut self.documents[id];
285+ let doc = self.documents.get_mut(&id).unwrap();
285286 doc.selections.insert(view_id, Selection::point(0));
286287 }
287288 Action::VerticalSplit => {
288289 let view = View::new(id);
289290 let view_id = self.tree.split(view, Layout::Vertical);
290291 // initialize selection for view
291− let doc = &mut self.documents[id];
292+ let doc = self.documents.get_mut(&id).unwrap();
292293 doc.selections.insert(view_id, Selection::point(0));
293294 }
294295 }
@@ -297,9 +298,11 @@ impl Editor {
297298 }
298299
299300 pub fn new_file(&mut self, action: Action) -> DocumentId {
300− let doc = Document::default();
301− let id = self.documents.insert(doc);
302− self.documents[id].id = id;
301+ let id = DocumentId(self.next_document_id);
302+ self.next_document_id += 1;
303+ let mut doc = Document::default();
304+ doc.id = id;
305+ self.documents.insert(id, doc);
303306 self.switch(id, action);
304307 id
305308 }
@@ -349,8 +352,10 @@ impl Editor {
349352 doc.set_language_server(Some(language_server));
350353 }
351354
352− let id = self.documents.insert(doc);
353− self.documents[id].id = id;
355+ let id = DocumentId(self.next_document_id);
356+ self.next_document_id += 1;
357+ doc.id = id;
358+ self.documents.insert(id, doc);
354359 id
355360 };
356361
@@ -361,16 +366,20 @@ impl Editor {
361366 pub fn close(&mut self, id: ViewId, close_buffer: bool) {
362367 let view = self.tree.get(self.tree.focus);
363368 // remove selection
364− self.documents[view.doc].selections.remove(&id);
369+ self.documents
370+ .get_mut(&view.doc)
371+ .unwrap()
372+ .selections
373+ .remove(&id);
365374
366375 if close_buffer {
367376 // get around borrowck issues
368− let doc = &self.documents[view.doc];
377+ let doc = &self.documents[&view.doc];
369378
370379 if let Some(language_server) = doc.language_server() {
371380 tokio::spawn(language_server.text_document_did_close(doc.identifier()));
372381 }
373− self.documents.remove(view.doc);
382+ self.documents.remove(&view.doc);
374383 }
375384
376385 self.tree.remove(id);
@@ -409,18 +418,18 @@ impl Editor {
409418
410419 pub fn ensure_cursor_in_view(&mut self, id: ViewId) {
411420 let view = self.tree.get_mut(id);
412− let doc = &self.documents[view.doc];
421+ let doc = &self.documents[&view.doc];
413422 view.ensure_cursor_in_view(doc, self.config.scrolloff)
414423 }
415424
416425 #[inline]
417426 pub fn document(&self, id: DocumentId) -> Option<&Document> {
418− self.documents.get(id)
427+ self.documents.get(&id)
419428 }
420429
421430 #[inline]
422431 pub fn document_mut(&mut self, id: DocumentId) -> Option<&mut Document> {
423− self.documents.get_mut(id)
432+ self.documents.get_mut(&id)
424433 }
425434
426435 #[inline]
@@ -445,7 +454,7 @@ impl Editor {
445454
446455 pub fn cursor(&self) -> (Option<Position>, CursorKind) {
447456 let view = view!(self);
448− let doc = &self.documents[view.doc];
457+ let doc = &self.documents[&view.doc];
449458 let cursor = doc
450459 .selection(view.id)
451460 .primary()