private static string CreateName(CodeRefactoringContext context, SyntaxNode root, SyntaxNode blockSyntax, string typeName) { string name = typeName + "Const"; name = NameGenerator.GenerateUniqueName(name, root.ChildTokens().Where(t => t.IsKind(SyntaxKind.IdentifierToken)).Select(t => t.ToString()).ToSet(), StringComparer.Ordinal); var newConstName = NameProposalService.GetNameProposal(name, SyntaxKind.MethodDeclaration, Accessibility.Private, true, context.Document, blockSyntax.SpanStart); return(newConstName); }
static PropertyDeclarationSyntax GeneratePropertyDeclaration(FieldDeclarationSyntax field, VariableDeclaratorSyntax initializer) { var modifiers = SyntaxFactory.TokenList(SyntaxFactory.Token(SyntaxKind.PublicKeyword)); if (field.Modifiers.Any(m => m.IsKind(SyntaxKind.StaticKeyword))) { modifiers = modifiers.Add(SyntaxFactory.Token(SyntaxKind.StaticKeyword)); } string propertyName = NameProposalService.GetNameProposal(initializer.Identifier.ValueText, SyntaxKind.PropertyDeclaration); var block = SyntaxFactory.Block(SyntaxFactory.ReturnStatement(SyntaxFactory.IdentifierName(initializer.Identifier))); return(SyntaxFactory.PropertyDeclaration(field.Declaration.Type, propertyName) .WithModifiers(modifiers) .WithAccessorList( SyntaxFactory.AccessorList(SyntaxFactory.SingletonList( SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration, block) )) ).WithAdditionalAnnotations(Formatter.Annotation)); }
static string GetEventMethodName(string eventName) { return(NameProposalService.GetNameProposal("On" + char.ToUpper(eventName[0]) + eventName.Substring(1), SyntaxKind.MethodDeclaration)); }
public override async Task ComputeRefactoringsAsync(CodeRefactoringContext context) { var document = context.Document; if (document.Project.Solution.Workspace.Kind == WorkspaceKind.MiscellaneousFiles) { return; } var span = context.Span; if (!span.IsEmpty) { return; } var cancellationToken = context.CancellationToken; if (cancellationToken.IsCancellationRequested) { return; } var model = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); if (model.IsFromGeneratedCode(cancellationToken)) { return; } var root = await model.SyntaxTree.GetRootAsync(cancellationToken).ConfigureAwait(false); var token = root.FindToken(span.Start); var parameter = token.Parent as ParameterSyntax; if (parameter != null) { var ctor = parameter.Parent.Parent as ConstructorDeclarationSyntax; if (ctor == null) { return; } context.RegisterRefactoring( CodeActionFactory.Create( parameter.Span, DiagnosticSeverity.Info, GettextCatalog.GetString("Initialize field from parameter"), t2 => { var newFieldName = NameProposalService.GetNameProposal(parameter.Identifier.ValueText, SyntaxKind.FieldDeclaration, Accessibility.Private, false, context.Document, ctor.SpanStart); var newField = SyntaxFactory.FieldDeclaration( SyntaxFactory.VariableDeclaration( parameter.Type, SyntaxFactory.SingletonSeparatedList <VariableDeclaratorSyntax>(SyntaxFactory.VariableDeclarator(newFieldName))) ).WithModifiers(SyntaxFactory.TokenList(SyntaxFactory.Token(SyntaxKind.ReadOnlyKeyword))) .WithAdditionalAnnotations(Formatter.Annotation); var assignmentStatement = SyntaxFactory.ExpressionStatement( SyntaxFactory.AssignmentExpression( SyntaxKind.SimpleAssignmentExpression, SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.ThisExpression(), SyntaxFactory.IdentifierName(newFieldName)), SyntaxFactory.IdentifierName(newFieldName) ) ).WithAdditionalAnnotations(Formatter.Annotation); var trackedRoot = root.TrackNodes(ctor); var newRoot = trackedRoot.InsertNodesBefore(trackedRoot.GetCurrentNode(ctor), new List <SyntaxNode>() { newField }); newRoot = newRoot.ReplaceNode(newRoot.GetCurrentNode(ctor), ctor.WithBody( ctor.Body.WithStatements(SyntaxFactory.List <StatementSyntax>(new[] { assignmentStatement }.Concat(ctor.Body.Statements))) )); return(Task.FromResult(document.WithSyntaxRoot(newRoot))); }) ); } }