AutocompleteProvider.h 2.1 KB

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