-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSessionState.cs
More file actions
108 lines (93 loc) · 2.94 KB
/
SessionState.cs
File metadata and controls
108 lines (93 loc) · 2.94 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace XsltDebugger.DebugAdapter;
internal sealed class SessionState
{
private readonly Dictionary<string, HashSet<int>> _breakpoints = new(StringComparer.OrdinalIgnoreCase);
private IXsltEngine? _engine;
public IXsltEngine? Engine => _engine;
public bool DebugEnabled { get; private set; } = true;
public LogLevel CurrentLogLevel { get; private set; } = LogLevel.Log;
public void SetEngine(IXsltEngine engine, bool debug = true, LogLevel logLevel = LogLevel.Log)
{
_engine = engine ?? throw new ArgumentNullException(nameof(engine));
DebugEnabled = debug;
CurrentLogLevel = logLevel;
XsltEngineManager.ActiveEngine = engine;
XsltEngineManager.SetDebugFlags(debug, logLevel);
ApplyBreakpointsToEngine();
}
public void ClearEngine()
{
_engine = null;
XsltEngineManager.ActiveEngine = null;
}
public IReadOnlyList<int> SetBreakpoints(string file, IEnumerable<int> lines)
{
var normalized = NormalizePath(file);
if (string.IsNullOrWhiteSpace(normalized))
{
return Array.Empty<int>();
}
var set = new HashSet<int>(lines ?? Array.Empty<int>());
if (set.Count == 0)
{
_breakpoints.Remove(normalized);
}
else
{
_breakpoints[normalized] = set;
}
ApplyBreakpointsToEngine();
return set.OrderBy(l => l).ToArray();
}
public IEnumerable<(string file, int line)> GetBreakpointsFor(string file)
{
var normalized = NormalizePath(file);
if (string.IsNullOrWhiteSpace(normalized))
{
return Enumerable.Empty<(string, int)>();
}
if (_breakpoints.TryGetValue(normalized, out var set))
{
return set.Select(line => (normalized, line));
}
return Enumerable.Empty<(string, int)>();
}
public List<(string file, int line)> GetAllBreakpoints()
{
var result = new List<(string file, int line)>();
foreach (var entry in _breakpoints)
{
foreach (var line in entry.Value)
{
result.Add((entry.Key, line));
}
}
return result;
}
private void ApplyBreakpointsToEngine()
{
var engine = _engine;
if (engine == null)
{
return;
}
engine.SetBreakpoints(GetAllBreakpoints());
}
private static string NormalizePath(string path)
{
var result = path ?? string.Empty;
if (!string.IsNullOrWhiteSpace(result))
{
if (result.StartsWith("file://", StringComparison.OrdinalIgnoreCase))
{
try { result = new Uri(result).LocalPath; } catch { }
}
try { result = Path.GetFullPath(result); } catch { }
}
return result;
}
}