< Summary

Information
Class: UIBlazor.Components.Chat.DiffView
Assembly: UIBlazor
File(s): /home/runner/work/InvAit/InvAit/UIBlazor/Components/Chat/DiffView.razor
Tag: 14_22728831704
Line coverage
0%
Covered lines: 0
Uncovered lines: 42
Coverable lines: 42
Total lines: 111
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 22
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
BuildRenderTree(...)0%620%
get_Lines()100%210%
get_FilePath()100%210%
.ctor()100%210%
OnParametersSet()100%210%
ParseDiff()0%2040%
UpdateOrAddBlock(...)0%4260%
DetermineType(...)0%110100%
get_Type()100%210%
get_Content()100%210%

File(s)

/home/runner/work/InvAit/InvAit/UIBlazor/Components/Chat/DiffView.razor

#LineLine coverage
 1@using System.IO
 2@using System.Text
 3
 4<div class="diff-view">
 05    @if (!string.IsNullOrEmpty(FilePath))
 6    {
 07        <div @key="@FilePath" class="tool-file-header">@FilePath</div>
 8    }
 9
 10    <div class="diff-block">
 11        <Virtualize Items="@_blocks" Context="block" ItemSize="16" >@* Обязательно все элементы 16px *@
 12            <DiffLine Model="block" />
 13        </Virtualize>
 14    </div>
 15</div>
 16
 17@code {
 018    [Parameter] public List<string> Lines { get; set; } = [];
 19
 020    private string? FilePath { get; set; }
 021    private List<DiffBlock> _blocks = [];
 22
 23    protected override void OnParametersSet()
 24    {
 025        ParseDiff();
 026    }
 27
 28    private void ParseDiff()
 29    {
 030        bool inSearch = false;
 031        bool inReplace = false;
 032        int blockIndex = 0;
 33
 34        // Читаем первую строку для FilePath
 035        FilePath = Lines[0];
 036        foreach (var line in Lines)
 37        {
 038            UpdateOrAddBlock(blockIndex++, line, ref inSearch, ref inReplace);
 39        }
 40
 41        // Удаляем лишние блоки, если текст сократился
 042        if (_blocks.Count > blockIndex)
 043            _blocks.RemoveRange(blockIndex, _blocks.Count - blockIndex);
 044    }
 45
 46    private void UpdateOrAddBlock(int index, string line, ref bool inSearch, ref bool inReplace)
 47    {
 048        var type = DetermineType(line, ref inSearch, ref inReplace);
 49
 050        if (index < _blocks.Count)
 51        {
 52            // ВАЖНО: Обновляем свойства существующего объекта.
 53            // Blazor увидит, что экземпляр тот же, и проверит только Content.
 054            var block = _blocks[index];
 055            if (block.Type != type || block.Content.Length < line.Length)
 56            {
 057                block.Type = type;
 058                block.Content = line;
 59            }
 60        }
 61        else
 62        {
 063            _blocks.Add(new DiffBlock { Type = type, Content = line });
 64        }
 065    }
 66
 67    private DiffBlockType DetermineType(string line, ref bool inSearch, ref bool inReplace)
 68    {
 069        var trimmed = line.Trim();
 70
 071        if (trimmed.StartsWith("<<<<<<< SEARCH"))
 72        {
 073            inSearch = true;
 074            inReplace = false; // Сброс на всякий случай
 075            return DiffBlockType.SearchHeader;
 76        }
 077        if (trimmed.StartsWith("======="))
 78        {
 079            inSearch = false;
 080            inReplace = true;
 081            return DiffBlockType.Separator;
 82        }
 083        if (trimmed.StartsWith(">>>>>>> REPLACE"))
 84        {
 085            inSearch = false;
 086            inReplace = false;
 087            return DiffBlockType.ReplaceHeader;
 88        }
 89
 090        if (inSearch) return DiffBlockType.Removed;
 091        if (inReplace) return DiffBlockType.Added;
 92
 093        return DiffBlockType.Context;
 94    }
 95
 96    public enum DiffBlockType
 97    {
 98        SearchHeader,
 99        Separator,
 100        ReplaceHeader,
 101        Removed,
 102        Added,
 103        Context
 104    }
 105
 106    public class DiffBlock
 107    {
 0108        public DiffBlockType Type { get; set; }
 0109        public string Content { get; set; } = string.Empty;
 110    }
 111}