AutocompleteProvider.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibGUI/AutocompleteProvider.h>
  27. #include <LibGUI/Model.h>
  28. #include <LibGUI/TableView.h>
  29. #include <LibGUI/TextEditor.h>
  30. #include <LibGUI/Window.h>
  31. #include <LibGfx/Bitmap.h>
  32. static RefPtr<Gfx::Bitmap> s_cpp_identifier_icon;
  33. static RefPtr<Gfx::Bitmap> s_unspecified_identifier_icon;
  34. namespace GUI {
  35. class AutocompleteSuggestionModel final : public GUI::Model {
  36. public:
  37. explicit AutocompleteSuggestionModel(Vector<AutocompleteProvider::Entry>&& suggestions)
  38. : m_suggestions(move(suggestions))
  39. {
  40. }
  41. enum Column {
  42. Icon,
  43. Name,
  44. __Column_Count,
  45. };
  46. enum InternalRole {
  47. __ModelRoleCustom = (int)GUI::ModelRole::Custom,
  48. PartialInputLength,
  49. Kind,
  50. };
  51. virtual int row_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override { return m_suggestions.size(); }
  52. virtual int column_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override { return Column::__Column_Count; }
  53. virtual GUI::Variant data(const GUI::ModelIndex& index, GUI::ModelRole role) const override
  54. {
  55. auto& suggestion = m_suggestions.at(index.row());
  56. if (role == GUI::ModelRole::Display) {
  57. if (index.column() == Column::Name) {
  58. return suggestion.completion;
  59. }
  60. if (index.column() == Column::Icon) {
  61. if (suggestion.language == GUI::AutocompleteProvider::Language::Cpp) {
  62. if (!s_cpp_identifier_icon) {
  63. s_cpp_identifier_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/completion/cpp-identifier.png");
  64. }
  65. return *s_cpp_identifier_icon;
  66. }
  67. if (suggestion.language == GUI::AutocompleteProvider::Language::Unspecified) {
  68. if (!s_unspecified_identifier_icon) {
  69. s_unspecified_identifier_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/completion/unspecified-identifier.png");
  70. }
  71. return *s_unspecified_identifier_icon;
  72. }
  73. return {};
  74. }
  75. }
  76. if ((int)role == InternalRole::Kind)
  77. return (u32)suggestion.kind;
  78. if ((int)role == InternalRole::PartialInputLength)
  79. return (i64)suggestion.partial_input_length;
  80. return {};
  81. }
  82. virtual void update() override {};
  83. void set_suggestions(Vector<AutocompleteProvider::Entry>&& suggestions) { m_suggestions = move(suggestions); }
  84. private:
  85. Vector<AutocompleteProvider::Entry> m_suggestions;
  86. };
  87. AutocompleteBox::~AutocompleteBox() { }
  88. AutocompleteBox::AutocompleteBox(TextEditor& editor)
  89. : m_editor(editor)
  90. {
  91. m_popup_window = GUI::Window::construct(m_editor->window());
  92. m_popup_window->set_window_type(GUI::WindowType::Tooltip);
  93. m_popup_window->set_rect(0, 0, 200, 100);
  94. m_suggestion_view = m_popup_window->set_main_widget<GUI::TableView>();
  95. m_suggestion_view->set_column_headers_visible(false);
  96. m_suggestion_view->set_column_width(1, 100);
  97. }
  98. void AutocompleteBox::update_suggestions(Vector<AutocompleteProvider::Entry>&& suggestions)
  99. {
  100. bool has_suggestions = !suggestions.is_empty();
  101. if (m_suggestion_view->model()) {
  102. auto& model = *static_cast<AutocompleteSuggestionModel*>(m_suggestion_view->model());
  103. model.set_suggestions(move(suggestions));
  104. } else {
  105. m_suggestion_view->set_model(adopt(*new AutocompleteSuggestionModel(move(suggestions))));
  106. m_suggestion_view->update();
  107. if (has_suggestions)
  108. m_suggestion_view->set_cursor(m_suggestion_view->model()->index(0), GUI::AbstractView::SelectionUpdate::Set);
  109. }
  110. m_suggestion_view->model()->update();
  111. m_suggestion_view->update();
  112. if (!has_suggestions)
  113. close();
  114. }
  115. bool AutocompleteBox::is_visible() const
  116. {
  117. return m_popup_window->is_visible();
  118. }
  119. void AutocompleteBox::show(Gfx::IntPoint suggstion_box_location)
  120. {
  121. if (!m_suggestion_view->model() || m_suggestion_view->model()->row_count() == 0)
  122. return;
  123. m_popup_window->move_to(suggstion_box_location);
  124. if (!is_visible())
  125. m_suggestion_view->move_cursor(GUI::AbstractView::CursorMovement::Home, GUI::AbstractTableView::SelectionUpdate::Set);
  126. m_popup_window->show();
  127. }
  128. void AutocompleteBox::close()
  129. {
  130. m_popup_window->hide();
  131. }
  132. void AutocompleteBox::next_suggestion()
  133. {
  134. GUI::ModelIndex new_index = m_suggestion_view->selection().first();
  135. if (new_index.is_valid())
  136. new_index = m_suggestion_view->model()->index(new_index.row() + 1);
  137. else
  138. new_index = m_suggestion_view->model()->index(0);
  139. if (m_suggestion_view->model()->is_valid(new_index)) {
  140. m_suggestion_view->selection().set(new_index);
  141. m_suggestion_view->scroll_into_view(new_index, Orientation::Vertical);
  142. }
  143. }
  144. void AutocompleteBox::previous_suggestion()
  145. {
  146. GUI::ModelIndex new_index = m_suggestion_view->selection().first();
  147. if (new_index.is_valid())
  148. new_index = m_suggestion_view->model()->index(new_index.row() - 1);
  149. else
  150. new_index = m_suggestion_view->model()->index(0);
  151. if (m_suggestion_view->model()->is_valid(new_index)) {
  152. m_suggestion_view->selection().set(new_index);
  153. m_suggestion_view->scroll_into_view(new_index, Orientation::Vertical);
  154. }
  155. }
  156. void AutocompleteBox::apply_suggestion()
  157. {
  158. if (m_editor.is_null())
  159. return;
  160. if (!m_editor->is_editable())
  161. return;
  162. auto selected_index = m_suggestion_view->selection().first();
  163. if (!selected_index.is_valid())
  164. return;
  165. auto suggestion_index = m_suggestion_view->model()->index(selected_index.row(), AutocompleteSuggestionModel::Column::Name);
  166. auto suggestion = suggestion_index.data().to_string();
  167. size_t partial_length = suggestion_index.data((GUI::ModelRole)AutocompleteSuggestionModel::InternalRole::PartialInputLength).to_i64();
  168. VERIFY(suggestion.length() >= partial_length);
  169. auto completion = suggestion.substring_view(partial_length, suggestion.length() - partial_length);
  170. m_editor->insert_at_cursor_or_replace_selection(completion);
  171. }
  172. }