Skip to content

Commit

Permalink
change to check if readonly on set_path()
Browse files Browse the repository at this point in the history
  • Loading branch information
connortsui20 committed Jul 26, 2023
1 parent d91b946 commit b64b1e6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
4 changes: 2 additions & 2 deletions helix-term/src/ui/statusline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,8 +432,8 @@ fn render_file_modification_indicator<F>(context: &mut RenderContext, write: F)
where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{
debug_assert!(!(context.doc.is_read_only() && context.doc.is_modified()));
let title = (if context.doc.is_read_only() {
debug_assert!(!(context.doc.readonly.unwrap_or(false) && context.doc.is_modified()));
let title = (if context.doc.readonly.unwrap_or(false) {
"[readonly]"
} else if context.doc.is_modified() {
"[+]"
Expand Down
23 changes: 13 additions & 10 deletions helix-view/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ pub struct Document {

// when document was used for most-recent-used buffer picker
pub focused_at: std::time::Instant,

// logically true when equal to Some(true); None implies we haven't checked yet
pub readonly: Option<bool>,
}

/// Inlay hints for a single `(Document, View)` combo.
Expand Down Expand Up @@ -673,6 +676,7 @@ impl Document {
config,
version_control_head: None,
focused_at: std::time::Instant::now(),
readonly: None
}
}

Expand Down Expand Up @@ -1018,6 +1022,15 @@ impl Document {
// and error out when document is saved
self.path = path;

// Check if the file is readonly or not
self.readonly = Some(match &self.path {
None => false,
Some(p) => match std::fs::metadata(p) {
Err(_) => false,
Ok(metadata) => metadata.permissions().readonly(),
},
});

Ok(())
}

Expand Down Expand Up @@ -1416,16 +1429,6 @@ impl Document {
self.id
}

pub fn is_read_only(&self) -> bool {
match &self.path {
None => false,
Some(p) => match std::fs::metadata(p) {
Err(_) => false,
Ok(metadata) => metadata.permissions().readonly(),
},
}
}

/// If there are unsaved modifications.
pub fn is_modified(&self) -> bool {
let history = self.history.take();
Expand Down

0 comments on commit b64b1e6

Please sign in to comment.