AutocompleteProvider.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (c) 2020, 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<AutocompleteProvider::Entry>&& 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. };
  32. virtual int row_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override { return m_suggestions.size(); }
  33. virtual int column_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override { return Column::__Column_Count; }
  34. virtual GUI::Variant data(const GUI::ModelIndex& index, GUI::ModelRole role) const override
  35. {
  36. auto& suggestion = m_suggestions.at(index.row());
  37. if (role == GUI::ModelRole::Display) {
  38. if (index.column() == Column::Name) {
  39. if (!suggestion.display_text.is_empty())
  40. return suggestion.display_text;
  41. else
  42. return suggestion.completion;
  43. }
  44. if (index.column() == Column::Icon) {
  45. if (suggestion.language == GUI::AutocompleteProvider::Language::Cpp) {
  46. if (!s_cpp_identifier_icon) {
  47. s_cpp_identifier_icon = Gfx::Bitmap::try_load_from_file("/res/icons/16x16/completion/cpp-identifier.png");
  48. }
  49. return *s_cpp_identifier_icon;
  50. }
  51. if (suggestion.language == GUI::AutocompleteProvider::Language::Unspecified) {
  52. if (!s_unspecified_identifier_icon) {
  53. s_unspecified_identifier_icon = Gfx::Bitmap::try_load_from_file("/res/icons/16x16/completion/unspecified-identifier.png");
  54. }
  55. return *s_unspecified_identifier_icon;
  56. }
  57. return {};
  58. }
  59. }
  60. if ((int)role == InternalRole::PartialInputLength)
  61. return (i64)suggestion.partial_input_length;
  62. if ((int)role == InternalRole::Completion)
  63. return suggestion.completion;
  64. return {};
  65. }
  66. void set_suggestions(Vector<AutocompleteProvider::Entry>&& suggestions) { m_suggestions = move(suggestions); }
  67. private:
  68. Vector<AutocompleteProvider::Entry> m_suggestions;
  69. };
  70. AutocompleteBox::~AutocompleteBox() { }
  71. AutocompleteBox::AutocompleteBox(TextEditor& editor)
  72. : m_editor(editor)
  73. {
  74. m_popup_window = GUI::Window::construct(m_editor->window());
  75. m_popup_window->set_window_type(GUI::WindowType::Tooltip);
  76. m_popup_window->set_rect(0, 0, 175, 25);
  77. auto& main_widget = m_popup_window->set_main_widget<GUI::Widget>();
  78. main_widget.set_fill_with_background_color(true);
  79. main_widget.set_layout<GUI::VerticalBoxLayout>();
  80. m_suggestion_view = main_widget.add<GUI::TableView>();
  81. m_suggestion_view->set_column_headers_visible(false);
  82. m_suggestion_view->set_visible(false);
  83. m_no_suggestions_view = main_widget.add<GUI::Label>("No suggestions");
  84. }
  85. void AutocompleteBox::update_suggestions(Vector<AutocompleteProvider::Entry>&& suggestions)
  86. {
  87. // FIXME: There's a potential race here if, after the user selected an autocomplete suggestion,
  88. // the LanguageServer sends an update and this function is executed before AutocompleteBox::apply_suggestion()
  89. // is executed.
  90. bool has_suggestions = !suggestions.is_empty();
  91. if (m_suggestion_view->model()) {
  92. auto& model = *static_cast<AutocompleteSuggestionModel*>(m_suggestion_view->model());
  93. model.set_suggestions(move(suggestions));
  94. } else {
  95. m_suggestion_view->set_model(adopt_ref(*new AutocompleteSuggestionModel(move(suggestions))));
  96. if (has_suggestions)
  97. m_suggestion_view->set_cursor(m_suggestion_view->model()->index(0), GUI::AbstractView::SelectionUpdate::Set);
  98. }
  99. m_suggestion_view->model()->invalidate();
  100. m_suggestion_view->set_visible(has_suggestions);
  101. m_no_suggestions_view->set_visible(!has_suggestions);
  102. m_popup_window->resize(has_suggestions ? Gfx::IntSize(300, 100) : Gfx::IntSize(175, 25));
  103. m_suggestion_view->update();
  104. }
  105. bool AutocompleteBox::is_visible() const
  106. {
  107. return m_popup_window->is_visible();
  108. }
  109. void AutocompleteBox::show(Gfx::IntPoint suggestion_box_location)
  110. {
  111. if (!m_suggestion_view->model())
  112. return;
  113. m_popup_window->move_to(suggestion_box_location);
  114. if (!is_visible())
  115. m_suggestion_view->move_cursor(GUI::AbstractView::CursorMovement::Home, GUI::AbstractTableView::SelectionUpdate::Set);
  116. m_popup_window->show();
  117. }
  118. void AutocompleteBox::close()
  119. {
  120. m_popup_window->hide();
  121. }
  122. void AutocompleteBox::next_suggestion()
  123. {
  124. GUI::ModelIndex new_index = m_suggestion_view->selection().first();
  125. if (new_index.is_valid())
  126. new_index = m_suggestion_view->model()->index(new_index.row() + 1);
  127. else
  128. new_index = m_suggestion_view->model()->index(0);
  129. if (m_suggestion_view->model()->is_within_range(new_index)) {
  130. m_suggestion_view->selection().set(new_index);
  131. m_suggestion_view->scroll_into_view(new_index, Orientation::Vertical);
  132. }
  133. }
  134. void AutocompleteBox::previous_suggestion()
  135. {
  136. GUI::ModelIndex new_index = m_suggestion_view->selection().first();
  137. if (new_index.is_valid())
  138. new_index = m_suggestion_view->model()->index(new_index.row() - 1);
  139. else
  140. new_index = m_suggestion_view->model()->index(0);
  141. if (m_suggestion_view->model()->is_within_range(new_index)) {
  142. m_suggestion_view->selection().set(new_index);
  143. m_suggestion_view->scroll_into_view(new_index, Orientation::Vertical);
  144. }
  145. }
  146. void AutocompleteBox::apply_suggestion()
  147. {
  148. if (m_editor.is_null())
  149. return;
  150. if (!m_editor->is_editable())
  151. return;
  152. auto selected_index = m_suggestion_view->selection().first();
  153. if (!selected_index.is_valid() || !m_suggestion_view->model()->is_within_range(selected_index))
  154. return;
  155. auto suggestion_index = m_suggestion_view->model()->index(selected_index.row());
  156. auto completion = suggestion_index.data((GUI::ModelRole)AutocompleteSuggestionModel::InternalRole::Completion).to_string();
  157. size_t partial_length = suggestion_index.data((GUI::ModelRole)AutocompleteSuggestionModel::InternalRole::PartialInputLength).to_i64();
  158. VERIFY(completion.length() >= partial_length);
  159. if (!m_editor->has_selection()) {
  160. auto cursor = m_editor->cursor();
  161. VERIFY(m_editor->cursor().column() >= partial_length);
  162. TextPosition start(cursor.line(), cursor.column() - partial_length);
  163. auto end = cursor;
  164. m_editor->delete_text_range(TextRange(start, end));
  165. }
  166. m_editor->insert_at_cursor_or_replace_selection(completion);
  167. }
  168. bool AutocompleteProvider::Declaration::operator==(const AutocompleteProvider::Declaration& other) const
  169. {
  170. return name == other.name && position == other.position && type == other.type && scope == other.scope;
  171. }
  172. bool AutocompleteProvider::ProjectLocation::operator==(const ProjectLocation& other) const
  173. {
  174. return file == other.file && line == other.line && column == other.column;
  175. }
  176. }