protected override ReplaceInfo CreateReplaceInfo() { var usingDirective = _workFlow.ImportedNamespacePointer; if (usingDirective == null) return null; _suggestedName = CamelCaseSelector.GetCamelCaseSuggestion(usingDirective.QualifiedName); var factory = CSharpElementFactory.GetInstance(usingDirective.GetPsiModule()); var scope = usingDirective.FindParent<ITypeAndNamespaceHolderDeclaration>(); if (scope == null) return null; CallExtensionMethodsAsStatic(scope, factory, usingDirective.QualifiedName); var replacedNodes = new List<ITreeNode>(); var myReferenceCollector = new MyReferenceCollector(); scope.ProcessDescendantsForResolve(myReferenceCollector); using (WriteLockCookie.Create()) { // Add alias to namespace var newchild = factory.CreateUsingDirective("$0 = $1", _suggestedName, usingDirective.QualifiedName); newchild = ModificationUtil.AddChildAfter(usingDirective, newchild); replacedNodes.Add((newchild as IUsingAliasDirective).Alias); AppendUsages(myReferenceCollector.Referenced, replacedNodes, usingDirective.QualifiedName); // delete old using ModificationUtil.DeleteChild(usingDirective); } return new ReplaceInfo( null, null, replacedNodes.ToArray(), new NameSuggestionsExpression(new[] { _suggestedName }), Solution.GetPsiServices().Files); }
protected override ReplaceInfo CreateReplaceInfo() { var usingDirective = _workFlow.ImportedNamespacePointer.GetTreeNode(); if (usingDirective == null) return null; var importedNs = usingDirective.ImportedNamespace; if (importedNs == null) return null; _suggestedName = CamelCaseSelector.GetCamelCaseSuggestion(importedNs.QualifiedName); var factory = CSharpElementFactory.GetInstance(usingDirective.GetPsiModule()); // replace using of extension methods var refname = usingDirective.Children<IReferenceName>().SingleOrDefault(); var selectedName = importedNs.QualifiedName; var file = usingDirective.Root() as IFile; if (file == null) return null; CallExtensionMethodsAsStatic(file, importedNs, factory); var replacedNodes = new List<ITreeNode>(); var myReferenceCollector = new MyReferenceCollector(); file.ProcessDescendantsForResolve(myReferenceCollector); var referencesTypesReplace = myReferenceCollector.Referenced; // Add alias to namespace var newchild = factory.CreateUsingDirective("$0 = $1", _suggestedName, importedNs.QualifiedName); //ModificationUtil(usingDirective.Parent, newchild); newchild = ModificationUtil.AddChildAfter(usingDirective, newchild); replacedNodes.Add((newchild as IUsingAliasDirective).Alias); // Modify call foreach (var bindedResult in referencesTypesReplace) { //var type = bindedResult.GetTreeNode(); //if (type == null) continue; var declaredElement = bindedResult.Resolve().DeclaredElement; var astypeElement = declaredElement as ITypeElement; var asMethod = declaredElement as IMethod; IClrTypeName clrName = null; string containedns = null; string methodName = null; if (astypeElement != null) { clrName = astypeElement.GetClrName(); containedns = astypeElement.GetContainingNamespace().QualifiedName; } else if (asMethod != null) { var containingType = asMethod.GetContainingType(); clrName = containingType.GetClrName(); containedns = containingType.GetContainingNamespace().QualifiedName; methodName = asMethod.ShortName; } if ((astypeElement != null || (asMethod != null && asMethod.IsStatic)) && containedns == importedNs.QualifiedName) { var list = new List<string> { _suggestedName }; foreach (var typeParameterNumber in clrName.TypeNames) list.Add(CSharpImplUtil.MakeSafeName(typeParameterNumber.TypeName)); if (methodName != null) { list.Add(CSharpImplUtil.MakeSafeName(methodName)); } var result = string.Join(".", list); var treeNode = bindedResult.GetTreeNode(); var refa = CSharpReferenceBindingUtil.ReplaceReferenceElement(treeNode, result, true); var node = refa.GetTreeNode(); var firstChild = node.GetFirstTokenIn(); replacedNodes.Add(firstChild); } } ModificationUtil.DeleteChild(usingDirective); return new ReplaceInfo( null, null, replacedNodes.ToArray(), new NameSuggestionsExpression(new[] { _suggestedName }), PsiManager.GetInstance(Solution)); }