AutocompleteProvider.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Copyright (c) 2020-2022, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGUI/AutocompleteProvider.h>
  7. #include <LibGUI/BoxLayout.h>
  8. #include <LibGUI/Model.h>
  9. #include <LibGUI/TableView.h>
  10. #include <LibGUI/TextEditor.h>
  11. #include <LibGUI/Window.h>
  12. #include <LibGfx/Bitmap.h>
  13. static RefPtr<Gfx::Bitmap> s_cpp_identifier_icon;
  14. static RefPtr<Gfx::Bitmap> s_unspecified_identifier_icon;
  15. namespace GUI {
  16. class AutocompleteSuggestionModel final : public GUI::Model {
  17. public:
  18. explicit AutocompleteSuggestionModel(Vector<CodeComprehension::AutocompleteResultEntry>&& suggestions)
  19. : m_suggestions(move(suggestions))
  20. {
  21. }
  22. enum Column {
  23. Icon,
  24. Name,
  25. __Column_Count,
  26. };
  27. enum InternalRole {
  28. __ModelRoleCustom = (int)GUI::ModelRole::Custom,
  29. PartialInputLength,
  30. Completion,
  31. HideAutocompleteAfterApplying,
  32. };
  33. virtual int row_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override { return m_suggestions.size(); }
  34. virtual int column_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override { return Column::__Column_Count; }
  35. virtual GUI::Variant data(const GUI::ModelIndex& index, GUI::ModelRole role) const override
  36. {
  37. auto& suggestion = m_suggestions.at(index.row());
  38. if (role == GUI::ModelRole::Display) {
  39. if (index.column() == Column::Name) {
  40. if (!suggestion.display_text.is_empty())
  41. return suggestion.display_text;
  42. else
  43. return suggestion.completion;
  44. }
  45. if (index.column() == Column::Icon) {
  46. if (suggestion.language == CodeComprehension::Language::Cpp) {
  47. if (!s_cpp_identifier_icon) {
  48. s_cpp_identifier_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/completion/cpp-identifier.png"sv).release_value_but_fixme_should_propagate_errors();
  49. }
  50. return *s_cpp_identifier_icon;
  51. }
  52. if (suggestion.language == CodeComprehension::Language::Unspecified) {
  53. if (!s_unspecified_identifier_icon) {
  54. s_unspecified_identifier_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/completion/unspecified-identifier.png"sv).release_value_but_fixme_should_propagate_errors();
  55. }
  56. return *s_unspecified_identifier_icon;
  57. }
  58. return {};
  59. }
  60. }
  61. if ((int)role == InternalRole::PartialInputLength)
  62. return (i64)suggestion.partial_input_length;
  63. if ((int)role == InternalRole::Completion)
  64. return suggestion.completion;
  65. if ((int)role == InternalRole::HideAutocompleteAfterApplying)
  66. return suggestion.hide_autocomplete_after_applying == CodeComprehension::AutocompleteResultEntry::HideAutocompleteAfterApplying::Yes;
  67. return {};
  68. }
  69. void set_suggestions(Vector<CodeComprehension::AutocompleteResultEntry>&& suggestions) { m_suggestions = move(suggestions); }
  70. private:
  71. Vector<CodeComprehension::AutocompleteResultEntry> m_suggestions;
  72. };
  73. AutocompleteBox::AutocompleteBox(TextEditor& editor)
  74. : m_editor(editor)
  75. {
  76. m_popup_window = GUI::Window::construct(m_editor->window());
  77. m_popup_window->set_window_type(GUI::WindowType::Autocomplete);
  78. m_popup_window->set_obey_widget_min_size(false);
  79. m_popup_window->set_rect(0, 0, 175, 25);
  80. auto main_widget = m_popup_window->set_main_widget<GUI::Widget>().release_value_but_fixme_should_propagate_errors();
  81. main_widget->set_fill_with_background_color(true);
  82. main_widget->set_layout<GUI::VerticalBoxLayout>();
  83. m_suggestion_view = main_widget->add<GUI::TableView>();
  84. m_suggestion_view->set_frame_shadow(Gfx::FrameShadow::Plain);
  85. m_suggestion_view->set_frame_thickness(1);
  86. m_suggestion_view->set_column_headers_visible(false);
  87. m_suggestion_view->set_visible(false);
  88. m_suggestion_view->on_activation = [&](GUI::ModelIndex const& index) {
  89. if (!m_suggestion_view->model()->is_within_range(index))
  90. return;
  91. m_suggestion_view->selection().set(index);
  92. m_suggestion_view->scroll_into_view(index, Orientation::Vertical);
  93. apply_suggestion();
  94. };
  95. m_no_suggestions_view = main_widget->add<GUI::Label>("No suggestions");
  96. }
  97. void AutocompleteBox::update_suggestions(Vector<CodeComprehension::AutocompleteResultEntry>&& suggestions)
  98. {
  99. // FIXME: There's a potential race here if, after the user selected an autocomplete suggestion,
  100. // the LanguageServer sends an update and this function is executed before AutocompleteBox::apply_suggestion()
  101. // is executed.
  102. bool has_suggestions = !suggestions.is_empty();
  103. if (m_suggestion_view->model()) {
  104. auto& model = *static_cast<AutocompleteSuggestionModel*>(m_suggestion_view->model());
  105. model.set_suggestions(move(suggestions));
  106. } else {
  107. m_suggestion_view->set_model(adopt_ref(*new AutocompleteSuggestionModel(move(suggestions))));
  108. }
  109. m_suggestion_view->model()->invalidate();
  110. if (has_suggestions)
  111. m_suggestion_view->set_cursor(m_suggestion_view->model()->index(0), GUI::AbstractView::SelectionUpdate::Set);
  112. m_suggestion_view->set_visible(has_suggestions);
  113. m_suggestion_view->set_focus(has_suggestions);
  114. m_no_suggestions_view->set_visible(!has_suggestions);
  115. m_popup_window->resize(has_suggestions ? Gfx::IntSize(300, 100) : Gfx::IntSize(175, 25));
  116. m_suggestion_view->update();
  117. }
  118. bool AutocompleteBox::is_visible() const
  119. {
  120. return m_popup_window->is_visible();
  121. }
  122. void AutocompleteBox::show(Gfx::IntPoint suggestion_box_location)
  123. {
  124. if (!m_suggestion_view->model())
  125. return;
  126. m_popup_window->move_to(suggestion_box_location);
  127. m_popup_window->show();
  128. }
  129. void AutocompleteBox::close()
  130. {
  131. m_popup_window->hide();
  132. }
  133. void AutocompleteBox::next_suggestion()
  134. {
  135. m_suggestion_view->move_cursor(GUI::AbstractView::CursorMovement::Down, GUI::AbstractView::SelectionUpdate::Set);
  136. }
  137. void AutocompleteBox::previous_suggestion()
  138. {
  139. m_suggestion_view->move_cursor(GUI::AbstractView::CursorMovement::Up, GUI::AbstractView::SelectionUpdate::Set);
  140. }
  141. CodeComprehension::AutocompleteResultEntry::HideAutocompleteAfterApplying AutocompleteBox::apply_suggestion()
  142. {
  143. auto hide_when_done = CodeComprehension::AutocompleteResultEntry::HideAutocompleteAfterApplying::Yes;
  144. if (m_editor.is_null())
  145. return hide_when_done;
  146. if (!m_editor->is_editable())
  147. return hide_when_done;
  148. auto selected_index = m_suggestion_view->selection().first();
  149. if (!selected_index.is_valid() || !m_suggestion_view->model()->is_within_range(selected_index))
  150. return hide_when_done;
  151. auto suggestion_index = m_suggestion_view->model()->index(selected_index.row());
  152. auto completion = suggestion_index.data((GUI::ModelRole)AutocompleteSuggestionModel::InternalRole::Completion).to_deprecated_string();
  153. size_t partial_length = suggestion_index.data((GUI::ModelRole)AutocompleteSuggestionModel::InternalRole::PartialInputLength).to_i64();
  154. auto hide_after_applying = suggestion_index.data((GUI::ModelRole)AutocompleteSuggestionModel::InternalRole::HideAutocompleteAfterApplying).to_bool();
  155. if (!hide_after_applying)
  156. hide_when_done = CodeComprehension::AutocompleteResultEntry::HideAutocompleteAfterApplying::No;
  157. VERIFY(completion.length() >= partial_length);
  158. if (!m_editor->has_selection()) {
  159. auto cursor = m_editor->cursor();
  160. VERIFY(m_editor->cursor().column() >= partial_length);
  161. TextPosition start(cursor.line(), cursor.column() - partial_length);
  162. auto end = cursor;
  163. m_editor->delete_text_range(TextRange(start, end));
  164. }
  165. m_editor->insert_at_cursor_or_replace_selection(completion);
  166. return hide_when_done;
  167. }
  168. }