Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,18 @@ private void ConvertToUnresolvedDirectiveAttribute(
var directiveNameSpan = nameSpan;
if (directiveNameSpan is SourceSpan ns && attributeName.StartsWith('@'))
{
directiveNameSpan = new SourceSpan(ns.FilePath, ns.AbsoluteIndex + 1, ns.LineIndex, ns.CharacterIndex + 1, ns.Length - 1, ns.LineCount, ns.EndCharacterIndex);
directiveNameSpan = ns.WithAbsoluteIndex(ns.AbsoluteIndex + 1)
.WithCharacterIndex(ns.CharacterIndex + 1)
.WithLength(ns.Length - 1);
}

// Strip parameter suffix from OriginalAttributeSpan for parameter matches
var parameterOriginalSpan = directiveNameSpan;
if (match.IsParameterMatch && directiveAttributeName.HasParameter && parameterOriginalSpan is SourceSpan ps)
{
var nameWithoutParamLen = directiveAttributeName.TextWithoutParameter.Length;
parameterOriginalSpan = new SourceSpan(ps.FilePath, ps.AbsoluteIndex, ps.LineIndex, ps.CharacterIndex, nameWithoutParamLen, ps.LineCount, ps.CharacterIndex + nameWithoutParamLen);
parameterOriginalSpan = ps.WithLength(nameWithoutParamLen)
.WithEndCharacterIndex(ps.CharacterIndex + nameWithoutParamLen);
}

IntermediateNode directiveNode = match.IsParameterMatch && directiveAttributeName.HasParameter
Expand Down Expand Up @@ -869,14 +872,9 @@ private static void ConvertComponentAttributeToTagHelper(
var directiveNameSpan = attributeNameSpan;
if (directiveNameSpan is SourceSpan nameSpan && attributeName.StartsWith('@'))
{
directiveNameSpan = new SourceSpan(
nameSpan.FilePath,
nameSpan.AbsoluteIndex + 1,
nameSpan.LineIndex,
nameSpan.CharacterIndex + 1,
nameSpan.Length - 1,
nameSpan.LineCount,
nameSpan.EndCharacterIndex);
directiveNameSpan = nameSpan.WithAbsoluteIndex(nameSpan.AbsoluteIndex + 1)
.WithCharacterIndex(nameSpan.CharacterIndex + 1)
.WithLength(nameSpan.Length - 1);
}

IntermediateNode directiveNode = match.IsParameterMatch && directiveAttributeName.HasParameter
Expand Down Expand Up @@ -983,7 +981,7 @@ private static void CopyAsTagHelperAttributeValues(HtmlAttributeIntermediateNode
var totalLength = source.Children[^1] is HtmlAttributeValueIntermediateNode lastValue && lastValue.Source is { } ls
? (ls.AbsoluteIndex + ls.Length) - fs.AbsoluteIndex
: mergedText.Length;
spanSource = new SourceSpan(fs.FilePath, fs.AbsoluteIndex, fs.LineIndex, fs.CharacterIndex, totalLength, fs.LineCount, fs.EndCharacterIndex);
spanSource = fs.WithLength(totalLength);
}

mergedContent.Source = spanSource;
Expand Down Expand Up @@ -1044,14 +1042,11 @@ private static void CopyAsTagHelperAttributeValues(HtmlAttributeIntermediateNode

var nameCharIndex = attrSource.CharacterIndex + nameIndex;

return new SourceSpan(
attrSource.FilePath,
attrSource.AbsoluteIndex + nameIndex,
attrSource.LineIndex,
nameCharIndex,
nameLength,
0,
nameCharIndex + nameLength);
return attrSource.WithAbsoluteIndex(attrSource.AbsoluteIndex + nameIndex)
.WithCharacterIndex(nameCharIndex)
.WithLength(nameLength)
.WithLineCount(0)
.WithEndCharacterIndex(nameCharIndex + nameLength);
}

private static SourceSpan? ComputeAttributeValueSpan(HtmlAttributeIntermediateNode htmlAttr)
Expand All @@ -1072,16 +1067,11 @@ private static void CopyAsTagHelperAttributeValues(HtmlAttributeIntermediateNode
var endIndex = lastSource.AbsoluteIndex + lastSource.Length;
var length = endIndex - childSource.AbsoluteIndex;
var endCharIndex = lastSource.CharacterIndex + lastSource.Length;
return new SourceSpan(
childSource.FilePath,
childSource.AbsoluteIndex,
childSource.LineIndex,
childSource.CharacterIndex,
length,
return childSource.WithLength(length)
// Note: does not incorporate lastSource.LineCount; attribute values
// spanning multiple lines are uncommon and the old pipeline had the same limitation.
lastSource.LineIndex - childSource.LineIndex,
endCharIndex);
.WithLineCount(lastSource.LineIndex - childSource.LineIndex)
.WithEndCharacterIndex(endCharIndex);
}

return childSource;
Expand All @@ -1105,14 +1095,11 @@ private static void CopyAsTagHelperAttributeValues(HtmlAttributeIntermediateNode

var valueCharIndex = attrSource.CharacterIndex + valueStart;

return new SourceSpan(
attrSource.FilePath,
attrSource.AbsoluteIndex + valueStart,
attrSource.LineIndex,
valueCharIndex,
valueLength,
0,
valueCharIndex + valueLength);
return attrSource.WithAbsoluteIndex(attrSource.AbsoluteIndex + valueStart)
.WithCharacterIndex(valueCharIndex)
.WithLength(valueLength)
.WithLineCount(0)
.WithEndCharacterIndex(valueCharIndex + valueLength);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -806,8 +806,10 @@ private static SourceSpan MergeSpans(SourceSpan a, SourceSpan b)
var first = a.AbsoluteIndex <= b.AbsoluteIndex ? a : b;
var last = a.AbsoluteIndex + a.Length >= b.AbsoluteIndex + b.Length ? a : b;
var lineCount = (last.LineIndex + last.LineCount) - first.LineIndex;
return new SourceSpan(first.FilePath, start, first.LineIndex, first.CharacterIndex,
end - start, lineCount, last.EndCharacterIndex);
return first.WithAbsoluteIndex(start)
.WithLength(end - start)
.WithLineCount(lineCount)
.WithEndCharacterIndex(last.EndCharacterIndex);
}

/// <summary>
Expand Down Expand Up @@ -918,9 +920,10 @@ private static void EmitEscapedAtCSharpExpression(
EmitExplicitExpressionTokens(expr, expressionSource.AbsoluteIndex, expressionSource.Length, sourceDocument);

var exprLoc = sourceDocument.Text.Lines.GetLinePosition(expressionSource.AbsoluteIndex);
expr.Source = new SourceSpan(
expressionSource.FilePath, expressionSource.AbsoluteIndex,
exprLoc.Line, exprLoc.Character, expressionSource.Length, 0, exprLoc.Character + expressionSource.Length);
expr.Source = expressionSource.WithLineIndex(exprLoc.Line)
.WithCharacterIndex(exprLoc.Character)
.WithLineCount(0)
.WithEndCharacterIndex(exprLoc.Character + expressionSource.Length);
target.Children.Add(expr);
}

Expand Down Expand Up @@ -1002,14 +1005,9 @@ private static bool AreAllChildrenOfType<T>(IntermediateNodeCollection children)
return null;
}

return new SourceSpan(
s.FilePath,
s.AbsoluteIndex - prefixLength,
s.LineIndex,
s.CharacterIndex - prefixLength,
s.Length + prefixLength,
s.LineCount,
s.EndCharacterIndex);
return s.WithAbsoluteIndex(s.AbsoluteIndex - prefixLength)
.WithCharacterIndex(s.CharacterIndex - prefixLength)
.WithLength(s.Length + prefixLength);
}

/// <summary>
Expand Down Expand Up @@ -1102,9 +1100,11 @@ private static void TryAddMalformedEndTagDiagnostic(
var diagSource = elementNode.Source;
if (elementNode.EndTagSpan is SourceSpan ets)
{
diagSource = new SourceSpan(
ets.FilePath, ets.AbsoluteIndex + 2, ets.LineIndex, ets.CharacterIndex + 2,
tagName.Length, 0, ets.CharacterIndex + 2 + tagName.Length);
diagSource = ets.WithAbsoluteIndex(ets.AbsoluteIndex + 2)
.WithCharacterIndex(ets.CharacterIndex + 2)
.WithLength(tagName.Length)
.WithLineCount(0)
.WithEndCharacterIndex(ets.CharacterIndex + 2 + tagName.Length);
}

if (diagSource is SourceSpan ds)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Microsoft.AspNetCore.Razor.Language;

public struct SourceSpan : IEquatable<SourceSpan>
public readonly struct SourceSpan : IEquatable<SourceSpan>
{
public static readonly SourceSpan Undefined = new SourceSpan(SourceLocation.Undefined, 0);

Expand Down Expand Up @@ -126,4 +126,12 @@ public readonly int CompareByStartThenLength(SourceSpan other)

return Length.CompareTo(other.Length);
}

public readonly SourceSpan WithFilePath(string filePath) => new(filePath, AbsoluteIndex, LineIndex, CharacterIndex, Length, LineCount, EndCharacterIndex);
public readonly SourceSpan WithAbsoluteIndex(int absoluteIndex) => new(FilePath, absoluteIndex, LineIndex, CharacterIndex, Length, LineCount, EndCharacterIndex);
public readonly SourceSpan WithLineIndex(int lineIndex) => new(FilePath, AbsoluteIndex, lineIndex, CharacterIndex, Length, LineCount, EndCharacterIndex);
public readonly SourceSpan WithCharacterIndex(int characterIndex) => new(FilePath, AbsoluteIndex, LineIndex, characterIndex, Length, LineCount, EndCharacterIndex);
public readonly SourceSpan WithLength(int length) => new(FilePath, AbsoluteIndex, LineIndex, CharacterIndex, length, LineCount, EndCharacterIndex);
public readonly SourceSpan WithLineCount(int lineCount) => new(FilePath, AbsoluteIndex, LineIndex, CharacterIndex, Length, lineCount, EndCharacterIndex);
public readonly SourceSpan WithEndCharacterIndex(int endCharacterIndex) => new(FilePath, AbsoluteIndex, LineIndex, CharacterIndex, Length, LineCount, endCharacterIndex);
}