Skip to content

Commit e8fcd6f

Browse files
committed
[wip] Display file content
1 parent 67f4330 commit e8fcd6f

File tree

3 files changed

+260
-1
lines changed

3 files changed

+260
-1
lines changed

Diff for: src/ViewModels/Conflict.cs

+12
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,21 @@ public bool IsResolved
4141
private set;
4242
}
4343

44+
public Models.Change Change
45+
{
46+
get => _change;
47+
}
48+
49+
public Repository Repository
50+
{
51+
get => _repo;
52+
}
53+
4454
public Conflict(Repository repo, WorkingCopy wc, Models.Change change)
4555
{
4656
_wc = wc;
4757
_change = change;
58+
_repo = repo;
4859

4960
IsResolved = new Commands.IsConflictResolved(repo.FullPath, change).ReadToEnd().IsSuccess;
5061

@@ -98,5 +109,6 @@ public void OpenExternalMergeTool()
98109

99110
private WorkingCopy _wc = null;
100111
private Models.Change _change = null;
112+
private Repository _repo = null;
101113
}
102114
}

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;
@@ -2016,4 +2017,111 @@ private void OnDiscardChunk(object _1, RoutedEventArgs _2)
20162017
repo.SetWatcherEnabled(true);
20172018
}
20182019
}
2020+
2021+
public class ConflictTextDiffPresenter: SingleSideTextDiffPresenter
2022+
{
2023+
public enum Side : int
2024+
{
2025+
Ours = 0, WorkingCopy = 1, Theirs = 2
2026+
}
2027+
2028+
public Side DisplaySide {
2029+
get; set;
2030+
}
2031+
protected override void OnLoaded(RoutedEventArgs e)
2032+
{
2033+
base.OnLoaded(e);
2034+
}
2035+
protected override void OnUnloaded(RoutedEventArgs e)
2036+
{
2037+
base.OnUnloaded(e);
2038+
}
2039+
2040+
protected override void OnDataContextChanged(EventArgs e)
2041+
{
2042+
base.OnDataContextChanged(e);
2043+
2044+
if (DataContext is ViewModels.Conflict diff)
2045+
{
2046+
string conflict_start_marker = "<<<<<<< HEAD";
2047+
string conflict_separator_marker = "=======";
2048+
string conflict_end_marker = ">>>>>>> ";
2049+
2050+
var cmd = new Commands.QueryRefsContainsCommit(diff.Repository.FullPath, (diff.Theirs as Models.Commit)?.SHA);
2051+
var their_branches = cmd.Result().ToArray();
2052+
2053+
string filePath = Path.Combine(diff.Repository.FullPath, diff.Change.Path);
2054+
var lines =File.ReadAllLines(filePath).ToList<string>();
2055+
2056+
bool shouldFilter = DisplaySide != Side.WorkingCopy;
2057+
while(shouldFilter) {
2058+
int idx_start = lines.IndexOf(conflict_start_marker);
2059+
if (idx_start == -1) {
2060+
shouldFilter = false;
2061+
break;
2062+
}
2063+
2064+
int idx_sep = lines.IndexOf(conflict_separator_marker, idx_start);
2065+
if (idx_sep == -1) {
2066+
shouldFilter = false;
2067+
break;
2068+
}
2069+
2070+
int idx_end = idx_sep + 1;
2071+
while(idx_end < lines.Count - 1) {
2072+
bool found = false;
2073+
for (int i = 0; i < their_branches.Length; i++) {
2074+
string ce_marker = conflict_end_marker + their_branches[i].Name;
2075+
if(lines[idx_end].StartsWith(ce_marker)) {
2076+
found = true;
2077+
break;
2078+
}
2079+
}
2080+
if (found) {
2081+
break;
2082+
}
2083+
idx_end++;
2084+
}
2085+
if (idx_end == lines.Count - 1) {
2086+
shouldFilter = false;
2087+
break;
2088+
}
2089+
2090+
switch (DisplaySide) {
2091+
case Side.Ours:
2092+
lines.RemoveRange(idx_sep, idx_end - idx_sep + 1);
2093+
lines.RemoveAt(idx_start);
2094+
break;
2095+
case Side.WorkingCopy:
2096+
break;
2097+
case Side.Theirs:
2098+
lines.RemoveAt(idx_end);
2099+
lines.RemoveRange(idx_start, idx_sep - idx_start + 1);
2100+
break;
2101+
}
2102+
}
2103+
2104+
var builder = new StringBuilder();
2105+
foreach (var line in lines)
2106+
{
2107+
if (line.Length > 10000)
2108+
{
2109+
builder.Append(line.Substring(0, 1000));
2110+
builder.Append($"...({line.Length - 1000} characters trimmed)");
2111+
builder.AppendLine();
2112+
}
2113+
else
2114+
{
2115+
builder.AppendLine(line);
2116+
}
2117+
}
2118+
2119+
Text = builder.ToString();
2120+
}
2121+
else
2122+
{
2123+
Text = string.Empty;
2124+
}
2125+
}
2126+
}
20192127
}

Diff for: src/Views/WorkingCopy.axaml

+140-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,146 @@
203203
<ContentControl Content="{Binding DetailContext}">
204204
<ContentControl.DataTemplates>
205205
<DataTemplate DataType="vm:Conflict">
206-
<v:Conflict/>
206+
<Border Background="{DynamicResource Brush.Window}" BorderThickness="1" BorderBrush="{DynamicResource Brush.Border2}">
207+
<Grid VerticalAlignment="Center">
208+
<StackPanel Orientation="Vertical" IsVisible="{Binding !IsResolved}">
209+
<StackPanel.DataTemplates>
210+
<DataTemplate DataType="m:Branch">
211+
<StackPanel Orientation="Horizontal">
212+
<Path Width="12" Height="12" Data="{StaticResource Icons.Branch}"/>
213+
<TextBlock Margin="4,0,0,0" Text="{Binding FriendlyName}"/>
214+
<TextBlock Margin="4,0,0,0" Text="{Binding Head, Converter={x:Static c:StringConverters.ToShortSHA}}" Foreground="DarkOrange"/>
215+
</StackPanel>
216+
</DataTemplate>
217+
218+
<DataTemplate DataType="m:Commit">
219+
<StackPanel Orientation="Horizontal">
220+
<Path Width="12" Height="12" Data="{StaticResource Icons.Commit}"/>
221+
<v:CommitRefsPresenter Margin="8,0,0,0"
222+
TagBackground="{DynamicResource Brush.DecoratorTag}"
223+
Foreground="{DynamicResource Brush.FG1}"
224+
FontFamily="{DynamicResource Fonts.Primary}"
225+
FontSize="11"
226+
VerticalAlignment="Center"
227+
UseGraphColor="False"/>
228+
<TextBlock Margin="4,0,0,0" Text="{Binding SHA, Converter={x:Static c:StringConverters.ToShortSHA}}" Foreground="DarkOrange"/>
229+
<TextBlock Margin="4,0,0,0" Text="{Binding Subject}"/>
230+
</StackPanel>
231+
</DataTemplate>
232+
233+
<DataTemplate DataType="m:Tag">
234+
<StackPanel Orientation="Horizontal">
235+
<Path Width="12" Height="12" Data="{StaticResource Icons.Tag}"/>
236+
<TextBlock Margin="4,0,0,0" Text="{Binding Name}"/>
237+
<TextBlock Margin="4,0,0,0" Text="{Binding SHA, Converter={x:Static c:StringConverters.ToShortSHA}}" Foreground="DarkOrange"/>
238+
</StackPanel>
239+
</DataTemplate>
240+
</StackPanel.DataTemplates>
241+
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>
249+
250+
<Border Margin="16,0" Padding="8" CornerRadius="4" BorderThickness="1" BorderBrush="{DynamicResource Brush.Border2}">
251+
<Border.IsVisible>
252+
<MultiBinding Converter="{x:Static BoolConverters.And}">
253+
<Binding Path="Theirs" Converter="{x:Static ObjectConverters.IsNotNull}"/>
254+
<Binding Path="Mine" Converter="{x:Static ObjectConverters.IsNotNull}"/>
255+
</MultiBinding>
256+
</Border.IsVisible>
257+
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}"/>
329+
</Grid>
330+
</Border>
331+
332+
<StackPanel Margin="0,8,0,0" Orientation="Horizontal" HorizontalAlignment="Center">
333+
<Button Classes="flat" Content="USE THEIRS" Command="{Binding UseTheirs}"/>
334+
<Button Classes="flat" Margin="8,0,0,0" Content="USE MINE" Command="{Binding UseMine}"/>
335+
<Button Classes="flat" Margin="8,0,0,0" Content="OPEN EXTERNAL MERGETOOL" Command="{Binding OpenExternalMergeTool}"/>
336+
</StackPanel>
337+
</StackPanel>
338+
339+
<StackPanel Orientation="Vertical" IsVisible="{Binding IsResolved}">
340+
<Path Width="64" Height="64" Data="{StaticResource Icons.Check}" Fill="Green"/>
341+
<TextBlock Margin="0,16,0,8" FontSize="20" FontWeight="Bold" Text="{DynamicResource Text.WorkingCopy.Conflicts.Resolved}" Foreground="{DynamicResource Brush.FG2}" HorizontalAlignment="Center"/>
342+
<TextBlock Text="{DynamicResource Text.WorkingCopy.CanStageTip}" Foreground="{DynamicResource Brush.FG2}" HorizontalAlignment="Center"/>
343+
</StackPanel>
344+
</Grid>
345+
</Border>
207346
</DataTemplate>
208347

209348
<DataTemplate DataType="vm:DiffContext">

0 commit comments

Comments
 (0)