diff --git a/book/src/generated/typable-cmd.md b/book/src/generated/typable-cmd.md index 6390ef858e7b..3d476300d22e 100644 --- a/book/src/generated/typable-cmd.md +++ b/book/src/generated/typable-cmd.md @@ -72,3 +72,4 @@ | `:append-output` | Run shell command, appending output after each selection. | | `:pipe` | Pipe each selection to the shell command. | | `:run-shell-command`, `:sh` | Run a shell command | +| `:read`, `:r` | Load a file into buffer | diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index b8f99ff369d2..2710f54b2d4c 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -1,4 +1,4 @@ -use std::ops::Deref; +use std::{io::BufReader, ops::Deref}; use crate::job::Job; @@ -1783,6 +1783,87 @@ fn run_shell_command( Ok(()) } +fn read_file_info_buffer( + cx: &mut compositor::Context, + args: &[Cow], + 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 = from_reader(&mut reader, 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(()) +} + +/// Stripped down version of [`helix_view::document::from_reader`] which is adapted to use encoding_rs::Decoder::read_to_string +fn from_reader( + reader: &mut R, + encoding: &'static helix_core::encoding::Encoding, +) -> anyhow::Result { + let mut buf = [0u8; 8192]; + + let (mut decoder, mut slice, read) = { + let read = reader.read(&mut buf)?; + let decoder = encoding.new_decoder(); + let slice = &buf[..read]; + (decoder, slice, read) + }; + + let mut is_empty = read == 0; + let mut buf_str = String::with_capacity(buf.len()); + + loop { + let mut total_read = 0usize; + + loop { + let (result, read, ..) = + decoder.decode_to_string(&slice[total_read..], &mut buf_str, is_empty); + + total_read += read; + + match result { + helix_core::encoding::CoderResult::InputEmpty => { + debug_assert_eq!(slice.len(), total_read); + break; + } + helix_core::encoding::CoderResult::OutputFull => { + debug_assert!(slice.len() > total_read); + buf_str.reserve(buf.len()) + } + } + } + + if is_empty { + debug_assert_eq!(reader.read(&mut buf)?, 0); + break; + } + + let read = reader.read(&mut buf)?; + slice = &buf[..read]; + is_empty = read == 0; + } + Ok(buf_str) +} + pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[ TypableCommand { name: "quit", @@ -2291,6 +2372,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[ fun: run_shell_command, completer: Some(completers::directory), }, + TypableCommand { + name: "read", + aliases: &["r"], + doc: "Load a file into buffer", + fun: read_file_info_buffer, + completer: Some(completers::filename), + }, ]; pub static TYPABLE_COMMAND_MAP: Lazy> = diff --git a/helix-term/tests/test/commands.rs b/helix-term/tests/test/commands.rs index 114bf22211a5..4b3438691942 100644 --- a/helix-term/tests/test/commands.rs +++ b/helix-term/tests/test/commands.rs @@ -31,6 +31,33 @@ async fn test_write_quit_fail() -> anyhow::Result<()> { } #[tokio::test(flavor = "multi_thread")] +async fn test_read_file() -> anyhow::Result<()> { + let mut file = tempfile::NamedTempFile::new()?; + let expected_content = String::from("some contents"); + let output_file = helpers::temp_file_with_contents(&expected_content)?; + let mut command = String::new(); + let cmd = format!(":r {:?}:w", output_file.path()); + command.push_str(&cmd); + + test_key_sequence( + &mut helpers::AppBuilder::new() + .with_file(file.path(), None) + .build()?, + Some(&command), + Some(&|app| { + assert!(!app.editor.is_err(), "error: {:?}", app.editor.get_status()); + }), + false, + ) + .await?; + + helpers::assert_file_has_content(file.as_file_mut(), expected_content.as_str())?; + + Ok(()) +} + +#[tokio::test(flavor = "multi_thread")] +#[ignore] async fn test_buffer_close_concurrent() -> anyhow::Result<()> { test_key_sequences( &mut helpers::AppBuilder::new().build()?,