GMLAutocompleteProvider.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. // FIXME: Can this be done without a StringBuilder?
  102. auto make_fuzzy = [](StringView str) {
  103. auto fuzzy_str_builder = StringBuilder(str.length() * 2 + 1);
  104. fuzzy_str_builder.append('*');
  105. for (auto character : str) {
  106. fuzzy_str_builder.append(character);
  107. fuzzy_str_builder.append('*');
  108. }
  109. return fuzzy_str_builder.build();
  110. };
  111. Vector<GUI::AutocompleteProvider::Entry> class_entries, identifier_entries;
  112. switch (state) {
  113. case Free:
  114. if (last_seen_token && last_seen_token->m_end.column != cursor.column() && last_seen_token->m_end.line == cursor.line()) {
  115. // After some token, but with extra space, not on a new line.
  116. // Nothing to put here.
  117. break;
  118. }
  119. Core::ObjectClassRegistration::for_each([&](const Core::ObjectClassRegistration& registration) {
  120. if (!registration.is_derived_from(widget_class))
  121. return;
  122. class_entries.empend(String::formatted("@{}", registration.class_name()), 0u);
  123. });
  124. break;
  125. case InClassName: {
  126. if (class_names.is_empty())
  127. break;
  128. if (last_seen_token && last_seen_token->m_end.column != cursor.column() && last_seen_token->m_end.line == cursor.line()) {
  129. // After a class name, but haven't seen braces.
  130. // TODO: Suggest braces?
  131. break;
  132. }
  133. auto fuzzy_class = make_fuzzy(class_names.last());
  134. if (last_identifier_token && last_identifier_token->m_end.line == last_seen_token->m_end.line && identifier_string == "layout") {
  135. Core::ObjectClassRegistration::for_each([&](const Core::ObjectClassRegistration& registration) {
  136. if (&registration == &layout_class || !registration.is_derived_from(layout_class))
  137. return;
  138. if (registration.class_name().matches(fuzzy_class))
  139. identifier_entries.empend(registration.class_name(), class_names.last().length());
  140. });
  141. break;
  142. }
  143. Core::ObjectClassRegistration::for_each([&](const Core::ObjectClassRegistration& registration) {
  144. if (!registration.is_derived_from(widget_class))
  145. return;
  146. if (registration.class_name().matches(fuzzy_class))
  147. identifier_entries.empend(registration.class_name(), class_names.last().length());
  148. });
  149. break;
  150. }
  151. case InIdentifier: {
  152. if (class_names.is_empty())
  153. break;
  154. if (last_seen_token && last_seen_token->m_end.column != cursor.column() && last_seen_token->m_end.line == cursor.line()) {
  155. // After an identifier, but with extra space
  156. // TODO: Maybe suggest a colon?
  157. break;
  158. }
  159. auto fuzzy_identifier_string = make_fuzzy(identifier_string);
  160. auto registration = Core::ObjectClassRegistration::find(class_names.last());
  161. if (registration && (registration->is_derived_from(widget_class) || registration->is_derived_from(layout_class))) {
  162. if (auto instance = registration->construct()) {
  163. for (auto& it : instance->properties()) {
  164. if (it.key.matches(fuzzy_identifier_string))
  165. identifier_entries.empend(String::formatted("{}: ", it.key), identifier_string.length(), Language::Unspecified, it.key);
  166. }
  167. }
  168. }
  169. if (can_have_declared_layout(class_names.last()) && "layout"sv.matches(fuzzy_identifier_string))
  170. identifier_entries.empend("layout: ", identifier_string.length(), Language::Unspecified, "layout");
  171. break;
  172. }
  173. case AfterClassName: {
  174. if (last_seen_token && last_seen_token->m_end.line == cursor.line()) {
  175. if (last_seen_token->m_type != GUI::GMLToken::Type::Identifier || last_seen_token->m_end.column != cursor.column()) {
  176. // Inside braces, but on the same line as some other stuff (and not the continuation of one!)
  177. // The user expects nothing here.
  178. break;
  179. }
  180. }
  181. if (!class_names.is_empty()) {
  182. auto registration = Core::ObjectClassRegistration::find(class_names.last());
  183. if (registration && (registration->is_derived_from(widget_class) || registration->is_derived_from(layout_class))) {
  184. if (auto instance = registration->construct()) {
  185. for (auto& it : instance->properties()) {
  186. if (!it.value->is_readonly())
  187. identifier_entries.empend(String::formatted("{}: ", it.key), 0u, Language::Unspecified, it.key);
  188. }
  189. }
  190. }
  191. }
  192. if (can_have_declared_layout(class_names.last()))
  193. identifier_entries.empend("layout: ", 0u, Language::Unspecified, "layout");
  194. Core::ObjectClassRegistration::for_each([&](const Core::ObjectClassRegistration& registration) {
  195. if (!registration.is_derived_from(widget_class))
  196. return;
  197. class_entries.empend(String::formatted("@{}", registration.class_name()), 0u);
  198. });
  199. break;
  200. }
  201. case AfterIdentifier:
  202. if (last_seen_token && last_seen_token->m_end.line != cursor.line())
  203. break;
  204. if (identifier_string == "layout") {
  205. Core::ObjectClassRegistration::for_each([&](const Core::ObjectClassRegistration& registration) {
  206. if (&registration == &layout_class || !registration.is_derived_from(layout_class))
  207. return;
  208. class_entries.empend(String::formatted("@{}", registration.class_name()), 0u);
  209. });
  210. }
  211. break;
  212. default:
  213. break;
  214. }
  215. quick_sort(class_entries, [](auto& a, auto& b) { return a.completion < b.completion; });
  216. quick_sort(identifier_entries, [](auto& a, auto& b) { return a.completion < b.completion; });
  217. Vector<GUI::AutocompleteProvider::Entry> entries;
  218. entries.extend(move(identifier_entries));
  219. entries.extend(move(class_entries));
  220. callback(move(entries));
  221. }
  222. }