mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 07:57:46 +00:00
LibDiff: Change underlying representation of Hunk to allow context
The existing hunk data structure does not contain any way to easily store information about context surrounding the additions and removals in a hunk. While this does work fine for normal diffs (where there is never any surrounding context) this data structure is quite limiting for other use cases. Without support for surrounding context it is not possible to: * Add support for unified or context format to the diff utility to output surrounding context. * Be able to implement a patch utility that uses the surrounding context to reliably locate where to apply a patch when a hunk range does not apply perfectly. This patch changes Diff::Hunk such that its data structure more closely resembles a unified diff. Each line in a hunk is now either a change, removal, addition or context. Allowing hunks to have context inside of them exposes that HackStudio heavily relies on there being no context in the hunks that it uses for its' git gutter implementation. The fix here is simple - ask git to produce us a diff that has no context in it!
This commit is contained in:
parent
ef8f0f4f68
commit
f690807c5a
8 changed files with 169 additions and 127 deletions
|
@ -1,38 +1,96 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
|
||||
* Copyright (c) 2023, Shannon Booth <shannon.ml.booth@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Assertions.h>
|
||||
#include <AK/Format.h>
|
||||
#include <AK/String.h>
|
||||
#include <AK/StringView.h>
|
||||
#include <AK/Vector.h>
|
||||
|
||||
namespace Diff {
|
||||
|
||||
struct HunkLocation {
|
||||
size_t original_start_line { 0 };
|
||||
size_t original_length { 0 };
|
||||
size_t target_start_line { 0 };
|
||||
size_t target_length { 0 };
|
||||
struct Range {
|
||||
size_t start_line { 0 };
|
||||
size_t number_of_lines { 0 };
|
||||
};
|
||||
|
||||
struct HunkLocation {
|
||||
Range old_range;
|
||||
Range new_range;
|
||||
};
|
||||
|
||||
struct Line {
|
||||
enum class Operation {
|
||||
Addition = '+',
|
||||
Removal = '-',
|
||||
Context = ' ',
|
||||
|
||||
enum class LocationType {
|
||||
Original,
|
||||
Target,
|
||||
Both
|
||||
};
|
||||
void apply_offset(size_t offset, LocationType);
|
||||
|
||||
static constexpr Operation operation_from_symbol(char symbol)
|
||||
{
|
||||
switch (symbol) {
|
||||
case '+':
|
||||
return Operation::Addition;
|
||||
case '-':
|
||||
return Operation::Removal;
|
||||
case ' ':
|
||||
return Operation::Context;
|
||||
default:
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
}
|
||||
|
||||
Operation operation;
|
||||
String content;
|
||||
};
|
||||
|
||||
struct Hunk {
|
||||
size_t original_start_line { 0 };
|
||||
size_t target_start_line { 0 };
|
||||
Vector<String> removed_lines;
|
||||
Vector<String> added_lines;
|
||||
HunkLocation location;
|
||||
Vector<Line> lines;
|
||||
};
|
||||
|
||||
ErrorOr<Vector<Hunk>> parse_hunks(StringView diff);
|
||||
HunkLocation parse_hunk_location(StringView location_line);
|
||||
};
|
||||
|
||||
template<>
|
||||
struct AK::Formatter<Diff::Line::Operation> : Formatter<FormatString> {
|
||||
ErrorOr<void> format(FormatBuilder& builder, Diff::Line::Operation operation)
|
||||
{
|
||||
return Formatter<FormatString>::format(builder, "{}"sv, static_cast<char>(operation));
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct AK::Formatter<Diff::Line> : Formatter<FormatString> {
|
||||
ErrorOr<void> format(FormatBuilder& builder, Diff::Line const& line)
|
||||
{
|
||||
return Formatter<FormatString>::format(builder, "{}{}"sv, line.operation, line.content);
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct AK::Formatter<Diff::HunkLocation> : Formatter<FormatString> {
|
||||
static ErrorOr<void> format(FormatBuilder& format_builder, Diff::HunkLocation const& location)
|
||||
{
|
||||
auto& builder = format_builder.builder();
|
||||
TRY(builder.try_appendff("@@ -{}"sv, location.old_range.start_line));
|
||||
|
||||
if (location.old_range.number_of_lines != 1)
|
||||
TRY(builder.try_appendff(",{}", location.old_range.number_of_lines));
|
||||
|
||||
TRY(builder.try_appendff(" +{}", location.new_range.start_line));
|
||||
|
||||
if (location.new_range.number_of_lines != 1)
|
||||
TRY(builder.try_appendff(",{}", location.new_range.number_of_lines));
|
||||
|
||||
return builder.try_appendff(" @@");
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue