Click here to Skip to main content
15,893,508 members
Articles / Programming Languages / C#

Roslyn CTP: Three Introductory Projects

Rate me:
Please Sign up or sign in to vote.
4.92/5 (21 votes)
8 May 2012CPOL18 min read 76.7K   1K   42  
An introduction to the Roslyn CTP
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Threading;
using Roslyn.Compilers;
using Roslyn.Compilers.CSharp;
using Roslyn.Services;
using Roslyn.Services.Editor;
using RoslynCTPLibrary.Refactoring;

namespace RefactoringExtension {

    [ExportCodeRefactoringProvider("RefactoringExtension", LanguageNames.CSharp)]
    class CodeRefactoringProvider : ICodeRefactoringProvider {
        private readonly ICodeActionEditFactory editFactory;

        [ImportingConstructor]
        public CodeRefactoringProvider(ICodeActionEditFactory editFactory) {
            this.editFactory = editFactory;
        }

        public CodeRefactoring GetRefactoring(IDocument document, TextSpan textSpan, CancellationToken cancellationToken) {
            var token = document.GetSyntaxTree().Root.FindToken(textSpan.Start);

            if (token.Parent == null) return null;

            var methodDeclaration = token.Parent.FirstAncestorOrSelf<BaseMethodDeclarationSyntax>();

            if (methodDeclaration == null || methodDeclaration.BodyOpt == null || methodDeclaration.BodyOpt.Statements.Count < 1) return null;

            return new CodeRefactoring(new[] { new CodeAction(editFactory, document, methodDeclaration) }, 
                methodDeclaration is ConstructorDeclarationSyntax ? ((ConstructorDeclarationSyntax)methodDeclaration).Identifier.Span :
                methodDeclaration is MethodDeclarationSyntax ? ((MethodDeclarationSyntax)methodDeclaration).Identifier.Span :
                methodDeclaration.BodyOpt.OpenBraceToken.Span);
        }


        class CodeAction : ICodeAction {
            public string Description { get { return "Declare variables at the begining of the method"; } }
            public System.Windows.Media.ImageSource Icon { get { return null; } }

            private ICodeActionEditFactory editFactory;
            private IDocument document;
            private BaseMethodDeclarationSyntax methodDeclaration;

            public CodeAction(ICodeActionEditFactory editFactory, IDocument document, BaseMethodDeclarationSyntax methodDeclaration) {
                this.editFactory = editFactory;
                this.document = document;
                this.methodDeclaration = methodDeclaration;
            }

            public ICodeActionEdit GetEdit(CancellationToken cancellationToken) {
                var tree = document.GetSyntaxTree(cancellationToken) as SyntaxTree;
                var sm = document.GetSemanticModel(cancellationToken) as SemanticModel;

                return editFactory.CreateTreeTransformEdit(document.Project.Solution, tree, 
                    ((SyntaxNode)tree.Root).ReplaceNode(methodDeclaration, DeclarationExtractor.ProcessMethod(methodDeclaration, sm)));
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Hungary Hungary
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions