SuggestionManager.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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(const StringView& completion, const 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. const CompletionSuggestion& 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. if (m_last_shown_suggestion_display_length)
  72. m_last_shown_suggestion.start_index = index - m_next_suggestion_static_offset - m_last_shown_suggestion_display_length;
  73. else
  74. m_last_shown_suggestion.start_index = index - m_next_suggestion_static_offset - m_next_suggestion_invariant_offset;
  75. m_last_shown_suggestion_display_length = m_last_shown_suggestion.text_view.length();
  76. m_last_shown_suggestion_was_complete = true;
  77. }
  78. SuggestionManager::CompletionAttemptResult SuggestionManager::attempt_completion(CompletionMode mode, size_t initiation_start_index)
  79. {
  80. CompletionAttemptResult result { mode };
  81. if (m_next_suggestion_index < m_suggestions.size()) {
  82. auto can_complete = m_next_suggestion_invariant_offset <= m_largest_common_suggestion_prefix_length;
  83. ssize_t actual_offset;
  84. size_t shown_length = m_last_shown_suggestion_display_length;
  85. switch (mode) {
  86. case CompletePrefix:
  87. actual_offset = 0;
  88. break;
  89. case ShowSuggestions:
  90. actual_offset = 0 - m_largest_common_suggestion_prefix_length + m_next_suggestion_invariant_offset;
  91. if (can_complete)
  92. shown_length = m_largest_common_suggestion_prefix_length + m_last_shown_suggestion.trivia_view.length();
  93. break;
  94. default:
  95. if (m_last_shown_suggestion_display_length == 0)
  96. actual_offset = 0;
  97. else
  98. actual_offset = 0 - m_last_shown_suggestion_display_length + m_next_suggestion_invariant_offset;
  99. break;
  100. }
  101. result.offset_region_to_remove = { m_next_suggestion_invariant_offset, shown_length };
  102. result.new_cursor_offset = actual_offset;
  103. auto& suggestion = suggest();
  104. set_current_suggestion_initiation_index(initiation_start_index);
  105. if (mode == CompletePrefix) {
  106. // Only auto-complete *if possible*.
  107. if (can_complete) {
  108. result.insert.append(suggestion.text_view.substring_view(m_next_suggestion_invariant_offset, m_largest_common_suggestion_prefix_length - m_next_suggestion_invariant_offset));
  109. m_last_shown_suggestion_display_length = m_largest_common_suggestion_prefix_length;
  110. // Do not increment the suggestion index, as the first tab should only be a *peek*.
  111. if (m_suggestions.size() == 1) {
  112. // If there's one suggestion, commit and forget.
  113. result.new_completion_mode = DontComplete;
  114. // Add in the trivia of the last selected suggestion.
  115. result.insert.append(suggestion.trivia_view);
  116. m_last_shown_suggestion_display_length = 0;
  117. result.style_to_apply = suggestion.style;
  118. m_last_shown_suggestion_was_complete = true;
  119. return result;
  120. }
  121. } else {
  122. m_last_shown_suggestion_display_length = 0;
  123. }
  124. result.new_completion_mode = CompletionMode::ShowSuggestions;
  125. m_last_shown_suggestion_was_complete = false;
  126. m_last_shown_suggestion = String::empty();
  127. } else {
  128. result.insert.append(suggestion.text_view.substring_view(m_next_suggestion_invariant_offset, suggestion.text_view.length() - m_next_suggestion_invariant_offset));
  129. // Add in the trivia of the last selected suggestion.
  130. result.insert.append(suggestion.trivia_view);
  131. m_last_shown_suggestion_display_length += suggestion.trivia_view.length();
  132. }
  133. } else {
  134. m_next_suggestion_index = 0;
  135. }
  136. return result;
  137. }
  138. size_t SuggestionManager::for_each_suggestion(Function<IterationDecision(const CompletionSuggestion&, size_t)> callback) const
  139. {
  140. size_t start_index { 0 };
  141. for (auto& suggestion : m_suggestions) {
  142. if (start_index++ < m_last_displayed_suggestion_index)
  143. continue;
  144. if (callback(suggestion, start_index - 1) == IterationDecision::Break)
  145. break;
  146. }
  147. return start_index;
  148. }
  149. }