1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 11:18:11 +00:00

gml-format+Playground: Print GML parsing error on formatting failure

This commit is contained in:
Idan Horowitz 2022-02-12 19:16:13 +02:00
parent d4f08b3096
commit 4c451422c3
3 changed files with 10 additions and 12 deletions

View file

@ -215,13 +215,13 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
auto edit_menu = TRY(window->try_add_menu("&Edit")); auto edit_menu = TRY(window->try_add_menu("&Edit"));
TRY(edit_menu->try_add_action(GUI::Action::create("&Format GML", { Mod_Ctrl | Mod_Shift, Key_I }, [&](auto&) { TRY(edit_menu->try_add_action(GUI::Action::create("&Format GML", { Mod_Ctrl | Mod_Shift, Key_I }, [&](auto&) {
auto formatted_gml = GUI::GML::format_gml(editor->text()); auto formatted_gml_or_error = GUI::GML::format_gml(editor->text());
if (!formatted_gml.is_null()) { if (!formatted_gml_or_error.is_error()) {
editor->set_text(formatted_gml); editor->set_text(formatted_gml_or_error.release_value());
} else { } else {
GUI::MessageBox::show( GUI::MessageBox::show(
window, window,
"GML could not be formatted, please check the debug console for parsing errors.", String::formatted("GML could not be formatted: {}", formatted_gml_or_error.error()),
"Error", "Error",
GUI::MessageBox::Type::Error); GUI::MessageBox::Type::Error);
} }

View file

@ -13,12 +13,9 @@
namespace GUI::GML { namespace GUI::GML {
inline String format_gml(StringView string) inline ErrorOr<String> format_gml(StringView string)
{ {
auto ast = parse_gml(string); return TRY(parse_gml(string))->to_string();
if (ast.is_error())
return {};
return ast.value()->to_string();
} }
} }

View file

@ -23,11 +23,12 @@ ErrorOr<bool> format_file(StringView path, bool inplace)
file = TRY(Core::File::open(path, open_mode)); file = TRY(Core::File::open(path, open_mode));
} }
auto contents = file->read_all(); auto contents = file->read_all();
auto formatted_gml = GUI::GML::format_gml(contents); auto formatted_gml_or_error = GUI::GML::format_gml(contents);
if (formatted_gml.is_null()) { if (formatted_gml_or_error.is_error()) {
warnln("Failed to parse GML!"); warnln("Failed to parse GML: {}", formatted_gml_or_error.error());
return false; return false;
} }
auto formatted_gml = formatted_gml_or_error.release_value();
if (inplace && !read_from_stdin) { if (inplace && !read_from_stdin) {
if (!file->seek(0) || !file->truncate(0)) { if (!file->seek(0) || !file->truncate(0)) {
warnln("Could not truncate {}: {}", path, file->error_string()); warnln("Could not truncate {}: {}", path, file->error_string());