private static Task <Document> MakeSealedAsync(DocumentEditor editor, SyntaxNode declaration) { DeclarationModifiers modifiers = editor.Generator.GetModifiers(declaration); editor.SetModifiers(declaration, modifiers + DeclarationModifiers.Sealed); return(Task.FromResult(editor.GetChangedDocument())); }
/// <summary> /// if a nested type is being moved, this ensures its containing type is partial. /// </summary> private void AddPartialModifiersToTypeChain( DocumentEditor documentEditor, bool removeAttributesAndComments) { var semanticFacts = State.SemanticDocument.Document.GetLanguageService <ISemanticFactsService>(); var typeChain = State.TypeNode.Ancestors().OfType <TTypeDeclarationSyntax>(); foreach (var node in typeChain) { var symbol = (ITypeSymbol)State.SemanticDocument.SemanticModel.GetDeclaredSymbol(node, CancellationToken); if (!semanticFacts.IsPartial(symbol, CancellationToken)) { documentEditor.SetModifiers(node, documentEditor.Generator.GetModifiers(node) | DeclarationModifiers.Partial); } if (removeAttributesAndComments) { documentEditor.RemoveAllAttributes(node); documentEditor.RemoveAllComments(node); } } documentEditor.ReplaceNode(State.TypeNode, (currentNode, generator) => { var currentTypeNode = (TTypeDeclarationSyntax)currentNode; // Trim leading whitespace from the type so we don't have excessive // leading blank lines. return(RemoveLeadingWhitespace(currentTypeNode)); }); }
private static void ChangeMemberToPublicAndNonStatic( ICodeGenerationService codeGenerationService, DocumentEditor editor, SyntaxNode memberDeclaration, ISymbol member ) { var modifiers = DeclarationModifiers.From(member).WithIsStatic(false); // Event is different since several events may be declared in one line. if (member is IEventSymbol eventSymbol) { ChangeEventToPublicAndNonStatic( codeGenerationService, editor, eventSymbol, memberDeclaration, modifiers ); } else { editor.SetAccessibility(memberDeclaration, Accessibility.Public); editor.SetModifiers(memberDeclaration, modifiers); } }
private Task <Document> MakeSealed(DocumentEditor editor, SyntaxNode declaration, CancellationToken ct) { DeclarationModifiers modifiers = editor.Generator.GetModifiers(declaration); editor.SetModifiers(declaration, modifiers + DeclarationModifiers.Sealed); return(Task.FromResult(editor.GetChangedDocument())); }
private static void ChangeMemberToPublicAndNonStatic( ICodeGenerationService codeGenerationService, DocumentEditor editor, SyntaxNode memberDeclaration, ISymbol member) { var modifiers = DeclarationModifiers.From(member).WithIsStatic(false); editor.SetAccessibility(memberDeclaration, Accessibility.Public); editor.SetModifiers(memberDeclaration, modifiers); }
/// <summary> /// if a nested type is being moved, this ensures its containing type is partial. /// </summary> /// <param name="documentEditor">document editor for the new document being created</param> private void AddPartialModifiersToTypeChain(DocumentEditor documentEditor) { var semanticFacts = State.SemanticDocument.Document.GetLanguageService <ISemanticFactsService>(); var typeChain = State.TypeNode.Ancestors().OfType <TTypeDeclarationSyntax>(); foreach (var node in typeChain) { var symbol = (ITypeSymbol)State.SemanticDocument.SemanticModel.GetDeclaredSymbol(node, CancellationToken); if (!semanticFacts.IsPartial(symbol, CancellationToken)) { documentEditor.SetModifiers(node, DeclarationModifiers.Partial); } } }
private static void ChangeEventToPublicAndNonStatic( ICodeGenerationService codeGenerationService, DocumentEditor editor, IEventSymbol eventSymbol, SyntaxNode eventDeclaration, DeclarationModifiers modifiers ) { var declaration = editor.Generator.GetDeclaration(eventDeclaration); var isEventHasExplicitAddOrRemoveMethod = (eventSymbol.AddMethod != null && !eventSymbol.AddMethod.IsImplicitlyDeclared) || ( eventSymbol.RemoveMethod != null && !eventSymbol.RemoveMethod.IsImplicitlyDeclared ); // There are three situations here: // 1. Single Event. // 2. Several events exist in one declaration. // 3. Event has add or remove method(user declared). // For situation 1, declaration is EventFieldDeclaration, eventDeclaration is variableDeclaration. // For situation 2, declaration and eventDeclaration are both EventDeclaration, which are same. // For situation 3, it is same as situation 2, but has add or remove method. if (declaration.Equals(eventDeclaration) && !isEventHasExplicitAddOrRemoveMethod) { // Several events are declared in same line var publicAndNonStaticSymbol = CodeGenerationSymbolFactory.CreateEventSymbol( eventSymbol, accessibility: Accessibility.Public, modifiers: modifiers ); var options = new CodeGenerationOptions(generateMethodBodies: false); var publicAndNonStaticSyntax = codeGenerationService.CreateEventDeclaration( publicAndNonStaticSymbol, destination: CodeGenerationDestination.ClassType, options: options ); // Insert a new declaration and remove the original declaration editor.InsertAfter(declaration, publicAndNonStaticSyntax); editor.RemoveNode(eventDeclaration); } else { // Handle both single event and event has add or remove method editor.SetAccessibility(declaration, Accessibility.Public); editor.SetModifiers(declaration, modifiers); } }
private async Task <Document> MakeClassStatic(Document document, ClassDeclarationSyntax classDeclaration, CancellationToken ct) { DocumentEditor editor = await DocumentEditor.CreateAsync(document, ct).ConfigureAwait(false); DeclarationModifiers modifiers = editor.Generator.GetModifiers(classDeclaration); editor.SetModifiers(classDeclaration, modifiers - DeclarationModifiers.Sealed + DeclarationModifiers.Static); SyntaxList <MemberDeclarationSyntax> members = classDeclaration.Members; MemberDeclarationSyntax defaultConstructor = members.FirstOrDefault(m => m.IsDefaultConstructor()); if (defaultConstructor != null) { editor.RemoveNode(defaultConstructor); } return(editor.GetChangedDocument()); }
private static void RemoveAsyncFromMethod(DocumentEditor documentEditor, MethodDeclarationSyntax methodDeclarationSyntax, SemanticModel semanticModel) { documentEditor.SetModifiers(methodDeclarationSyntax, DeclarationModifiers.From(semanticModel.GetDeclaredSymbol(methodDeclarationSyntax)).WithAsync(false)); }