AutocompleteProvider.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibGUI/Forward.h>
  8. #include <LibGUI/Label.h>
  9. #include <LibGUI/TableView.h>
  10. #include <LibGUI/TextEditor.h>
  11. #include <LibGUI/Window.h>
  12. #include <LibIPC/Decoder.h>
  13. namespace GUI {
  14. class AutocompleteProvider {
  15. AK_MAKE_NONCOPYABLE(AutocompleteProvider);
  16. AK_MAKE_NONMOVABLE(AutocompleteProvider);
  17. public:
  18. virtual ~AutocompleteProvider() { }
  19. enum class Language {
  20. Unspecified,
  21. Cpp,
  22. };
  23. struct Entry {
  24. String completion;
  25. size_t partial_input_length { 0 };
  26. Language language { Language::Unspecified };
  27. String display_text {};
  28. enum class HideAutocompleteAfterApplying {
  29. No,
  30. Yes,
  31. };
  32. HideAutocompleteAfterApplying hide_autocomplete_after_applying { HideAutocompleteAfterApplying::Yes };
  33. };
  34. struct ProjectLocation {
  35. String file;
  36. size_t line { 0 };
  37. size_t column { 0 };
  38. bool operator==(const ProjectLocation&) const;
  39. };
  40. enum class DeclarationType {
  41. Function,
  42. Struct,
  43. Class,
  44. Variable,
  45. PreprocessorDefinition,
  46. Namespace,
  47. Member,
  48. };
  49. struct Declaration {
  50. String name;
  51. ProjectLocation position;
  52. DeclarationType type;
  53. String scope;
  54. bool operator==(const Declaration&) const;
  55. };
  56. virtual void provide_completions(Function<void(Vector<Entry>)>) = 0;
  57. void attach(TextEditor& editor)
  58. {
  59. VERIFY(!m_editor);
  60. m_editor = editor;
  61. }
  62. void detach() { m_editor.clear(); }
  63. protected:
  64. AutocompleteProvider() { }
  65. WeakPtr<TextEditor> m_editor;
  66. };
  67. class AutocompleteBox final {
  68. public:
  69. explicit AutocompleteBox(TextEditor&);
  70. ~AutocompleteBox();
  71. void update_suggestions(Vector<AutocompleteProvider::Entry>&& suggestions);
  72. bool is_visible() const;
  73. void show(Gfx::IntPoint suggestion_box_location);
  74. void close();
  75. bool has_suggestions() { return m_suggestion_view->model()->row_count() > 0; }
  76. void next_suggestion();
  77. void previous_suggestion();
  78. AutocompleteProvider::Entry::HideAutocompleteAfterApplying apply_suggestion();
  79. private:
  80. WeakPtr<TextEditor> m_editor;
  81. RefPtr<GUI::Window> m_popup_window;
  82. RefPtr<GUI::TableView> m_suggestion_view;
  83. RefPtr<GUI::Label> m_no_suggestions_view;
  84. };
  85. }