GMLAutocompleteProvider.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "GMLAutocompleteProvider.h"
  7. #include "GMLLexer.h"
  8. #include <AK/QuickSort.h>
  9. namespace GUI {
  10. void GMLAutocompleteProvider::provide_completions(Function<void(Vector<Entry>)> callback)
  11. {
  12. auto cursor = m_editor->cursor();
  13. auto text = m_editor->text();
  14. GUI::GMLLexer lexer(text);
  15. // FIXME: Provide a begin() and end() for lexers PLEASE!
  16. auto all_tokens = lexer.lex();
  17. enum State {
  18. Free,
  19. InClassName,
  20. AfterClassName,
  21. InIdentifier,
  22. AfterIdentifier, // Can we introspect this?
  23. } state { Free };
  24. String identifier_string;
  25. Vector<String> class_names;
  26. Vector<State> previous_states;
  27. bool should_push_state { true };
  28. GUI::GMLToken* last_seen_token { nullptr };
  29. for (auto& token : all_tokens) {
  30. auto handle_class_child = [&] {
  31. if (token.m_type == GUI::GMLToken::Type::Identifier) {
  32. state = InIdentifier;
  33. identifier_string = token.m_view;
  34. } else if (token.m_type == GUI::GMLToken::Type::ClassMarker) {
  35. previous_states.append(AfterClassName);
  36. state = Free;
  37. should_push_state = false;
  38. }
  39. };
  40. if (token.m_start.line > cursor.line() || (token.m_start.line == cursor.line() && token.m_start.column > cursor.column()))
  41. break;
  42. last_seen_token = &token;
  43. switch (state) {
  44. case Free:
  45. if (token.m_type == GUI::GMLToken::Type::ClassName) {
  46. if (should_push_state)
  47. previous_states.append(state);
  48. else
  49. should_push_state = true;
  50. state = InClassName;
  51. class_names.append(token.m_view);
  52. break;
  53. }
  54. break;
  55. case InClassName:
  56. if (token.m_type != GUI::GMLToken::Type::LeftCurly) {
  57. // Close empty class and immediately handle our parent's next child
  58. class_names.take_last();
  59. state = previous_states.take_last();
  60. if (state == AfterClassName)
  61. handle_class_child();
  62. break;
  63. }
  64. state = AfterClassName;
  65. break;
  66. case AfterClassName:
  67. handle_class_child();
  68. if (token.m_type == GUI::GMLToken::Type::RightCurly) {
  69. class_names.take_last();
  70. state = previous_states.take_last();
  71. break;
  72. }
  73. break;
  74. case InIdentifier:
  75. if (token.m_type == GUI::GMLToken::Type::Colon)
  76. state = AfterIdentifier;
  77. break;
  78. case AfterIdentifier:
  79. if (token.m_type == GUI::GMLToken::Type::RightCurly || token.m_type == GUI::GMLToken::Type::LeftCurly)
  80. break;
  81. if (token.m_type == GUI::GMLToken::Type::ClassMarker) {
  82. previous_states.append(AfterClassName);
  83. state = Free;
  84. should_push_state = false;
  85. } else {
  86. state = AfterClassName;
  87. }
  88. break;
  89. }
  90. }
  91. if (state == InClassName && last_seen_token && last_seen_token->m_end.line < cursor.line()) {
  92. // Close empty class
  93. class_names.take_last();
  94. state = previous_states.take_last();
  95. }
  96. auto& widget_class = *Core::ObjectClassRegistration::find("GUI::Widget");
  97. Vector<GUI::AutocompleteProvider::Entry> class_entries, identifier_entries;
  98. switch (state) {
  99. case Free:
  100. if (last_seen_token && last_seen_token->m_end.column != cursor.column() && last_seen_token->m_end.line == cursor.line()) {
  101. // After some token, but with extra space, not on a new line.
  102. // Nothing to put here.
  103. break;
  104. }
  105. Core::ObjectClassRegistration::for_each([&](const Core::ObjectClassRegistration& registration) {
  106. if (!registration.is_derived_from(widget_class))
  107. return;
  108. class_entries.empend(String::formatted("@{}", registration.class_name()), 0u);
  109. });
  110. break;
  111. case InClassName:
  112. if (class_names.is_empty())
  113. break;
  114. if (last_seen_token && last_seen_token->m_end.column != cursor.column() && last_seen_token->m_end.line == cursor.line()) {
  115. // After a class name, but haven't seen braces.
  116. // TODO: Suggest braces?
  117. break;
  118. }
  119. Core::ObjectClassRegistration::for_each([&](const Core::ObjectClassRegistration& registration) {
  120. if (!registration.is_derived_from(widget_class))
  121. return;
  122. if (registration.class_name().starts_with(class_names.last()))
  123. identifier_entries.empend(registration.class_name(), class_names.last().length());
  124. });
  125. break;
  126. case InIdentifier: {
  127. if (class_names.is_empty())
  128. break;
  129. if (last_seen_token && last_seen_token->m_end.column != cursor.column() && last_seen_token->m_end.line == cursor.line()) {
  130. // After an identifier, but with extra space
  131. // TODO: Maybe suggest a colon?
  132. break;
  133. }
  134. auto registration = Core::ObjectClassRegistration::find(class_names.last());
  135. if (registration && registration->is_derived_from(widget_class)) {
  136. auto instance = registration->construct();
  137. for (auto& it : instance->properties()) {
  138. if (it.key.starts_with(identifier_string))
  139. identifier_entries.empend(it.key, identifier_string.length());
  140. }
  141. }
  142. if (can_have_declared_layout(class_names.last()) && "layout"sv.starts_with(identifier_string))
  143. identifier_entries.empend("layout", identifier_string.length());
  144. // No need to suggest anything if it's already completely typed out!
  145. if (identifier_entries.size() == 1 && identifier_entries.first().completion == identifier_string)
  146. identifier_entries.clear();
  147. break;
  148. }
  149. case AfterClassName: {
  150. if (last_seen_token && last_seen_token->m_end.line == cursor.line()) {
  151. if (last_seen_token->m_type != GUI::GMLToken::Type::Identifier || last_seen_token->m_end.column != cursor.column()) {
  152. // Inside braces, but on the same line as some other stuff (and not the continuation of one!)
  153. // The user expects nothing here.
  154. break;
  155. }
  156. }
  157. if (!class_names.is_empty()) {
  158. auto registration = Core::ObjectClassRegistration::find(class_names.last());
  159. if (registration && registration->is_derived_from(widget_class)) {
  160. auto instance = registration->construct();
  161. for (auto& it : instance->properties()) {
  162. if (!it.value->is_readonly())
  163. identifier_entries.empend(it.key, 0u);
  164. }
  165. }
  166. }
  167. Core::ObjectClassRegistration::for_each([&](const Core::ObjectClassRegistration& registration) {
  168. if (!registration.is_derived_from(widget_class))
  169. return;
  170. class_entries.empend(String::formatted("@{}", registration.class_name()), 0u);
  171. });
  172. break;
  173. }
  174. case AfterIdentifier:
  175. if (last_seen_token && last_seen_token->m_end.line != cursor.line()) {
  176. break;
  177. }
  178. if (identifier_string == "layout") {
  179. Core::ObjectClassRegistration::for_each([&](const Core::ObjectClassRegistration& registration) {
  180. if (!registration.is_derived_from(widget_class))
  181. return;
  182. if (registration.class_name().contains("Layout"))
  183. class_entries.empend(String::formatted("@{}", registration.class_name()), 0u);
  184. });
  185. }
  186. break;
  187. default:
  188. break;
  189. }
  190. quick_sort(class_entries, [](auto& a, auto& b) { return a.completion < b.completion; });
  191. quick_sort(identifier_entries, [](auto& a, auto& b) { return a.completion < b.completion; });
  192. Vector<GUI::AutocompleteProvider::Entry> entries;
  193. entries.extend(move(identifier_entries));
  194. entries.extend(move(class_entries));
  195. callback(move(entries));
  196. }
  197. }