private static async Task <Document> RemoveLinearAttributeOptionFromAttribute(Document document, TextSpan span, FieldDeclarationSyntax declaration, CancellationToken cancellationToken) { var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); var canUseSimplifiedName = semanticModel.CanReferenceNamedSymbol(span, "UdonSyncedAttribute"); AttributeSyntax ModifyUdonSyncedAttribute(AttributeSyntax attribute) { var symbol = semanticModel.GetSymbolInfo(attribute); if (symbol.Symbol is not IMethodSymbol m) { return(attribute); // UNREACHABLE } if (UdonSharpBehaviourUtility.PrettyTypeName(m) == "UdonSharp.UdonSyncedAttribute") { if (canUseSimplifiedName) { return(SyntaxFactory.Attribute(SyntaxFactory.IdentifierName("UdonSynced"), null)); } return(SyntaxFactory.Attribute(SyntaxFactory.QualifiedName(SyntaxFactory.IdentifierName("UdonSharp"), SyntaxFactory.IdentifierName("UdonSynced")), null)); } return(attribute); } var oldNode = declaration.FirstAncestorOrSelf <FieldDeclarationSyntax>(); var attributes = oldNode.AttributeLists.Select(w => SyntaxFactory.AttributeList(SyntaxFactory.SeparatedList(w.Attributes.Select(ModifyUdonSyncedAttribute)))); var newNode = oldNode.WithAttributeLists(SyntaxFactory.List(attributes)); return(await document.ReplaceNodeAsync(oldNode, newNode, cancellationToken).ConfigureAwait(false)); }
public CodeRefactoring GetRefactoring(IDocument document, TextSpan textSpan, CancellationToken cancellationToken) { SyntaxNode root = (SyntaxNode)document.GetSyntaxRoot(cancellationToken); SyntaxToken token = root.FindToken(textSpan.Start, findInsideTrivia: true); SyntaxNode parentNode = token.Parent; if (parentNode == null) { return(null); } // Verify is the selected token an identifier within field declaration if (token.Kind == SyntaxKind.IdentifierToken && parentNode.Kind == SyntaxKind.VariableDeclarator && token.Span.Start <= textSpan.End && textSpan.End <= token.Span.End) { VariableDeclaratorSyntax variableDeclarator = (VariableDeclaratorSyntax)parentNode; FieldDeclarationSyntax fieldDeclaration = parentNode.FirstAncestorOrSelf <FieldDeclarationSyntax>(); // Skip if field has diagnostics if (fieldDeclaration == null || fieldDeclaration.HasDiagnostics) { return(null); } ISemanticModel model = document.GetSemanticModel(cancellationToken); ISymbol declaredSymbol = model.GetDeclaredSymbol(variableDeclarator); ClassDeclarationSyntax typeDeclaration = fieldDeclaration.FirstAncestorOrSelf <ClassDeclarationSyntax>(); if (typeDeclaration == null) { return(null); } // Verify is there already a getter property that returns the value of field if (typeDeclaration.ChildNodes().OfType <PropertyDeclarationSyntax>().Any(n => IsGetterProperty(n, declaredSymbol, model))) { return(null); } // Verify is there already a getter method that returns the value of field if (typeDeclaration.ChildNodes().OfType <MethodDeclarationSyntax>().Any(n => IsGetterMethod(n, declaredSymbol, model))) { return(null); } return(new CodeRefactoring( new[] { new EncapsulateFieldAction(document, declaredSymbol, fieldDeclaration, variableDeclarator) } , variableDeclarator.Identifier.Span)); } return(null); }
public override SyntaxNode VisitFieldDeclaration(FieldDeclarationSyntax node) { if (node.FirstAncestorOrSelf <ClassDeclarationSyntax>().Identifier.Text.Contains("Mapper")) { if (!this._finder.IsNodeWithinCustomCode(node)) { return(null); } } return(base.VisitFieldDeclaration(node)); }
private async Task <Document> Encapsulate(Document document, FieldDeclarationSyntax f, CancellationToken cancellationToken) { var fieldType = GetType(f.Declaration.Type); if (fieldType == null) { return(document); } var fieldName = f.Declaration.Variables.First().Identifier.ValueText; var lower = fieldName.TrimStart('_'); var upper = char.ToUpper(lower[0]) + lower.Substring(1, lower.Length - 1); var elementType = fieldType.TypeArgumentList.Arguments.First(); var oldNode = f.FirstAncestorOrSelf <ClassDeclarationSyntax>(); var p = SyntaxFactory.PropertyDeclaration(elementType, upper) .WithModifiers(SyntaxTokenList.Create(PublicToken)) .WithExpressionBody( SyntaxFactory.ArrowExpressionClause( ArrowToken, SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName(fieldName), SyntaxFactory.IdentifierName("Value") ) ) ) .WithSemicolonToken(SemicolonToken) .WithAdditionalAnnotations(Formatter.Annotation) ; var xx = p.ToString(); var xx1 = p.GetText().ToString(); var newNode = oldNode.InsertNodesBefore(f, new[] { p }); // .WithExpressionBody(SyntaxFactory.ArrowExpressionClause( var oldRoot = await document.GetSyntaxRootAsync(cancellationToken); var newRoot = oldRoot.ReplaceNode(oldNode, newNode); return(document.WithSyntaxRoot(newRoot)); }
private async Task <Document> Initialize(Document document, FieldDeclarationSyntax localDeclaration, CancellationToken cancellationToken) { var tree = await document.GetSyntaxTreeAsync(cancellationToken); var root = tree.GetRoot(cancellationToken); var constructors = root.DescendantNodes().OfType <ConstructorDeclarationSyntax>().ToList(); var csor = constructors.FirstOrDefault(); SyntaxNode visitingRoot = root; if (csor == null) { var oldClass = localDeclaration.FirstAncestorOrSelf <ClassDeclarationSyntax>(); var className = oldClass.Identifier.ToString(); var paramList = RoslynExtensions.GenerateParameters(new[] { localDeclaration.Declaration.Type }); var newCtor = SyntaxFactory.ConstructorDeclaration( attributeLists: SyntaxFactory.List(new AttributeListSyntax[] { }), modifiers: SyntaxFactory.TokenList(SyntaxFactory.Token(SyntaxKind.PublicKeyword)), identifier: oldClass.Identifier, parameterList: paramList, initializer: null, body: SyntaxFactory.Block(new[] { RoslynExtensions.GenerateCtorStatement(RoslynExtensions.GetFieldName(localDeclaration), RoslynExtensions.GetFieldVariableName(localDeclaration.Declaration.Type)) }), semicolonToken: default(SyntaxToken) ); csor = newCtor; visitingRoot = root.InsertNodesAfter(localDeclaration, new[] { newCtor }); } var cr = new ConstructorRewriter(csor, localDeclaration); var newRoot = cr.Visit(visitingRoot).WithAdditionalAnnotations(Formatter.Annotation); //var workspace = MSBuildWorkspace.Create(); //var formatted = Formatter.Format(newRoot, workspace); return(document.WithSyntaxRoot(newRoot)); }
public CodeRefactoring GetRefactoring(IDocument document, TextSpan textSpan, CancellationToken cancellationToken) { SyntaxNode root = (SyntaxNode)document.GetSyntaxRoot(cancellationToken); ISemanticModel model = document.GetSemanticModel(cancellationToken); SyntaxToken token = root.FindToken(textSpan.Start, findInsideTrivia: true); // Verify is the selected token an identifier within field declaration if (token.Kind == SyntaxKind.IdentifierToken && token.Span.Start <= textSpan.End && textSpan.End <= token.Span.End) { SyntaxNode parentNode = token.Parent; if (parentNode == null) { return(null); } FieldDeclarationSyntax fieldDeclaration = parentNode.FirstAncestorOrSelf <FieldDeclarationSyntax>(); if (fieldDeclaration == null) { return(null); } // If the FieldDeclaration has some errors, then no refactoring should take place if (fieldDeclaration.HasDiagnostics) { return(null); } // Get the container that the field belongs to TypeDeclarationSyntax containingType = fieldDeclaration.FirstAncestorOrSelf <TypeDeclarationSyntax>(); if (containingType == null) { return(null); } // Obtain TypeSymbol for class declaration ITypeSymbol containingTypeSymbol = model.GetDeclaredSymbol(containingType, cancellationToken) as ITypeSymbol; if (containingTypeSymbol == null) { return(null); } // Check is there a base class that the field can be moved to INamedTypeSymbol baseTypeSymbol = containingTypeSymbol.BaseType; if (baseTypeSymbol == null) { return(null); } // Check is the class defined in source, so that it can be extended CommonLocation baseTypeLocation = baseTypeSymbol.Locations.First(); if (baseTypeLocation != null && baseTypeLocation.IsInSource) { int position = baseTypeLocation.SourceSpan.Start; BaseTypeDeclarationSyntax baseTypeDeclaration = baseTypeLocation.SourceTree.GetRoot().FindToken(position).Parent.FirstAncestorOrSelf <BaseTypeDeclarationSyntax>(); if (baseTypeDeclaration == null) { return(null); } return(new CodeRefactoring( new[] { new PullUpFieldAction(document, fieldDeclaration, containingType, baseTypeDeclaration) } , fieldDeclaration.Span)); } } return(null); }