| 1 | namespace CascadeIDE.Features.Editor.Application.Monaco; |
| 2 | |
| 3 | /// <summary>Live snapshot for <see cref="MonacoWebViewSurfaceAdapter"/>.</summary> |
| 4 | public sealed class MonacoEditorSessionState |
| 5 | { |
| 6 | private readonly object _gate = new(); |
| 7 | |
| 8 | public int Version { get; private set; } |
| 9 | public string Text { get; private set; } = ""; |
| 10 | public int CaretOffset { get; private set; } |
| 11 | public int SelectionStart { get; private set; } |
| 12 | public int SelectionLength { get; private set; } |
| 13 | |
| 14 | public void ApplyInbound(CideEditorInboundMessage msg) |
| 15 | { |
| 16 | lock (_gate) |
| 17 | { |
| 18 | if (msg.Version is int v) |
| 19 | Version = v; |
| 20 | if (msg.Text is not null) |
| 21 | Text = msg.Text; |
| 22 | if (msg.CaretOffset is int caret) |
| 23 | CaretOffset = caret; |
| 24 | if (msg.SelectionStart is int selStart) |
| 25 | SelectionStart = selStart; |
| 26 | if (msg.SelectionLength is int selLen) |
| 27 | SelectionLength = selLen; |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | public void Seed(string text, int version) |
| 32 | { |
| 33 | lock (_gate) |
| 34 | { |
| 35 | Text = text; |
| 36 | Version = version; |
| 37 | CaretOffset = 0; |
| 38 | SelectionStart = 0; |
| 39 | SelectionLength = 0; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | public void ReadSnapshot(out int version, out string text, out int caret, out int selStart, out int selLen) |
| 44 | { |
| 45 | lock (_gate) |
| 46 | { |
| 47 | version = Version; |
| 48 | text = Text; |
| 49 | caret = CaretOffset; |
| 50 | selStart = SelectionStart; |
| 51 | selLen = SelectionLength; |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /// <summary><see cref="IEditorSurfaceAdapter"/> over Monaco WebView host (ADR 0162).</summary> |
| 57 | public sealed class MonacoWebViewSurfaceAdapter : IEditorSurfaceAdapter |
| 58 | { |
| 59 | private readonly MonacoEditorSessionState _state; |
| 60 | private readonly string? _filePath; |
| 61 | |
| 62 | public MonacoWebViewSurfaceAdapter(MonacoEditorSessionState state, string? filePath) |
| 63 | { |
| 64 | _state = state; |
| 65 | _filePath = filePath; |
| 66 | } |
| 67 | |
| 68 | public string? FilePath => _filePath; |
| 69 | |
| 70 | public int CaretOffset |
| 71 | { |
| 72 | get |
| 73 | { |
| 74 | _state.ReadSnapshot(out _, out _, out var caret, out _, out _); |
| 75 | return caret; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | public int TextLength |
| 80 | { |
| 81 | get |
| 82 | { |
| 83 | _state.ReadSnapshot(out _, out var text, out _, out _, out _); |
| 84 | return text.Length; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | public void GetSelection(out int start, out int length) |
| 89 | { |
| 90 | _state.ReadSnapshot(out _, out _, out _, out start, out length); |
| 91 | } |
| 92 | } |
| 93 | |