@@ -11,6 +11,7 @@ use futures_util::future;
12 collections::BTreeMap,
12 collections::BTreeMap,
13 io::stdin,
13 io::stdin,
14 path::{Path, PathBuf},
15 path::{Path, PathBuf},
16 sync::Arc,
17 sync::Arc,
@@ -154,7 +155,7 @@ impl std::fmt::Debug for Motion {
154#[derive(Debug)]
155#[derive(Debug)]
155pub struct Editor {
156pub struct Editor {
156 pub tree: Tree,
157 pub tree: Tree,
157− pub next_document_id: usize,
158+ pub next_document_id: DocumentId,
158 pub documents: BTreeMap<DocumentId, Document>,
159 pub documents: BTreeMap<DocumentId, Document>,
159 pub count: Option<std::num::NonZeroUsize>,
160 pub count: Option<std::num::NonZeroUsize>,
160 pub selected_register: Option<char>,
161 pub selected_register: Option<char>,
@@ -198,7 +199,8 @@ impl Editor {
200 tree: Tree::new(area),
201 tree: Tree::new(area),
202+ // Safety: 1 is non-zero
201− next_document_id: 0,
203+ next_document_id: DocumentId::default(),
202 documents: BTreeMap::new(),
204 documents: BTreeMap::new(),
203 count: None,
205 count: None,
204 selected_register: None,
206 selected_register: None,
@@ -367,16 +369,19 @@ impl Editor {
367 self._refresh();
369 self._refresh();
372+ /// Generate an id for a new document and register it.
370− fn new_document(&mut self, mut document: Document) -> DocumentId {
373+ fn new_document(&mut self, mut doc: Document) -> DocumentId {
371− let id = DocumentId(self.next_document_id);
374+ let id = self.next_document_id;
375+ // Safety: adding 1 from 1 is fine, probably impossible to reach usize max
376+ self.next_document_id =
372− self.next_document_id += 1;
377+ DocumentId(unsafe { NonZeroUsize::new_unchecked(self.next_document_id.0.get() + 1) });
373− document.id = id;
378+ doc.id = id;
374− self.documents.insert(id, document);
379+ self.documents.insert(id, doc);
378− fn new_file_from_document(&mut self, action: Action, document: Document) -> DocumentId {
383+ fn new_file_from_document(&mut self, action: Action, doc: Document) -> DocumentId {
379− let id = self.new_document(document);
384+ let id = self.new_document(doc);
380 self.switch(id, action);
385 self.switch(id, action);
@@ -435,11 +440,7 @@ impl Editor {
435 doc.set_language_server(Some(language_server));
440 doc.set_language_server(Some(language_server));
438− let id = DocumentId(self.next_document_id);
443+ self.new_document(doc)
439− self.next_document_id += 1;
441− self.documents.insert(id, doc);
445 self.switch(id, action);
446 self.switch(id, action);