AutocompleteProvider.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. 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();
  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. }
  97. void AutocompleteBox::update_suggestions(Vector<AutocompleteProvider::Entry>&& suggestions)
  98. {
  99. if (suggestions.is_empty())
  100. return;
  101. bool has_suggestions = !suggestions.is_empty();
  102. m_suggestion_view->set_model(adopt(*new AutocompleteSuggestionModel(move(suggestions))));
  103. if (!has_suggestions)
  104. m_suggestion_view->selection().clear();
  105. else
  106. m_suggestion_view->selection().set(m_suggestion_view->model()->index(0));
  107. }
  108. bool AutocompleteBox::is_visible() const
  109. {
  110. return m_popup_window->is_visible();
  111. }
  112. void AutocompleteBox::show(Gfx::IntPoint suggstion_box_location)
  113. {
  114. m_popup_window->move_to(suggstion_box_location);
  115. m_popup_window->show();
  116. }
  117. void AutocompleteBox::close()
  118. {
  119. m_popup_window->hide();
  120. }
  121. void AutocompleteBox::next_suggestion()
  122. {
  123. GUI::ModelIndex new_index = m_suggestion_view->selection().first();
  124. if (new_index.is_valid())
  125. new_index = m_suggestion_view->model()->index(new_index.row() + 1);
  126. else
  127. new_index = m_suggestion_view->model()->index(0);
  128. if (m_suggestion_view->model()->is_valid(new_index)) {
  129. m_suggestion_view->selection().set(new_index);
  130. m_suggestion_view->scroll_into_view(new_index, Orientation::Vertical);
  131. }
  132. }
  133. void AutocompleteBox::previous_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::apply_suggestion()
  146. {
  147. if (m_editor.is_null())
  148. return;
  149. if (!m_editor->is_editable())
  150. return;
  151. auto selected_index = m_suggestion_view->selection().first();
  152. if (!selected_index.is_valid())
  153. return;
  154. auto suggestion_index = m_suggestion_view->model()->index(selected_index.row(), AutocompleteSuggestionModel::Column::Name);
  155. auto suggestion = suggestion_index.data().to_string();
  156. size_t partial_length = suggestion_index.data((GUI::ModelRole)AutocompleteSuggestionModel::InternalRole::PartialInputLength).to_i64();
  157. ASSERT(suggestion.length() >= partial_length);
  158. auto completion = suggestion.substring_view(partial_length, suggestion.length() - partial_length);
  159. m_editor->insert_at_cursor_or_replace_selection(completion);
  160. }
  161. }