AutocompleteProvider.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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/TextEditor.h>
  10. #include <LibGUI/Window.h>
  11. #include <LibIPC/Decoder.h>
  12. namespace GUI {
  13. class AutocompleteProvider {
  14. AK_MAKE_NONCOPYABLE(AutocompleteProvider);
  15. AK_MAKE_NONMOVABLE(AutocompleteProvider);
  16. public:
  17. virtual ~AutocompleteProvider() { }
  18. enum class Language {
  19. Unspecified,
  20. Cpp,
  21. };
  22. struct Entry {
  23. String completion;
  24. size_t partial_input_length { 0 };
  25. Language language { Language::Unspecified };
  26. String display_text {};
  27. };
  28. struct ProjectLocation {
  29. String file;
  30. size_t line { 0 };
  31. size_t column { 0 };
  32. bool operator==(const ProjectLocation&) const;
  33. };
  34. enum class DeclarationType {
  35. Function,
  36. Struct,
  37. Class,
  38. Variable,
  39. PreprocessorDefinition,
  40. Namespace,
  41. Member,
  42. };
  43. struct Declaration {
  44. String name;
  45. ProjectLocation position;
  46. DeclarationType type;
  47. String scope;
  48. bool operator==(const Declaration&) const;
  49. };
  50. virtual void provide_completions(Function<void(Vector<Entry>)>) = 0;
  51. void attach(TextEditor& editor)
  52. {
  53. VERIFY(!m_editor);
  54. m_editor = editor;
  55. }
  56. void detach() { m_editor.clear(); }
  57. protected:
  58. AutocompleteProvider() { }
  59. WeakPtr<TextEditor> m_editor;
  60. };
  61. class AutocompleteBox final {
  62. public:
  63. explicit AutocompleteBox(TextEditor&);
  64. ~AutocompleteBox();
  65. void update_suggestions(Vector<AutocompleteProvider::Entry>&& suggestions);
  66. bool is_visible() const;
  67. void show(Gfx::IntPoint suggestion_box_location);
  68. void close();
  69. void next_suggestion();
  70. void previous_suggestion();
  71. void apply_suggestion();
  72. private:
  73. WeakPtr<TextEditor> m_editor;
  74. RefPtr<GUI::Window> m_popup_window;
  75. RefPtr<GUI::TableView> m_suggestion_view;
  76. RefPtr<GUI::Label> m_no_suggestions_view;
  77. };
  78. }