Skip to content

Commit

Permalink
Updating editor features to adopt new syntax model
Browse files Browse the repository at this point in the history
  • Loading branch information
jcouv committed Nov 4, 2016
1 parent d0f94c3 commit ecce74d
Show file tree
Hide file tree
Showing 19 changed files with 99 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,8 @@ public async Task DiagnosticAnalyzerDriverAllInOne()
var syntaxKindsMissing = new HashSet<SyntaxKind>();

// AllInOneCSharpCode has no deconstruction or declaration expression
syntaxKindsMissing.Add(SyntaxKind.TypedVariableComponent);
syntaxKindsMissing.Add(SyntaxKind.ParenthesizedVariableComponent);
syntaxKindsMissing.Add(SyntaxKind.SingleVariableDesignation);
syntaxKindsMissing.Add(SyntaxKind.ParenthesizedVariableDesignation);
syntaxKindsMissing.Add(SyntaxKind.DeconstructionDeclarationStatement);
syntaxKindsMissing.Add(SyntaxKind.VariableComponentAssignment);
syntaxKindsMissing.Add(SyntaxKind.ForEachVariableStatement);
syntaxKindsMissing.Add(SyntaxKind.DeclarationExpression);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8155,7 +8155,7 @@ static void F(object x)
var active = GetActiveStatements(src1, src2);

edits.VerifyRudeDiagnostics(active,
Diagnostic(RudeEditKind.UpdateAroundActiveStatement, "var (x, y) = x;", CSharpFeaturesResources.deconstruction));
Diagnostic(RudeEditKind.UpdateAroundActiveStatement, "var (x, y) = x", CSharpFeaturesResources.deconstruction));
}

[Fact]
Expand Down Expand Up @@ -8424,7 +8424,7 @@ static void F(object o1, object o2)
var active = GetActiveStatements(src1, src2);

edits.VerifyRudeDiagnostics(active,
Diagnostic(RudeEditKind.UpdateAroundActiveStatement, "var (x, y) = o2;", CSharpFeaturesResources.deconstruction));
Diagnostic(RudeEditKind.UpdateAroundActiveStatement, "var (x, y) = o2", CSharpFeaturesResources.deconstruction));
}

[Fact]
Expand Down Expand Up @@ -8456,7 +8456,7 @@ static void F(object o1, object o2)
var active = GetActiveStatements(src1, src2);

edits.VerifyRudeDiagnostics(active,
Diagnostic(RudeEditKind.UpdateAroundActiveStatement, "(x, y)", CSharpFeaturesResources.tuple));
Diagnostic(RudeEditKind.UpdateAroundActiveStatement, "(x, y) = o2", CSharpFeaturesResources.deconstruction));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ internal CompilationContext(
MethodDebugInfo<TypeSymbol, LocalSymbol> methodDebugInfo,
CSharpSyntaxNode syntax)
{
Debug.Assert((syntax == null) || (syntax is ExpressionSyntax) || (syntax is LocalDeclarationStatementSyntax) || (syntax is DeconstructionDeclarationStatementSyntax));
Debug.Assert((syntax == null) || (syntax is ExpressionSyntax) || (syntax is LocalDeclarationStatementSyntax));

// TODO: syntax.SyntaxTree should probably be added to the compilation,
// but it isn't rooted by a CompilationUnitSyntax so it doesn't work (yet).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,7 @@ private static CSharpSyntaxNode Parse(
{
formatSpecifiers = null;

if (statementSyntax.IsKind(SyntaxKind.LocalDeclarationStatement) ||
statementSyntax.IsKind(SyntaxKind.DeconstructionDeclarationStatement))
if (statementSyntax.IsKind(SyntaxKind.LocalDeclarationStatement))
{
return statementSyntax;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ private bool IsValidContextInForEachClause(CSharpSyntaxContext context)
}
else if (token.Kind() == SyntaxKind.CloseParenToken)
{
var statement = token.GetAncestor<ForEachComponentStatementSyntax>();
if (statement != null && token.Span.End == statement.VariableComponent.Span.End)
var statement = token.GetAncestor<ForEachVariableStatementSyntax>();
if (statement != null && token.Span.End == statement.Variable.Span.End)
{
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ private static TextSpan CreateSpanForBlock(BlockSyntax block, int position)
}
else
{
return ((ForEachComponentStatementSyntax)statement).VariableComponent.Span;
return ((ForEachVariableStatementSyntax)statement).Variable.Span;
}
}
else if (position < forEachStatement.Expression.FullSpan.Start)
Expand Down Expand Up @@ -554,7 +554,6 @@ private static TextSpan CreateSpanForBlock(BlockSyntax block, int position)
case SyntaxKind.ThrowStatement:
case SyntaxKind.ExpressionStatement:
case SyntaxKind.EmptyStatement:
case SyntaxKind.DeconstructionDeclarationStatement:
default:
// Fallback case. If it was none of the above types of statements, then we make a span
// over the entire statement. Note: this is not a very desirable thing to do (as
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1346,19 +1346,16 @@ internal static TextSpan GetDiagnosticSpanImpl(SyntaxKind kind, SyntaxNode node,
return ((GroupClauseSyntax)node).GroupKeyword.Span;

case SyntaxKind.ForEachVariableStatement:
return ((ForEachComponentStatementSyntax)node).VariableComponent.Span;
return ((ForEachVariableStatementSyntax)node).Variable.Span;

case SyntaxKind.IsPatternExpression:
case SyntaxKind.DeconstructionDeclarationStatement:
case SyntaxKind.ParenthesizedVariableComponent:
case SyntaxKind.TypedVariableComponent:
case SyntaxKind.TupleType:
case SyntaxKind.TupleExpression:
case SyntaxKind.DeclarationExpression:
case SyntaxKind.RefType:
case SyntaxKind.RefExpression:
case SyntaxKind.DeclarationPattern:
case SyntaxKind.VariableComponentAssignment:
case SyntaxKind.SimpleAssignmentExpression:
return node.Span;

default:
Expand Down Expand Up @@ -1607,13 +1604,19 @@ internal static string GetStatementDisplayNameImpl(SyntaxNode node)
case SyntaxKind.IsPatternExpression:
return CSharpFeaturesResources.is_pattern;

case SyntaxKind.DeconstructionDeclarationStatement:
case SyntaxKind.ForEachVariableStatement:
case SyntaxKind.ParenthesizedVariableComponent:
case SyntaxKind.TypedVariableComponent:
case SyntaxKind.VariableComponentAssignment:
return CSharpFeaturesResources.deconstruction;

case SyntaxKind.SimpleAssignmentExpression:
if (IsDeconstruction((AssignmentExpressionSyntax)node))
{
return CSharpFeaturesResources.deconstruction;
}
else
{
goto default;
}

case SyntaxKind.TupleType:
case SyntaxKind.TupleExpression:
return CSharpFeaturesResources.tuple;
Expand Down Expand Up @@ -3106,11 +3109,7 @@ private static bool IsUnsupportedCSharp7EnCNode(SyntaxNode n)
switch (n.Kind())
{
case SyntaxKind.IsPatternExpression:
case SyntaxKind.DeconstructionDeclarationStatement:
case SyntaxKind.VariableComponentAssignment:
case SyntaxKind.ForEachVariableStatement:
case SyntaxKind.ParenthesizedVariableComponent:
case SyntaxKind.TypedVariableComponent:
case SyntaxKind.TupleType:
case SyntaxKind.TupleExpression:
case SyntaxKind.LocalFunctionStatement:
Expand All @@ -3119,12 +3118,19 @@ private static bool IsUnsupportedCSharp7EnCNode(SyntaxNode n)
case SyntaxKind.RefExpression:
case SyntaxKind.DeclarationPattern:
return true;

case SyntaxKind.SimpleAssignmentExpression:
return IsDeconstruction((AssignmentExpressionSyntax)n);
default:
return false;
}
}

private static bool IsDeconstruction(AssignmentExpressionSyntax assignment)
{
return assignment.Left.Kind() == SyntaxKind.TupleExpression ||
assignment.Left.Kind() == SyntaxKind.DeclarationExpression;
}

private void ReportRudeEditsForCheckedStatements(
List<RudeEditDiagnostic> diagnostics,
SyntaxNode oldActiveStatement,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,11 @@ public override SyntaxNode VisitForEachStatement(ForEachStatementSyntax node)
.WithStatement(ReplaceStatementIfNeeded(node.Statement));
}

public override SyntaxNode VisitForEachComponentStatement(ForEachComponentStatementSyntax node)
public override SyntaxNode VisitForEachVariableStatement(ForEachVariableStatementSyntax node)
{
if (node != this.ContainerOfStatementsOrFieldToReplace)
{
return base.VisitForEachComponentStatement(node);
return base.VisitForEachVariableStatement(node);
}

return node.WithExpression(VisitNode(node.Expression))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,25 +420,20 @@ private StatementSyntax FixDeclarationExpressionsAndDeclarationPatterns(Statemen
switch (node.Kind())
{
case SyntaxKind.DeclarationExpression:
var declaration = (DeclarationExpressionSyntax)node;
if (declaration.VariableComponent.Kind() != SyntaxKind.TypedVariableComponent)
{
break;
}

var variableComponent = (TypedVariableComponentSyntax)declaration.VariableComponent;
if (variableComponent.Designation.Kind() != SyntaxKind.SingleVariableDesignation)
var declaration = (DeclarationExpressionSyntax)node;
if (declaration.Designation.Kind() != SyntaxKind.SingleVariableDesignation)
{
break;
}

var designation = (SingleVariableDesignationSyntax)variableComponent.Designation;
var designation = (SingleVariableDesignationSyntax)declaration.Designation;
var name = designation.Identifier.ValueText;
if (variablesToRemove.HasSyntaxAnnotation(designation))
{
var newLeadingTrivia = new SyntaxTriviaList();
newLeadingTrivia = newLeadingTrivia.AddRange(variableComponent.Type.GetLeadingTrivia());
newLeadingTrivia = newLeadingTrivia.AddRange(variableComponent.Type.GetTrailingTrivia());
newLeadingTrivia = newLeadingTrivia.AddRange(declaration.Type.GetLeadingTrivia());
newLeadingTrivia = newLeadingTrivia.AddRange(declaration.Type.GetTrailingTrivia());
newLeadingTrivia = newLeadingTrivia.AddRange(designation.GetLeadingTrivia());

replacements.Add(declaration, SyntaxFactory.IdentifierName(designation.Identifier)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,7 @@ private static DeclarationExpressionSyntax GetDeclarationExpression(
}
}

return SyntaxFactory.DeclarationExpression(
SyntaxFactory.TypedVariableComponent(newType, designation));
return SyntaxFactory.DeclarationExpression(newType, designation);
}

private static IEnumerable<SyntaxTrivia> MassageTrivia(IEnumerable<SyntaxTrivia> triviaList)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ public static ITypeSymbol DetermineParameterType(
if (argument.Expression.Kind() == SyntaxKind.DeclarationExpression)
{
var decl = (DeclarationExpressionSyntax)argument.Expression;
var component = decl.VariableComponent as TypedVariableComponentSyntax;
if (component == null) return semanticModel.Compilation.ObjectType;
typeInfo = semanticModel.GetTypeInfo(component.Type);
typeInfo = semanticModel.GetTypeInfo(decl.Type);
return typeInfo.Type?.IsErrorType() == false ? typeInfo.Type : semanticModel.Compilation.ObjectType;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ public override void VisitForEachStatement(ForEachStatementSyntax node)
AddExpressionTerms(node.Expression, _expressions);
}

public override void VisitForEachComponentStatement(ForEachComponentStatementSyntax node)
public override void VisitForEachVariableStatement(ForEachVariableStatementSyntax node)
{
AddVariableExpressions(node.VariableComponent, _expressions);
AddVariableExpressions(node.Variable, _expressions);
AddExpressionTerms(node.Expression, _expressions);
}

Expand Down Expand Up @@ -125,22 +125,23 @@ private void AddVariableExpressions(
}

private void AddVariableExpressions(
VariableComponentSyntax component,
ExpressionSyntax component,
IList<string> expressions)
{
if (!_includeDeclarations) return;

switch (component.Kind())
{
case SyntaxKind.ParenthesizedVariableComponent:
case SyntaxKind.TupleExpression:
{
var t = (ParenthesizedVariableComponentSyntax)component;
foreach (var v in t.Variables) AddVariableExpressions(component, expressions);
var t = (TupleExpressionSyntax)component;
foreach (ArgumentSyntax a in t.Arguments) AddVariableExpressions(a.Expression, expressions);
// TODO REVIEW I think there was a bug there. Are we missing tests?
break;
}
case SyntaxKind.TypedVariableComponent:
case SyntaxKind.DeclarationExpression:
{
var t = (TypedVariableComponentSyntax)component;
var t = (DeclarationExpressionSyntax)component;
AddVariableExpressions(t.Designation, expressions);
break;
}
Expand All @@ -158,7 +159,7 @@ private void AddVariableExpressions(
case SyntaxKind.ParenthesizedVariableDesignation:
{
var t = (ParenthesizedVariableDesignationSyntax)component;
foreach (var v in t.Variables) AddVariableExpressions(component, expressions);
foreach (VariableDesignationSyntax v in t.Variables) AddVariableExpressions(v, expressions);
break;
}
case SyntaxKind.SingleVariableDesignation:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ private bool IsInVarContext(NameSyntax name)
return
name.CheckParent<VariableDeclarationSyntax>(v => v.Type == name) ||
name.CheckParent<ForEachStatementSyntax>(f => f.Type == name) ||
name.CheckParent<TypedVariableComponentSyntax>(f => f.Type == name);
name.CheckParent<DeclarationExpressionSyntax>(f => f.Type == name);
}

private static ISymbol TryGetSymbol(NameSyntax name, SymbolInfo symbolInfo, SemanticModel semanticModel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2161,12 +2161,13 @@ private static bool IsReplaceableByVar(
return true;
}

if (simpleName.IsParentKind(SyntaxKind.TypedVariableComponent))
{
replacementNode = candidateReplacementNode;
issueSpan = candidateIssueSpan;
return true;
}
//if (simpleName.IsParentKind(SyntaxKind.TypedVariableComponent))
//{
// replacementNode = candidateReplacementNode;
// issueSpan = candidateIssueSpan;
// return true;
//}
// TODO REVIEW Are we lacking tests? It seems this code should be needed for DeclarationExpression

return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public static bool IsTypeInferred(this CommonForEachStatementSyntax forEachState
case SyntaxKind.ForEachStatement:
return ((ForEachStatementSyntax)forEachStatement).Type.IsTypeInferred(semanticModel);
case SyntaxKind.ForEachVariableStatement:
return (((ForEachComponentStatementSyntax)forEachStatement).VariableComponent as TypedVariableComponentSyntax)?.Type.IsTypeInferred(semanticModel) == true;
return (((ForEachVariableStatementSyntax)forEachStatement).Variable as DeclarationExpressionSyntax)?.Type
.IsTypeInferred(semanticModel) == true;
default:
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public static bool CanRemoveParentheses(this ParenthesizedExpressionSyntax node)
// using ((x)) -> using (x)
// catch when ((x)) -> catch when (x)
if ((node.IsParentKind(SyntaxKind.EqualsValueClause) && ((EqualsValueClauseSyntax)node.Parent).Value == node) ||
(node.IsParentKind(SyntaxKind.VariableComponentAssignment) && ((VariableComponentAssignmentSyntax)node.Parent).Value == node) ||
(node.IsParentKind(SyntaxKind.IfStatement) && ((IfStatementSyntax)node.Parent).Condition == node) ||
(node.IsParentKind(SyntaxKind.ReturnStatement) && ((ReturnStatementSyntax)node.Parent).Expression == node) ||
(node.IsParentKind(SyntaxKind.YieldReturnStatement) && ((YieldStatementSyntax)node.Parent).Expression == node) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,7 @@ public static string GenerateNameForExpression(
else if (current is DeclarationExpressionSyntax)
{
var decl = (DeclarationExpressionSyntax)current;
var component = decl.VariableComponent as TypedVariableComponentSyntax;
var name = component?.Designation as SingleVariableDesignationSyntax;
var name = decl.Designation as SingleVariableDesignationSyntax;
if (name == null) break;
return name.Identifier.ValueText.ToCamelCase();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ public static bool IsOpenParenInVarDeconstructionDeclaration(this SyntaxToken cu
{
return currentToken.Kind() == SyntaxKind.OpenParenToken &&
currentToken.Parent is ParenthesizedVariableDesignationSyntax &&
currentToken.Parent.Parent is TypedVariableComponentSyntax;
currentToken.Parent.Parent is DeclarationExpressionSyntax;
}
}
}
Loading

0 comments on commit ecce74d

Please sign in to comment.