AutocompleteProvider.h 2.1 KB

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