SuggestionManager.cpp 7.3 KB

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