Static Method Code Action: Listing 2

Full GetEdit Method Implementation

Listing 2: StaticMethodCodeAction GetEdit Function
 public ICodeActionEdit GetEdit(System.Threading.CancellationToken cancellationT
{
    var syntaxTree = (SyntaxTree)_document.GetSyntaxTree(cancellationToken);

    var firstToken = _methodDeclaration.GetFirstToken();
    var firstTokenTrivia = firstToken.LeadingTrivia;

    var formattedMethodDeclaration = _methodDeclaration.ReplaceToken(
        firstToken, firstToken.WithLeadingTrivia(Syntax.TriviaList()));

    var staticToken = Syntax.Token(firstTokenTrivia, SyntaxKind.StaticKeyword);

    var newModifiers = Syntax.TokenList(
        new[] { staticToken }.Concat(formattedMethodDeclaration.Modifiers));

    var newMethodDeclaration = formattedMethodDeclaration.Update(
        formattedMethodDeclaration.Attributes,
        newModifiers,
        formattedMethodDeclaration.ReturnType,
        formattedMethodDeclaration.ExplicitInterfaceSpecifierOpt,
        formattedMethodDeclaration.Identifier,
        formattedMethodDeclaration.TypeParameterListOpt,
        formattedMethodDeclaration.ParameterList,
        formattedMethodDeclaration.ConstraintClauses,
        formattedMethodDeclaration.BodyOpt,
        formattedMethodDeclaration.SemicolonTokenOpt);
  
    var formattedLocalDeclaration = CodeActionAnnotations.FormattingAnnotation
        .AddAnnotationTo(newMethodDeclaration);

    var tree = (SyntaxTree)_document.GetSyntaxTree();
    var newRoot = tree.Root.ReplaceNode(_methodDeclaration,
        formattedLocalDeclaration);

    return _editFactory.CreateTreeTransformEdit(_document.Project.Solution,
        syntaxTree, newRoot, cancellationToken: cancellationToken);
}

About the Author

Eric Vogel is a Senior Software Developer for Red Cedar Solutions Group in Okemos, Michigan. He is the president of the Greater Lansing User Group for .NET. Eric enjoys learning about software architecture and craftsmanship, and is always looking for ways to create more robust and testable applications. Contact him at [email protected].

comments powered by Disqus

Featured

  • GitLab Takes Over VS Code Extension, Plans Improvements

    DevOps specialist GitLab has officially taken over the control of a GitLab extension for Microsoft's open source, cross-platform Visual Studio Code editor.

  • VS Code Python Tool Now Does Native Notebooks

    The Python Extension for VS Code Insiders team is previewing the newest implementation of notebooks, used frequently in data science with offerings such as Jupyter Notebooks.

  • As .NET 5 Nears, Content/Documentation Reorganization Starts

    A GitHub project is seeking to reorganize documentation and developer content in advance of the November debut of .NET 5, a unification of all things .NET that combines. .NET Core and other components.

  • Windows Devs Get Cross-Platform Page, Issues Repo

    Developers doing their coding on the Windows OS have received two new resource gifts from Microsoft: a new landing page for those using cross-platform technologies and a new GitHub repo with which to report issues to Windows engineering teams.Developers doing their coding on the Windows OS have received two new resource gifts from Microsoft: a new landing page for those using cross-platform technologies and a new GitHub repo with which to report issues to Windows engineering teams.

  • What's New in Entity Framework Core 5.0 Preview 7

    Microsoft shipped the seventh preview of Entity Framework Core 5.0, boosting its data access technology with a factory to create DbContext instances and more.

.NET Insight

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.

Upcoming Events