1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 17:17:44 +00:00

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.
This commit is contained in:
Linus Groh 2021-01-03 20:03:11 +01:00 committed by Andreas Kling
parent e608723031
commit 7b21335caa

View file

@ -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);