-
-
Notifications
You must be signed in to change notification settings - Fork 201
Expand file tree
/
Copy pathExamples.razor
More file actions
432 lines (385 loc) · 12.3 KB
/
Copy pathExamples.razor
File metadata and controls
432 lines (385 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
@page "/examples"
@using DiffPlex.Blazor.Components
@using DiffPlex
@rendermode InteractiveServer
<PageTitle>DiffPlex Examples</PageTitle>
<div class="container-fluid">
<div class="row">
<div class="col-12">
<h1>DiffPlex Examples</h1>
<p>Various examples demonstrating different diff scenarios.</p>
</div>
</div>
<div class="row mb-3">
<div class="col-12">
<div class="btn-group" role="group">
@foreach (var example in examples)
{
<button type="button" class="btn @(selectedExample == example.Key ? "btn-primary" : "btn-outline-primary")"
@onclick="() => SelectExample(example.Key)">
@example.Value.Name
</button>
}
</div>
</div>
</div>
@if (currentExample != null)
{
<div class="row mb-3">
<div class="col-12">
<div class="alert alert-info">
<strong>@currentExample.Name:</strong> @currentExample.Description
</div>
</div>
</div>
<div class="row mb-3">
<div class="col-12">
<div class="btn-group" role="group">
<button type="button" class="btn @(viewMode == "side-by-side" ? "btn-secondary" : "btn-outline-secondary")"
@onclick='() => SetViewMode("side-by-side")'>
Side by Side
</button>
<button type="button" class="btn @(viewMode == "inline" ? "btn-secondary" : "btn-outline-secondary")"
@onclick='() => SetViewMode("inline")'>
Inline
</button>
@if (IsThreeWayExample())
{
<button type="button" class="btn @(viewMode == "three-way" ? "btn-secondary" : "btn-outline-secondary")"
@onclick='() => SetViewMode("three-way")'>
Three-Way Merge
</button>
}
</div>
</div>
</div>
<div class="row">
<div class="col-12">
@if (viewMode == "side-by-side")
{
<DiffViewer OldText="@currentExample.OldText"
NewText="@currentExample.NewText"
OldTextHeader="@currentExample.OldHeader"
NewTextHeader="@currentExample.NewHeader"
IgnoreWhiteSpace="true" />
}
else if (viewMode == "three-way")
{
<ThreeWayMergeViewer BaseText="@currentExample.BaseText"
YoursText="@currentExample.YoursText"
TheirsText="@currentExample.TheirsText"
BaseHeader="@currentExample.BaseHeader"
YoursHeader="@currentExample.YoursHeader"
TheirsHeader="@currentExample.TheirsHeader"
IgnoreWhiteSpace="true" />
}
else
{
<InlineDiffViewer OldText="@currentExample.OldText"
NewText="@currentExample.NewText"
Header="@currentExample.Name"
IgnoreWhiteSpace="true" />
}
</div>
</div>
}
</div>
@code {
private string viewMode = "side-by-side";
private string selectedExample = "merge-conflict";
private ExampleData? currentExample;
private Dictionary<string, ExampleData> examples = new()
{
["merge-conflict"] = new ExampleData
{
Name = "Merge Conflict",
Description = "Shows a three-way merge scenario with conflicts that need manual resolution.",
BaseHeader = "Common Base",
YoursHeader = "Your Changes",
TheirsHeader = "Their Changes",
BaseText = @"function calculateTotal(items) {
let total = 0;
for (let item of items) {
total += item.price;
}
return total;
}
function processOrder(order) {
// Basic processing
return order;
}",
YoursText = @"function calculateTotal(items, tax = 0.08) {
let total = 0;
for (let item of items) {
total += item.price * item.quantity;
}
return total * (1 + tax);
}
function processOrder(order) {
// Validate order before processing
if (!order.items || order.items.length === 0) {
throw new Error('Order must contain items');
}
return order;
}",
TheirsText = @"function calculateTotal(items, discountRate = 0) {
let total = 0;
for (let item of items) {
total += item.price;
}
return total * (1 - discountRate);
}
function processOrder(order) {
// Add logging
console.log('Processing order:', order.id);
return order;
}"
},
["merge-clean"] = new ExampleData
{
Name = "Clean Merge",
Description = "Shows a three-way merge scenario where changes can be automatically merged without conflicts.",
BaseHeader = "Original Code",
YoursHeader = "Feature Branch",
TheirsHeader = "Main Branch",
BaseText = @"class UserService {
constructor() {
this.users = [];
}
addUser(user) {
this.users.push(user);
}
getUser(id) {
return this.users.find(u => u.id === id);
}
}",
YoursText = @"class UserService {
constructor() {
this.users = [];
this.cache = new Map();
}
addUser(user) {
this.users.push(user);
this.cache.set(user.id, user);
}
getUser(id) {
// Check cache first
if (this.cache.has(id)) {
return this.cache.get(id);
}
return this.users.find(u => u.id === id);
}
}",
TheirsText = @"class UserService {
constructor(database) {
this.users = [];
this.database = database;
}
addUser(user) {
this.users.push(user);
}
getUser(id) {
return this.users.find(u => u.id === id);
}
deleteUser(id) {
this.users = this.users.filter(u => u.id !== id);
}
}"
},
["code-refactor"] = new ExampleData
{
Name = "Code Refactor",
Description = "Shows a typical code refactoring scenario with method extraction and variable renaming.",
OldHeader = "Before Refactor",
NewHeader = "After Refactor",
OldText = @"public class Calculator
{
public double Calculate(double a, double b, string operation)
{
if (operation == ""add"")
{
return a + b;
}
else if (operation == ""subtract"")
{
return a - b;
}
else if (operation == ""multiply"")
{
return a * b;
}
else if (operation == ""divide"")
{
if (b != 0)
{
return a / b;
}
else
{
throw new ArgumentException(""Cannot divide by zero"");
}
}
else
{
throw new ArgumentException(""Unknown operation"");
}
}
}",
NewText = @"public class Calculator
{
public double Calculate(double firstNumber, double secondNumber, string operation)
{
return operation switch
{
""add"" => Add(firstNumber, secondNumber),
""subtract"" => Subtract(firstNumber, secondNumber),
""multiply"" => Multiply(firstNumber, secondNumber),
""divide"" => Divide(firstNumber, secondNumber),
_ => throw new ArgumentException(""Unknown operation"")
};
}
private double Add(double a, double b) => a + b;
private double Subtract(double a, double b) => a - b;
private double Multiply(double a, double b) => a * b;
private double Divide(double a, double b)
{
if (b == 0)
throw new ArgumentException(""Cannot divide by zero"");
return a / b;
}
}"
},
["json-config"] = new ExampleData
{
Name = "JSON Config",
Description = "Configuration file changes showing property additions and modifications.",
OldHeader = "Old Config",
NewHeader = "New Config",
OldText = @"{
""database"": {
""connectionString"": ""Server=localhost;Database=mydb;"",
""timeout"": 30
},
""logging"": {
""level"": ""Info"",
""path"": ""/var/log/app.log""
},
""features"": {
""enableCache"": true
}
}",
NewText = @"{
""database"": {
""connectionString"": ""Server=prod-server;Database=mydb;Encrypt=true;"",
""timeout"": 60,
""retryCount"": 3
},
""logging"": {
""level"": ""Warning"",
""path"": ""/var/log/app.log"",
""maxFileSize"": ""10MB""
},
""features"": {
""enableCache"": true,
""enableMetrics"": true
},
""security"": {
""apiKey"": ""${API_KEY}"",
""encryption"": true
}
}"
},
["markdown-doc"] = new ExampleData
{
Name = "Documentation",
Description = "Documentation updates showing content additions and restructuring.",
OldHeader = "Original Docs",
NewHeader = "Updated Docs",
OldText = @"# DiffPlex Library
DiffPlex is a .NET library for generating textual diffs.
## Installation
Install via NuGet:
```
Install-Package DiffPlex
```
## Basic Usage
```csharp
var differ = new Differ();
var result = differ.CreateLineDiffs(oldText, newText, false);
```
## Features
- Line-by-line comparison
- Character-by-character comparison
- Ignore whitespace option",
NewText = @"# DiffPlex Library
DiffPlex is a powerful .NET library for generating textual diffs with support for multiple comparison modes.
## Installation
Install via NuGet Package Manager:
```
Install-Package DiffPlex
```
Or via .NET CLI:
```
dotnet add package DiffPlex
```
## Quick Start
```csharp
using DiffPlex;
using DiffPlex.DiffBuilder;
var differ = new InlineDiffBuilder(new Differ());
var diff = differ.BuildDiffModel(oldText, newText);
foreach (var line in diff.Lines)
{
Console.WriteLine($""{line.Type}: {line.Text}"");
}
```
## Features
- **Line-by-line comparison** - Compare texts line by line
- **Character-by-character comparison** - Detailed character-level diffs
- **Word-by-word comparison** - Compare at word boundaries
- **Ignore whitespace option** - Flexible whitespace handling
- **Side-by-side view** - Display diffs in split view
- **Inline view** - Display diffs in unified view
- **Custom chunkers** - Define your own comparison units
## Advanced Usage
### Side-by-Side Diff
```csharp
var differ = new SideBySideDiffBuilder(new Differ());
var diffModel = differ.BuildDiffModel(oldText, newText);
```"
}
};
protected override void OnInitialized()
{
currentExample = examples[selectedExample];
}
private void SelectExample(string exampleKey)
{
selectedExample = exampleKey;
currentExample = examples[exampleKey];
}
private void SetViewMode(string mode)
{
viewMode = mode;
}
private bool IsThreeWayExample()
{
return currentExample != null && !string.IsNullOrEmpty(currentExample.BaseText);
}
private class ExampleData
{
public string Name { get; set; } = "";
public string Description { get; set; } = "";
public string OldHeader { get; set; } = "";
public string NewHeader { get; set; } = "";
public string OldText { get; set; } = "";
public string NewText { get; set; } = "";
// Three-way merge properties
public string BaseText { get; set; } = "";
public string YoursText { get; set; } = "";
public string TheirsText { get; set; } = "";
public string BaseHeader { get; set; } = "";
public string YoursHeader { get; set; } = "";
public string TheirsHeader { get; set; } = "";
}
}