Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add --readonly flag to enter readonly mode #7128

Closed
wants to merge 13 commits into from
Next Next commit
add --readonly flag to enter readonly mode
  • Loading branch information
ds-cbo committed May 24, 2023
commit 34d4c559b3c444371b06d839510411619d83149f
2 changes: 2 additions & 0 deletions helix-term/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ impl Application {
args: Args,
config: Config,
syn_loader_conf: syntax::Configuration,
readonly: bool,
) -> Result<Self, Error> {
#[cfg(feature = "integration")]
setup_integration_logging();
Expand Down Expand Up @@ -151,6 +152,7 @@ impl Application {
Arc::new(Map::new(Arc::clone(&config), |config: &Config| {
&config.editor
})),
readonly,
);

let keys = Box::new(Map::new(Arc::clone(&config), |config: &Config| {
Expand Down
2 changes: 2 additions & 0 deletions helix-term/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct Args {
pub log_file: Option<PathBuf>,
pub config_file: Option<PathBuf>,
pub files: Vec<(PathBuf, Position)>,
pub readonly: bool,
}

impl Args {
Expand Down Expand Up @@ -51,6 +52,7 @@ impl Args {
anyhow::bail!("--grammar must be followed by either 'fetch' or 'build'")
}
},
"--readonly" => args.readonly = true,
"-c" | "--config" => match argv.next().as_deref() {
Some(path) => args.config_file = Some(path.into()),
None => anyhow::bail!("--config must specify a path to read"),
Expand Down
5 changes: 4 additions & 1 deletion helix-term/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ FLAGS:
or 'all'. 'all' is the default if not specified.
-g, --grammar {{fetch|build}} Fetches or builds tree-sitter grammars listed in languages.toml
-c, --config <file> Specifies a file to use for configuration
--readonly Open all files in readonly mode
-v Increases logging verbosity each use for up to 3 times
--log Specifies a file to use for logging
(default file: {})
Expand Down Expand Up @@ -150,8 +151,10 @@ FLAGS:
helix_core::config::default_syntax_loader()
});

let readonly = args.readonly;

// TODO: use the thread local executor to spawn the application task separately from the work pool
let mut app = Application::new(args, config, syn_loader_conf)
let mut app = Application::new(args, config, syn_loader_conf, readonly)
.context("unable to create new application")?;

let exit_code = app.run(&mut EventStream::new()).await?;
Expand Down
2 changes: 1 addition & 1 deletion helix-term/src/ui/picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ impl<T: Item + 'static> FilePicker<T> {
}
_ => {
// TODO: enable syntax highlighting; blocked by async rendering
Document::open(path, None, None, editor.config.clone())
Document::open(path, None, None, editor.config.clone(), false)
.map(|doc| CachedPreview::Document(Box::new(doc)))
.unwrap_or(CachedPreview::NotFound)
}
Expand Down
14 changes: 12 additions & 2 deletions helix-view/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ pub struct Document {
pub(crate) id: DocumentId,
text: Rope,
selections: HashMap<ViewId, Selection>,
readonly: bool,

/// Inlay hints annotations for the document, by view.
///
Expand Down Expand Up @@ -588,6 +589,7 @@ impl Document {
text: Rope,
encoding_with_bom_info: Option<(&'static Encoding, bool)>,
config: Arc<dyn DynAccess<Config>>,
readonly: bool,
) -> Self {
let (encoding, has_bom) = encoding_with_bom_info.unwrap_or((encoding::UTF_8, false));
let changes = ChangeSet::new(&text);
Expand All @@ -599,6 +601,7 @@ impl Document {
encoding,
has_bom,
text,
readonly,
selections: HashMap::default(),
inlay_hints: HashMap::default(),
inlay_hints_oudated: false,
Expand All @@ -625,7 +628,7 @@ impl Document {
}
pub fn default(config: Arc<dyn DynAccess<Config>>) -> Self {
let text = Rope::from(DEFAULT_LINE_ENDING.as_str());
Self::from(text, None, config)
Self::from(text, None, config, false)
}
// TODO: async fn?
/// Create a new document from `path`. Encoding is auto-detected, but it can be manually
Expand All @@ -635,6 +638,7 @@ impl Document {
encoding: Option<&'static Encoding>,
config_loader: Option<Arc<syntax::Loader>>,
config: Arc<dyn DynAccess<Config>>,
readonly: bool,
) -> Result<Self, Error> {
// Open the file if it exists, otherwise assume it is a new file (and thus empty).
let (rope, encoding, has_bom) = if path.exists() {
Expand All @@ -646,7 +650,7 @@ impl Document {
(Rope::from(DEFAULT_LINE_ENDING.as_str()), encoding, false)
};

let mut doc = Self::from(rope, Some((encoding, has_bom)), config);
let mut doc = Self::from(rope, Some((encoding, has_bom)), config, readonly);

// set the path and try detecting the language
doc.set_path(Some(path))?;
Expand Down Expand Up @@ -789,6 +793,10 @@ impl Document {
self.path().map(|path| path.to_string_lossy())
);

if self.readonly {
bail!("opened in readonly mode");
}

// we clone and move text + path into the future so that we asynchronously save the current
// state without blocking any further edits.
let text = self.text().clone();
Expand Down Expand Up @@ -1767,6 +1775,7 @@ mod test {
text,
None,
Arc::new(ArcSwap::new(Arc::new(Config::default()))),
false,
);
let view = ViewId::default();
doc.set_selection(view, Selection::single(0, 0));
Expand Down Expand Up @@ -1805,6 +1814,7 @@ mod test {
text,
None,
Arc::new(ArcSwap::new(Arc::new(Config::default()))),
false,
);
let view = ViewId::default();
doc.set_selection(view, Selection::single(5, 5));
Expand Down
10 changes: 9 additions & 1 deletion helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,11 @@ pub struct Editor {
/// field is set and any old requests are automatically
/// canceled as a result
pub completion_request_handle: Option<oneshot::Sender<()>>,

/// Whether the editor is running in readonly mode
/// If true, files are opened in read-only mode and
/// cannot be written to
pub readonly: bool,
}

pub type RedrawHandle = (Arc<Notify>, Arc<RwLock<()>>);
Expand Down Expand Up @@ -940,6 +945,7 @@ impl Editor {
theme_loader: Arc<theme::Loader>,
syn_loader: Arc<syntax::Loader>,
config: Arc<dyn DynAccess<Config>>,
readonly: bool,
) -> Self {
let language_servers = helix_lsp::Registry::new(syn_loader.clone());
let conf = config.load();
Expand Down Expand Up @@ -986,6 +992,7 @@ impl Editor {
needs_redraw: false,
cursor_cache: Cell::new(None),
completion_request_handle: None,
readonly,
}
}

Expand Down Expand Up @@ -1329,7 +1336,7 @@ impl Editor {
let (rope, encoding, has_bom) = crate::document::from_reader(&mut stdin(), None)?;
Ok(self.new_file_from_document(
action,
Document::from(rope, Some((encoding, has_bom)), self.config.clone()),
Document::from(rope, Some((encoding, has_bom)), self.config.clone(), false),
))
}

Expand All @@ -1346,6 +1353,7 @@ impl Editor {
None,
Some(self.syn_loader.clone()),
self.config.clone(),
self.readonly,
)?;

if let Some(diff_base) = self.diff_providers.get_diff_base(&path) {
Expand Down
5 changes: 5 additions & 0 deletions helix-view/src/gutter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ mod tests {
rope,
None,
Arc::new(ArcSwap::new(Arc::new(Config::default()))),
false,
);

assert_eq!(view.gutters.layout.len(), 5);
Expand All @@ -380,6 +381,7 @@ mod tests {
rope,
None,
Arc::new(ArcSwap::new(Arc::new(Config::default()))),
false,
);

assert_eq!(view.gutters.layout.len(), 1);
Expand All @@ -398,6 +400,7 @@ mod tests {
rope,
None,
Arc::new(ArcSwap::new(Arc::new(Config::default()))),
false,
);

assert_eq!(view.gutters.layout.len(), 2);
Expand All @@ -420,13 +423,15 @@ mod tests {
rope,
None,
Arc::new(ArcSwap::new(Arc::new(Config::default()))),
false,
);

let rope = Rope::from_str("a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl\nm\nn\no\np");
let doc_long = Document::from(
rope,
None,
Arc::new(ArcSwap::new(Arc::new(Config::default()))),
false,
);

assert_eq!(view.gutters.layout.len(), 2);
Expand Down
5 changes: 5 additions & 0 deletions helix-view/src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,7 @@ mod tests {
rope,
None,
Arc::new(ArcSwap::new(Arc::new(Config::default()))),
false,
);

assert_eq!(
Expand Down Expand Up @@ -819,6 +820,7 @@ mod tests {
rope,
None,
Arc::new(ArcSwap::new(Arc::new(Config::default()))),
false,
);
assert_eq!(
view.text_pos_at_screen_coords(
Expand Down Expand Up @@ -848,6 +850,7 @@ mod tests {
rope,
None,
Arc::new(ArcSwap::new(Arc::new(Config::default()))),
false,
);
assert_eq!(
view.text_pos_at_screen_coords(
Expand All @@ -871,6 +874,7 @@ mod tests {
rope,
None,
Arc::new(ArcSwap::new(Arc::new(Config::default()))),
false,
);

assert_eq!(
Expand Down Expand Up @@ -954,6 +958,7 @@ mod tests {
rope,
None,
Arc::new(ArcSwap::new(Arc::new(Config::default()))),
false,
);

assert_eq!(
Expand Down
Loading