AutocompleteProvider.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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/Model.h>
  8. #include <LibGUI/TableView.h>
  9. #include <LibGUI/TextEditor.h>
  10. #include <LibGUI/Window.h>
  11. #include <LibGfx/Bitmap.h>
  12. static RefPtr<Gfx::Bitmap> s_cpp_identifier_icon;
  13. static RefPtr<Gfx::Bitmap> s_unspecified_identifier_icon;
  14. namespace GUI {
  15. class AutocompleteSuggestionModel final : public GUI::Model {
  16. public:
  17. explicit AutocompleteSuggestionModel(Vector<AutocompleteProvider::Entry>&& suggestions)
  18. : m_suggestions(move(suggestions))
  19. {
  20. }
  21. enum Column {
  22. Icon,
  23. Name,
  24. __Column_Count,
  25. };
  26. enum InternalRole {
  27. __ModelRoleCustom = (int)GUI::ModelRole::Custom,
  28. PartialInputLength,
  29. Kind,
  30. };
  31. virtual int row_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override { return m_suggestions.size(); }
  32. virtual int column_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override { return Column::__Column_Count; }
  33. virtual GUI::Variant data(const GUI::ModelIndex& index, GUI::ModelRole role) const override
  34. {
  35. auto& suggestion = m_suggestions.at(index.row());
  36. if (role == GUI::ModelRole::Display) {
  37. if (index.column() == Column::Name) {
  38. return suggestion.completion;
  39. }
  40. if (index.column() == Column::Icon) {
  41. if (suggestion.language == GUI::AutocompleteProvider::Language::Cpp) {
  42. if (!s_cpp_identifier_icon) {
  43. s_cpp_identifier_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/completion/cpp-identifier.png");
  44. }
  45. return *s_cpp_identifier_icon;
  46. }
  47. if (suggestion.language == GUI::AutocompleteProvider::Language::Unspecified) {
  48. if (!s_unspecified_identifier_icon) {
  49. s_unspecified_identifier_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/completion/unspecified-identifier.png");
  50. }
  51. return *s_unspecified_identifier_icon;
  52. }
  53. return {};
  54. }
  55. }
  56. if ((int)role == InternalRole::Kind)
  57. return (u32)suggestion.kind;
  58. if ((int)role == InternalRole::PartialInputLength)
  59. return (i64)suggestion.partial_input_length;
  60. return {};
  61. }
  62. virtual void update() override {};
  63. void set_suggestions(Vector<AutocompleteProvider::Entry>&& suggestions) { m_suggestions = move(suggestions); }
  64. private:
  65. Vector<AutocompleteProvider::Entry> m_suggestions;
  66. };
  67. AutocompleteBox::~AutocompleteBox() { }
  68. AutocompleteBox::AutocompleteBox(TextEditor& editor)
  69. : m_editor(editor)
  70. {
  71. m_popup_window = GUI::Window::construct(m_editor->window());
  72. m_popup_window->set_window_type(GUI::WindowType::Tooltip);
  73. m_popup_window->set_rect(0, 0, 300, 100);
  74. m_suggestion_view = m_popup_window->set_main_widget<GUI::TableView>();
  75. m_suggestion_view->set_column_headers_visible(false);
  76. }
  77. void AutocompleteBox::update_suggestions(Vector<AutocompleteProvider::Entry>&& suggestions)
  78. {
  79. bool has_suggestions = !suggestions.is_empty();
  80. if (m_suggestion_view->model()) {
  81. auto& model = *static_cast<AutocompleteSuggestionModel*>(m_suggestion_view->model());
  82. model.set_suggestions(move(suggestions));
  83. } else {
  84. m_suggestion_view->set_model(adopt_ref(*new AutocompleteSuggestionModel(move(suggestions))));
  85. m_suggestion_view->set_column_width(1, 100);
  86. m_suggestion_view->update();
  87. if (has_suggestions)
  88. m_suggestion_view->set_cursor(m_suggestion_view->model()->index(0), GUI::AbstractView::SelectionUpdate::Set);
  89. }
  90. m_suggestion_view->model()->update();
  91. m_suggestion_view->update();
  92. if (!has_suggestions)
  93. close();
  94. }
  95. bool AutocompleteBox::is_visible() const
  96. {
  97. return m_popup_window->is_visible();
  98. }
  99. void AutocompleteBox::show(Gfx::IntPoint suggestion_box_location)
  100. {
  101. if (!m_suggestion_view->model() || m_suggestion_view->model()->row_count() == 0)
  102. return;
  103. m_popup_window->move_to(suggestion_box_location);
  104. if (!is_visible())
  105. m_suggestion_view->move_cursor(GUI::AbstractView::CursorMovement::Home, GUI::AbstractTableView::SelectionUpdate::Set);
  106. m_popup_window->show();
  107. }
  108. void AutocompleteBox::close()
  109. {
  110. m_popup_window->hide();
  111. }
  112. void AutocompleteBox::next_suggestion()
  113. {
  114. GUI::ModelIndex new_index = m_suggestion_view->selection().first();
  115. if (new_index.is_valid())
  116. new_index = m_suggestion_view->model()->index(new_index.row() + 1);
  117. else
  118. new_index = m_suggestion_view->model()->index(0);
  119. if (m_suggestion_view->model()->is_valid(new_index)) {
  120. m_suggestion_view->selection().set(new_index);
  121. m_suggestion_view->scroll_into_view(new_index, Orientation::Vertical);
  122. }
  123. }
  124. void AutocompleteBox::previous_suggestion()
  125. {
  126. GUI::ModelIndex new_index = m_suggestion_view->selection().first();
  127. if (new_index.is_valid())
  128. new_index = m_suggestion_view->model()->index(new_index.row() - 1);
  129. else
  130. new_index = m_suggestion_view->model()->index(0);
  131. if (m_suggestion_view->model()->is_valid(new_index)) {
  132. m_suggestion_view->selection().set(new_index);
  133. m_suggestion_view->scroll_into_view(new_index, Orientation::Vertical);
  134. }
  135. }
  136. void AutocompleteBox::apply_suggestion()
  137. {
  138. if (m_editor.is_null())
  139. return;
  140. if (!m_editor->is_editable())
  141. return;
  142. auto selected_index = m_suggestion_view->selection().first();
  143. if (!selected_index.is_valid() || !m_suggestion_view->model()->is_valid(selected_index))
  144. return;
  145. auto suggestion_index = m_suggestion_view->model()->index(selected_index.row(), AutocompleteSuggestionModel::Column::Name);
  146. auto suggestion = suggestion_index.data().to_string();
  147. size_t partial_length = suggestion_index.data((GUI::ModelRole)AutocompleteSuggestionModel::InternalRole::PartialInputLength).to_i64();
  148. VERIFY(suggestion.length() >= partial_length);
  149. auto completion_view = suggestion.substring_view(partial_length, suggestion.length() - partial_length);
  150. auto completion_kind = (GUI::AutocompleteProvider::CompletionKind)suggestion_index.data((GUI::ModelRole)AutocompleteSuggestionModel::InternalRole::Kind).as_u32();
  151. String completion;
  152. if (completion_view.ends_with(".h") && completion_kind == GUI::AutocompleteProvider::CompletionKind::SystemInclude)
  153. completion = String::formatted("{}{}", completion_view, ">");
  154. else if (completion_view.ends_with(".h") && completion_kind == GUI::AutocompleteProvider::CompletionKind::ProjectInclude)
  155. completion = String::formatted("{}{}", completion_view, "\"");
  156. else
  157. completion = completion_view;
  158. m_editor->insert_at_cursor_or_replace_selection(completion);
  159. }
  160. bool AutocompleteProvider::Declaration::operator==(const AutocompleteProvider::Declaration& other) const
  161. {
  162. return name == other.name && position == other.position && type == other.type && scope == other.scope;
  163. }
  164. bool AutocompleteProvider::ProjectLocation::operator==(const ProjectLocation& other) const
  165. {
  166. return file == other.file && line == other.line && column == other.column;
  167. }
  168. }