diff --git a/UIX/Microsoft/Iris/Debug/Symbols/ApplicationDebugSymbols.cs b/UIX/Microsoft/Iris/Debug/Symbols/ApplicationDebugSymbols.cs index 959177a..dc8fbd0 100644 --- a/UIX/Microsoft/Iris/Debug/Symbols/ApplicationDebugSymbols.cs +++ b/UIX/Microsoft/Iris/Debug/Symbols/ApplicationDebugSymbols.cs @@ -30,8 +30,7 @@ public class FileDebugSymbols if (!HasSourceCode()) throw new InvalidOperationException($"Must supply source with {nameof(SetSourceCode)}"); - span.Start.AsZeroIndexed(out var startLine, out var startColumn); - span.End.AsZeroIndexed(out var endLine, out var endColumn); + span.AsZeroIndexed(out var startLine, out var startColumn, out var endLine, out var endColumn); StringBuilder sb = new(); @@ -45,14 +44,9 @@ public class FileDebugSymbols Index lineEndIndex = currentLineIndex == endLine ? endColumn - : ^1; + : ^0; - var start = lineStartIndex.GetOffset(line.Length); - var end = lineEndIndex.GetOffset(line.Length); - var length = end - start; - - if (length > 0) - sb.AppendLine(line.Substring(start, length)); + sb.AppendLine(line[lineStartIndex..lineEndIndex]); } return sb.ToString(); @@ -69,31 +63,33 @@ public class FileDebugSymbols } [CLSCompliant(false)] -public class SourceMap +public class SourceMap : Dictionary { - public Dictionary Xml { get; } = []; - public Dictionary> Script { get; } = []; - - public (uint Offset, SourceSpan Span)? GetLocationFromPosition(SourcePosition pos) + public Entry GetLocationFromPosition(SourcePosition pos) { - var foundLocation = Xml.FirstOrDefault(kvp => - { - var offset = kvp.Key; - var span = kvp.Value; - return span.Contains(pos); - }); + var foundLocation = this + .Where(kvp => + { + var offset = kvp.Key; + var span = kvp.Value; + return span.Contains(pos); + }) + .OrderBy(kvp => kvp.Value.Size) + .FirstOrDefault(); if (foundLocation.Equals(default(KeyValuePair))) return null; - return (foundLocation.Key, foundLocation.Value); + return new Entry(foundLocation.Key, foundLocation.Value); } - public uint? OffsetByLineAndColumn(int line, int col) => OffsetByLineAndColumn(new(line, col)); + public Entry GetLocationFromOffset(uint offset) => new(offset, this[offset]); - public SourceSpan GetContainingSpan(SourcePosition pos) => GetLocationFromPosition(pos)?.Span; - - public uint? OffsetByLineAndColumn(SourcePosition pos) => GetLocationFromPosition(pos)?.Offset; + public sealed class Entry(uint offset, SourceSpan span) + { + public uint Offset { get; } = offset; + public SourceSpan Span { get; } = span; + } } [DebuggerDisplay("L{Line}C{Column}")] @@ -103,7 +99,7 @@ public class SourcePosition : IComparable, IEquatable ((ulong)Line << (sizeof(uint) * 8)) | (uint)Column; + internal ulong Value => ((ulong)Line << (sizeof(uint) * 8)) | (uint)Column; public SourcePosition(int line, int column) { @@ -123,9 +119,9 @@ public class SourcePosition : IComparable, IEquatable Position.CompareTo(other.Position); + public int CompareTo(SourcePosition other) => Value.CompareTo(other.Value); - public bool Equals(SourcePosition other) => Position == other.Position; + public bool Equals(SourcePosition other) => Value == other.Value; public override bool Equals(object obj) { @@ -139,7 +135,7 @@ public class SourcePosition : IComparable, IEquatable End.Value - Start.Value; + public bool Contains(SourcePosition pos) => Start <= pos && pos < End; + + public void AsZeroIndexed(out int startLine, out int startColumn, out int endLine, out int endColumn) + { + Start.AsZeroIndexed(out startLine, out startColumn); + End.AsZeroIndexed(out endLine, out endColumn); + } }