on master help sign in
Replace documents SlotMap with BTreeMap
commite2560f427ef5e75155071e39da342628f5d5896a authorBlaž Hrastnik <blaz@mxxn.io> dateThu 4 Nov 2021, 13:43 (5 years ago) parent39584cb
5 files changed+44−32
helix-term/src/commands.rs+3−3 helix-term/src/ui/editor.rs+5−5 helix-view/src/editor.rs+30−21 helix-view/src/lib.rs+3−1 helix-view/src/macros.rs+3−2
helix-term/src/commands.rs+3−3
@@ -1818,7 +1818,7 @@ mod cmd {
18181818 let mut errors = String::new();
18191819
18201820 // save all documents
1821− for (_, doc) in &mut cx.editor.documents {
1821+ for doc in &mut cx.editor.documents.values_mut() {
18221822 if doc.path().is_none() {
18231823 errors.push_str("cannot write a buffer without a filename\n");
18241824 continue;
@@ -2512,7 +2512,7 @@ fn buffer_picker(cx: &mut Context) {
25122512 cx.editor
25132513 .documents
25142514 .iter()
2515− .map(|(id, doc)| (id, doc.path().cloned()))
2515+ .map(|(id, doc)| (*id, doc.path().cloned()))
25162516 .collect(),
25172517 move |(id, path): &(DocumentId, Option<PathBuf>)| {
25182518 let path = path.as_deref().map(helix_core::path::get_relative_path);
@@ -2531,7 +2531,7 @@ fn buffer_picker(cx: &mut Context) {
25312531 editor.switch(*id, Action::Replace);
25322532 },
25332533 |editor, (id, path)| {
2534− let doc = &editor.documents.get(*id)?;
2534+ let doc = &editor.documents.get(id)?;
25352535 let &view_id = doc.selections().keys().next()?;
25362536 let line = doc
25372537 .selection(view_id)
helix-term/src/ui/editor.rs+5−5
@@ -813,12 +813,12 @@ impl EditorView {
813813 let editor = &mut cxt.editor;
814814
815815 let result = editor.tree.views().find_map(|(view, _focus)| {
816− view.pos_at_screen_coords(&editor.documents[view.doc], row, column)
816+ view.pos_at_screen_coords(&editor.documents[&view.doc], row, column)
817817 .map(|pos| (pos, view.id))
818818 });
819819
820820 if let Some((pos, view_id)) = result {
821− let doc = &mut editor.documents[editor.tree.get(view_id).doc];
821+ let doc = editor.document_mut(editor.tree.get(view_id).doc).unwrap();
822822
823823 if modifiers == crossterm::event::KeyModifiers::ALT {
824824 let selection = doc.selection(view_id).clone();
@@ -870,7 +870,7 @@ impl EditorView {
870870 };
871871
872872 let result = cxt.editor.tree.views().find_map(|(view, _focus)| {
873− view.pos_at_screen_coords(&cxt.editor.documents[view.doc], row, column)
873+ view.pos_at_screen_coords(&cxt.editor.documents[&view.doc], row, column)
874874 .map(|_| view.id)
875875 });
876876
@@ -926,12 +926,12 @@ impl EditorView {
926926 }
927927
928928 let result = editor.tree.views().find_map(|(view, _focus)| {
929− view.pos_at_screen_coords(&editor.documents[view.doc], row, column)
929+ view.pos_at_screen_coords(&editor.documents[&view.doc], row, column)
930930 .map(|pos| (pos, view.id))
931931 });
932932
933933 if let Some((pos, view_id)) = result {
934− let doc = &mut editor.documents[editor.tree.get(view_id).doc];
934+ let doc = editor.document_mut(editor.tree.get(view_id).doc).unwrap();
935935 doc.set_selection(view_id, Selection::point(pos));
936936 editor.tree.focus = view_id;
937937 commands::Command::paste_primary_clipboard_before.execute(cxt);
helix-view/src/editor.rs+30−21
@@ -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()
helix-view/src/lib.rs+3−1
@@ -12,8 +12,10 @@ pub mod theme;
1212pub mod tree;
1313pub mod view;
1414
15+#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Debug)]
16+pub struct DocumentId(usize);
17+
1518slotmap::new_key_type! {
16− pub struct DocumentId;
1719 pub struct ViewId;
1820}
1921
helix-view/src/macros.rs+3−2
@@ -13,7 +13,8 @@
1313macro_rules! current {
1414 ( $( $editor:ident ).+ ) => {{
1515 let view = $crate::view_mut!( $( $editor ).+ );
16− let doc = &mut $( $editor ).+ .documents[view.doc];
16+ let id = view.doc;
17+ let doc = $( $editor ).+ .documents.get_mut(&id).unwrap();
1718 (view, doc)
1819 }};
1920}
@@ -56,7 +57,7 @@ macro_rules! doc {
5657macro_rules! current_ref {
5758 ( $( $editor:ident ).+ ) => {{
5859 let view = $( $editor ).+ .tree.get($( $editor ).+ .tree.focus);
59− let doc = &$( $editor ).+ .documents[view.doc];
60+ let doc = &$( $editor ).+ .documents[&view.doc];
6061 (view, doc)
6162 }};
6263}