refactor highlighting hack to be generic

This commit is contained in:
Matt Parker
2025-10-17 18:18:48 +10:00
parent dd3678d50c
commit ebc77f596e
2 changed files with 36 additions and 26 deletions

View File

@@ -458,7 +458,7 @@ public static class RoslynAnalysis
var syntaxTree = await document.GetSyntaxTreeAsync(cancellationToken); var syntaxTree = await document.GetSyntaxTreeAsync(cancellationToken);
var root = await syntaxTree!.GetRootAsync(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(); var result = classifiedSpans.Select(s => (syntaxTree.GetMappedLineSpan(s.TextSpan), s)).ToList();
return result; return result;
} }

View File

@@ -53,43 +53,53 @@ public partial class CustomHighlighter : SyntaxHighlighter
private void LinesAdded(long fromLine, int difference, SharpIdeCodeEdit.LineEditOrigin origin) private void LinesAdded(long fromLine, int difference, SharpIdeCodeEdit.LineEditOrigin origin)
{ {
var newRazorDict = new System.Collections.Generic.Dictionary<int, ImmutableArray<SharpIdeRazorClassifiedSpan>>(); _razorClassifiedSpansByLine = Rearrange(_razorClassifiedSpansByLine, fromLine, difference, origin);
_classifiedSpansByLine = Rearrange(_classifiedSpansByLine, fromLine, difference, origin);
return;
foreach (var kvp in _razorClassifiedSpansByLine) static System.Collections.Generic.Dictionary<int, T> Rearrange<T>(System.Collections.Generic.Dictionary<int, T> existingDictionary, long fromLine, int difference, SharpIdeCodeEdit.LineEditOrigin origin)
{ {
bool shouldShift = var newDict = new System.Collections.Generic.Dictionary<int, T>();
kvp.Key > fromLine || // always shift lines after the insertion point foreach (var kvp in existingDictionary)
(origin == SharpIdeCodeEdit.LineEditOrigin.StartOfLine && kvp.Key == fromLine); // shift current line if origin is Start {
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; int newKey = shouldShift ? kvp.Key + difference : kvp.Key;
newRazorDict[newKey] = kvp.Value; newDict[newKey] = kvp.Value;
}
return newDict;
} }
_razorClassifiedSpansByLine = newRazorDict;
} }
private void LinesRemoved(long fromLine, int numberOfLinesRemoved) private void LinesRemoved(long fromLine, int numberOfLinesRemoved)
{ {
// everything from 'fromLine' onwards needs to be shifted up by numberOfLinesRemoved _classifiedSpansByLine = Rearrange(_classifiedSpansByLine, fromLine, numberOfLinesRemoved);
var newRazorDict = new System.Collections.Generic.Dictionary<int, ImmutableArray<SharpIdeRazorClassifiedSpan>>(); _razorClassifiedSpansByLine = Rearrange(_razorClassifiedSpansByLine, fromLine, numberOfLinesRemoved);
return;
foreach (var kvp in _razorClassifiedSpansByLine)
static System.Collections.Generic.Dictionary<int, T> Rearrange<T>(System.Collections.Generic.Dictionary<int, T> 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<int, T>();
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) return newDict;
{
newRazorDict[kvp.Key - numberOfLinesRemoved] = kvp.Value;
}
else if (kvp.Key >= fromLine + numberOfLinesRemoved)
{
newRazorDict[kvp.Key - numberOfLinesRemoved] = kvp.Value;
}
} }
_razorClassifiedSpansByLine = newRazorDict;
} }
public override Dictionary _GetLineSyntaxHighlighting(int line) public override Dictionary _GetLineSyntaxHighlighting(int line)