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

Merged
merged 1 commit into from
Feb 11, 2025
Merged

Conversation

deephbz
Copy link
Contributor

@deephbz deephbz commented Dec 31, 2024

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

Note only last commit in this PR should be reviewed as part of this PR as this is on top of:

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
Contributor 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.

Copy link
Contributor Author

@deephbz deephbz Jan 18, 2025

Choose a reason for hiding this comment

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

This was indeed my mistake initially. Thanks for helping review! This specific issue should be addressed in #874 , atomic Cut/CopyInside pair of brackets/quotes.

Comment on lines 118 to 132
(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 +743
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.

@deephbz deephbz force-pushed the feature/vi-yank branch 2 times, most recently from bb76b01 to 1f73f9a Compare January 18, 2025 10:25
@deephbz
Copy link
Contributor Author

deephbz commented Jan 18, 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.

Indeed I see a common pattern Move/Cut/Copy + selection range (word, big word, line end, etc.). 3+ actions plus ~10 selections give us numerous EditCommands.

@132ikl
Copy link

132ikl commented Feb 8, 2025

is anything blocking this? I think we're all in agreement that the vi mode interface should be reworked eventually but this PR seems like a helpful stop-gap for now

@deephbz
Copy link
Contributor Author

deephbz commented Feb 9, 2025

is anything blocking this? I think we're all in agreement that the vi mode interface should be reworked eventually but this PR seems like a helpful stop-gap for now

I don't think there's any block except for the dependent PRs #867 , #874.
I've personally used this branch for months on a daily basis without any issue and the Vi mode works perfectly. I'm open to owners modify my above PRs if that help get things merged.

@sholderbach
Copy link
Member

Should be good for a rebase/merge following the merge of #867.

Is the refactor proposed/documented in #874 already part of this branch or am I misreading this?

Otherwise I am good with landing this (@132ikl if you want to look into something feel free to chime in otherwise I would give the green light after resolving the branch)

@deephbz
Copy link
Contributor Author

deephbz commented Feb 11, 2025

Should be good for a rebase/merge following the merge of #867.

Is the refactor proposed/documented in #874 already part of this branch or am I misreading this?

Otherwise I am good with landing this (@132ikl if you want to look into something feel free to chime in otherwise I would give the green light after resolving the branch)

I've rebased this to be on top of #874. Current PR includes all of #874 with one additional commit for Vi-Yank. Once 874 is merged, we can merge this PR with a single-clean commit for yank.

@132ikl
Copy link

132ikl commented Feb 11, 2025

sounds good, let me or @sholderbach know when you update the branch

- Vi Command;
- Editor Command;
- Editor methods;
- Mode changes back to Normal after yank
@deephbz
Copy link
Contributor Author

deephbz commented Feb 11, 2025

sounds good, let me or @sholderbach know when you update the branch

Done ready for review :D

@132ikl 132ikl merged commit 446fb1d into nushell:main Feb 11, 2025
6 checks passed
@132ikl
Copy link

132ikl commented Feb 11, 2025

thank you!!

@deephbz deephbz deleted the feature/vi-yank branch February 12, 2025 00:13
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.

3 participants