Skip to content

Commit 13b6865

Browse files
committed
[wip] Display file content
1 parent bc66e24 commit 13b6865

File tree

3 files changed

+199
-8
lines changed

3 files changed

+199
-8
lines changed

Diff for: src/ViewModels/Conflict.cs

+12
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,21 @@ public bool IsResolved
2020
private set;
2121
}
2222

23+
public Models.Change Change
24+
{
25+
get => _change;
26+
}
27+
28+
public Repository Repository
29+
{
30+
get => _repo;
31+
}
32+
2333
public Conflict(Repository repo, WorkingCopy wc, Models.Change change)
2434
{
2535
_wc = wc;
2636
_change = change;
37+
_repo = repo;
2738

2839
IsResolved = new Commands.IsConflictResolved(repo.FullPath, change).ReadToEnd().IsSuccess;
2940

@@ -74,5 +85,6 @@ public void OpenExternalMergeTool()
7485

7586
private WorkingCopy _wc = null;
7687
private Models.Change _change = null;
88+
private Repository _repo = null;
7789
}
7890
}

Diff for: src/Views/TextDiffView.axaml.cs

+108
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.ComponentModel;
44
using System.Globalization;
55
using System.IO;
6+
using System.Linq;
67
using System.Text;
78

89
using Avalonia;
@@ -1936,4 +1937,111 @@ private void OnDiscardChunk(object _1, RoutedEventArgs _2)
19361937
repo.SetWatcherEnabled(true);
19371938
}
19381939
}
1940+
1941+
public class ConflictTextDiffPresenter: SingleSideTextDiffPresenter
1942+
{
1943+
public enum Side : int
1944+
{
1945+
Ours = 0, WorkingCopy = 1, Theirs = 2
1946+
}
1947+
1948+
public Side DisplaySide {
1949+
get; set;
1950+
}
1951+
protected override void OnLoaded(RoutedEventArgs e)
1952+
{
1953+
base.OnLoaded(e);
1954+
}
1955+
protected override void OnUnloaded(RoutedEventArgs e)
1956+
{
1957+
base.OnUnloaded(e);
1958+
}
1959+
1960+
protected override void OnDataContextChanged(EventArgs e)
1961+
{
1962+
base.OnDataContextChanged(e);
1963+
1964+
if (DataContext is ViewModels.Conflict diff)
1965+
{
1966+
string conflict_start_marker = "<<<<<<< HEAD";
1967+
string conflict_separator_marker = "=======";
1968+
string conflict_end_marker = ">>>>>>> ";
1969+
1970+
var cmd = new Commands.QueryRefsContainsCommit(diff.Repository.FullPath, (diff.Theirs as Models.Commit)?.SHA);
1971+
var their_branches = cmd.Result().ToArray();
1972+
1973+
string filePath = Path.Combine(diff.Repository.FullPath, diff.Change.Path);
1974+
var lines =File.ReadAllLines(filePath).ToList<string>();
1975+
1976+
bool shouldFilter = DisplaySide != Side.WorkingCopy;
1977+
while(shouldFilter) {
1978+
int idx_start = lines.IndexOf(conflict_start_marker);
1979+
if (idx_start == -1) {
1980+
shouldFilter = false;
1981+
break;
1982+
}
1983+
1984+
int idx_sep = lines.IndexOf(conflict_separator_marker, idx_start);
1985+
if (idx_sep == -1) {
1986+
shouldFilter = false;
1987+
break;
1988+
}
1989+
1990+
int idx_end = idx_sep + 1;
1991+
while(idx_end < lines.Count - 1) {
1992+
bool found = false;
1993+
for (int i = 0; i < their_branches.Length; i++) {
1994+
string ce_marker = conflict_end_marker + their_branches[i].Name;
1995+
if(lines[idx_end].StartsWith(ce_marker)) {
1996+
found = true;
1997+
break;
1998+
}
1999+
}
2000+
if (found) {
2001+
break;
2002+
}
2003+
idx_end++;
2004+
}
2005+
if (idx_end == lines.Count - 1) {
2006+
shouldFilter = false;
2007+
break;
2008+
}
2009+
2010+
switch (DisplaySide) {
2011+
case Side.Ours:
2012+
lines.RemoveRange(idx_sep, idx_end - idx_sep + 1);
2013+
lines.RemoveAt(idx_start);
2014+
break;
2015+
case Side.WorkingCopy:
2016+
break;
2017+
case Side.Theirs:
2018+
lines.RemoveAt(idx_end);
2019+
lines.RemoveRange(idx_start, idx_sep - idx_start + 1);
2020+
break;
2021+
}
2022+
}
2023+
2024+
var builder = new StringBuilder();
2025+
foreach (var line in lines)
2026+
{
2027+
if (line.Length > 10000)
2028+
{
2029+
builder.Append(line.Substring(0, 1000));
2030+
builder.Append($"...({line.Length - 1000} characters trimmed)");
2031+
builder.AppendLine();
2032+
}
2033+
else
2034+
{
2035+
builder.AppendLine(line);
2036+
}
2037+
}
2038+
2039+
Text = builder.ToString();
2040+
}
2041+
else
2042+
{
2043+
Text = string.Empty;
2044+
}
2045+
}
2046+
}
19392047
}

Diff for: src/Views/WorkingCopy.axaml

+79-8
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,13 @@
239239
</DataTemplate>
240240
</StackPanel.DataTemplates>
241241

242-
<Path Width="64" Height="64" Data="{StaticResource Icons.Conflict}" Fill="{DynamicResource Brush.FG2}" HorizontalAlignment="Center"/>
243-
<TextBlock Margin="0,16" FontSize="20" FontWeight="Bold" Text="{DynamicResource Text.WorkingCopy.Conflicts}" Foreground="{DynamicResource Brush.FG2}" HorizontalAlignment="Center"/>
242+
<StackPanel Orientation="Horizontal">
243+
<Path Width="32" Height="32" Margin="10,10,10,10"
244+
Data="{StaticResource Icons.Conflict}" Fill="{DynamicResource Brush.FG2}" HorizontalAlignment="Center"/>
245+
<TextBlock Margin="10,10,10,10" FontSize="20" FontWeight="Bold"
246+
Text="{DynamicResource Text.WorkingCopy.Conflicts}"
247+
Foreground="{DynamicResource Brush.FG2}" HorizontalAlignment="Center"/>
248+
</StackPanel>
244249

245250
<Border Margin="16,0" Padding="8" CornerRadius="4" BorderThickness="1" BorderBrush="{DynamicResource Brush.Border2}">
246251
<Border.IsVisible>
@@ -250,14 +255,80 @@
250255
</MultiBinding>
251256
</Border.IsVisible>
252257

253-
<Grid Margin="8,0,0,0" RowDefinitions="32,32" ColumnDefinitions="Auto,*">
254-
<TextBlock Grid.Row="0" Grid.Column="0" Classes="info_label" Text="THEIRS"/>
255-
<ContentControl Grid.Row="0" Grid.Column="1" Margin="16,0,0,0" Content="{Binding Theirs}"/>
256-
<TextBlock Grid.Row="1" Grid.Column="0" Classes="info_label" Text="MINE"/>
257-
<ContentControl Grid.Row="1" Grid.Column="1" Margin="16,0,0,0" Content="{Binding Mine}"/>
258+
<Grid Margin="8,0,0,0" RowDefinitions="32,32,*" ColumnDefinitions="*,1,*,1,*,1,12">
259+
<TextBlock Grid.Row="0" Grid.Column="0" Classes="info_label" HorizontalAlignment="Left" Text="MINE"/>
260+
<ContentControl Grid.Row="1" Grid.Column="0" Margin="16,0,0,0" HorizontalAlignment="Left" Content="{Binding Mine}"/>
261+
262+
<TextBlock Grid.Row="0" Grid.RowSpan="2" Grid.Column="2" Classes="info_label" HorizontalAlignment="Center" Text="{DynamicResource Text.WorkingCopy}"/>
263+
264+
<TextBlock Grid.Row="0" Grid.Column="4" Classes="info_label" HorizontalAlignment="Right" Text="THEIRS"/>
265+
<ContentControl Grid.Row="1" Grid.Column="4" Margin="16,0,0,0" HorizontalAlignment="Right" Content="{Binding Theirs}"/>
266+
267+
<v:ConflictTextDiffPresenter Grid.Row="2" Grid.Column="0"
268+
x:Name="OurSidePresenter"
269+
DisplaySide="0"
270+
Foreground="{DynamicResource Brush.FG1}"
271+
LineBrush="{DynamicResource Brush.Border2}"
272+
EmptyContentBackground="{DynamicResource Brush.Diff.EmptyBG}"
273+
AddedContentBackground="{DynamicResource Brush.Diff.AddedBG}"
274+
DeletedContentBackground="{DynamicResource Brush.Diff.DeletedBG}"
275+
AddedHighlightBrush="{DynamicResource Brush.Diff.AddedHighlight}"
276+
DeletedHighlightBrush="{DynamicResource Brush.Diff.DeletedHighlight}"
277+
IndicatorForeground="{DynamicResource Brush.FG2}"
278+
FontFamily="{DynamicResource Fonts.Monospace}"
279+
FontSize="{Binding Source={x:Static vm:Preferences.Instance}, Path=EditorFontSize}"
280+
UseSyntaxHighlighting="{Binding Source={x:Static vm:Preferences.Instance}, Path=UseSyntaxHighlighting}"
281+
WordWrap="False"
282+
ShowHiddenSymbols="{Binding Source={x:Static vm:Preferences.Instance}, Path=ShowHiddenSymbolsInDiffView}"
283+
EnableChunkSelection="True"/>
284+
285+
<Rectangle Grid.Row="2" Grid.Column="1" Fill="{DynamicResource Brush.Border2}" Width="1" HorizontalAlignment="Center" VerticalAlignment="Stretch"/>
286+
287+
<v:ConflictTextDiffPresenter Grid.Row="2" Grid.Column="2"
288+
x:Name="WorkingCopyPresenter"
289+
DisplaySide="1"
290+
Foreground="{DynamicResource Brush.FG1}"
291+
LineBrush="{DynamicResource Brush.Border2}"
292+
EmptyContentBackground="{DynamicResource Brush.Diff.EmptyBG}"
293+
AddedContentBackground="{DynamicResource Brush.Diff.AddedBG}"
294+
DeletedContentBackground="{DynamicResource Brush.Diff.DeletedBG}"
295+
AddedHighlightBrush="{DynamicResource Brush.Diff.AddedHighlight}"
296+
DeletedHighlightBrush="{DynamicResource Brush.Diff.DeletedHighlight}"
297+
IndicatorForeground="{DynamicResource Brush.FG2}"
298+
FontFamily="{DynamicResource Fonts.Monospace}"
299+
FontSize="{Binding Source={x:Static vm:Preferences.Instance}, Path=EditorFontSize}"
300+
UseSyntaxHighlighting="{Binding Source={x:Static vm:Preferences.Instance}, Path=UseSyntaxHighlighting}"
301+
WordWrap="False"
302+
ShowHiddenSymbols="{Binding Source={x:Static vm:Preferences.Instance}, Path=ShowHiddenSymbolsInDiffView}"/>
303+
304+
<Rectangle Grid.Row="2" Grid.Column="3" Fill="{DynamicResource Brush.Border2}" Width="1" HorizontalAlignment="Center" VerticalAlignment="Stretch"/>
305+
306+
<v:ConflictTextDiffPresenter Grid.Row="2" Grid.Column="4"
307+
x:Name="TherrSidePresenter"
308+
DisplaySide="2"
309+
Foreground="{DynamicResource Brush.FG1}"
310+
LineBrush="{DynamicResource Brush.Border2}"
311+
EmptyContentBackground="{DynamicResource Brush.Diff.EmptyBG}"
312+
AddedContentBackground="{DynamicResource Brush.Diff.AddedBG}"
313+
DeletedContentBackground="{DynamicResource Brush.Diff.DeletedBG}"
314+
AddedHighlightBrush="{DynamicResource Brush.Diff.AddedHighlight}"
315+
DeletedHighlightBrush="{DynamicResource Brush.Diff.DeletedHighlight}"
316+
IndicatorForeground="{DynamicResource Brush.FG2}"
317+
FontFamily="{DynamicResource Fonts.Monospace}"
318+
FontSize="{Binding Source={x:Static vm:Preferences.Instance}, Path=EditorFontSize}"
319+
UseSyntaxHighlighting="{Binding Source={x:Static vm:Preferences.Instance}, Path=UseSyntaxHighlighting}"
320+
WordWrap="False"
321+
ShowHiddenSymbols="{Binding Source={x:Static vm:Preferences.Instance}, Path=ShowHiddenSymbolsInDiffView}"/>
322+
323+
<Rectangle Grid.Row="2" Grid.Column="5" Fill="{DynamicResource Brush.Border2}" Width="1" HorizontalAlignment="Center" VerticalAlignment="Stretch"/>
324+
325+
<v:TextDiffViewMinimap Grid.Column="6"
326+
DisplayRange="{Binding #OurSidePresenter.DisplayRange}"
327+
AddedLineBrush="{DynamicResource Brush.Diff.AddedBG}"
328+
DeletedLineBrush="{DynamicResource Brush.Diff.DeletedBG}"/>
258329
</Grid>
259330
</Border>
260-
331+
261332
<StackPanel Margin="0,8,0,0" Orientation="Horizontal" HorizontalAlignment="Center">
262333
<Button Classes="flat" Content="USE THEIRS" Command="{Binding UseTheirs}"/>
263334
<Button Classes="flat" Margin="8,0,0,0" Content="USE MINE" Command="{Binding UseMine}"/>

0 commit comments

Comments
 (0)