From cb7e4622e8d736983e76ed20619be6a4c8ba15e4 Mon Sep 17 00:00:00 2001 From: routersys Date: Tue, 30 Jun 2026 07:56:50 +0900 Subject: [PATCH] =?UTF-8?q?=E3=82=A8=E3=83=87=E3=82=A3=E3=82=BF=E3=81=AB?= =?UTF-8?q?=E3=82=A8=E3=83=A9=E3=83=BC=E3=81=AE=E8=B5=A4=E3=81=84=E6=B3=A2?= =?UTF-8?q?=E7=B7=9A=E8=A1=A8=E7=A4=BA=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Diagnostics/LuaScriptDiagnosticOverlay.cs | 254 ++++++++++++++++++ LuaScript/LuaFoldingStrategy.cs | 2 + 2 files changed, 256 insertions(+) create mode 100644 LuaScript/Diagnostics/LuaScriptDiagnosticOverlay.cs diff --git a/LuaScript/Diagnostics/LuaScriptDiagnosticOverlay.cs b/LuaScript/Diagnostics/LuaScriptDiagnosticOverlay.cs new file mode 100644 index 0000000..ba0ee21 --- /dev/null +++ b/LuaScript/Diagnostics/LuaScriptDiagnosticOverlay.cs @@ -0,0 +1,254 @@ +using System; +using System.Collections.Generic; +using System.Reflection; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Controls.Primitives; +using System.Windows.Input; +using System.Windows.Media; +using ICSharpCode.AvalonEdit.Document; +using ICSharpCode.AvalonEdit.Folding; +using ICSharpCode.AvalonEdit.Rendering; + +namespace LuaScript.Diagnostics +{ + internal sealed class LuaScriptDiagnosticOverlay : IBackgroundRenderer, ILuaScriptDiagnosticsListener + { + private static readonly FieldInfo? s_textViewsField = + typeof(FoldingManager).GetField("textViews", BindingFlags.Instance | BindingFlags.NonPublic); + + private static readonly Pen s_squigglePen = CreateSquigglePen(); + + private sealed class Marker : ISegment + { + public required int Offset { get; init; } + public required int Length { get; init; } + public int EndOffset => Offset + Length; + public required LuaScriptDiagnostic Diagnostic { get; init; } + } + + private readonly TextView _textView; + private readonly ToolTip _toolTip = new() { Placement = PlacementMode.Mouse }; + private readonly List _markers = []; + private object? _renderedText; + private bool _isDirty = true; + + public static void Attach(FoldingManager manager) + { + var textView = ResolveTextView(manager); + if (textView is null) + return; + + foreach (var renderer in textView.BackgroundRenderers) + { + if (renderer is LuaScriptDiagnosticOverlay) + return; + } + + _ = new LuaScriptDiagnosticOverlay(textView); + } + + private LuaScriptDiagnosticOverlay(TextView textView) + { + _textView = textView; + textView.BackgroundRenderers.Add(this); + textView.MouseHover += OnMouseHover; + textView.MouseHoverStopped += OnMouseHoverStopped; + LuaScriptDiagnostics.Instance.Subscribe(this); + } + + public KnownLayer Layer => KnownLayer.Selection; + + public void OnDiagnosticsChanged() + { + _textView.Dispatcher.BeginInvoke(() => + { + _isDirty = true; + _textView.InvalidateLayer(KnownLayer.Selection); + }); + } + + public void Draw(TextView textView, DrawingContext drawingContext) + { + var document = textView.Document; + if (document is null) + return; + + if (_isDirty || !ReferenceEquals(document.Text, _renderedText)) + Rebuild(document); + + if (_markers.Count == 0 || !textView.VisualLinesValid) + return; + + foreach (var marker in _markers) + { + foreach (var rect in BackgroundGeometryBuilder.GetRectsForSegment(textView, marker, false)) + DrawSquiggle(drawingContext, rect); + } + } + + private void Rebuild(TextDocument document) + { + _isDirty = false; + _renderedText = document.Text; + _markers.Clear(); + + foreach (var diagnostic in LuaScriptDiagnostics.Instance.Get(document.Text)) + { + if (TryCreateMarker(document, diagnostic, out var marker)) + _markers.Add(marker); + } + } + + private static bool TryCreateMarker(TextDocument document, LuaScriptDiagnostic diagnostic, out Marker marker) + { + marker = null!; + + int lineNumber = diagnostic.Line > 0 ? diagnostic.Line : 1; + if (lineNumber > document.LineCount) + return false; + + var line = document.GetLineByNumber(lineNumber); + + int offset; + int length; + if (diagnostic.Line > 0 && diagnostic.Column > 0 && diagnostic.Length > 0) + { + offset = Math.Min(line.Offset + diagnostic.Column - 1, line.EndOffset); + length = Math.Min(diagnostic.Length, line.EndOffset - offset); + } + else + { + offset = SkipLeadingWhitespace(document, line); + length = line.EndOffset - offset; + } + + if (length <= 0) + { + if (line.Length == 0) + return false; + offset = line.Offset; + length = line.Length; + } + + marker = new Marker { Offset = offset, Length = length, Diagnostic = diagnostic }; + return true; + } + + private static int SkipLeadingWhitespace(TextDocument document, DocumentLine line) + { + int offset = line.Offset; + while (offset < line.EndOffset) + { + char c = document.GetCharAt(offset); + if (c != ' ' && c != '\t') + break; + offset++; + } + return offset; + } + + private void OnMouseHover(object? sender, MouseEventArgs e) + { + var document = _textView.Document; + if (document is null) + return; + + var position = _textView.GetPositionFloor(e.GetPosition(_textView) + _textView.ScrollOffset); + if (position is null) + return; + + int offset = document.GetOffset(position.Value.Location); + foreach (var marker in _markers) + { + if (offset < marker.Offset || offset > marker.EndOffset) + continue; + + _toolTip.Content = BuildToolTipContent(marker.Diagnostic); + _toolTip.IsOpen = true; + e.Handled = true; + return; + } + } + + private void OnMouseHoverStopped(object? sender, MouseEventArgs e) + { + _toolTip.IsOpen = false; + } + + private static object BuildToolTipContent(LuaScriptDiagnostic diagnostic) + { + var panel = new StackPanel { MaxWidth = 480 }; + panel.Children.Add(new TextBlock { Text = KindLabel(diagnostic.Kind), FontWeight = FontWeights.Bold }); + if (!string.IsNullOrEmpty(diagnostic.Message)) + panel.Children.Add(new TextBlock { Text = diagnostic.Message, TextWrapping = TextWrapping.Wrap }); + return panel; + } + + private static string KindLabel(LuaScriptDiagnosticKind kind) => kind switch + { + LuaScriptDiagnosticKind.Compile => Texts.DiagnosticCompile, + LuaScriptDiagnosticKind.Timeout => Texts.DiagnosticTimeout, + _ => Texts.DiagnosticRuntime, + }; + + private static void DrawSquiggle(DrawingContext drawingContext, Rect rect) + { + if (rect.Width <= 0) + return; + + const double step = 3.0; + double baseline = rect.Bottom - 0.5; + + var geometry = new StreamGeometry(); + using (var context = geometry.Open()) + { + context.BeginFigure(new Point(rect.Left, baseline), false, false); + + var points = new List(); + int index = 0; + for (double x = rect.Left; x <= rect.Right; x += step) + { + points.Add(new Point(x, baseline - (index % 2 == 0 ? 0.0 : step))); + index++; + } + context.PolyLineTo(points, true, false); + } + geometry.Freeze(); + + drawingContext.DrawGeometry(null, s_squigglePen, geometry); + } + + private static Pen CreateSquigglePen() + { + var pen = new Pen(new SolidColorBrush(Color.FromRgb(0xE5, 0x1A, 0x1A)), 1.0); + pen.Brush.Freeze(); + pen.Freeze(); + return pen; + } + + private static TextView? ResolveTextView(FoldingManager? manager) + { + if (manager is null || s_textViewsField is null) + return null; + + try + { + if (s_textViewsField.GetValue(manager) is IEnumerable views) + { + foreach (var view in views) + { + if (view is not null) + return view; + } + } + } + catch + { + return null; + } + + return null; + } + } +} diff --git a/LuaScript/LuaFoldingStrategy.cs b/LuaScript/LuaFoldingStrategy.cs index 9f3e1a8..5e83ed1 100644 --- a/LuaScript/LuaFoldingStrategy.cs +++ b/LuaScript/LuaFoldingStrategy.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Text.RegularExpressions; using ICSharpCode.AvalonEdit.Document; using ICSharpCode.AvalonEdit.Folding; +using LuaScript.Diagnostics; using YukkuriMovieMaker.Controls.AvalonEdit.FoldingStrategy; namespace LuaScript @@ -25,6 +26,7 @@ namespace LuaScript public void UpdateFoldings(FoldingManager manager, TextDocument document) { manager.UpdateFoldings(CreateNewFoldings(document, out int firstErrorOffset), firstErrorOffset); + LuaScriptDiagnosticOverlay.Attach(manager); } private static IEnumerable CreateNewFoldings(TextDocument document, out int firstErrorOffset)