1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 07:57:47 +00:00

Playground: Improve the autocompleted suggestions

- Now sorted
- Add a "layout" property to GUI::Widget and GUI::Frame
- Only complete Layouts for the values of "layout"
- Don't suggest anything if the only suggestion is what the user has
  already typed
This commit is contained in:
AnotherTest 2021-01-03 03:00:02 +03:30 committed by Andreas Kling
parent 432c37cafc
commit 6dbdbec835

View file

@ -24,7 +24,9 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#include <AK/QuickSort.h>
#include <LibCore/File.h> #include <LibCore/File.h>
#include <LibCore/Property.h>
#include <LibGUI/AboutDialog.h> #include <LibGUI/AboutDialog.h>
#include <LibGUI/Application.h> #include <LibGUI/Application.h>
#include <LibGUI/AutocompleteProvider.h> #include <LibGUI/AutocompleteProvider.h>
@ -46,6 +48,11 @@ public:
virtual ~GMLAutocompleteProvider() override { } virtual ~GMLAutocompleteProvider() override { }
private: private:
static bool can_have_declared_layout(const StringView& class_name)
{
return class_name.is_one_of("GUI::Widget", "GUI::Frame");
}
virtual void provide_completions(Function<void(Vector<Entry>)> callback) override virtual void provide_completions(Function<void(Vector<Entry>)> callback) override
{ {
auto cursor = m_editor->cursor(); auto cursor = m_editor->cursor();
@ -121,7 +128,7 @@ private:
} }
} }
Vector<GUI::AutocompleteProvider::Entry> entries; Vector<GUI::AutocompleteProvider::Entry> class_entries, identifier_entries;
switch (state) { switch (state) {
case Free: case Free:
if (last_seen_token && last_seen_token->m_end.column + 1 != cursor.column() && last_seen_token->m_end.line == cursor.line()) { if (last_seen_token && last_seen_token->m_end.column + 1 != cursor.column() && last_seen_token->m_end.line == cursor.line()) {
@ -130,7 +137,7 @@ private:
break; break;
} }
GUI::WidgetClassRegistration::for_each([&](const GUI::WidgetClassRegistration& registration) { GUI::WidgetClassRegistration::for_each([&](const GUI::WidgetClassRegistration& registration) {
entries.empend(String::formatted("@{}", registration.class_name()), 0u); class_entries.empend(String::formatted("@{}", registration.class_name()), 0u);
}); });
break; break;
case InClassName: case InClassName:
@ -143,7 +150,7 @@ private:
} }
GUI::WidgetClassRegistration::for_each([&](const GUI::WidgetClassRegistration& registration) { GUI::WidgetClassRegistration::for_each([&](const GUI::WidgetClassRegistration& registration) {
if (registration.class_name().starts_with(class_names.last())) if (registration.class_name().starts_with(class_names.last()))
entries.empend(registration.class_name(), class_names.last().length()); identifier_entries.empend(registration.class_name(), class_names.last().length());
}); });
break; break;
case InIdentifier: case InIdentifier:
@ -158,9 +165,14 @@ private:
auto instance = registration->construct(); auto instance = registration->construct();
for (auto& it : instance->properties()) { for (auto& it : instance->properties()) {
if (it.key.starts_with(identifier_string)) if (it.key.starts_with(identifier_string))
entries.empend(it.key, identifier_string.length()); identifier_entries.empend(it.key, identifier_string.length());
} }
} }
if (can_have_declared_layout(class_names.last()) && StringView { "layout" }.starts_with(identifier_string))
identifier_entries.empend("layout", identifier_string.length());
// No need to suggest anything if it's already completely typed out!
if (identifier_entries.size() == 1 && identifier_entries.first().completion == identifier_string)
identifier_entries.clear();
break; break;
case AfterClassName: case AfterClassName:
if (last_seen_token && last_seen_token->m_end.line == cursor.line()) { if (last_seen_token && last_seen_token->m_end.line == cursor.line()) {
@ -174,19 +186,37 @@ private:
if (auto registration = GUI::WidgetClassRegistration::find(class_names.last())) { if (auto registration = GUI::WidgetClassRegistration::find(class_names.last())) {
auto instance = registration->construct(); auto instance = registration->construct();
for (auto& it : instance->properties()) { for (auto& it : instance->properties()) {
entries.empend(it.key, 0u); if (!it.value->is_readonly())
identifier_entries.empend(it.key, 0u);
} }
} }
} }
GUI::WidgetClassRegistration::for_each([&](const GUI::WidgetClassRegistration& registration) { GUI::WidgetClassRegistration::for_each([&](const GUI::WidgetClassRegistration& registration) {
entries.empend(String::formatted("@{}", registration.class_name()), 0u); class_entries.empend(String::formatted("@{}", registration.class_name()), 0u);
}); });
break; break;
case AfterIdentifier: case AfterIdentifier:
if (last_seen_token && last_seen_token->m_end.line != cursor.line()) {
break;
}
if (identifier_string == "layout") {
GUI::WidgetClassRegistration::for_each([&](const GUI::WidgetClassRegistration& registration) {
if (registration.class_name().contains("Layout"))
class_entries.empend(String::formatted("@{}", registration.class_name()), 0u);
});
}
break;
default: default:
break; break;
} }
quick_sort(class_entries, [](auto& a, auto& b) { return a.completion < b.completion; });
quick_sort(identifier_entries, [](auto& a, auto& b) { return a.completion < b.completion; });
Vector<GUI::AutocompleteProvider::Entry> entries;
entries.append(move(identifier_entries));
entries.append(move(class_entries));
callback(move(entries)); callback(move(entries));
} }
}; };