GMLAutocompleteProvider.cpp 9.2 KB

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