GMLAutocompleteProvider.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. auto& layout_class = *Core::ObjectClassRegistration::find("GUI::Layout");
  98. Vector<GUI::AutocompleteProvider::Entry> class_entries, identifier_entries;
  99. switch (state) {
  100. case Free:
  101. if (last_seen_token && last_seen_token->m_end.column != cursor.column() && last_seen_token->m_end.line == cursor.line()) {
  102. // After some token, but with extra space, not on a new line.
  103. // Nothing to put here.
  104. break;
  105. }
  106. Core::ObjectClassRegistration::for_each([&](const Core::ObjectClassRegistration& registration) {
  107. if (!registration.is_derived_from(widget_class))
  108. return;
  109. class_entries.empend(String::formatted("@{}", registration.class_name()), 0u);
  110. });
  111. break;
  112. case InClassName:
  113. if (class_names.is_empty())
  114. break;
  115. if (last_seen_token && last_seen_token->m_end.column != cursor.column() && last_seen_token->m_end.line == cursor.line()) {
  116. // After a class name, but haven't seen braces.
  117. // TODO: Suggest braces?
  118. break;
  119. }
  120. Core::ObjectClassRegistration::for_each([&](const Core::ObjectClassRegistration& registration) {
  121. if (!registration.is_derived_from(widget_class))
  122. return;
  123. if (registration.class_name().starts_with(class_names.last()))
  124. identifier_entries.empend(registration.class_name(), class_names.last().length());
  125. });
  126. break;
  127. case InIdentifier: {
  128. if (class_names.is_empty())
  129. break;
  130. if (last_seen_token && last_seen_token->m_end.column != cursor.column() && last_seen_token->m_end.line == cursor.line()) {
  131. // After an identifier, but with extra space
  132. // TODO: Maybe suggest a colon?
  133. break;
  134. }
  135. auto registration = Core::ObjectClassRegistration::find(class_names.last());
  136. if (registration && registration->is_derived_from(widget_class)) {
  137. if (auto instance = registration->construct()) {
  138. for (auto& it : instance->properties()) {
  139. if (it.key.starts_with(identifier_string))
  140. identifier_entries.empend(it.key, identifier_string.length());
  141. }
  142. }
  143. }
  144. if (can_have_declared_layout(class_names.last()) && "layout"sv.starts_with(identifier_string))
  145. identifier_entries.empend("layout", identifier_string.length());
  146. // No need to suggest anything if it's already completely typed out!
  147. if (identifier_entries.size() == 1 && identifier_entries.first().completion == identifier_string)
  148. identifier_entries.clear();
  149. break;
  150. }
  151. case AfterClassName: {
  152. if (last_seen_token && last_seen_token->m_end.line == cursor.line()) {
  153. if (last_seen_token->m_type != GUI::GMLToken::Type::Identifier || last_seen_token->m_end.column != cursor.column()) {
  154. // Inside braces, but on the same line as some other stuff (and not the continuation of one!)
  155. // The user expects nothing here.
  156. break;
  157. }
  158. }
  159. if (!class_names.is_empty()) {
  160. auto registration = Core::ObjectClassRegistration::find(class_names.last());
  161. if (registration && registration->is_derived_from(widget_class)) {
  162. if (auto instance = registration->construct()) {
  163. for (auto& it : instance->properties()) {
  164. if (!it.value->is_readonly())
  165. identifier_entries.empend(it.key, 0u);
  166. }
  167. }
  168. }
  169. }
  170. Core::ObjectClassRegistration::for_each([&](const Core::ObjectClassRegistration& registration) {
  171. if (!registration.is_derived_from(widget_class))
  172. return;
  173. class_entries.empend(String::formatted("@{}", registration.class_name()), 0u);
  174. });
  175. break;
  176. }
  177. case AfterIdentifier:
  178. if (last_seen_token && last_seen_token->m_end.line != cursor.line())
  179. break;
  180. if (identifier_string == "layout") {
  181. Core::ObjectClassRegistration::for_each([&](const Core::ObjectClassRegistration& registration) {
  182. if (&registration == &layout_class || !registration.is_derived_from(layout_class))
  183. return;
  184. class_entries.empend(String::formatted("@{}", registration.class_name()), 0u);
  185. });
  186. }
  187. break;
  188. default:
  189. break;
  190. }
  191. quick_sort(class_entries, [](auto& a, auto& b) { return a.completion < b.completion; });
  192. quick_sort(identifier_entries, [](auto& a, auto& b) { return a.completion < b.completion; });
  193. Vector<GUI::AutocompleteProvider::Entry> entries;
  194. entries.extend(move(identifier_entries));
  195. entries.extend(move(class_entries));
  196. callback(move(entries));
  197. }
  198. }