public override async Task <bool> IsAvailableAsync(EditorRefactoringContext context, System.Threading.CancellationToken cancellationToken) { SyntaxTree st = await context.GetSyntaxTreeAsync().ConfigureAwait(false); Identifier identifier = (Identifier)st.GetNodeAt(context.CaretLocation, node => node.Role == Roles.Identifier); if (identifier == null) { return(false); } ParameterDeclaration parameterDeclaration = identifier.Parent as ParameterDeclaration; if (parameterDeclaration == null) { return(false); } ICompilation compilation = await context.GetCompilationAsync().ConfigureAwait(false); ITypeReference typeRef = parameterDeclaration.Type.ToTypeReference(); if (typeRef == null) { return(false); } IType type = typeRef.Resolve(compilation); if (type == null) { return(false); } return(type.HasRange()); }
public override async void Execute(EditorRefactoringContext context) { SyntaxTree st = await context.GetSyntaxTreeAsync().ConfigureAwait(false); ICompilation compilation = await context.GetCompilationAsync().ConfigureAwait(false); CSharpFullParseInformation info = await context.GetParseInformationAsync().ConfigureAwait(false) as CSharpFullParseInformation; EntityDeclaration node = (EntityDeclaration)st.GetNodeAt(context.CaretLocation, n => n is TypeDeclaration || n is DelegateDeclaration); IDocument document = context.Editor.Document; FileName newFileName = FileName.Create(Path.Combine(Path.GetDirectoryName(context.FileName), MakeValidFileName(node.Name))); string header = CopyFileHeader(document, info); string footer = CopyFileEnd(document, info); AstNode newNode = node.Clone(); foreach (var ns in node.Ancestors.OfType <NamespaceDeclaration>()) { var newNS = new NamespaceDeclaration(ns.Name); newNS.Members.AddRange(ns.Children.Where(ch => ch is UsingDeclaration || ch is UsingAliasDeclaration || ch is ExternAliasDeclaration).Select(usingDecl => usingDecl.Clone())); newNS.AddMember(newNode); newNode = newNS; } var topLevelUsings = st.Children.Where(ch => ch is UsingDeclaration || ch is UsingAliasDeclaration || ch is ExternAliasDeclaration); StringBuilder newCode = new StringBuilder(header); CSharpOutputVisitor visitor = new CSharpOutputVisitor(new StringWriter(newCode), FormattingOptionsFactory.CreateSharpDevelop()); foreach (var topLevelUsing in topLevelUsings) { topLevelUsing.AcceptVisitor(visitor); } newNode.AcceptVisitor(visitor); newCode.AppendLine(footer); IViewContent viewContent = FileService.NewFile(newFileName, newCode.ToString()); viewContent.PrimaryFile.SaveToDisk(newFileName); // now that the code is saved in the other file, remove it from the original document RemoveExtractedNode(context, node); IProject project = (IProject)compilation.GetProject(); if (project != null) { FileProjectItem projectItem = new FileProjectItem(project, ItemType.Compile); projectItem.FileName = newFileName; ProjectService.AddProjectItem(project, projectItem); FileService.FireFileCreated(newFileName, false); project.Save(); ProjectBrowserPad.RefreshViewAsync(); } }
public override async void Execute(EditorRefactoringContext context) { SyntaxTree st = await context.GetSyntaxTreeAsync().ConfigureAwait(false); ICompilation compilation = await context.GetCompilationAsync().ConfigureAwait(false); CSharpFullParseInformation info = await context.GetParseInformationAsync().ConfigureAwait(false) as CSharpFullParseInformation; EntityDeclaration node = (EntityDeclaration)st.GetNodeAt(context.CaretLocation, n => n is TypeDeclaration || n is DelegateDeclaration); IDocument document = context.Editor.Document; FileName newFileName = FileName.Create(Path.Combine(Path.GetDirectoryName(context.FileName), MakeValidFileName(node.Name))); string header = CopyFileHeader(document, info); string footer = CopyFileEnd(document, info); AstNode newNode = node.Clone(); foreach (var ns in node.Ancestors.OfType<NamespaceDeclaration>()) { var newNS = new NamespaceDeclaration(ns.Name); newNS.Members.AddRange(ns.Children.Where(ch => ch is UsingDeclaration || ch is UsingAliasDeclaration || ch is ExternAliasDeclaration).Select(usingDecl => usingDecl.Clone())); newNS.AddMember(newNode); newNode = newNS; } var topLevelUsings = st.Children.Where(ch => ch is UsingDeclaration || ch is UsingAliasDeclaration || ch is ExternAliasDeclaration); StringBuilder newCode = new StringBuilder(header); CSharpOutputVisitor visitor = new CSharpOutputVisitor(new StringWriter(newCode), FormattingOptionsFactory.CreateSharpDevelop()); foreach (var topLevelUsing in topLevelUsings) topLevelUsing.AcceptVisitor(visitor); newNode.AcceptVisitor(visitor); newCode.AppendLine(footer); IViewContent viewContent = FileService.NewFile(newFileName, newCode.ToString()); viewContent.PrimaryFile.SaveToDisk(newFileName); // now that the code is saved in the other file, remove it from the original document RemoveExtractedNode(context, node); IProject project = (IProject)compilation.GetProject(); if (project != null) { FileProjectItem projectItem = new FileProjectItem(project, ItemType.Compile); projectItem.FileName = newFileName; ProjectService.AddProjectItem(project, projectItem); FileService.FireFileCreated(newFileName, false); project.Save(); ProjectBrowserPad.RefreshViewAsync(); } }
public override async Task<bool> IsAvailableAsync(EditorRefactoringContext context, System.Threading.CancellationToken cancellationToken) { SyntaxTree st = await context.GetSyntaxTreeAsync().ConfigureAwait(false); Identifier identifier = (Identifier) st.GetNodeAt(context.CaretLocation, node => node.Role == Roles.Identifier); if (identifier == null) return false; ParameterDeclaration parameterDeclaration = identifier.Parent as ParameterDeclaration; if (parameterDeclaration == null) return false; ICompilation compilation = await context.GetCompilationAsync().ConfigureAwait(false); ITypeReference typeRef = parameterDeclaration.Type.ToTypeReference(); if (typeRef == null) return false; IType type = typeRef.Resolve(compilation); if (type == null) return false; return type.HasRange(); }