- RoslynCTPSamples.zip
- CodeGenerator
- bin
- Debug
- Release
- CodeGenerator.csproj
- CodeGenerator.csproj.user
- obj
- x86
- Debug
- DesignTimeResolveAssemblyReferences.cache
- DesignTimeResolveAssemblyReferencesInput.cache
- TempPE
- Program.cs
- Properties
- CodeGeneratorTest
- EventImplementer
- RefactoringConsole
- RefactoringExtension
- RoslynCTPLibrary
- RoslynCTPSamples.sln
- RoslynCTPSamples.suo
- RoslynCTPSamples.vsmdi
|
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.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.