RegularEditingEngine.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/QuickSort.h>
  27. #include <LibGUI/RegularEditingEngine.h>
  28. #include <LibGUI/TextEditor.h>
  29. namespace GUI {
  30. CursorWidth RegularEditingEngine::cursor_width() const
  31. {
  32. return CursorWidth::NARROW;
  33. }
  34. bool RegularEditingEngine::on_key(const KeyEvent& event)
  35. {
  36. if (EditingEngine::on_key(event))
  37. return true;
  38. if (event.key() == KeyCode::Key_Escape) {
  39. if (m_editor->on_escape_pressed)
  40. m_editor->on_escape_pressed();
  41. return true;
  42. }
  43. if (event.alt() && event.shift() && event.key() == KeyCode::Key_S) {
  44. sort_selected_lines();
  45. return true;
  46. }
  47. return false;
  48. }
  49. static int strcmp_utf32(const u32* s1, const u32* s2, size_t n)
  50. {
  51. while (n-- > 0) {
  52. if (*s1++ != *s2++)
  53. return s1[-1] < s2[-1] ? -1 : 1;
  54. }
  55. return 0;
  56. }
  57. void RegularEditingEngine::sort_selected_lines()
  58. {
  59. if (!m_editor->is_editable())
  60. return;
  61. if (!m_editor->has_selection())
  62. return;
  63. size_t first_line;
  64. size_t last_line;
  65. get_selection_line_boundaries(first_line, last_line);
  66. auto& lines = m_editor->document().lines();
  67. auto start = lines.begin() + (int)first_line;
  68. auto end = lines.begin() + (int)last_line + 1;
  69. quick_sort(start, end, [](auto& a, auto& b) {
  70. return strcmp_utf32(a.code_points(), b.code_points(), min(a.length(), b.length())) < 0;
  71. });
  72. m_editor->did_change();
  73. m_editor->update();
  74. }
  75. }