diff --git a/csharp_mod/Extensions.cs b/csharp_mod/Extensions.cs index 71499a6..0e1f8db 100644 --- a/csharp_mod/Extensions.cs +++ b/csharp_mod/Extensions.cs @@ -42,9 +42,9 @@ public static unsafe class SlangExtensions * Rust allocation after the List is created, there is no need to Drop this memory. * */ - public static Line ToLine(this Vec_FfiToken_t vec, string sourceText) + public static StyledLine ToLine(this Vec_FfiToken_t vec, string sourceText) { - var list = new Line(sourceText); + var tokens = new List(); var currentPtr = vec.ptr; @@ -63,9 +63,8 @@ public static unsafe class SlangExtensions 0, colIndex, token.length, - color, token.token_kind, - 0, + color, token.tooltip.AsString() ); @@ -76,12 +75,12 @@ public static unsafe class SlangExtensions semanticToken.Data = errMsg; semanticToken.Color = ICodeFormatter.ColorError; } - list.AddToken(semanticToken); + tokens.Add(semanticToken); } Ffi.free_ffi_token_vec(vec); - return list; + return new StyledLine(sourceText, tokens); } public static unsafe List ToList(this Vec_FfiDiagnostic_t vec) diff --git a/csharp_mod/Formatter.cs b/csharp_mod/Formatter.cs index 13eaf16..791826d 100644 --- a/csharp_mod/Formatter.cs +++ b/csharp_mod/Formatter.cs @@ -4,7 +4,6 @@ using System; using System.Collections.Generic; using System.Linq; using System.Threading; -using System.Timers; using Cysharp.Threading.Tasks; using StationeersIC10Editor; @@ -70,8 +69,9 @@ public class SlangFormatter : ICodeFormatter return this.Lines.RawText; } - public override Line ParseLine(string line) + public override StyledLine ParseLine(string line) { + L.Debug($"Parsing line for syntax highlighting: {line}"); return Marshal.TokenizeLine(line); } @@ -90,8 +90,6 @@ public class SlangFormatter : ICodeFormatter HandleLsp(inputSrc, token).Forget(); } - private void OnTimerElapsed(object sender, ElapsedEventArgs e) { } - private async UniTaskVoid HandleLsp(string inputSrc, CancellationToken cancellationToken) { try @@ -139,33 +137,26 @@ public class SlangFormatter : ICodeFormatter if (line is null) continue; - line.ClearTokens(); - - Dictionary lineDict = Marshal - .TokenizeLine(line.Text) - .Tokens.ToDictionary((t) => t.Column); - if (dict.ContainsKey(lineIndex)) { + var tokens = new List(); foreach (var lineDiagnostic in dict[lineIndex]) { - lineDict[(int)lineDiagnostic.Range.StartCol] = new SemanticToken - { - Column = Math.Abs((int)lineDiagnostic.Range.StartCol), - Length = Math.Abs( - (int)(lineDiagnostic.Range.EndCol - lineDiagnostic.Range.StartCol) - ), - Line = (int)lineIndex, - IsError = true, - Data = lineDiagnostic.Message, - Color = SlangFormatter.ColorError, - }; + tokens.Add( + new SemanticToken( + line: (int)lineIndex, + column: Math.Abs((int)lineDiagnostic.Range.StartCol), + length: Math.Abs( + (int)(lineDiagnostic.Range.EndCol - lineDiagnostic.Range.StartCol) + ), + type: 0, + style: ICodeFormatter.ColorError, + data: lineDiagnostic.Message, + isError: true + ) + ); } - } - - foreach (var token in lineDict.Values) - { - line.AddToken(token); + line.Update(tokens); } } diff --git a/csharp_mod/Marshal.cs b/csharp_mod/Marshal.cs index 3a7b385..e64e964 100644 --- a/csharp_mod/Marshal.cs +++ b/csharp_mod/Marshal.cs @@ -131,11 +131,11 @@ public static class Marshal } } - public static unsafe Line TokenizeLine(string inputString) + public static unsafe StyledLine TokenizeLine(string inputString) { if (string.IsNullOrEmpty(inputString) || !EnsureLibLoaded()) { - return new Line(inputString); + return new StyledLine(inputString ?? ""); } fixed (char* ptrInputStr = inputString) @@ -147,7 +147,7 @@ public static class Marshal }; var tokens = Ffi.tokenize_line(strRef); - + L.Debug($"Tokenized line '{inputString}' into {tokens.len} tokens."); return tokens.ToLine(inputString); } }