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

Select text inserted by shell or paste #4458

Merged
merged 1 commit into from
Nov 4, 2022
Merged
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
22 changes: 20 additions & 2 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3458,6 +3458,8 @@ fn paste_impl(values: &[String], doc: &mut Document, view: &mut View, action: Pa
let text = doc.text();
let selection = doc.selection(view.id);

let mut ranges = SmallVec::with_capacity(selection.len());

let transaction = Transaction::change_by_selection(text, selection, |range| {
let pos = match (action, linewise) {
// paste linewise before
Expand All @@ -3474,8 +3476,21 @@ fn paste_impl(values: &[String], doc: &mut Document, view: &mut View, action: Pa
// paste at cursor
(Paste::Cursor, _) => range.cursor(text.slice(..)),
};
(pos, pos, values.next())

let value = values.next();

let value_len = value
.as_ref()
.map(|content| content.chars().count())
.unwrap_or_default();

ranges.push(Range::new(pos, pos + value_len));

(pos, pos, value)
});

let transaction = transaction.with_selection(Selection::new(ranges, selection.primary_index()));

apply_transaction(&transaction, doc, view);
}

Expand Down Expand Up @@ -4714,6 +4729,7 @@ fn shell(cx: &mut compositor::Context, cmd: &str, behavior: &ShellBehavior) {
let selection = doc.selection(view.id);

let mut changes = Vec::with_capacity(selection.len());
let mut ranges = SmallVec::with_capacity(selection.len());
let text = doc.text().slice(..);

for range in selection.ranges() {
Expand All @@ -4737,11 +4753,13 @@ fn shell(cx: &mut compositor::Context, cmd: &str, behavior: &ShellBehavior) {
ShellBehavior::Append => (range.to(), range.to()),
_ => (range.from(), range.from()),
};
ranges.push(Range::new(to, to + output.chars().count()));
changes.push((from, to, Some(output)));
}

if behavior != &ShellBehavior::Ignore {
let transaction = Transaction::change(doc.text(), changes.into_iter());
let transaction = Transaction::change(doc.text(), changes.into_iter())
.with_selection(Selection::new(ranges, selection.primary_index()));
apply_transaction(&transaction, doc, view);
doc.append_changes_to_history(view.id);
}
Expand Down