on master help sign in
Implement `hx --tutor` and `:tutor` to load `tutor.txt` (#898)

* Implement `hx --tutor` and `:tutor` to load `tutor.txt` * Document `hx --tutor` and `:tutor` * Change `Document::set_path` to take an `Option` * `Document::set_path` accepts an `Option<&Path>` instead of `&Path`. * Remove `Editor::open_tutor` and make tutor-open functionality use `Editor::open` and `Document::set_path`. * Use `PathBuf::join` Co-authored-by: Ivan Tham <pickfire@riseup.net> * Add comments explaining unsetting tutor path Co-authored-by: Ivan Tham <pickfire@riseup.net>

commite2ed6915373adf27881325ebc4e2c6b98e207af3 authorOmnikar <omnikar5@gmail.com> dateWed 27 Oct 2021, 21:23 (5 years ago) parent3b0c5e9
5 files changed+39−8
book/src/usage.md+1−1 helix-term/src/application.rs+6−1 helix-term/src/args.rs+2 helix-term/src/commands.rs+22−2 helix-view/src/document.rs+8−4
book/src/usage.md+1−1
@@ -2,7 +2,7 @@
22
33(Currently not fully documented, see the [keymappings](./keymap.md) list for more.)
44
5−See [tutor.txt](https://github.com/helix-editor/helix/blob/master/runtime/tutor.txt) for a vimtutor-like introduction.
5+See [tutor.txt](https://github.com/helix-editor/helix/blob/master/runtime/tutor.txt) (accessible via `hx --tutor` or `:tutor`) for a vimtutor-like introduction.
66
77## Registers
88
helix-term/src/application.rs+6−1
@@ -97,7 +97,12 @@ impl Application {
9797 let editor_view = Box::new(ui::EditorView::new(std::mem::take(&mut config.keys)));
9898 compositor.push(editor_view);
9999
100− if !args.files.is_empty() {
100+ if args.load_tutor {
101+ let path = helix_core::runtime_dir().join("tutor.txt");
102+ editor.open(path, Action::VerticalSplit)?;
103+ // Unset path to prevent accidentally saving to the original tutor file.
104+ doc_mut!(editor).set_path(None)?;
105+ } else if !args.files.is_empty() {
101106 let first = &args.files[0]; // we know it's not empty
102107 if first.is_dir() {
103108 std::env::set_current_dir(&first)?;
helix-term/src/args.rs+2
@@ -5,6 +5,7 @@ use std::path::PathBuf;
55pub struct Args {
66 pub display_help: bool,
77 pub display_version: bool,
8+ pub load_tutor: bool,
89 pub verbosity: u64,
910 pub files: Vec<PathBuf>,
1011}
@@ -22,6 +23,7 @@ impl Args {
2223 "--" => break, // stop parsing at this point treat the remaining as files
2324 "--version" => args.display_version = true,
2425 "--help" => args.display_help = true,
26+ "--tutor" => args.load_tutor = true,
2527 arg if arg.starts_with("--") => {
2628 return Err(Error::msg(format!(
2729 "unexpected double dash argument: {}",
helix-term/src/commands.rs+22−2
@@ -1557,7 +1557,8 @@ mod cmd {
15571557 let (_, doc) = current!(cx.editor);
15581558
15591559 if let Some(path) = path {
1560− doc.set_path(path.as_ref()).context("invalid filepath")?;
1560+ doc.set_path(Some(path.as_ref()))
1561+ .context("invalid filepath")?;
15611562 }
15621563 if doc.path().is_none() {
15631564 bail!("cannot write a buffer without a filename");
@@ -2099,6 +2100,18 @@ mod cmd {
20992100 Ok(())
21002101 }
21012102
2103+ fn tutor(
2104+ cx: &mut compositor::Context,
2105+ _args: &[&str],
2106+ _event: PromptEvent,
2107+ ) -> anyhow::Result<()> {
2108+ let path = helix_core::runtime_dir().join("tutor.txt");
2109+ cx.editor.open(path, Action::Replace)?;
2110+ // Unset path to prevent accidentally saving to the original tutor file.
2111+ doc_mut!(cx.editor).set_path(None)?;
2112+ Ok(())
2113+ }
2114+
21022115 pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
21032116 TypableCommand {
21042117 name: "quit",
@@ -2351,7 +2364,14 @@ mod cmd {
23512364 doc: "Open the file in a horizontal split.",
23522365 fun: hsplit,
23532366 completer: Some(completers::filename),
2354− }
2367+ },
2368+ TypableCommand {
2369+ name: "tutor",
2370+ aliases: &[],
2371+ doc: "Open the tutorial.",
2372+ fun: tutor,
2373+ completer: None,
2374+ },
23552375 ];
23562376
23572377 pub static COMMANDS: Lazy<HashMap<&'static str, &'static TypableCommand>> = Lazy::new(|| {
helix-view/src/document.rs+8−4
@@ -368,7 +368,7 @@ impl Document {
368368 let mut doc = Self::from(rope, Some(encoding));
369369
370370 // set the path and try detecting the language
371− doc.set_path(path)?;
371+ doc.set_path(Some(path))?;
372372 if let Some(loader) = config_loader {
373373 doc.detect_language(theme, loader);
374374 }
@@ -553,12 +553,16 @@ impl Document {
553553 self.encoding
554554 }
555555
556− pub fn set_path(&mut self, path: &Path) -> Result<(), std::io::Error> {
557− let path = helix_core::path::get_canonicalized_path(path)?;
556+ pub fn set_path(&mut self, path: Option<&Path>) -> Result<(), std::io::Error> {
557+ let path = if let Some(p) = path {
558+ Some(helix_core::path::get_canonicalized_path(p)?)
559+ } else {
560+ path.map(|p| p.into())
561+ };
558562
559563 // if parent doesn't exist we still want to open the document
560564 // and error out when document is saved
561− self.path = Some(path);
565+ self.path = path;
562566
563567 Ok(())
564568 }