SuggestionManager.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Function.h>
  7. #include <LibLine/SuggestionManager.h>
  8. namespace Line {
  9. CompletionSuggestion::CompletionSuggestion(StringView completion, StringView trailing_trivia, StringView display_trivia, Style style)
  10. : style(style)
  11. , text_string(completion)
  12. , display_trivia_string(display_trivia)
  13. , is_valid(true)
  14. {
  15. Utf8View text_u8 { completion };
  16. Utf8View trivia_u8 { trailing_trivia };
  17. Utf8View display_u8 { display_trivia };
  18. for (auto cp : text_u8)
  19. text.append(cp);
  20. for (auto cp : trivia_u8)
  21. this->trailing_trivia.append(cp);
  22. for (auto cp : display_u8)
  23. this->display_trivia.append(cp);
  24. text_view = Utf32View { text.data(), text.size() };
  25. trivia_view = Utf32View { this->trailing_trivia.data(), this->trailing_trivia.size() };
  26. display_trivia_view = Utf32View { this->display_trivia.data(), this->display_trivia.size() };
  27. }
  28. void SuggestionManager::set_suggestions(Vector<CompletionSuggestion>&& suggestions)
  29. {
  30. m_suggestions = move(suggestions);
  31. // Set the views and make sure we were not given invalid suggestions
  32. for (auto& suggestion : m_suggestions) {
  33. VERIFY(suggestion.is_valid);
  34. suggestion.text_view = { suggestion.text.data(), suggestion.text.size() };
  35. suggestion.trivia_view = { suggestion.trailing_trivia.data(), suggestion.trailing_trivia.size() };
  36. suggestion.display_trivia_view = { suggestion.display_trivia.data(), suggestion.display_trivia.size() };
  37. }
  38. size_t common_suggestion_prefix { 0 };
  39. if (m_suggestions.size() == 1) {
  40. m_largest_common_suggestion_prefix_length = m_suggestions[0].text_view.length();
  41. } else if (m_suggestions.size()) {
  42. u32 last_valid_suggestion_code_point;
  43. for (;; ++common_suggestion_prefix) {
  44. if (m_suggestions[0].text_view.length() <= common_suggestion_prefix)
  45. goto no_more_commons;
  46. last_valid_suggestion_code_point = m_suggestions[0].text_view.code_points()[common_suggestion_prefix];
  47. for (auto& suggestion : m_suggestions) {
  48. if (suggestion.text_view.length() <= common_suggestion_prefix || suggestion.text_view.code_points()[common_suggestion_prefix] != last_valid_suggestion_code_point) {
  49. goto no_more_commons;
  50. }
  51. }
  52. }
  53. no_more_commons:;
  54. m_largest_common_suggestion_prefix_length = common_suggestion_prefix;
  55. } else {
  56. m_largest_common_suggestion_prefix_length = 0;
  57. }
  58. }
  59. void SuggestionManager::next()
  60. {
  61. if (m_suggestions.size())
  62. m_next_suggestion_index = (m_next_suggestion_index + 1) % m_suggestions.size();
  63. else
  64. m_next_suggestion_index = 0;
  65. }
  66. void SuggestionManager::previous()
  67. {
  68. if (m_next_suggestion_index == 0)
  69. m_next_suggestion_index = m_suggestions.size();
  70. m_next_suggestion_index--;
  71. }
  72. CompletionSuggestion const& SuggestionManager::suggest()
  73. {
  74. m_last_shown_suggestion = m_suggestions[m_next_suggestion_index];
  75. m_selected_suggestion_index = m_next_suggestion_index;
  76. return m_last_shown_suggestion;
  77. }
  78. void SuggestionManager::set_current_suggestion_initiation_index(size_t index)
  79. {
  80. auto& suggestion = m_suggestions[m_next_suggestion_index];
  81. if (m_last_shown_suggestion_display_length)
  82. m_last_shown_suggestion.start_index = index - suggestion.static_offset - m_last_shown_suggestion_display_length;
  83. else
  84. m_last_shown_suggestion.start_index = index - suggestion.static_offset - suggestion.invariant_offset;
  85. m_last_shown_suggestion_display_length = m_last_shown_suggestion.text_view.length();
  86. m_last_shown_suggestion_was_complete = true;
  87. }
  88. SuggestionManager::CompletionAttemptResult SuggestionManager::attempt_completion(CompletionMode mode, size_t initiation_start_index)
  89. {
  90. CompletionAttemptResult result { mode };
  91. if (m_next_suggestion_index < m_suggestions.size()) {
  92. auto& next_suggestion = m_suggestions[m_next_suggestion_index];
  93. if (mode == CompletePrefix && !next_suggestion.allow_commit_without_listing) {
  94. result.new_completion_mode = CompletionMode::ShowSuggestions;
  95. result.avoid_committing_to_single_suggestion = true;
  96. m_last_shown_suggestion_display_length = 0;
  97. m_last_shown_suggestion_was_complete = false;
  98. m_last_shown_suggestion = DeprecatedString::empty();
  99. return result;
  100. }
  101. auto can_complete = next_suggestion.invariant_offset <= m_largest_common_suggestion_prefix_length;
  102. ssize_t actual_offset;
  103. size_t shown_length = m_last_shown_suggestion_display_length;
  104. switch (mode) {
  105. case CompletePrefix:
  106. actual_offset = 0;
  107. break;
  108. case ShowSuggestions:
  109. actual_offset = 0 - m_largest_common_suggestion_prefix_length + next_suggestion.invariant_offset;
  110. if (can_complete && next_suggestion.allow_commit_without_listing)
  111. shown_length = m_largest_common_suggestion_prefix_length + m_last_shown_suggestion.trivia_view.length();
  112. break;
  113. default:
  114. if (m_last_shown_suggestion_display_length == 0)
  115. actual_offset = 0;
  116. else
  117. actual_offset = 0 - m_last_shown_suggestion_display_length + next_suggestion.invariant_offset;
  118. break;
  119. }
  120. auto& suggestion = suggest();
  121. set_current_suggestion_initiation_index(initiation_start_index);
  122. result.offset_region_to_remove = { next_suggestion.invariant_offset, shown_length };
  123. result.new_cursor_offset = actual_offset;
  124. result.static_offset_from_cursor = next_suggestion.static_offset;
  125. if (mode == CompletePrefix) {
  126. // Only auto-complete *if possible*.
  127. if (can_complete) {
  128. result.insert.append(suggestion.text_view.substring_view(suggestion.invariant_offset, m_largest_common_suggestion_prefix_length - suggestion.invariant_offset));
  129. m_last_shown_suggestion_display_length = m_largest_common_suggestion_prefix_length;
  130. // Do not increment the suggestion index, as the first tab should only be a *peek*.
  131. if (m_suggestions.size() == 1) {
  132. // If there's one suggestion, commit and forget.
  133. result.new_completion_mode = DontComplete;
  134. // Add in the trivia of the last selected suggestion.
  135. result.insert.append(suggestion.trivia_view);
  136. m_last_shown_suggestion_display_length = 0;
  137. result.style_to_apply = suggestion.style;
  138. m_last_shown_suggestion_was_complete = true;
  139. return result;
  140. }
  141. } else {
  142. m_last_shown_suggestion_display_length = 0;
  143. }
  144. result.new_completion_mode = CompletionMode::ShowSuggestions;
  145. m_last_shown_suggestion_was_complete = false;
  146. m_last_shown_suggestion = DeprecatedString::empty();
  147. } else {
  148. result.insert.append(suggestion.text_view.substring_view(suggestion.invariant_offset, suggestion.text_view.length() - suggestion.invariant_offset));
  149. // Add in the trivia of the last selected suggestion.
  150. result.insert.append(suggestion.trivia_view);
  151. m_last_shown_suggestion_display_length += suggestion.trivia_view.length();
  152. }
  153. } else {
  154. m_next_suggestion_index = 0;
  155. }
  156. return result;
  157. }
  158. ErrorOr<size_t> SuggestionManager::for_each_suggestion(Function<ErrorOr<IterationDecision>(CompletionSuggestion const&, size_t)> callback) const
  159. {
  160. size_t start_index { 0 };
  161. for (auto& suggestion : m_suggestions) {
  162. if (start_index++ < m_last_displayed_suggestion_index)
  163. continue;
  164. if (TRY(callback(suggestion, start_index - 1)) == IterationDecision::Break)
  165. break;
  166. }
  167. return start_index;
  168. }
  169. }