AutocompleteProvider.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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/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() { }
  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==(const ProjectLocation&) 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==(const Declaration&) const;
  55. };
  56. virtual void provide_completions(Function<void(Vector<Entry>)>) = 0;
  57. struct TokenInfo {
  58. enum class SemanticType : u32 {
  59. Unknown,
  60. Regular,
  61. Keyword,
  62. Type,
  63. Identifier,
  64. String,
  65. Number,
  66. IncludePath,
  67. PreprocessorStatement,
  68. Comment,
  69. Whitespace,
  70. Function,
  71. Variable,
  72. CustomType,
  73. Namespace,
  74. Member,
  75. Parameter,
  76. } type { SemanticType::Unknown };
  77. size_t start_line { 0 };
  78. size_t start_column { 0 };
  79. size_t end_line { 0 };
  80. size_t end_column { 0 };
  81. };
  82. void attach(TextEditor& editor)
  83. {
  84. VERIFY(!m_editor);
  85. m_editor = editor;
  86. }
  87. void detach() { m_editor.clear(); }
  88. protected:
  89. AutocompleteProvider() { }
  90. WeakPtr<TextEditor> m_editor;
  91. };
  92. class AutocompleteBox final {
  93. public:
  94. explicit AutocompleteBox(TextEditor&);
  95. ~AutocompleteBox();
  96. void update_suggestions(Vector<AutocompleteProvider::Entry>&& suggestions);
  97. bool is_visible() const;
  98. void show(Gfx::IntPoint suggestion_box_location);
  99. void close();
  100. bool has_suggestions() { return m_suggestion_view->model()->row_count() > 0; }
  101. void next_suggestion();
  102. void previous_suggestion();
  103. AutocompleteProvider::Entry::HideAutocompleteAfterApplying apply_suggestion();
  104. private:
  105. WeakPtr<TextEditor> m_editor;
  106. RefPtr<GUI::Window> m_popup_window;
  107. RefPtr<GUI::TableView> m_suggestion_view;
  108. RefPtr<GUI::Label> m_no_suggestions_view;
  109. };
  110. }