From 45a7a6b38bb3f22560bfb1adda8171c69f9cb529 Mon Sep 17 00:00:00 2001 From: Devin Bidwell Date: Fri, 19 Dec 2025 20:46:24 -0700 Subject: [PATCH] wip -- show ic10 alongside of Slang --- csharp_mod/Formatter.cs | 81 ++++++++++++++++++++++++++++++ csharp_mod/stationeersSlang.csproj | 4 ++ 2 files changed, 85 insertions(+) diff --git a/csharp_mod/Formatter.cs b/csharp_mod/Formatter.cs index 9ed20dd..1efae5f 100644 --- a/csharp_mod/Formatter.cs +++ b/csharp_mod/Formatter.cs @@ -5,13 +5,20 @@ using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; +using ImGuiNET; using StationeersIC10Editor; +using StationeersIC10Editor.IC10; +using UnityEngine; public class SlangFormatter : ICodeFormatter { private CancellationTokenSource? _lspCancellationToken; private object _tokenLock = new(); + private IC10CodeFormatter iC10CodeFormatter = new IC10CodeFormatter(); + private string ic10CompilationResult = ""; + private List ic10SourceMap = new(); + // VS Code Dark Theme Palette public static readonly uint ColorControl = ColorFromHTML("#C586C0"); // Pink (if, return, loop) public static readonly uint ColorDeclaration = ColorFromHTML("#569CD6"); // Blue (let, device, fn) @@ -33,6 +40,7 @@ public class SlangFormatter : ICodeFormatter : base() { OnCodeChanged += HandleCodeChanged; + OnCaretMoved += UpdateIc10Formatter; } public static double MatchingScore(string input) @@ -70,6 +78,34 @@ public class SlangFormatter : ICodeFormatter return this.Lines.RawText; } + public override void DrawLine(int lineIndex, TextRange selection, bool drawLineNumber = true) + { + Vector2 cursorPos = ImGui.GetCursorScreenPos(); + Vector2 space = ImGui.GetContentRegionAvail(); + base.DrawLine(lineIndex, selection, drawLineNumber); + + var charWidth = Settings.CharWidth; + + var width = Mathf.Max(Lines.Width + 10.0f + LineNumberOffset * charWidth, space.x / 2); + + ImGui + .GetWindowDrawList() + .AddLine( + new Vector2(cursorPos.x + width + 4.5f * charWidth, cursorPos.y), + new Vector2( + cursorPos.x + width + 4.5f * charWidth, + cursorPos.y + space.y + Settings.LineHeight + ), + ColorLineNumber, + 1.0f + ); + + cursorPos.x += width; + ImGui.SetCursorScreenPos(cursorPos); + if (lineIndex < iC10CodeFormatter.Lines.Count) + iC10CodeFormatter.DrawLine(lineIndex, new TextRange(), true); + } + public override StyledLine ParseLine(string line) { // We create the line first @@ -126,6 +162,17 @@ public class SlangFormatter : ICodeFormatter ); ApplyDiagnostics(dict); + + // If we have valid code, update the IC10 output + if ( + dict.Count < 1 + && Marshal.CompileFromString(inputSrc, out var compiled, out var sourceMap) + ) + { + ic10CompilationResult = compiled; + ic10SourceMap = sourceMap; + UpdateIc10Formatter(); + } } catch (OperationCanceledException) { } catch (Exception ex) @@ -134,6 +181,40 @@ public class SlangFormatter : ICodeFormatter } } + private void UpdateIc10Formatter() + { + iC10CodeFormatter.Editor = Editor; + var caretPos = Editor.CaretPos.Line; + + // get the slang sourceMap at the current editor line + var lines = ic10SourceMap.FindAll(entry => + entry.SlangSource.StartLine == caretPos || entry.SlangSource.EndLine == caretPos + ); + + // 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 + // IC10 code that directly represents the Slang source + + iC10CodeFormatter.ResetCode(ic10CompilationResult); + + if (lines.Count < 1) + { + return; + } + + iC10CodeFormatter.Lines = new StyledText(); + + var minLine = lines.Min(map => map.Ic10Line); + var maxLine = lines.Max(map => map.Ic10Line); + + foreach (var line in lines) + { + iC10CodeFormatter + .Lines[(int)line.Ic10Line] + .ForEach(token => token.Style.Color = ColorIdentifier); + } + } + // This runs on the Main Thread private void ApplyDiagnostics(Dictionary> dict) { diff --git a/csharp_mod/stationeersSlang.csproj b/csharp_mod/stationeersSlang.csproj index 3e4c048..e8210ec 100644 --- a/csharp_mod/stationeersSlang.csproj +++ b/csharp_mod/stationeersSlang.csproj @@ -39,6 +39,10 @@ ./ref/Assembly-CSharp.dll False + + ./ref/RG.ImGui.dll + False + ./ref/IC10Editor.dll