AutocompleteProvider.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Copyright (c) 2020-2022, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibCodeComprehension/Types.h>
  8. #include <LibGUI/Forward.h>
  9. #include <LibGUI/Label.h>
  10. #include <LibGUI/TableView.h>
  11. #include <LibGUI/TextEditor.h>
  12. #include <LibGUI/Window.h>
  13. #include <LibIPC/Decoder.h>
  14. namespace GUI {
  15. class AutocompleteProvider {
  16. AK_MAKE_NONCOPYABLE(AutocompleteProvider);
  17. AK_MAKE_NONMOVABLE(AutocompleteProvider);
  18. public:
  19. virtual ~AutocompleteProvider() = default;
  20. virtual void provide_completions(Function<void(Vector<CodeComprehension::AutocompleteResultEntry>)>) = 0;
  21. void attach(TextEditor& editor)
  22. {
  23. VERIFY(!m_editor);
  24. m_editor = editor;
  25. }
  26. void detach() { m_editor.clear(); }
  27. protected:
  28. AutocompleteProvider() = default;
  29. WeakPtr<TextEditor> m_editor;
  30. };
  31. class AutocompleteBox final {
  32. public:
  33. explicit AutocompleteBox(TextEditor&);
  34. ~AutocompleteBox() = default;
  35. void update_suggestions(Vector<CodeComprehension::AutocompleteResultEntry>&& suggestions);
  36. bool is_visible() const;
  37. void show(Gfx::IntPoint suggestion_box_location);
  38. void close();
  39. bool has_suggestions() { return m_suggestion_view->model()->row_count() > 0; }
  40. void next_suggestion();
  41. void previous_suggestion();
  42. CodeComprehension::AutocompleteResultEntry::HideAutocompleteAfterApplying apply_suggestion();
  43. private:
  44. WeakPtr<TextEditor> m_editor;
  45. RefPtr<GUI::Window> m_popup_window;
  46. RefPtr<GUI::TableView> m_suggestion_view;
  47. RefPtr<GUI::Label> m_no_suggestions_view;
  48. };
  49. }