AutocompleteProvider.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. // TODO
  62. if (suggestion.language == GUI::AutocompleteProvider::Language::Cpp) {
  63. if (!s_cpp_identifier_icon) {
  64. s_cpp_identifier_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/completion/cpp-identifier.png");
  65. }
  66. return *s_cpp_identifier_icon;
  67. }
  68. if (suggestion.language == GUI::AutocompleteProvider::Language::Unspecified) {
  69. if (!s_unspecified_identifier_icon) {
  70. s_unspecified_identifier_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/completion/unspecified-identifier.png");
  71. }
  72. return *s_unspecified_identifier_icon;
  73. }
  74. return {};
  75. }
  76. }
  77. if ((int)role == InternalRole::Kind)
  78. return (u32)suggestion.kind;
  79. if ((int)role == InternalRole::PartialInputLength)
  80. return (i64)suggestion.partial_input_length;
  81. return {};
  82. }
  83. virtual void update() override {};
  84. void set_suggestions(Vector<AutocompleteProvider::Entry>&& suggestions) { m_suggestions = move(suggestions); }
  85. private:
  86. Vector<AutocompleteProvider::Entry> m_suggestions;
  87. };
  88. AutocompleteBox::~AutocompleteBox() { }
  89. AutocompleteBox::AutocompleteBox(TextEditor& editor)
  90. : m_editor(editor)
  91. {
  92. m_popup_window = GUI::Window::construct(m_editor->window());
  93. m_popup_window->set_window_type(GUI::WindowType::Tooltip);
  94. m_popup_window->set_rect(0, 0, 200, 100);
  95. m_suggestion_view = m_popup_window->set_main_widget<GUI::TableView>();
  96. m_suggestion_view->set_column_headers_visible(false);
  97. m_suggestion_view->set_column_width(1, 100);
  98. }
  99. void AutocompleteBox::update_suggestions(Vector<AutocompleteProvider::Entry>&& suggestions)
  100. {
  101. bool has_suggestions = !suggestions.is_empty();
  102. if (m_suggestion_view->model()) {
  103. auto& model = *static_cast<AutocompleteSuggestionModel*>(m_suggestion_view->model());
  104. model.set_suggestions(move(suggestions));
  105. } else {
  106. m_suggestion_view->set_model(adopt(*new AutocompleteSuggestionModel(move(suggestions))));
  107. m_suggestion_view->update();
  108. if (has_suggestions)
  109. m_suggestion_view->set_cursor(m_suggestion_view->model()->index(0), GUI::AbstractView::SelectionUpdate::Set);
  110. }
  111. m_suggestion_view->model()->update();
  112. m_suggestion_view->update();
  113. if (!has_suggestions)
  114. close();
  115. }
  116. bool AutocompleteBox::is_visible() const
  117. {
  118. return m_popup_window->is_visible();
  119. }
  120. void AutocompleteBox::show(Gfx::IntPoint suggstion_box_location)
  121. {
  122. if (!m_suggestion_view->model() || m_suggestion_view->model()->row_count() == 0)
  123. return;
  124. m_popup_window->move_to(suggstion_box_location);
  125. if (!is_visible())
  126. m_suggestion_view->move_cursor(GUI::AbstractView::CursorMovement::Home, GUI::AbstractTableView::SelectionUpdate::Set);
  127. m_popup_window->show();
  128. }
  129. void AutocompleteBox::close()
  130. {
  131. m_popup_window->hide();
  132. }
  133. void AutocompleteBox::next_suggestion()
  134. {
  135. GUI::ModelIndex new_index = m_suggestion_view->selection().first();
  136. if (new_index.is_valid())
  137. new_index = m_suggestion_view->model()->index(new_index.row() + 1);
  138. else
  139. new_index = m_suggestion_view->model()->index(0);
  140. if (m_suggestion_view->model()->is_valid(new_index)) {
  141. m_suggestion_view->selection().set(new_index);
  142. m_suggestion_view->scroll_into_view(new_index, Orientation::Vertical);
  143. }
  144. }
  145. void AutocompleteBox::previous_suggestion()
  146. {
  147. GUI::ModelIndex new_index = m_suggestion_view->selection().first();
  148. if (new_index.is_valid())
  149. new_index = m_suggestion_view->model()->index(new_index.row() - 1);
  150. else
  151. new_index = m_suggestion_view->model()->index(0);
  152. if (m_suggestion_view->model()->is_valid(new_index)) {
  153. m_suggestion_view->selection().set(new_index);
  154. m_suggestion_view->scroll_into_view(new_index, Orientation::Vertical);
  155. }
  156. }
  157. void AutocompleteBox::apply_suggestion()
  158. {
  159. if (m_editor.is_null())
  160. return;
  161. if (!m_editor->is_editable())
  162. return;
  163. auto selected_index = m_suggestion_view->selection().first();
  164. if (!selected_index.is_valid())
  165. return;
  166. auto suggestion_index = m_suggestion_view->model()->index(selected_index.row(), AutocompleteSuggestionModel::Column::Name);
  167. auto suggestion = suggestion_index.data().to_string();
  168. size_t partial_length = suggestion_index.data((GUI::ModelRole)AutocompleteSuggestionModel::InternalRole::PartialInputLength).to_i64();
  169. VERIFY(suggestion.length() >= partial_length);
  170. auto completion = suggestion.substring_view(partial_length, suggestion.length() - partial_length);
  171. m_editor->insert_at_cursor_or_replace_selection(completion);
  172. }
  173. }