diff --git a/Userland/Libraries/LibCpp/SemanticSyntaxHighlighter.cpp b/Userland/Libraries/LibCpp/SemanticSyntaxHighlighter.cpp index 2d147d3afb..08dcea3991 100644 --- a/Userland/Libraries/LibCpp/SemanticSyntaxHighlighter.cpp +++ b/Userland/Libraries/LibCpp/SemanticSyntaxHighlighter.cpp @@ -55,7 +55,7 @@ void SemanticSyntaxHighlighter::rehighlight(Palette const& palette) // FIXME: Computing the diff on the entire document's tokens is quite inefficient. // An improvement over this could be only including the tokens that are in edited text ranges in the diff. - auto diff_hunks = Diff::from_text(previous.view(), current.view()); + auto diff_hunks = Diff::from_text(previous.view(), current.view()).release_value_but_fixme_should_propagate_errors(); for (auto& token : current_tokens) { new_tokens_info.append(CodeComprehension::TokenInfo { CodeComprehension::TokenInfo::SemanticType::Unknown, token.start().line, token.start().column, token.end().line, token.end().column }); diff --git a/Userland/Libraries/LibDiff/Generator.cpp b/Userland/Libraries/LibDiff/Generator.cpp index cf65e18b61..677022b570 100644 --- a/Userland/Libraries/LibDiff/Generator.cpp +++ b/Userland/Libraries/LibDiff/Generator.cpp @@ -8,7 +8,7 @@ namespace Diff { -Vector from_text(StringView old_text, StringView new_text) +ErrorOr> from_text(StringView old_text, StringView new_text) { auto old_lines = old_text.lines(); auto new_lines = new_text.lines(); @@ -33,7 +33,7 @@ Vector from_text(StringView old_text, StringView new_text) }; auto dp_matrix = Vector(); - dp_matrix.resize((old_lines.size() + 1) * (new_lines.size() + 1)); + TRY(dp_matrix.try_resize((old_lines.size() + 1) * (new_lines.size() + 1))); auto dp = [&dp_matrix, width = old_lines.size() + 1](size_t i, size_t j) -> Cell& { return dp_matrix[i + width * j]; @@ -66,27 +66,31 @@ Vector from_text(StringView old_text, StringView new_text) Hunk cur_hunk; bool in_hunk = false; - auto update_hunk = [&](size_t i, size_t j, Direction direction) { + auto update_hunk = [&](size_t i, size_t j, Direction direction) -> ErrorOr { if (!in_hunk) { in_hunk = true; cur_hunk = { i, j, {}, {} }; } if (direction == Direction::Down) { - cur_hunk.added_lines.append(new_lines[j]); + TRY(cur_hunk.added_lines.try_append(new_lines[j])); } else if (direction == Direction::Right) { - cur_hunk.removed_lines.append(old_lines[i]); + TRY(cur_hunk.removed_lines.try_append(old_lines[i])); } + + return {}; }; - auto flush_hunk = [&]() { + auto flush_hunk = [&]() -> ErrorOr { if (in_hunk) { if (cur_hunk.added_lines.size() > 0) cur_hunk.target_start_line++; if (cur_hunk.removed_lines.size() > 0) cur_hunk.original_start_line++; - hunks.append(cur_hunk); + TRY(hunks.try_append(cur_hunk)); in_hunk = false; } + + return {}; }; size_t i = 0; @@ -95,28 +99,28 @@ Vector from_text(StringView old_text, StringView new_text) while (i < old_lines.size() && j < new_lines.size()) { auto& cell = dp(i, j); if (cell.direction == Direction::Down) { - update_hunk(i, j, cell.direction); + TRY(update_hunk(i, j, cell.direction)); ++j; } else if (cell.direction == Direction::Right) { - update_hunk(i, j, cell.direction); + TRY(update_hunk(i, j, cell.direction)); ++i; } else { ++i; ++j; - flush_hunk(); + TRY(flush_hunk()); } } while (i < old_lines.size()) { - update_hunk(i, new_lines.is_empty() ? 0 : new_lines.size() - 1, Direction::Right); // Remove a line + TRY(update_hunk(i, new_lines.is_empty() ? 0 : new_lines.size() - 1, Direction::Right)); // Remove a line ++i; } while (j < new_lines.size()) { - update_hunk(old_lines.is_empty() ? 0 : old_lines.size() - 1, j, Direction::Down); // Add a line + TRY(update_hunk(old_lines.is_empty() ? 0 : old_lines.size() - 1, j, Direction::Down)); // Add a line ++j; } - flush_hunk(); + TRY(flush_hunk()); return hunks; } diff --git a/Userland/Libraries/LibDiff/Generator.h b/Userland/Libraries/LibDiff/Generator.h index d1b061edb8..17567e8480 100644 --- a/Userland/Libraries/LibDiff/Generator.h +++ b/Userland/Libraries/LibDiff/Generator.h @@ -7,9 +7,10 @@ #pragma once #include "Hunks.h" +#include namespace Diff { -Vector from_text(StringView old_text, StringView new_text); +ErrorOr> from_text(StringView old_text, StringView new_text); } diff --git a/Userland/Utilities/diff.cpp b/Userland/Utilities/diff.cpp index 774f6eb697..bad475f161 100644 --- a/Userland/Utilities/diff.cpp +++ b/Userland/Utilities/diff.cpp @@ -31,7 +31,7 @@ ErrorOr serenity_main(Main::Arguments arguments) auto const color_output = TRY(Core::System::isatty(STDOUT_FILENO)) ? Diff::ColorOutput::Yes : Diff::ColorOutput::No; - auto hunks = Diff::from_text(TRY(file1->read_until_eof()), TRY(file2->read_until_eof())); + auto hunks = TRY(Diff::from_text(TRY(file1->read_until_eof()), TRY(file2->read_until_eof()))); for (auto const& hunk : hunks) TRY(Diff::write_normal(hunk, *out, color_output)); diff --git a/Userland/Utilities/headless-browser.cpp b/Userland/Utilities/headless-browser.cpp index c67357fb2b..e990479e5a 100644 --- a/Userland/Utilities/headless-browser.cpp +++ b/Userland/Utilities/headless-browser.cpp @@ -254,7 +254,7 @@ static ErrorOr run_test(HeadlessWebContentView& view, StringView inp else outln("\nTest failed: {}", input_path); - auto hunks = Diff::from_text(expectation, actual); + auto hunks = TRY(Diff::from_text(expectation, actual)); auto out = TRY(Core::File::standard_output()); for (auto const& hunk : hunks) { TRY(out->write_formatted("Hunk: "));