Repository navigation
Expand file tree
/
Copy pathAvalonEditHelper.cs
More file actions
107 lines (96 loc) · 3.64 KB
/
Copy pathAvalonEditHelper.cs
File metadata and controls
107 lines (96 loc) · 3.64 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
using System.Windows;
using ICSharpCode.AvalonEdit;
using ICSharpCode.AvalonEdit.Rendering;
namespace HttpTraceAnalyser
{
/// <summary>
/// Helper class to configure AvalonEdit TextEditor controls globally.
/// Prevents performance issues caused by LinkElementGenerator's expensive regex operations
/// when displaying large HTTP trace files.
/// </summary>
public static class AvalonEditHelper
{
/// <summary>
/// Attached property to control whether link detection should be disabled.
/// </summary>
public static readonly DependencyProperty DisableLinkDetectionProperty =
DependencyProperty.RegisterAttached(
"DisableLinkDetection",
typeof(bool),
typeof(AvalonEditHelper),
new PropertyMetadata(false, OnDisableLinkDetectionChanged));
public static bool GetDisableLinkDetection(DependencyObject obj)
{
return (bool)obj.GetValue(DisableLinkDetectionProperty);
}
public static void SetDisableLinkDetection(DependencyObject obj, bool value)
{
obj.SetValue(DisableLinkDetectionProperty, value);
}
private static void OnDisableLinkDetectionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is TextEditor editor && e.NewValue is true)
{
// Hook into Loaded event to ensure TextArea is fully initialized
if (editor.IsLoaded)
{
DisableLinkDetectionCore(editor);
}
else
{
editor.Loaded += OnEditorLoaded;
}
}
}
private static void OnEditorLoaded(object sender, RoutedEventArgs e)
{
if (sender is TextEditor editor)
{
editor.Loaded -= OnEditorLoaded;
DisableLinkDetectionCore(editor);
}
}
/// <summary>
/// Removes LinkElementGenerator from the TextEditor to prevent regex catastrophic backtracking.
/// </summary>
private static void DisableLinkDetectionCore(TextEditor editor)
{
if (editor.TextArea?.TextView == null)
return;
var generators = editor.TextArea.TextView.ElementGenerators;
var linkGenerators = new System.Collections.Generic.List<VisualLineElementGenerator>();
// Collect all LinkElementGenerator instances
foreach (var generator in generators)
{
if (generator is LinkElementGenerator)
{
linkGenerators.Add(generator);
}
}
// Remove them
foreach (var linkGen in linkGenerators)
{
generators.Remove(linkGen);
}
}
/// <summary>
/// Globally disables link detection for all TextEditor controls in the application.
/// Call this once during application startup.
/// </summary>
public static void DisableLinkDetectionGlobally()
{
// Register a handler for all TextEditor controls as they are loaded
EventManager.RegisterClassHandler(
typeof(TextEditor),
FrameworkElement.LoadedEvent,
new RoutedEventHandler(OnAnyTextEditorLoaded));
}
private static void OnAnyTextEditorLoaded(object sender, RoutedEventArgs e)
{
if (sender is TextEditor editor)
{
DisableLinkDetectionCore(editor);
}
}
}
}