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

feat: paste from file #4171

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions book/src/generated/typable-cmd.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
| `:clipboard-paste-after` | Paste system clipboard after selections. |
| `:clipboard-paste-before` | Paste system clipboard before selections. |
| `:clipboard-paste-replace` | Replace selections with content of system clipboard. |
| `:file-paste-after` | Read a file from disk and paste afer selections. |
| `:file-paste-before` | Read a file from disk and paste before selections. |
| `:primary-clipboard-paste-after` | Paste primary clipboard after selections. |
| `:primary-clipboard-paste-before` | Paste primary clipboard before selections. |
| `:primary-clipboard-paste-replace` | Replace selections with content of system primary clipboard. |
Expand Down
37 changes: 37 additions & 0 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3485,6 +3485,43 @@ fn paste_clipboard_impl(
}
}

pub(crate) fn paste_file_after<P: AsRef<Path>>(
cx: &mut compositor::Context,
paths: &[P],
) -> anyhow::Result<()> {
paste_file_impl(cx.editor, Paste::After, paths, 1)
}
pub(crate) fn paste_file_before<P: AsRef<Path>>(
cx: &mut compositor::Context,
paths: &[P],
) -> anyhow::Result<()> {
paste_file_impl(cx.editor, Paste::Before, paths, 1)
}

fn paste_file_impl<P: AsRef<Path>>(
editor: &mut Editor,
action: Paste,
paths: &[P],
count: usize,
) -> anyhow::Result<()> {
use std::io::Read;

// reverse the paths[] as its more intuative to expect the order to be respected
// paths[0], paths[1], paths[..N] when calling the function
for path in paths.iter().rev() {
let mut file =
std::fs::File::open(&path).context(format!("unable to open {:?}", path.as_ref()))?;
let mut contents = String::new();
file.read_to_string(&mut contents)
.context(format!("unable to read {:?}", path.as_ref()))?;

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

paste_impl(&[contents], doc, view, action, count);
}
Ok(())
}

fn paste_clipboard_after(cx: &mut Context) {
let _ = paste_clipboard_impl(
cx.editor,
Expand Down
56 changes: 56 additions & 0 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,48 @@ fn paste_clipboard_before(
paste_clipboard_impl(cx.editor, Paste::Before, ClipboardType::Clipboard, 1)
}

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

ensure!(!args.is_empty(), "wrong argument count");
let paths: Vec<PathBuf> = args
.iter()
.map(|arg| {
let (path, _) = args::parse_file(arg);
path
})
.collect();

paste_file_impl(cx.editor, Paste::After, &paths, 1)
}

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

ensure!(!args.is_empty(), "wrong argument count");
let paths: Vec<PathBuf> = args
.iter()
.map(|arg| {
let (path, _) = args::parse_file(arg);
path
})
.collect();

paste_file_impl(cx.editor, Paste::Before, &paths, 1)
}

fn paste_primary_clipboard_after(
cx: &mut compositor::Context,
_args: &[Cow<str>],
Expand Down Expand Up @@ -1862,6 +1904,20 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
fun: replace_selections_with_clipboard,
completer: None,
},
TypableCommand {
name: "file-paste-after",
aliases: &[],
doc: "Read a file from disk and paste afer selections.",
fun: paste_file_after,
completer: Some(completers::filename),
},
TypableCommand {
name: "file-paste-before",
aliases: &[],
doc: "Read a file from disk and paste before selections.",
fun: paste_file_before,
completer: Some(completers::filename),
},
TypableCommand {
name: "primary-clipboard-paste-after",
aliases: &[],
Expand Down
29 changes: 22 additions & 7 deletions helix-term/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,28 @@ pub fn file_picker(root: PathBuf, config: &helix_view::editor::Config) -> FilePi
files,
root,
move |cx, path: &PathBuf, action| {
if let Err(e) = cx.editor.open(path, action) {
let err = if let Some(err) = e.source() {
format!("{}", err)
} else {
format!("unable to open \"{}\"", path.display())
};
cx.editor.set_error(err);
if let Err(e) = match action {
helix_view::editor::Action::PasteBefore => {
crate::commands::paste_file_before(cx, &[path.clone()])
}
helix_view::editor::Action::PasteAfter => {
crate::commands::paste_file_after(cx, &[path.clone()])
}
_ => {
if let Err(e) = cx.editor.open(path, action) {
let err = if let Some(err) = e.source() {
format!("{}", err)
} else {
format!("unable to open \"{}\"", path.display())
};

Err(anyhow::anyhow!(err))
} else {
anyhow::Ok(())
}
}
} {
cx.editor.set_error(e.to_string());
}
},
|_editor, path| Some((path.clone(), None)),
Expand Down
6 changes: 6 additions & 0 deletions helix-term/src/ui/picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,12 @@ impl<T: Item + 'static> Component for Picker<T> {
ctrl!('t') => {
self.toggle_preview();
}
ctrl!('o') => {
if let Some(option) = self.selection() {
(self.callback_fn)(cx, option, Action::PasteAfter);
}
return close_fn;
}
_ => {
self.prompt_handle_event(event, cx);
}
Expand Down
5 changes: 5 additions & 0 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,8 @@ pub enum Action {
Replace,
HorizontalSplit,
VerticalSplit,
PasteBefore,
PasteAfter,
}

/// Error thrown on failed document closed
Expand Down Expand Up @@ -1018,6 +1020,9 @@ impl Editor {
let doc = doc_mut!(self, &id);
doc.ensure_view_init(view_id);
}
Action::PasteBefore | Action::PasteAfter => {
// do nothing?
}
}

self._refresh();
Expand Down