Click here to Skip to main content
12,702,068 members (35,596 online)
Click here to Skip to main content
Articles » Languages » C# » General » Downloads

Tagged as

Stats

44.1K views
933 downloads
42 bookmarked
Posted

Roslyn CTP: Three Introductory Projects

, 8 May 2012 CPOL
An introduction to the Roslyn CTP
CodeGenerator
bin
Debug
CodeGenerator.vshost.exe
CodeGenerator.vshost.exe.manifest
Release
CodeGenerator.csproj.user
obj
x86
Debug
DesignTimeResolveAssemblyReferences.cache
DesignTimeResolveAssemblyReferencesInput.cache
TempPE
Properties
EventImplementer
bin
Debug
Release
EventImplementer.csproj.user
obj
Debug
DesignTimeResolveAssemblyReferencesInput.cache
TempPE
Properties
source.extension.vsixmanifest
RoslynCTPSamples.suo
RoslynCTPSamples.vsmdi
RoslynCTPLibrary
bin
Debug
RoslynCTPSamples.dll
RoslynCTPSamples.pdb
Release
obj
Debug
DesignTimeResolveAssemblyReferencesInput.cache
RoslynCTPSamples.dll
RoslynCTPSamples.pdb
TempPE
Properties
CodeGeneratorTest
bin
Debug
obj
Debug
DesignTimeResolveAssemblyReferencesInput.cache
TempPE
Properties
RefactoringConsole
bin
Debug
RefactoringConsole.vshost.exe
RefactoringConsole.vshost.exe.manifest
obj
x86
Debug
DesignTimeResolveAssemblyReferencesInput.cache
TempPE
Properties
RefactoringExtension
bin
Debug
Release
obj
Debug
DesignTimeResolveAssemblyReferencesInput.cache
TempPE
Properties
RefactoringExtension.csproj.user
source.extension.vsixmanifest
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)

Share

About the Author

Whaeth
Hungary Hungary
No Biography provided

You may also be interested in...

Pro
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 8 May 2012
Article Copyright 2011 by Whaeth
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid