-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Auto indent change if selection is linewise #7316
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the general utility and I think @archseer had something similar in mind. However, I think the details should be improved: The current implementation doesn't work with multi cursors. Changing that should be easy (just perform the check for all selection ranges). I also think that the condition should be more strict: We should only insert a new line if the selections exactly covers 1 line (and not just when it starts at a line start)
I took some time to be used to the newline char and when it is selected and hit If sometimes it doesn't happens, the editor will not following a standard. I would prefer to add this as default or something like it:
or |
I found another implementation that is simpler and handles more edge case using fn delete_selection_impl(cx: &mut Context, op: Operation) {
let (view, doc) = current!(cx.editor);
let selection = doc.selection(view.id);
if cx.register != Some('_') {
// first yank the selection
let text = doc.text().slice(..);
let values: Vec<String> = selection.fragments(text).map(Cow::into_owned).collect();
let reg_name = cx.register.unwrap_or('"');
cx.editor.registers.write(reg_name, values);
};
if matches!(op, Operation::Change) {
trim_selections(cx);
}
let (view, doc) = current!(cx.editor);
let selection = doc.selection(view.id);
// then delete
let transaction =
Transaction::delete_by_selection(doc.text(), selection, |range| (range.from(), range.to()));
doc.apply(&transaction, view.id);
match op {
Operation::Delete => {
// exit select mode, if currently in select mode
exit_select_mode(cx);
}
Operation::Change => {
enter_insert_mode(cx);
}
}
} The current implementation with multiple cursors doesn't work if at least one selection doesn't cover a whole line, but this one does. |
Isn't that the same as using |
I agree in principle, _c is only one more keystroke and there's huge value in the consistency helix currently has. Although I think there's probably more value in making the default keybinds as ergonomic as possible. Most of the time _c is what I want to do. Vim makes the same choice with Maybe a solution is to implement the default |
I like the idea of introducing a new command. I think while this command is a bit less consistent than the current binding I think this is such a common usecase that it's worth introducing. Regarding the new implementation you posted I think I prefer the current implementation. |
Thanks for your feedback, I will implement it as a new command then. |
I've implemented as a new command. I've also added a "no yank" version, as it also exists for the previous change command. If you want to try it out, please add this to your config: [keys.normal]
"c" = "change_selection_with_indent"
[keys.select]
"c" = "change_selection_with_indent" |
This looks good to me for the most part but I prefer a better name for the command. I think we can make this the default mapping too |
Actually I think just having the one command ( I've tinkered around with |
This reverts commit 7192f20.
This make
c
auto indent if the selection begins by a whole line.I inspired from the other functions in
command.rs
. I'm not sure if the implementation is very good, all the text editing functions in the code base are new to me.Resolve #4507 #2783
This also works if multiple lines are selected. Otherwise it behaves exactly as before.
Why not use a custom key bindings for this?
For me if you select a whole line and go to insert mod with
c
, you want the auto indentation behavior all of the time.This is also consistent with the Vim
V c
orcc
behavior.This PR aims to be a QOL improvement, in the same spirit than #5837