private async Task <Document> MakeNameOfAsync(Document document, LiteralExpressionSyntax stringLiteral, CancellationToken cancelationToken) { var newNameof = SyntaxFactory.ParseExpression($"nameof({stringLiteral.Token.ValueText})") .WithLeadingTrivia(stringLiteral.GetLeadingTrivia()) .WithTrailingTrivia(stringLiteral.GetTrailingTrivia()) .WithAdditionalAnnotations(Formatter.Annotation); var root = await document.GetSyntaxRootAsync(cancelationToken); var newRoot = root.ReplaceNode(stringLiteral, newNameof); return(document.WithSyntaxRoot(newRoot)); }
public override SyntaxNode VisitLiteralExpression(LiteralExpressionSyntax node) { if (node.CSharpKind() != SyntaxKind.StringLiteralExpression) return node; var pos = node.GetLocation().GetMappedLineSpan(); var result = OnRewrite(pos.StartLinePosition.Line + 1, pos.StartLinePosition.Character + 1, Source.Lines[pos.StartLinePosition.Line].ToString(), node.Token.ValueText); if (result == null) return node; var names = result.Split('.'); ExpressionSyntax newNode = SyntaxFactory.IdentifierName(names.First()); foreach (var name in names.Skip(1)) { newNode = SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, newNode, SyntaxFactory.IdentifierName(name)); } return newNode.WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia()); }
public override SyntaxNode VisitLiteralExpression(LiteralExpressionSyntax node) { var kind = node.Kind(); if ((kind != SyntaxKind.NumericLiteralExpression) && (kind != SyntaxKind.StringLiteralExpression) && (kind != SyntaxKind.FalseLiteralExpression) && (kind != SyntaxKind.TrueLiteralExpression) && (kind != SyntaxKind.CharacterLiteralExpression) ) { return(node); } CheckCastContex(node); string value = node.ToString(); bool found = false; VirtualData constant = null; string requiredType = GetRequiredType(node); //in the case of return statements if (requiredType.Equals("void")) { requiredType = ""; // return statement was added as a refactoring "hack" } var typeInfo = _virtualizationContext.semanticModel.GetTypeInfo(node); var declaredType = typeInfo.Type.ToString(); foreach (var data in _virtualizationContext.data) { if (value.Equals(data.Name)) { if (requiredType.Equals("") && declaredType.Equals(data.Type)) { found = true; constant = data; requiredType = declaredType; break; } if (requiredType.Equals(data.Type)) { found = true; constant = data; break; } } } if (!found) { if (requiredType.Equals("")) { requiredType = declaredType; } int index = _virtualizationContext.DataIndex; string name = value; SyntaxAnnotation dataIndexMarker = new SyntaxAnnotation("index", index + ""); SyntaxAnnotation nameMarker = new SyntaxAnnotation("name", name); SyntaxAnnotation constantMarker = new SyntaxAnnotation("type", "constant"); SyntaxAnnotation codeIndexMarker = new SyntaxAnnotation("code", "undefined"); SyntaxAnnotation uniqueMarker = new SyntaxAnnotation("unique", "" + VirtualizationContext.UniqueId); constant = new VirtualData(); constant.Index = index; constant.Name = name; var info = requiredType; constant.Type = info; constant.Node = node; constant.DefaultValue = node; constant.Annotations.Add(dataIndexMarker); constant.Annotations.Add(nameMarker); constant.Annotations.Add(constantMarker); constant.Annotations.Add(codeIndexMarker); constant.Annotations.Add(uniqueMarker); _virtualizationContext.data.Add(constant); } ExpressionSyntax newNode = SyntaxFactoryExtensions.DataCodeVirtualAccess(); newNode = newNode.WithAdditionalAnnotations(constant.Annotations); ExpressionSyntax newExpression; if (CastEnabled) { newExpression = SyntaxFactory.CastExpression(SyntaxFactory.IdentifierName ( @"" + constant.Type ), newNode ); } else { newExpression = newNode; } //TODO: add annotations + comments newExpression = newExpression.WithLeadingTrivia(node.GetLeadingTrivia()) .WithTrailingTrivia(node.GetTrailingTrivia()) ; return(newExpression); }
private async Task <Solution> FixLiteralAsync(Document document, LiteralExpressionSyntax literal, CancellationToken cancellationToken) { if (document.Name == "F64.cs") { return(null); } var semanticModel = await document.GetSemanticModelAsync(cancellationToken); ISymbol symbol = semanticModel.GetSymbolInfo(literal).Symbol; var value = semanticModel.GetConstantValue(literal); string staticName = GetStaticNameFromValue(value); if (staticName == null) { return(null); } string staticDefinition = GetStaticDefinitionFromValue(value); var newLiteral = SyntaxFactory.ParseExpression("F64." + staticName) .WithLeadingTrivia(literal.GetLeadingTrivia()) .WithTrailingTrivia(literal.GetTrailingTrivia()) .WithAdditionalAnnotations(Formatter.Annotation); Solution solution = document.Project.Solution; var documentEditor = await DocumentEditor.CreateAsync(document); documentEditor.ReplaceNode(literal, newLiteral); var syntaxRoot = await document.GetSyntaxRootAsync(); bool hasUsing = syntaxRoot.DescendantNodes().OfType <UsingDirectiveSyntax>().Any(d => d.Name.ToString() == "BEPUutilities") || syntaxRoot.DescendantNodes().OfType <NamespaceDeclarationSyntax>().Any(d => d.Name.ToString() == "BEPUutilities"); if (!hasUsing) { var usingF64 = SyntaxFactory.UsingDirective(SyntaxFactory.IdentifierName("BEPUutilities")); documentEditor.InsertAfter(syntaxRoot.DescendantNodes().OfType <UsingDirectiveSyntax>().Last(), usingF64); } var newSolution = documentEditor.GetChangedDocument().Project.Solution; var BEPUutilities = newSolution.Projects.Single(proj => proj.AssemblyName == "BEPUutilities"); var F64 = BEPUutilities.Documents.Single(doc => doc.Name == "F64.cs"); syntaxRoot = await F64.GetSyntaxRootAsync(); bool constantDefined = syntaxRoot.DescendantNodes().OfType <FieldDeclarationSyntax>().Any(d => d.ToString().Contains(staticName)); if (!constantDefined) { var valExp = SyntaxFactory.ParseExpression("(Fix64)" + staticDefinition); var definition = SyntaxFactory.FieldDeclaration( SyntaxFactory.VariableDeclaration( SyntaxFactory.ParseTypeName("Fix64"), SyntaxFactory.SeparatedList(new[] { SyntaxFactory.VariableDeclarator( SyntaxFactory.Identifier(staticName), null, SyntaxFactory.EqualsValueClause(valExp) ) }) )).AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword), SyntaxFactory.Token(SyntaxKind.StaticKeyword), SyntaxFactory.Token(SyntaxKind.ReadOnlyKeyword));; ClassDeclarationSyntax cls = syntaxRoot.DescendantNodes().OfType <ClassDeclarationSyntax>().Single(); var newCls = cls.WithMembers(cls.Members.Add(definition)); newSolution = F64.WithSyntaxRoot(syntaxRoot.ReplaceNode(cls, newCls)).Project.Solution; } return(newSolution); }
public override SyntaxNode VisitLiteralExpression(LiteralExpressionSyntax node) { // Check are the real objects (boxed to `object' type) equal if (node != null && node.Token != null && node.Token.Value != null && node.Token.Value.Equals(this.literalToken.Value)) { // Replace literal with reference to symbolic constant IdentifierNameSyntax symbolicConstantReference = Syntax.IdentifierName(magicNumberName) .WithLeadingTrivia(node.GetLeadingTrivia()) .WithTrailingTrivia(node.GetTrailingTrivia()); return(symbolicConstantReference); } return(base.VisitLiteralExpression(node)); }
public override SyntaxNode VisitLiteralExpression(LiteralExpressionSyntax node) { if (node.CSharpKind() != SyntaxKind.StringLiteralExpression) { return(node); } var pos = node.GetLocation().GetMappedLineSpan(); var result = OnRewrite(pos.StartLinePosition.Line + 1, pos.StartLinePosition.Character + 1, Utils.EscapeString(node.Token.ValueText)); if (result == null) { return(node); } ExpressionSyntax newNode = null; foreach (var name in result.Split('.')) { if (newNode == null) { newNode = SyntaxFactory.IdentifierName(name); } else { newNode = SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, newNode, SyntaxFactory.IdentifierName(name)); } } return(newNode.WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia())); }