Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'helix-view/src/document.rs')
-rw-r--r--helix-view/src/document.rs30
1 files changed, 24 insertions, 6 deletions
diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs
index dcdc8dc2..29fd736a 100644
--- a/helix-view/src/document.rs
+++ b/helix-view/src/document.rs
@@ -13,6 +13,7 @@ use helix_core::text_annotations::{InlineAnnotation, Overlay};
use helix_lsp::util::lsp_pos_to_pos;
use helix_stdx::faccess::{copy_metadata, readonly};
use helix_vcs::{DiffHandle, DiffProviderRegistry};
+use once_cell::sync::OnceCell;
use thiserror;
use ::parking_lot::Mutex;
@@ -148,6 +149,7 @@ pub struct Document {
pub inlay_hints_oudated: bool,
path: Option<PathBuf>,
+ relative_path: OnceCell<Option<PathBuf>>,
encoding: &'static encoding::Encoding,
has_bom: bool,
@@ -300,6 +302,14 @@ impl fmt::Debug for DocumentInlayHintsId {
}
}
+impl Editor {
+ pub(crate) fn clear_doc_relative_paths(&mut self) {
+ for doc in self.documents_mut() {
+ doc.relative_path.take();
+ }
+ }
+}
+
enum Encoder {
Utf16Be,
Utf16Le,
@@ -659,6 +669,7 @@ impl Document {
id: DocumentId::default(),
active_snippet: None,
path: None,
+ relative_path: OnceCell::new(),
encoding,
has_bom,
text,
@@ -1172,6 +1183,10 @@ impl Document {
pub fn set_path(&mut self, path: Option<&Path>) {
let path = path.map(helix_stdx::path::canonicalize);
+ // `take` to remove any prior relative path that may have existed.
+ // This will get set in `relative_path()`.
+ self.relative_path.take();
+
// if parent doesn't exist we still want to open the document
// and error out when document is saved
self.path = path;
@@ -1867,16 +1882,19 @@ impl Document {
self.view_data_mut(view_id).view_position = new_offset;
}
- pub fn relative_path(&self) -> Option<Cow<Path>> {
- self.path
+ pub fn relative_path(&self) -> Option<&Path> {
+ self.relative_path
+ .get_or_init(|| {
+ self.path
+ .as_ref()
+ .map(|path| helix_stdx::path::get_relative_path(path).to_path_buf())
+ })
.as_deref()
- .map(helix_stdx::path::get_relative_path)
}
- pub fn display_name(&self) -> Cow<'static, str> {
+ pub fn display_name(&self) -> Cow<'_, str> {
self.relative_path()
- .map(|path| path.to_string_lossy().to_string().into())
- .unwrap_or_else(|| SCRATCH_BUFFER_NAME.into())
+ .map_or_else(|| SCRATCH_BUFFER_NAME.into(), |path| path.to_string_lossy())
}
// transact(Fn) ?