GMLAutocompleteProvider.cpp 9.2 KB

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