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

Implement vi-mode "Yank" (copy): #868

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

deephbz
Copy link

@deephbz deephbz commented Dec 31, 2024

Implement vi-mode "Yank" (copy):
- Vi Command;
- Editor Command;
- Editor methods;
- Mode changes back to Normal after yank

Depends on #867 (the first 3 commits of this PR are included in 867)

Copy link
Member

@sholderbach sholderbach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for giving this a shot and putting in the work! One thing that makes me hesitant about this, that the copying we do does not work with multipliers at all as each EditCommand will update the clipboard. Also operations you define in terms of two EditCommands suffer the same unfortunate fate.

One solution to that would be to do extra state management around clipboard but keep the old edit commands (suggested in #631 ) or otherwise rethink how we implement the vi mode interface to use an internal selection build up from the motion before performing the action. The latter would require way fewer additional EditCommand to be maintained, like the ones you propose here. Related issues where that came up was the impl of #849 where there are cases where the chaining of EditCommands did not achieve the expected result.

@deephbz
Copy link
Author

deephbz commented Jan 1, 2025

Thanks for giving this a shot and putting in the work! One thing that makes me hesitant about this, that the copying we do does not work with multipliers at all as each EditCommand will update the clipboard. Also operations you define in terms of two EditCommands suffer the same unfortunate fate.

One solution to that would be to do extra state management around clipboard but keep the old edit commands (suggested in #631 ) or otherwise rethink how we implement the vi mode interface to use an internal selection build up from the motion before performing the action. The latter would require way fewer additional EditCommand to be maintained, like the ones you propose here. Related issues where that came up was the impl of #849 where there are cases where the chaining of EditCommands did not achieve the expected result.

What you say are valid concerns and I do agree in the future a re-think/refactor is needed to make Vi mode elegant and better.
However, I think it's still of great utility (and also harmless) to merge this PR in order to make Nushell's Vi mode "feature-complete", in a sense that 99% users' 99% use cases are covered. It's really common to yank and paste (like moving CLI options) so missing it is a barrier to onboard new years.

@deephbz deephbz requested a review from sholderbach January 4, 2025 01:46
Comment on lines +249 to +262
Self::YankInside(left) if is_valid_change_inside_left(left) => {
let right = bracket_for(left);
vec![
ReedlineOption::Edit(EditCommand::CopyLeftBefore(*left)),
ReedlineOption::Edit(EditCommand::CopyRightBefore(right)),
]
}
Self::YankInside(right) if is_valid_change_inside_right(right) => {
let left = bracket_for(right);
vec![
ReedlineOption::Edit(EditCommand::CopyLeftBefore(left)),
ReedlineOption::Edit(EditCommand::CopyRightBefore(*right)),
]
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As those are guranteed to be broken for all cases where you are not on the beginning of the text object (putting the left half in the clipboard and then overwriting with the right half) I would omit them until we have a better solution. Yes to shipping but let's not ship broken stuff.

Comment on lines +118 to +127
(Some(Command::Delete), ParseResult::Incomplete)
| (Some(Command::DeleteChar), ParseResult::Incomplete)
| (Some(Command::DeleteToEnd), ParseResult::Incomplete)
| (Some(Command::Delete), ParseResult::Valid(_))
| (Some(Command::DeleteChar), ParseResult::Valid(_))
| (Some(Command::DeleteToEnd), ParseResult::Valid(_))
| (Some(Command::Yank), ParseResult::Valid(_))
| (Some(Command::Yank), ParseResult::Incomplete)
| (Some(Command::YankInside(_)), ParseResult::Valid(_))
| (Some(Command::YankInside(_)), ParseResult::Incomplete) => Some(ViMode::Normal),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not forget to rebase/merge onto the updated main branch when landing #867

Comment on lines +702 to +710
pub(crate) fn copy_from_start(&mut self) {
let insertion_offset = self.line_buffer.insertion_point();
if insertion_offset > 0 {
self.cut_buffer.set(
&self.line_buffer.get_buffer()[..insertion_offset],
ClipboardMode::Normal,
);
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't gone through all of those to check if their indexing is correct. That's an annoying part to deal with this level of duplication for the different versions referring to the same text-objects/motions.

Comment on lines +270 to +274
/// Copy from the start of the buffer to the insertion point
CopyFromStart,

/// Copy from the start of the current line to the insertion point
CopyFromLineStart,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the users trying to configure their emacs mode having all those extra variants certainly adds some potential for confusion, and when we remove them for a simpler VI mode more breaking changes where we hope people don't try to depend on them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants