Repository: helix-editor/helix
File: helix-term/src/commands/typed.rs (Rust)

Code before:
    cx: &mut compositor::Context,
    args: &[Cow<str>],
    event: PromptEvent,
) -> anyhow::Result<()> {
    if event != PromptEvent::Validate {
        return Ok(());
    }

    let (view, doc) = current!(cx.editor);

    ensure!(!args.is_empty(), "file name is expected");

    let filename = args.get(0).unwrap();
    let path = PathBuf::from(filename.to_string());
    if !path.exists() {
        bail!("file doesn't exist: {}", filename);
    }

    let file = std::fs::File::open(path).map_err(|err| anyhow!("error reading file {}", err))?;
    let mut reader = BufReader::new(file);
    let (contents, _, _) = read_to_string(&mut reader, Some(doc.encoding()))
        .map_err(|err| anyhow!("error reading file: {}", err))?;
    let contents = Tendril::from(contents);
    let selection = doc.selection(view.id);
    let transaction = Transaction::insert(doc.text(), selection, contents);
    doc.apply(&transaction, view.id);

    Ok(())
}

pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
    TypableCommand {
        name: "quit",
        aliases: &["q"],
        doc: "Close the current view.",
        fun: quit,
        signature: CommandSignature::none(),
    },
    TypableCommand {
        name: "quit!",
        aliases: &["q!"],
        doc: "Force close the current view, ignoring unsaved changes.",
        fun: force_quit,
        signature: CommandSignature::none(),
    },
    TypableCommand {
        name: "open",
        aliases: &["o"],
        doc: "Open a file from disk into the current view.",
        fun: open,
        signature: CommandSignature::all(completers::filename),

Review comment (suggestion):
```suggestion
    doc.apply(&transaction, view.id);
    doc.append_changes_to_history(view);
    view.ensure_cursor_in_view(doc, scrolloff);
```

Typable commands don't automatically do these steps so we need to do them explicitly. `scrolloff` is `cx.editor.config().scrolloff`

Code after:
    if event != PromptEvent::Validate {
        return Ok(());
    }

    let scrolloff = cx.editor.config().scrolloff;
    let (view, doc) = current!(cx.editor);

    ensure!(!args.is_empty(), "file name is expected");
    ensure!(args.len() == 1, "only the file name is expected");

    let filename = args.get(0).unwrap();
    let path = PathBuf::from(filename.to_string());
    ensure!(
        path.exists() && path.is_file(),
        "path is not a file: {:?}",
        path
    );

    let file = std::fs::File::open(path).map_err(|err| anyhow!("error opening file: {}", err))?;
    let mut reader = BufReader::new(file);
    let (contents, _, _) = read_to_string(&mut reader, Some(doc.encoding()))
        .map_err(|err| anyhow!("error reading file: {}", err))?;
    let contents = Tendril::from(contents);
    let selection = doc.selection(view.id);
    let transaction = Transaction::insert(doc.text(), selection, contents);
    doc.apply(&transaction, view.id);
    doc.append_changes_to_history(view);
    view.ensure_cursor_in_view(doc, scrolloff);

    Ok(())
}

pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
    TypableCommand {
        name: "quit",
        aliases: &["q"],
        doc: "Close the current view.",
        fun: quit,
        signature: CommandSignature::none(),
    },
    TypableCommand {
        name: "quit!",
        aliases: &["q!"],
        doc: "Force close the current view, ignoring unsaved changes.",
        fun: force_quit,
        signature: CommandSignature::none(),
    },
    TypableCommand {
        name: "open",
        aliases: &["o"],
        doc: "Open a file from disk into the current view.",