diff options
author | Kyle Lanmon <kyle.lanmon@gmail.com> | 2022-11-02 23:32:12 -0500 |
---|---|---|
committer | Andrew Kaster <andrewdkaster@gmail.com> | 2022-11-27 18:28:43 -0700 |
commit | 62a3de0c0a46642cf2ac01de3aa2f0f69ffaeeca (patch) | |
tree | 43545b89776907bb6e0654a837e70421c426780f | |
parent | ad250dd4849e4d0907e64201d03e4a68a3d7401d (diff) | |
download | serenity-62a3de0c0a46642cf2ac01de3aa2f0f69ffaeeca.zip |
TextEditor: Add keyboard shortcut to (un)comment a range of lines
-rw-r--r-- | Userland/Libraries/LibGUI/TextEditor.cpp | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/Userland/Libraries/LibGUI/TextEditor.cpp b/Userland/Libraries/LibGUI/TextEditor.cpp index e6ecc4cf98..897f02e532 100644 --- a/Userland/Libraries/LibGUI/TextEditor.cpp +++ b/Userland/Libraries/LibGUI/TextEditor.cpp @@ -1008,6 +1008,30 @@ void TextEditor::keydown_event(KeyEvent& event) return; } + if (event.ctrl() && event.key() == KeyCode::Key_Slash) { + if (m_highlighter != nullptr) { + auto prefix = m_highlighter->comment_prefix().value_or(""sv); + auto suffix = m_highlighter->comment_suffix().value_or(""sv); + auto range = has_selection() ? selection() : TextRange { { m_cursor.line(), m_cursor.column() }, { m_cursor.line(), m_cursor.column() } }; + + auto is_already_commented = true; + for (size_t i = range.start().line(); i <= range.end().line(); i++) { + auto text = m_document->line(i).to_utf8().trim_whitespace(); + if (!(text.starts_with(prefix) && text.ends_with(suffix))) { + is_already_commented = false; + break; + } + } + + if (is_already_commented) + execute<UncommentSelection>(prefix, suffix, range); + else + execute<CommentSelection>(prefix, suffix, range); + + return; + } + } + event.ignore(); } |