private SyntaxKind?GetExistingPartialVisibility(TypeBlockSyntax originalTypeBlockSyntax) { // Getting the SemanticModel is a relatively expensive operation. Can take a few seconds in // projects of significant size. It is delay created to avoid this in files which already // conform to the standards. if (_semanticModel == null) { _semanticModel = _document.GetSemanticModelAsync(_cancellationToken).Result; } var symbol = _semanticModel.GetDeclaredSymbol(originalTypeBlockSyntax, _cancellationToken); if (symbol == null) { return(null); } switch (symbol.DeclaredAccessibility) { case Accessibility.Friend: return(SyntaxKind.FriendKeyword); case Accessibility.Public: return(SyntaxKind.PublicKeyword); case Accessibility.Private: return(SyntaxKind.PrivateKeyword); case Accessibility.Protected: return(SyntaxKind.ProtectedKeyword); default: return(null); } }
private async Task RegisterGenerateToString(CodeRefactoringContext context, TypeBlockSyntax cds) { semanticModel = await context.Document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false); var action = CodeAction.Create(Strings.GenerateToString, c => GenerateToStringAsync(context.Document, cds, c)); context.RegisterRefactoring(action); }
private async Task RegisterReplaceToString(CodeRefactoringContext context, TypeBlockSyntax tds, MethodBlockSyntax toStringMethod) { semanticModel = await context.Document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false); var action = CodeAction.Create(Strings.ReplaceToString, c => ReplaceToStringAsync(context.Document, tds, toStringMethod, c)); context.RegisterRefactoring(action); }
public override bool VisitBaseType(TypeBlockSyntax basetype, string path) { var branch = this.parser.Dependencies.GetPath(path, "."); var leaf = this.parser.Create(basetype.BlockStatement.Identifier.ValueText, path + "." + basetype.BlockStatement.Identifier.ValueText, branch); leaf.LOC = Count(basetype.ToString()); this.sources.Create(leaf, new Core.SourceProvider(this.filename)); return(true); }
private async Task <Document> GenerateToStringAsync(Document document, TypeBlockSyntax typeBlockSyntax, CancellationToken cancellationToken) { var newTypeDec = typeBlockSyntax.AddMembers(GetToStringDeclarationSyntax(typeBlockSyntax)); var sr = await document.GetSyntaxRootAsync(cancellationToken); var nsr = sr.ReplaceNode(typeBlockSyntax, newTypeDec); return(document.WithSyntaxRoot(nsr)); }
private async Task <Document> ReplaceToStringAsync(Document document, TypeBlockSyntax typeDec, MethodBlockSyntax oldToString, CancellationToken cancellationToken) { var newClassDec = typeDec.ReplaceNode(oldToString, GetToStringDeclarationSyntax(typeDec)); var sr = await document.GetSyntaxRootAsync(cancellationToken); var nsr = sr.ReplaceNode(typeDec, newClassDec); return(document.WithSyntaxRoot(nsr)); }
private StatementSyntax GetToStringBody(TypeBlockSyntax classDeclarationSyntax) { var sm = semanticModel.GetDeclaredSymbol(classDeclarationSyntax); var properties = Helpers.FindAllProperties(sm); var r = string.Join(", ", properties.Select(p => p.GetPrintedValueForVB())); return(SyntaxFactory.ReturnStatement(SyntaxFactory.ParseExpression("$\"{{" + r + "}}\""))); }
public override bool VisitBaseType(TypeBlockSyntax basetype, string path) { if (path + "." + basetype.BlockStatement.Identifier == this.path) { this.Found = basetype; return(false); } return(true); }
private bool TryGetToStringMethod(TypeBlockSyntax cds, out MethodBlockSyntax toStringMethod) { toStringMethod = cds.Members .OfType <MethodBlockSyntax>() .FirstOrDefault(p => p.SubOrFunctionStatement.Identifier.ValueText == "ToString" && p.SubOrFunctionStatement.ParameterList.Parameters.Count == 0 && p.SubOrFunctionStatement.Modifiers.Any(SyntaxKind.PublicKeyword) && p.SubOrFunctionStatement.Modifiers.Any(SyntaxKind.OverridesKeyword)); return(toStringMethod != null); }
private SyntaxKind GetTypeDefaultVisibility(TypeBlockSyntax originalTypeBlockSyntax) { // In the case of partial types we need to use the existing visibility if it exists if (originalTypeBlockSyntax.BlockStatement.Modifiers.Any(x => x.Kind() == SyntaxKind.PartialKeyword)) { SyntaxKind?kind = GetExistingPartialVisibility(originalTypeBlockSyntax); if (kind.HasValue) { return(kind.Value); } } return(GetDelegateTypeDefaultVisibility(originalTypeBlockSyntax)); }
private MethodBlockSyntax GetToStringDeclarationSyntax(TypeBlockSyntax typeDeclarationSyntax) { var subStatement = SyntaxFactory.FunctionStatement(SyntaxFactory.List <AttributeListSyntax>(), SyntaxFactory.TokenList(SyntaxFactory.Token(SyntaxKind.PublicKeyword), SyntaxFactory.Token(SyntaxKind.OverridesKeyword)), SyntaxFactory.Identifier(Strings.ToStringMethod), null, SyntaxFactory.ParameterList(), SyntaxFactory.SimpleAsClause(SyntaxFactory.IdentifierName("String")), null, null); var statements = SyntaxFactory.List(new[] { GetToStringBody(typeDeclarationSyntax) }); var endSubStatement = SyntaxFactory.EndFunctionStatement(); return(SyntaxFactory.FunctionBlock(subStatement, statements, endSubStatement)); }
public override bool VisitBaseType(TypeBlockSyntax basetype, string path) { var branch = this.dependencies.GetPath(path, "."); foreach (var child in branch.Children) { if (child.Name == basetype.BlockStatement.Identifier.ValueText) { TypeWalker walker = new TypeWalker(this.parser, child, this.model); walker.Visit(basetype); break; } } return(true); }
public bool Visit(TypeBlockSyntax basetype, string path) { if (basetype == null) { return(true); } if (!this.VisitBaseType(basetype, path)) { return(false); } var fullPath = basetype.BlockStatement.Identifier.ToString(); if (path.Length > 0) { fullPath = path + "." + fullPath; } var classtype = basetype as ClassBlockSyntax; if (classtype != null) { foreach (var member in classtype.Members) { if (!this.Visit(member as TypeBlockSyntax, fullPath)) { return(false); } } return(true); } var moduletype = basetype as ModuleBlockSyntax; if (moduletype != null) { foreach (var member in moduletype.Members) { if (!this.Visit(member as TypeBlockSyntax, fullPath)) { return(false); } } return(true); } var structtype = basetype as StructureBlockSyntax; if (structtype != null) { foreach (var member in structtype.Members) { if (!this.Visit(member as TypeBlockSyntax, fullPath)) { return(false); } } return(true); } return(true); }
public abstract bool VisitBaseType(TypeBlockSyntax basetype, string path);
public FindLeaf(string path) { this.path = path; this.Found = null; }