1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 05:58:11 +00:00

TextEditor: Add keyboard shortcut to (un)comment a range of lines

This commit is contained in:
Kyle Lanmon 2022-11-02 23:32:12 -05:00 committed by Andrew Kaster
parent ad250dd484
commit 62a3de0c0a

View file

@ -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();
}