highlight background of IC10 for the current caret position of the Slang script

This commit is contained in:
2025-12-20 17:32:20 -07:00
parent 1439f9ee7e
commit 0962b3a5e7

View File

@@ -164,10 +164,25 @@ public class SlangFormatter : ICodeFormatter
ApplyDiagnostics(dict); ApplyDiagnostics(dict);
// If we have valid code, update the IC10 output // If we have valid code, update the IC10 output
if ( if (dict.Count > 0)
dict.Count < 1 {
&& Marshal.CompileFromString(inputSrc, out var compiled, out var sourceMap) return;
) }
var (compilationSuccess, compiled, sourceMap) = await Task.Run(
() =>
{
var successful = Marshal.CompileFromString(
inputSrc,
out var compiled,
out var sourceMap
);
return (successful, compiled, sourceMap);
},
cancellationToken
);
if (compilationSuccess)
{ {
ic10CompilationResult = compiled; ic10CompilationResult = compiled;
ic10SourceMap = sourceMap; ic10SourceMap = sourceMap;
@@ -187,12 +202,11 @@ public class SlangFormatter : ICodeFormatter
var caretPos = Editor.CaretPos.Line; var caretPos = Editor.CaretPos.Line;
// get the slang sourceMap at the current editor line // get the slang sourceMap at the current editor line
var lines = ic10SourceMap var lines = ic10SourceMap.FindAll(entry =>
.FindAll(entry => entry.SlangSource.StartLine == caretPos || entry.SlangSource.EndLine == caretPos
entry.SlangSource.StartLine == caretPos || entry.SlangSource.EndLine == caretPos );
)
.ConvertAll(el => el.Ic10Line) lines.Sort((itemA, itemB) => itemA.Ic10Line.CompareTo(itemB.Ic10Line));
.ToHashSet();
// extract the current "context" of the ic10 compilation. The current Slang source line // extract the current "context" of the ic10 compilation. The current Slang source line
// should be directly next to the compiled IC10 source line, and we should highlight the // should be directly next to the compiled IC10 source line, and we should highlight the
@@ -200,19 +214,35 @@ public class SlangFormatter : ICodeFormatter
iC10CodeFormatter.ResetCode(ic10CompilationResult); iC10CodeFormatter.ResetCode(ic10CompilationResult);
uint index = 0; if (lines.Count() < 1)
foreach (var line in iC10CodeFormatter.Lines)
{ {
if (lines.Contains(index)) return;
{ }
index += 1; // get the "middle" IC10 line
continue; var max = lines.Max(line => line.Ic10Line);
} var min = lines.Min(line => line.Ic10Line);
line.ForEach(token => var middle = (max + min) / 2;
{
token.Style.Color = ColorDefault; // first, update the line styles that are between min and max inclusive
}); foreach (var index in Enumerable.Range((int)min, (int)(max - min) + 1))
index += 1; {
var lineText = iC10CodeFormatter.Lines[index].Text;
var newLine = new StyledLine(
lineText,
[
new SemanticToken
{
Column = 0,
Length = lineText.Length,
Line = index,
Background = ColorIdentifier,
Color = ColorFromHTML("black"),
},
]
);
iC10CodeFormatter.Lines[index] = newLine;
} }
} }