From 7b21335caaaef312197e28d5cff389c775d776b0 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Sun, 3 Jan 2021 20:03:11 +0100 Subject: [PATCH] Playground: Add "Format GML" menu action This can be invoked via the Edit menu or with Ctrl+Alt+F. If the current text in the editor can be parsed as valid GML, it will be formatted and updated, otherwise an alert is shown (no specific error message as those are only printed to the debug console in the parser for now). If the source contains comments, which would be lost after formatting, the user will be notified and has to confirm the action. --- DevTools/Playground/main.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/DevTools/Playground/main.cpp b/DevTools/Playground/main.cpp index 9eea90ed5b..f6becd41ce 100644 --- a/DevTools/Playground/main.cpp +++ b/DevTools/Playground/main.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -300,6 +301,35 @@ int main(int argc, char** argv) app->quit(); })); + auto& edit_menu = menubar->add_menu("Edit"); + edit_menu.add_action(GUI::Action::create("Format GML", { Mod_Ctrl | Mod_Shift, Key_I }, [&](auto&) { + auto source = editor.text(); + GUI::GMLLexer lexer(source); + for (auto& token : lexer.lex()) { + if (token.m_type == GUI::GMLToken::Type::Comment) { + auto result = GUI::MessageBox::show( + window, + "Your GML contains comments, which currently are not supported by the formatter and will be removed. Proceed?", + "Warning", + GUI::MessageBox::Type::Warning, + GUI::MessageBox::InputType::OKCancel); + if (result == GUI::MessageBox::ExecCancel) + return; + break; + } + } + auto formatted_gml = GUI::format_gml(source); + if (!formatted_gml.is_null()) { + editor.set_text(formatted_gml); + } else { + GUI::MessageBox::show( + window, + "GML could not be formatted, please check the debug console for parsing errors.", + "Error", + GUI::MessageBox::Type::Error); + } + })); + auto& help_menu = menubar->add_menu("Help"); help_menu.add_action(GUI::Action::create("About", [&](auto&) { GUI::AboutDialog::show("GML Playground", app_icon.bitmap_for_size(32), window);