AutocompleteProvider.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (c) 2020-2022, 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() = default;
  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==(ProjectLocation const&) 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==(Declaration const&) const;
  55. };
  56. virtual void provide_completions(Function<void(Vector<Entry>)>) = 0;
  57. #define FOR_EACH_SEMANTIC_TYPE \
  58. __SEMANTIC(Unknown) \
  59. __SEMANTIC(Regular) \
  60. __SEMANTIC(Keyword) \
  61. __SEMANTIC(Type) \
  62. __SEMANTIC(Identifier) \
  63. __SEMANTIC(String) \
  64. __SEMANTIC(Number) \
  65. __SEMANTIC(IncludePath) \
  66. __SEMANTIC(PreprocessorStatement) \
  67. __SEMANTIC(Comment) \
  68. __SEMANTIC(Whitespace) \
  69. __SEMANTIC(Function) \
  70. __SEMANTIC(Variable) \
  71. __SEMANTIC(CustomType) \
  72. __SEMANTIC(Namespace) \
  73. __SEMANTIC(Member) \
  74. __SEMANTIC(Parameter) \
  75. __SEMANTIC(PreprocessorMacro)
  76. struct TokenInfo {
  77. enum class SemanticType : u32 {
  78. #define __SEMANTIC(x) x,
  79. FOR_EACH_SEMANTIC_TYPE
  80. #undef __SEMANTIC
  81. } type { SemanticType::Unknown };
  82. size_t start_line { 0 };
  83. size_t start_column { 0 };
  84. size_t end_line { 0 };
  85. size_t end_column { 0 };
  86. static constexpr char const* type_to_string(SemanticType t)
  87. {
  88. switch (t) {
  89. #define __SEMANTIC(x) \
  90. case SemanticType::x: \
  91. return #x;
  92. FOR_EACH_SEMANTIC_TYPE
  93. #undef __SEMANTIC
  94. }
  95. VERIFY_NOT_REACHED();
  96. };
  97. };
  98. void attach(TextEditor& editor)
  99. {
  100. VERIFY(!m_editor);
  101. m_editor = editor;
  102. }
  103. void detach() { m_editor.clear(); }
  104. protected:
  105. AutocompleteProvider() = default;
  106. WeakPtr<TextEditor> m_editor;
  107. };
  108. class AutocompleteBox final {
  109. public:
  110. explicit AutocompleteBox(TextEditor&);
  111. ~AutocompleteBox() = default;
  112. void update_suggestions(Vector<AutocompleteProvider::Entry>&& suggestions);
  113. bool is_visible() const;
  114. void show(Gfx::IntPoint suggestion_box_location);
  115. void close();
  116. bool has_suggestions() { return m_suggestion_view->model()->row_count() > 0; }
  117. void next_suggestion();
  118. void previous_suggestion();
  119. AutocompleteProvider::Entry::HideAutocompleteAfterApplying apply_suggestion();
  120. private:
  121. WeakPtr<TextEditor> m_editor;
  122. RefPtr<GUI::Window> m_popup_window;
  123. RefPtr<GUI::TableView> m_suggestion_view;
  124. RefPtr<GUI::Label> m_no_suggestions_view;
  125. };
  126. }