Skip to content

Commit

Permalink
BUGFIX: Always render full line range and allow to move to the virtua…
Browse files Browse the repository at this point in the history
…l cell
  • Loading branch information
Washi1337 committed Dec 13, 2024
1 parent 69006bc commit 373fe44
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
10 changes: 4 additions & 6 deletions src/AvaloniaHex/Editing/Caret.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public void GoToEndOfDocument()
if (PrimaryColumn is not { } primaryColumn)
return;

Location = primaryColumn.GetLastLocation(Mode == EditingMode.Insert);
Location = primaryColumn.GetLastLocation(true);
}

/// <summary>
Expand Down Expand Up @@ -225,7 +225,7 @@ public void GoBackward(ulong byteCount)
public void GoRight()
{
if (PrimaryColumn is { } column)
Location = column.GetNextLocation(Location, Mode == EditingMode.Insert, true);
Location = column.GetNextLocation(Location, true, true);
}

/// <summary>
Expand All @@ -249,12 +249,10 @@ public void GoForward(ulong byteCount)

// Note: We cannot use BitLocation.Clamp due to unsigned overflow that may happen.

ulong effectiveDocumentLength = document.Length - (Mode == EditingMode.Insert ? 0u : 1u);

if (document.Length < byteCount
|| Location.ByteIndex >= effectiveDocumentLength - byteCount)
|| Location.ByteIndex >= document.Length - byteCount)
{
Location = new BitLocation(effectiveDocumentLength, PrimaryColumn.FirstBitIndex);
Location = new BitLocation(document.Length, PrimaryColumn.FirstBitIndex);
return;
}

Expand Down
7 changes: 7 additions & 0 deletions src/AvaloniaHex/Rendering/CellBasedColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,11 @@ public Rect GetRelativeGroupBounds(VisualBytesLine line, BitLocation location)
);
}

/// <summary>
/// Aligns the provided location to the beginning of the cell that contains the location.
/// </summary>
/// <param name="location">The location to align.</param>
/// <returns>The aligned location.</returns>
public BitLocation AlignToCell(BitLocation location)
{
return new BitLocation(location.ByteIndex, location.BitIndex / BitsPerCell * BitsPerCell);
Expand Down Expand Up @@ -325,6 +330,8 @@ public BitLocation GetPreviousLocation(BitLocation location)
/// Given a bit location, gets the location of the cell after it.
/// </summary>
/// <param name="location">The location.</param>
/// <param name="includeVirtualCell"><c>true</c> if the virtual cell at the end of the document should be included.</param>
/// <param name="clamp"><c>true</c> if the location should be restricted to the current document length.</param>
/// <returns>The next cell's location.</returns>
public BitLocation GetNextLocation(BitLocation location, bool includeVirtualCell, bool clamp)
{
Expand Down
6 changes: 3 additions & 3 deletions src/AvaloniaHex/Rendering/HexView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -496,18 +496,18 @@ private VisualBytesLine GetOrCreateVisualLine(BitRange range)
{
// Exact match?
var currentLine = _visualLines[i];
if (currentLine.VirtualRange.Start == range.Start)
if (currentLine.Range.Start == range.Start)
{
// Edge-case: if our range is not exactly right, the line's range is outdated (e.g., as a result of
// inserting or removing a character at the end of the document).
if (currentLine.VirtualRange.End != range.End)
if (currentLine.Range.End != range.End)
_visualLines[i] = currentLine = new VisualBytesLine(this, range, Columns.Count);

return currentLine;
}

// If the next line is further than the requested start, the line does not exist.
if (currentLine.VirtualRange.Start > range.Start)
if (currentLine.Range.Start > range.Start)
{
newLine = new VisualBytesLine(this, range, Columns.Count);
_visualLines.Insert(i, newLine);
Expand Down

0 comments on commit 373fe44

Please sign in to comment.