Skip to content

Commit

Permalink
Merge pull request #1352 from sharwell/cache-properties
Browse files Browse the repository at this point in the history
Cache properties reported with diagnostics for OpenCloseSpacingCodeFixProvider
  • Loading branch information
sharwell committed Sep 1, 2015
2 parents 70cf167 + a943342 commit 5ff6557
Show file tree
Hide file tree
Showing 14 changed files with 96 additions and 193 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -321,13 +321,8 @@ private static void CheckIfLocationOfPreviousTokenAndOpenTokenAreTheSame(SyntaxN
openParenLine.IsValid &&
openParenLine.StartLinePosition.Line != prevTokenLine.StartLinePosition.Line)
{
var properties = new Dictionary<string, string>
{
[OpenCloseSpacingCodeFixProvider.LocationKey] = OpenCloseSpacingCodeFixProvider.LocationPreceding,
[OpenCloseSpacingCodeFixProvider.ActionKey] = OpenCloseSpacingCodeFixProvider.ActionRemove,
[OpenCloseSpacingCodeFixProvider.LayoutKey] = preserveLayout ? OpenCloseSpacingCodeFixProvider.LayoutPreserve : OpenCloseSpacingCodeFixProvider.LayoutPack
};
context.ReportDiagnostic(Diagnostic.Create(Descriptor, openToken.GetLocation(), properties.ToImmutableDictionary()));
var properties = preserveLayout ? OpenCloseSpacingCodeFixProvider.RemovePrecedingPreserveLayout : OpenCloseSpacingCodeFixProvider.RemovePreceding;
context.ReportDiagnostic(Diagnostic.Create(Descriptor, openToken.GetLocation(), properties));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,13 +347,8 @@ private static void CheckIfLocationOfLastArgumentOrParameterAndCloseTokenAreTheS
closeParenLine.IsValid &&
closeParenLine.StartLinePosition.Line != lastParameterLine.EndLinePosition.Line)
{
var properties = new Dictionary<string, string>
{
[OpenCloseSpacingCodeFixProvider.LocationKey] = OpenCloseSpacingCodeFixProvider.LocationPreceding,
[OpenCloseSpacingCodeFixProvider.ActionKey] = OpenCloseSpacingCodeFixProvider.ActionRemove,
[OpenCloseSpacingCodeFixProvider.LayoutKey] = OpenCloseSpacingCodeFixProvider.LayoutPack
};
context.ReportDiagnostic(Diagnostic.Create(Descriptor, closeToken.GetLocation(), properties.ToImmutableDictionary()));
var properties = OpenCloseSpacingCodeFixProvider.RemovePreceding;
context.ReportDiagnostic(Diagnostic.Create(Descriptor, closeToken.GetLocation(), properties));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,8 @@ private static void CheckIfLocationOfOpenAndCloseTokensAreTheSame(
openParenLine.IsValid &&
openParenLine.StartLinePosition.Line != closeParenLine.StartLinePosition.Line)
{
var properties = new Dictionary<string, string>
{
[OpenCloseSpacingCodeFixProvider.LocationKey] = OpenCloseSpacingCodeFixProvider.LocationPreceding,
[OpenCloseSpacingCodeFixProvider.ActionKey] = OpenCloseSpacingCodeFixProvider.ActionRemove,
[OpenCloseSpacingCodeFixProvider.LayoutKey] = OpenCloseSpacingCodeFixProvider.LayoutPack
};
context.ReportDiagnostic(Diagnostic.Create(Descriptor, closeToken.GetLocation(), properties.ToImmutableDictionary()));
var properties = OpenCloseSpacingCodeFixProvider.RemovePreceding;
context.ReportDiagnostic(Diagnostic.Create(Descriptor, closeToken.GetLocation(), properties));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,13 +247,8 @@ private static void CheckIfCommasAreAtTheSameLineAsThePreviousParameter(SyntaxNo

if (previousNode.GetEndLine() < nodeOrToken.GetLineSpan().StartLinePosition.Line)
{
var properties = new Dictionary<string, string>
{
[OpenCloseSpacingCodeFixProvider.LocationKey] = OpenCloseSpacingCodeFixProvider.LocationPreceding,
[OpenCloseSpacingCodeFixProvider.ActionKey] = OpenCloseSpacingCodeFixProvider.ActionRemove,
[OpenCloseSpacingCodeFixProvider.LayoutKey] = OpenCloseSpacingCodeFixProvider.LayoutPreserve
};
context.ReportDiagnostic(Diagnostic.Create(Descriptor, nodeOrToken.GetLocation(), properties.ToImmutableDictionary()));
var properties = OpenCloseSpacingCodeFixProvider.RemovePrecedingPreserveLayout;
context.ReportDiagnostic(Diagnostic.Create(Descriptor, nodeOrToken.GetLocation(), properties));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@
[Shared]
public class OpenCloseSpacingCodeFixProvider : CodeFixProvider
{
internal const string LocationKey = "location";
internal const string ActionKey = "action";
internal const string LayoutKey = "layout";
internal const string LocationPreceding = "preceding";
internal const string LocationFollowing = "following";
internal const string ActionInsert = "insert";
internal const string ActionRemove = "remove";
internal const string LayoutPack = "pack";
internal const string LayoutPreserve = "preserve";
private const string LocationKey = "location";
private const string ActionKey = "action";
private const string LayoutKey = "layout";
private const string LocationPreceding = "preceding";
private const string LocationFollowing = "following";
private const string ActionInsert = "insert";
private const string ActionRemove = "remove";
private const string LayoutPack = "pack";
private const string LayoutPreserve = "preserve";

private static readonly ImmutableArray<string> FixableDiagnostics =
ImmutableArray.Create(
Expand All @@ -49,6 +49,33 @@ public class OpenCloseSpacingCodeFixProvider : CodeFixProvider
/// <inheritdoc/>
public override ImmutableArray<string> FixableDiagnosticIds => FixableDiagnostics;

internal static ImmutableDictionary<string, string> InsertPreceding { get; } =
ImmutableDictionary<string, string>.Empty
.SetItem(LocationKey, LocationPreceding)
.SetItem(ActionKey, ActionInsert);

internal static ImmutableDictionary<string, string> RemovePreceding { get; } =
ImmutableDictionary<string, string>.Empty
.SetItem(LocationKey, LocationPreceding)
.SetItem(ActionKey, ActionRemove)
.SetItem(LayoutKey, LayoutPack);

internal static ImmutableDictionary<string, string> RemovePrecedingPreserveLayout { get; } =
ImmutableDictionary<string, string>.Empty
.SetItem(LocationKey, LocationPreceding)
.SetItem(ActionKey, ActionRemove)
.SetItem(LayoutKey, LayoutPreserve);

internal static ImmutableDictionary<string, string> InsertFollowing { get; } =
ImmutableDictionary<string, string>.Empty
.SetItem(LocationKey, LocationFollowing)
.SetItem(ActionKey, ActionInsert);

internal static ImmutableDictionary<string, string> RemoveFollowing { get; } =
ImmutableDictionary<string, string>.Empty
.SetItem(LocationKey, LocationFollowing)
.SetItem(ActionKey, ActionRemove);

/// <inheritdoc/>
public override FixAllProvider GetFixAllProvider()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,35 +185,23 @@ private static void HandleCloseParenToken(SyntaxTreeAnalysisContext context, Syn
if (precededBySpace)
{
// Closing parenthesis must{ not} be {preceded} by a space.
var properties = new Dictionary<string, string>
{
[OpenCloseSpacingCodeFixProvider.LocationKey] = OpenCloseSpacingCodeFixProvider.LocationPreceding,
[OpenCloseSpacingCodeFixProvider.ActionKey] = OpenCloseSpacingCodeFixProvider.ActionRemove
};
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), properties.ToImmutableDictionary(), " not", "preceded"));
var properties = OpenCloseSpacingCodeFixProvider.RemovePreceding;
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), properties, " not", "preceded"));
}

if (!suppressFollowingSpaceError)
{
if (!precedesStickyCharacter && !followedBySpace && !lastInLine)
{
// Closing parenthesis must{} be {followed} by a space.
var properties = new Dictionary<string, string>
{
[OpenCloseSpacingCodeFixProvider.LocationKey] = OpenCloseSpacingCodeFixProvider.LocationFollowing,
[OpenCloseSpacingCodeFixProvider.ActionKey] = OpenCloseSpacingCodeFixProvider.ActionInsert
};
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), properties.ToImmutableDictionary(), string.Empty, "followed"));
var properties = OpenCloseSpacingCodeFixProvider.InsertFollowing;
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), properties, string.Empty, "followed"));
}
else if (precedesStickyCharacter && followedBySpace && (!lastInLine || !allowEndOfLine))
{
// Closing parenthesis must{ not} be {followed} by a space.
var properties = new Dictionary<string, string>
{
[OpenCloseSpacingCodeFixProvider.LocationKey] = OpenCloseSpacingCodeFixProvider.LocationFollowing,
[OpenCloseSpacingCodeFixProvider.ActionKey] = OpenCloseSpacingCodeFixProvider.ActionRemove
};
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), properties.ToImmutableDictionary(), " not", "followed"));
var properties = OpenCloseSpacingCodeFixProvider.RemoveFollowing;
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), properties, " not", "followed"));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,35 +140,23 @@ private static void HandleCloseBracketToken(SyntaxTreeAnalysisContext context, S
if (!firstInLine && precededBySpace)
{
// Closing square bracket must{ not} be {preceded} by a space.
var properties = new Dictionary<string, string>
{
[OpenCloseSpacingCodeFixProvider.LocationKey] = OpenCloseSpacingCodeFixProvider.LocationPreceding,
[OpenCloseSpacingCodeFixProvider.ActionKey] = OpenCloseSpacingCodeFixProvider.ActionRemove
};
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), properties.ToImmutableDictionary(), " not", "preceded"));
var properties = OpenCloseSpacingCodeFixProvider.RemovePreceding;
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), properties, " not", "preceded"));
}

if (!lastInLine)
{
if (!precedesSpecialCharacter && !followedBySpace)
{
// Closing square bracket must{} be {followed} by a space.
var properties = new Dictionary<string, string>
{
[OpenCloseSpacingCodeFixProvider.LocationKey] = OpenCloseSpacingCodeFixProvider.LocationFollowing,
[OpenCloseSpacingCodeFixProvider.ActionKey] = OpenCloseSpacingCodeFixProvider.ActionInsert
};
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), properties.ToImmutableDictionary(), string.Empty, "followed"));
var properties = OpenCloseSpacingCodeFixProvider.InsertFollowing;
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), properties, string.Empty, "followed"));
}
else if (precedesSpecialCharacter && followedBySpace && !suppressFollowingSpaceError)
{
// Closing square brackets must {not} be {followed} by a space
var properties = new Dictionary<string, string>
{
[OpenCloseSpacingCodeFixProvider.LocationKey] = OpenCloseSpacingCodeFixProvider.LocationFollowing,
[OpenCloseSpacingCodeFixProvider.ActionKey] = OpenCloseSpacingCodeFixProvider.ActionRemove
};
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), properties.ToImmutableDictionary(), " not", "followed"));
var properties = OpenCloseSpacingCodeFixProvider.RemoveFollowing;
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), properties, " not", "followed"));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,8 @@ private static void HandleOpenBraceToken(SyntaxTreeAnalysisContext context, Synt
if (followedBySpace)
{
// Opening curly bracket must{} be {followed} by a space.
var properties = new Dictionary<string, string>
{
[OpenCloseSpacingCodeFixProvider.LocationKey] = OpenCloseSpacingCodeFixProvider.LocationFollowing,
[OpenCloseSpacingCodeFixProvider.ActionKey] = OpenCloseSpacingCodeFixProvider.ActionRemove
};
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), properties.ToImmutableDictionary(), " not", "followed"));
var properties = OpenCloseSpacingCodeFixProvider.RemoveFollowing;
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), properties, " not", "followed"));
}

return;
Expand All @@ -102,23 +98,15 @@ private static void HandleOpenBraceToken(SyntaxTreeAnalysisContext context, Synt
if (!precededBySpace)
{
// Opening curly bracket must{} be {preceded} by a space.
var properties = new Dictionary<string, string>
{
[OpenCloseSpacingCodeFixProvider.LocationKey] = OpenCloseSpacingCodeFixProvider.LocationPreceding,
[OpenCloseSpacingCodeFixProvider.ActionKey] = OpenCloseSpacingCodeFixProvider.ActionInsert
};
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), properties.ToImmutableDictionary(), string.Empty, "preceded"));
var properties = OpenCloseSpacingCodeFixProvider.InsertPreceding;
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), properties, string.Empty, "preceded"));
}

if (!token.IsLastInLine() && !followedBySpace)
{
// Opening curly bracket must{} be {followed} by a space.
var properties = new Dictionary<string, string>
{
[OpenCloseSpacingCodeFixProvider.LocationKey] = OpenCloseSpacingCodeFixProvider.LocationFollowing,
[OpenCloseSpacingCodeFixProvider.ActionKey] = OpenCloseSpacingCodeFixProvider.ActionInsert
};
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), properties.ToImmutableDictionary(), string.Empty, "followed"));
var properties = OpenCloseSpacingCodeFixProvider.InsertFollowing;
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), properties, string.Empty, "followed"));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,8 @@ private static void HandleCloseBraceToken(SyntaxTreeAnalysisContext context, Syn
if (precededBySpace)
{
// Closing curly bracket must{ not} be {preceded} by a space.
var properties = new Dictionary<string, string>
{
[OpenCloseSpacingCodeFixProvider.LocationKey] = OpenCloseSpacingCodeFixProvider.LocationPreceding,
[OpenCloseSpacingCodeFixProvider.ActionKey] = OpenCloseSpacingCodeFixProvider.ActionRemove
};
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), properties.ToImmutableDictionary(), " not", "preceded"));
var properties = OpenCloseSpacingCodeFixProvider.RemovePreceding;
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), properties, " not", "preceded"));
}

return;
Expand Down Expand Up @@ -120,23 +116,15 @@ private static void HandleCloseBraceToken(SyntaxTreeAnalysisContext context, Syn
if (!precededBySpace)
{
// Closing curly bracket must{} be {preceded} by a space.
var properties = new Dictionary<string, string>
{
[OpenCloseSpacingCodeFixProvider.LocationKey] = OpenCloseSpacingCodeFixProvider.LocationPreceding,
[OpenCloseSpacingCodeFixProvider.ActionKey] = OpenCloseSpacingCodeFixProvider.ActionInsert
};
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), properties.ToImmutableDictionary(), string.Empty, "preceded"));
var properties = OpenCloseSpacingCodeFixProvider.InsertPreceding;
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), properties, string.Empty, "preceded"));
}

if (!lastInLine && !precedesSpecialCharacter && !followedBySpace)
{
// Closing curly bracket must{} be {followed} by a space.
var properties = new Dictionary<string, string>
{
[OpenCloseSpacingCodeFixProvider.LocationKey] = OpenCloseSpacingCodeFixProvider.LocationFollowing,
[OpenCloseSpacingCodeFixProvider.ActionKey] = OpenCloseSpacingCodeFixProvider.ActionInsert
};
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), properties.ToImmutableDictionary(), string.Empty, "followed"));
var properties = OpenCloseSpacingCodeFixProvider.InsertFollowing;
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), properties, string.Empty, "followed"));
}
}
}
Expand Down
Loading

0 comments on commit 5ff6557

Please sign in to comment.