public override IReference BindTo(IDeclaredElement element) { var factory = JsonNewElementFactory.GetInstance(myOwner.GetPsiModule()); var literalExpression = factory.CreateStringLiteral(element.ShortName); using (WriteLockCookie.Create(myOwner.IsPhysical())) { var newExpression = ModificationUtil.ReplaceChild(myOwner, literalExpression); return(newExpression.FindReference <AsmDefNameReference>() ?? this); } }
public override void Rename(IRenameRefactoring executer, IProgressIndicator pi, bool hasConflictsWithDeclarations, IRefactoringDriver driver) { // Rename the "declaration" var declaredElement = myPointer.FindDeclaredElement(); var originalTreeNode = declaredElement?.GetTreeNode(); if (originalTreeNode == null) { return; } var originalRange = originalTreeNode.GetDocumentRange(); var factory = JsonNewElementFactory.GetInstance(originalTreeNode.GetPsiModule()); var literalExpression = factory.CreateStringLiteral(NewName); ITreeNode newExpression; using (WriteLockCookie.Create(originalTreeNode.IsPhysical())) { newExpression = ModificationUtil.ReplaceChild(originalTreeNode, literalExpression); } RemoveFromTextualOccurrences(executer, originalRange); var references = executer.Workflow.GetElementReferences(declaredElement); if (!Enumerable.Any(references)) { return; } pi.Start(references.Count); // Create a new declared element (other implementations don't appear to cache this, either) var element = new AsmDefNameDeclaredElement(NewName, declaredElement.SourceFile, newExpression.GetTreeStartOffset().Offset); // Rename/bind the references foreach (var pair in LanguageUtil.SortReferences(references.Where(r => r.IsValid()))) { foreach (var sortedReference in LanguageUtil.GetSortedReferences(pair.Value)) { InterruptableActivityCookie.CheckAndThrow(pi); var referenceRange = sortedReference.GetDocumentRange(); if (sortedReference.IsValid()) { sortedReference.BindTo(element); } RemoveFromTextualOccurrences(executer, referenceRange); pi.Advance(); } } element.GetPsiServices().Caches.Update(); myNewPointer = element.CreateElementPointer(); }