diff options
author | Linus Groh <mail@linusgroh.de> | 2021-01-03 20:03:11 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-01-03 22:12:08 +0100 |
commit | 7b21335caaaef312197e28d5cff389c775d776b0 (patch) | |
tree | 2e30fcfc7f12d8c2e76ad48d2253f8b7d9c84f71 /DevTools | |
parent | e6087230319851bfe140609f4b7e5fea2d1fe981 (diff) | |
download | serenity-7b21335caaaef312197e28d5cff389c775d776b0.zip |
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.
Diffstat (limited to 'DevTools')
-rw-r--r-- | DevTools/Playground/main.cpp | 30 |
1 files changed, 30 insertions, 0 deletions
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 <LibGUI/Application.h> #include <LibGUI/AutocompleteProvider.h> #include <LibGUI/FilePicker.h> +#include <LibGUI/GMLFormatter.h> #include <LibGUI/GMLLexer.h> #include <LibGUI/GMLSyntaxHighlighter.h> #include <LibGUI/Icon.h> @@ -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); |