@@ -11,6 +11,7 @@ use futures_util::future;
1111use std::{
1212 collections::BTreeMap,
1313 io::stdin,
14+ num::NonZeroUsize,
1415 path::{Path, PathBuf},
1516 pin::Pin,
1617 sync::Arc,
@@ -154,7 +155,7 @@ impl std::fmt::Debug for Motion {
154155#[derive(Debug)]
155156pub struct Editor {
156157 pub tree: Tree,
157− pub next_document_id: usize,
158+ pub next_document_id: DocumentId,
158159 pub documents: BTreeMap<DocumentId, Document>,
159160 pub count: Option<std::num::NonZeroUsize>,
160161 pub selected_register: Option<char>,
@@ -198,7 +199,8 @@ impl Editor {
198199
199200 Self {
200201 tree: Tree::new(area),
201− next_document_id: 0,
202+ // Safety: 1 is non-zero
203+ next_document_id: DocumentId::default(),
202204 documents: BTreeMap::new(),
203205 count: None,
204206 selected_register: None,
@@ -367,16 +369,19 @@ impl Editor {
367369 self._refresh();
368370 }
369371
370− fn new_document(&mut self, mut document: Document) -> DocumentId {
371− let id = DocumentId(self.next_document_id);
372− self.next_document_id += 1;
373− document.id = id;
374− self.documents.insert(id, document);
372+ /// Generate an id for a new document and register it.
373+ fn new_document(&mut self, mut doc: Document) -> DocumentId {
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 =
377+ DocumentId(unsafe { NonZeroUsize::new_unchecked(self.next_document_id.0.get() + 1) });
378+ doc.id = id;
379+ self.documents.insert(id, doc);
375380 id
376381 }
377382
378− fn new_file_from_document(&mut self, action: Action, document: Document) -> DocumentId {
379− let id = self.new_document(document);
383+ fn new_file_from_document(&mut self, action: Action, doc: Document) -> DocumentId {
384+ let id = self.new_document(doc);
380385 self.switch(id, action);
381386 id
382387 }
@@ -435,11 +440,7 @@ impl Editor {
435440 doc.set_language_server(Some(language_server));
436441 }
437442
438− let id = DocumentId(self.next_document_id);
439− self.next_document_id += 1;
440− doc.id = id;
441− self.documents.insert(id, doc);
442− id
443+ self.new_document(doc)
443444 };
444445
445446 self.switch(id, action);