-
-
Notifications
You must be signed in to change notification settings - Fork 21.1k
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
Add Duplicate Lines shortcut to CodeTextEditor #66553
Conversation
5a01ec3
to
bc668c4
Compare
65c9a01
to
711307d
Compare
Added support for multiple carets |
919f7d7
to
e95dc4f
Compare
I'll do a code review in a sec, but can you argue for why you think this feature should be added? We don't accept features just because PRs exist for them. I personally think Ctrl+Shift+D does what I want just fine. If I have no selections, this PR does the same, just adjusts the caret in one of the scenarios. If I want to duplicate multiple lines, I'm just going to do a proper selection with triple-clicking and then dragging to a line's end - both easy and very well-known functionalities. |
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.
Haven't tested the feature itself, just a code review.
editor/code_editor.cpp
Outdated
int select_from_column = -1; | ||
int select_to_column = -1; |
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.
select_from_column
and select_to_column
are unnecessary, this is line duplication. You only used this variable once.
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 tried to remove them, but when the text gets inserted, the selection gets deleted, which means that I need to store them before changing the caret. And I think that I should transfer the selection to the duplication since it means that you can perform the same duplication multiple times in a row. Which is something I use very often.
editor/code_editor.cpp
Outdated
int select_from_line = -1; | ||
int select_to_line = -1; |
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.
You don't use these much outside of places where has_selection()
is checked. You should define them close to the scope where the variables have a meaning.
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.
If I put them in their own scope I would need to write duplicate code.
I would love this feature. I have never once wanted to duplicate a subsection within a line using a hotkey, especially if that selection spans multiple lines, I couldn't imagine a scenario in which I would want to duplicate that content in-place. |
@torcado194 That makes sense, I think "Duplicate Lines" is a hard thing to argue against actually. What about this particular implementation, with separate "below" and "above" versions? That's the main part I find unnecessary. |
I agree, I hardly ever use "duplicate above". "below" is the default direction for the duplicate line action for any editor I use (and is what I'd expect). the "duplicate above" result can be easily substituted using the keyboard by duplicating below and swapping the new selected lines upward, at the cost of additional keystrokes equal to the line count. |
I tried to recreate the feature from VS Code and there they have both a above and below version. But I agree I hardly use it either. |
e95dc4f
to
4c7c4ca
Compare
I will merge all commits into one, once you are done reviewing |
4c7c4ca
to
98cb988
Compare
@MewPurPur @Paulb23 This PR has just been hanging here for quite some time. Can you please finish reviewing it. I would really like it to be merged. I think it adds some good keyboard shortcuts which can really improve your productivity. |
I still think it should be just "Duplicate Lines" that does it below, instead of above and below versions. It's not like regular duplicate is "before" and "after" I don't got much else to say, the code to me looks good and my remarks have been clarified or addressed. |
98cb988
to
731968a
Compare
d9cbdad
to
fef53e8
Compare
Done I also dropped the commit which replaces the |
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.
Code looks fine, few minor comments.
I'm wanting to move things like this into CodeEdit
, could then take advantage of unit tests as well. If you fancy doing that :)
editor/code_editor.cpp
Outdated
text_editor->deselect(caret_index); | ||
|
||
text_editor->insert_text_at_caret(insert_text, caret_index); | ||
text_editor->set_caret_line(new_caret_line, true, true, 0, caret_index); |
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.
Should set adjust_veiwport to false
here, and leave it up to set_caret_column
editor/code_editor.cpp
Outdated
} | ||
} | ||
|
||
text_editor->merge_overlapping_carets(); |
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.
Are we expecting carets to overlap here?
editor/code_editor.cpp
Outdated
} | ||
|
||
// The text will be inserted at the end of the current line. | ||
text_editor->set_caret_column(text_editor->get_line(text_editor->get_caret_line(caret_index)).length(), true, caret_index); |
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.
No need to adjust_veiwport here either
editor/code_editor.cpp
Outdated
new_caret_line = select_to_line + select_num_lines; | ||
|
||
for (int i = select_from_line; i <= select_to_line; i++) { | ||
text_editor->unfold_line(i); |
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.
Can unfold in the same loop above
@Paulb23 I moved the code into |
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.
Thanks!
One small change, could you bind it to to the CodeEdit
API? and again would appreciate any unit tests.
5baed4e
to
2c4dd12
Compare
@Paulb23 After a rebase and fixing a merge conflict I added unit tests and bound |
b8c78df
to
ba799fc
Compare
@@ -2395,6 +2399,8 @@ void ScriptTextEditor::register_editor() { | |||
ED_SHORTCUT("script_text_editor/unfold_all_lines", TTR("Unfold All Lines"), Key::NONE); | |||
ED_SHORTCUT("script_text_editor/duplicate_selection", TTR("Duplicate Selection"), KeyModifierMask::SHIFT | KeyModifierMask::CTRL | Key::D); | |||
ED_SHORTCUT_OVERRIDE("script_text_editor/duplicate_selection", "macos", KeyModifierMask::SHIFT | KeyModifierMask::META | Key::C); | |||
ED_SHORTCUT("script_text_editor/duplicate_lines", TTR("Duplicate Lines"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::ALT | Key::DOWN); | |||
ED_SHORTCUT_OVERRIDE("script_text_editor/duplicate_lines", "macos", KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::ALT | Key::Z); |
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.
Is this a common macOS shortcut for this purpose?
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 don't know. Honestly I just put anything there because it would have been a duplicate otherwise
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.
Would be good to know what's the expected macOS shortcut for this operation (e.g. in VS Code or Xcode). CC @bruvzg @coppolaemilio
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 just looked it up. On VS Code its shift+cmd+down
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.
That might work. Seems like we use this mapping but only for PC here, and there's a different override for macOS:
// core/input/input_map.cpp
inputs = List<Ref<InputEvent>>();
inputs.push_back(InputEventKey::create_reference(Key::DOWN | KeyModifierMask::SHIFT | KeyModifierMask::CMD_OR_CTRL));
default_builtin_cache.insert("ui_text_caret_add_below", inputs);
inputs = List<Ref<InputEvent>>();
inputs.push_back(InputEventKey::create_reference(Key::L | KeyModifierMask::SHIFT | KeyModifierMask::CMD_OR_CTRL));
default_builtin_cache.insert("ui_text_caret_add_below.macos", inputs);
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 changed it on macos to cmd_or_ctrl+shift+down
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.
Consensus seems to be for this, at a glance the style looks fine.
Could you please squash your commits into one? Make sure that the final commit has a short but descriptive message (the title of this PR is a good option). See this documentation, if you need help with squashing. |
00d7bf4
to
64bcb2f
Compare
@YuriSizov @Paulb23 @MewPurPur The commits are squashed. Everything is ready. Thank you for approving this PR. |
This keyboard shortcut has been made with inspiration from the VS Code keyboard shortcut editor.action.copyLinesDownAction. It duplicates all selected lines and inserts them below no matter where the caret is within the line.
64bcb2f
to
d2e651f
Compare
Thanks! |
This PR adds the shortcut
script_text_editor/duplicate_lines
. The default buttons areCtrl+Alt+Down
. For macOS the default buttons areCmd+Shift+Down
. These shortcuts are similiar to the VS Code commandeditor.action.copyLinesDownAction
. If no text is selected it behaves the same asduplicate_selection
, but if text is selected it will duplicate the whole lines of the selection.This also supports multiple carets.
script_text_editor/duplicate_lines
:script_text_editor/duplicate_selection
: