AutocompleteProvider.h 2.0 KB

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