SuggestionManager.cpp 6.7 KB

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