private void createSubChanges( final Variable parent, final TextFileChange change, final List<String> names) { final String parentMsgName = RUtil.formatVarName(RRefactoringAdapter.getUnquotedIdentifier(parent.fName)); for (final Variable variable : parent.fSubVariables.values()) { if (variable.fNewName != null) { final String oldName = RRefactoringAdapter.getUnquotedIdentifier(variable.fName); final String oldMsgName = RUtil.formatVarName(oldName); final boolean isQuoted = (variable.fNewName.charAt(0) == '`'); final GroupCategorySet set = new GroupCategorySet( new GroupCategory( ((IRFrameInSource) parent.getParent()).getFrameId() + '$' + parent.fName, NLS.bind(Messages.RenameInRegion_Changes_VariableGroup_name, parentMsgName), "")); //$NON-NLS-1$ final String message = NLS.bind( Messages.RenameInRegion_Changes_ReplaceOccurrenceOf_name, oldMsgName, parentMsgName); for (final RElementAccess access : variable.fAccessList) { final RAstNode nameNode = access.getNameNode(); if (nameNode == null) { continue; } final String text = (isQuoted && nameNode.getNodeType() == NodeType.SYMBOL && nameNode.getOperator(0) == RTerminal.SYMBOL) ? variable.fNewName : RRefactoringAdapter.getUnquotedIdentifier(variable.fNewName); final IRegion nameRegion = RAst.getElementNameRegion(nameNode); TextChangeCompatibility.addTextEdit( change, message, new ReplaceEdit(nameRegion.getOffset(), nameRegion.getLength(), text), set); } names.add(oldName); } } }
@Override public Change createChange(final IProgressMonitor monitor) throws CoreException { try { final SubMonitor progress = SubMonitor.convert(monitor, RefactoringMessages.Common_CreateChanges_label, 3); final TextFileChange textFileChange = new SourceUnitChange(fSourceUnit); if (fSourceUnit.getWorkingContext() == LTK.EDITOR_CONTEXT) { textFileChange.setSaveMode(TextFileChange.LEAVE_DIRTY); } final List<String> variableNames = createChanges(textFileChange, progress.newChild(1)); final Map<String, String> arguments = new HashMap<String, String>(); final String description = NLS.bind( Messages.RenameInRegion_Descriptor_description, RUtil.formatVarNames(variableNames)); final IProject resource = fElementSet.getSingleProject(); final String project = (resource != null) ? resource.getName() : null; final String source = (project != null) ? NLS.bind(RefactoringMessages.Common_Source_Project_label, project) : RefactoringMessages.Common_Source_Workspace_label; final int flags = 0; final String comment = ""; // $NON-NLS-1$ final CommonRefactoringDescriptor descriptor = new CommonRefactoringDescriptor( getIdentifier(), project, description, comment, arguments, flags); return new RefactoringChange( descriptor, Messages.RenameInRegion_label, new Change[] {textFileChange}); } catch (final BadLocationException e) { throw new CoreException( new Status(IStatus.ERROR, RCore.PLUGIN_ID, "Unexpected error (concurrent change?)", e)); } finally { monitor.done(); } }