From ebc77f596e3185160a2f877b15164ff37ccacf21 Mon Sep 17 00:00:00 2001 From: Matt Parker <61717342+MattParkerDev@users.noreply.github.com> Date: Fri, 17 Oct 2025 18:18:48 +1000 Subject: [PATCH] refactor highlighting hack to be generic --- .../Features/Analysis/RoslynAnalysis.cs | 2 +- .../CodeEditor/CustomSyntaxHighlighter.cs | 60 +++++++++++-------- 2 files changed, 36 insertions(+), 26 deletions(-) diff --git a/src/SharpIDE.Application/Features/Analysis/RoslynAnalysis.cs b/src/SharpIDE.Application/Features/Analysis/RoslynAnalysis.cs index 71e66c1..f51db5f 100644 --- a/src/SharpIDE.Application/Features/Analysis/RoslynAnalysis.cs +++ b/src/SharpIDE.Application/Features/Analysis/RoslynAnalysis.cs @@ -458,7 +458,7 @@ public static class RoslynAnalysis var syntaxTree = await document.GetSyntaxTreeAsync(cancellationToken); var root = await syntaxTree!.GetRootAsync(cancellationToken); - var classifiedSpans = await ClassifierHelper.GetClassifiedSpansAsync(document, root.FullSpan, ClassificationOptions.Default, false, cancellationToken); + var classifiedSpans = await Classifier.GetClassifiedSpansAsync(document, root.FullSpan, cancellationToken); var result = classifiedSpans.Select(s => (syntaxTree.GetMappedLineSpan(s.TextSpan), s)).ToList(); return result; } diff --git a/src/SharpIDE.Godot/Features/CodeEditor/CustomSyntaxHighlighter.cs b/src/SharpIDE.Godot/Features/CodeEditor/CustomSyntaxHighlighter.cs index b955fc7..e979ce9 100644 --- a/src/SharpIDE.Godot/Features/CodeEditor/CustomSyntaxHighlighter.cs +++ b/src/SharpIDE.Godot/Features/CodeEditor/CustomSyntaxHighlighter.cs @@ -53,43 +53,53 @@ public partial class CustomHighlighter : SyntaxHighlighter private void LinesAdded(long fromLine, int difference, SharpIdeCodeEdit.LineEditOrigin origin) { - var newRazorDict = new System.Collections.Generic.Dictionary>(); + _razorClassifiedSpansByLine = Rearrange(_razorClassifiedSpansByLine, fromLine, difference, origin); + _classifiedSpansByLine = Rearrange(_classifiedSpansByLine, fromLine, difference, origin); + return; - foreach (var kvp in _razorClassifiedSpansByLine) + static System.Collections.Generic.Dictionary Rearrange(System.Collections.Generic.Dictionary existingDictionary, long fromLine, int difference, SharpIdeCodeEdit.LineEditOrigin origin) { - bool shouldShift = - kvp.Key > fromLine || // always shift lines after the insertion point - (origin == SharpIdeCodeEdit.LineEditOrigin.StartOfLine && kvp.Key == fromLine); // shift current line if origin is Start + var newDict = new System.Collections.Generic.Dictionary(); + foreach (var kvp in existingDictionary) + { + bool shouldShift = + kvp.Key > fromLine || // always shift lines after the insertion point + (origin == SharpIdeCodeEdit.LineEditOrigin.StartOfLine && kvp.Key == fromLine); // shift current line if origin is Start - int newKey = shouldShift ? kvp.Key + difference : kvp.Key; - newRazorDict[newKey] = kvp.Value; + int newKey = shouldShift ? kvp.Key + difference : kvp.Key; + newDict[newKey] = kvp.Value; + } + return newDict; } - - _razorClassifiedSpansByLine = newRazorDict; } private void LinesRemoved(long fromLine, int numberOfLinesRemoved) { - // everything from 'fromLine' onwards needs to be shifted up by numberOfLinesRemoved - var newRazorDict = new System.Collections.Generic.Dictionary>(); - - foreach (var kvp in _razorClassifiedSpansByLine) + _classifiedSpansByLine = Rearrange(_classifiedSpansByLine, fromLine, numberOfLinesRemoved); + _razorClassifiedSpansByLine = Rearrange(_razorClassifiedSpansByLine, fromLine, numberOfLinesRemoved); + return; + + static System.Collections.Generic.Dictionary Rearrange(System.Collections.Generic.Dictionary existingDictionary, long fromLine, int numberOfLinesRemoved) { - if (kvp.Key < fromLine) + // everything from 'fromLine' onwards needs to be shifted up by numberOfLinesRemoved + var newDict = new System.Collections.Generic.Dictionary(); + foreach (var kvp in existingDictionary) { - newRazorDict[kvp.Key] = kvp.Value; + if (kvp.Key < fromLine) + { + newDict[kvp.Key] = kvp.Value; + } + else if (kvp.Key == fromLine) + { + newDict[kvp.Key - numberOfLinesRemoved] = kvp.Value; + } + else if (kvp.Key >= fromLine + numberOfLinesRemoved) + { + newDict[kvp.Key - numberOfLinesRemoved] = kvp.Value; + } } - else if (kvp.Key == fromLine) - { - newRazorDict[kvp.Key - numberOfLinesRemoved] = kvp.Value; - } - else if (kvp.Key >= fromLine + numberOfLinesRemoved) - { - newRazorDict[kvp.Key - numberOfLinesRemoved] = kvp.Value; - } + return newDict; } - - _razorClassifiedSpansByLine = newRazorDict; } public override Dictionary _GetLineSyntaxHighlighting(int line)