GTextDocument.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include <LibGUI/GTextDocument.h>
  2. #include <ctype.h>
  3. GTextDocument::GTextDocument(Client* client)
  4. {
  5. if (client)
  6. m_clients.set(client);
  7. append_line(make<GTextDocumentLine>(*this));
  8. }
  9. void GTextDocument::set_text(const StringView& text)
  10. {
  11. m_spans.clear();
  12. remove_all_lines();
  13. int start_of_current_line = 0;
  14. auto add_line = [&](int current_position) {
  15. int line_length = current_position - start_of_current_line;
  16. auto line = make<GTextDocumentLine>(*this);
  17. if (line_length)
  18. line->set_text(*this, text.substring_view(start_of_current_line, current_position - start_of_current_line));
  19. append_line(move(line));
  20. start_of_current_line = current_position + 1;
  21. };
  22. int i = 0;
  23. for (i = 0; i < text.length(); ++i) {
  24. if (text[i] == '\n')
  25. add_line(i);
  26. }
  27. add_line(i);
  28. }
  29. int GTextDocumentLine::first_non_whitespace_column() const
  30. {
  31. for (int i = 0; i < length(); ++i) {
  32. if (!isspace(m_text[i]))
  33. return i;
  34. }
  35. return length();
  36. }
  37. GTextDocumentLine::GTextDocumentLine(GTextDocument& document)
  38. {
  39. clear(document);
  40. }
  41. GTextDocumentLine::GTextDocumentLine(GTextDocument& document, const StringView& text)
  42. {
  43. set_text(document, text);
  44. }
  45. void GTextDocumentLine::clear(GTextDocument& document)
  46. {
  47. m_text.clear();
  48. m_text.append(0);
  49. document.update_views({});
  50. }
  51. void GTextDocumentLine::set_text(GTextDocument& document, const StringView& text)
  52. {
  53. if (text.length() == length() && !memcmp(text.characters_without_null_termination(), characters(), length()))
  54. return;
  55. if (text.is_empty()) {
  56. clear(document);
  57. return;
  58. }
  59. m_text.resize(text.length() + 1);
  60. memcpy(m_text.data(), text.characters_without_null_termination(), text.length() + 1);
  61. document.update_views({});
  62. }
  63. void GTextDocumentLine::append(GTextDocument& document, const char* characters, int length)
  64. {
  65. int old_length = m_text.size() - 1;
  66. m_text.resize(m_text.size() + length);
  67. memcpy(m_text.data() + old_length, characters, length);
  68. m_text.last() = 0;
  69. document.update_views({});
  70. }
  71. void GTextDocumentLine::append(GTextDocument& document, char ch)
  72. {
  73. insert(document, length(), ch);
  74. }
  75. void GTextDocumentLine::prepend(GTextDocument& document, char ch)
  76. {
  77. insert(document, 0, ch);
  78. }
  79. void GTextDocumentLine::insert(GTextDocument& document, int index, char ch)
  80. {
  81. if (index == length()) {
  82. m_text.last() = ch;
  83. m_text.append(0);
  84. } else {
  85. m_text.insert(index, move(ch));
  86. }
  87. document.update_views({});
  88. }
  89. void GTextDocumentLine::remove(GTextDocument& document, int index)
  90. {
  91. if (index == length()) {
  92. m_text.take_last();
  93. m_text.last() = 0;
  94. } else {
  95. m_text.remove(index);
  96. }
  97. document.update_views({});
  98. }
  99. void GTextDocumentLine::truncate(GTextDocument& document, int length)
  100. {
  101. m_text.resize(length + 1);
  102. m_text.last() = 0;
  103. document.update_views({});
  104. }
  105. void GTextDocument::append_line(NonnullOwnPtr<GTextDocumentLine> line)
  106. {
  107. lines().append(move(line));
  108. for (auto* client : m_clients)
  109. client->document_did_append_line();
  110. }
  111. void GTextDocument::insert_line(int line_index, NonnullOwnPtr<GTextDocumentLine> line)
  112. {
  113. lines().insert(line_index, move(line));
  114. for (auto* client : m_clients)
  115. client->document_did_insert_line(line_index);
  116. }
  117. void GTextDocument::remove_line(int line_index)
  118. {
  119. lines().remove(line_index);
  120. for (auto* client : m_clients)
  121. client->document_did_remove_line(line_index);
  122. }
  123. void GTextDocument::remove_all_lines()
  124. {
  125. lines().clear();
  126. for (auto* client : m_clients)
  127. client->document_did_remove_all_lines();
  128. }
  129. GTextDocument::Client::~Client()
  130. {
  131. }
  132. void GTextDocument::register_client(Client& client)
  133. {
  134. m_clients.set(&client);
  135. }
  136. void GTextDocument::unregister_client(Client& client)
  137. {
  138. m_clients.remove(&client);
  139. }
  140. void GTextDocument::update_views(Badge<GTextDocumentLine>)
  141. {
  142. for (auto* client : m_clients)
  143. client->document_did_change();
  144. }