use of org.eclipse.ltk.core.refactoring.TextChange in project xtext-eclipse by eclipse.
the class ChangeConverter method _handleReplacements.
protected void _handleReplacements(ITextDocumentChange change) {
if (!change.getReplacements().isEmpty()) {
IFile file = resourceUriConverter.toFile(change.getOldURI());
if (!canWrite(file)) {
issues.add(RefactoringIssueAcceptor.Severity.FATAL, "Affected file '" + file.getFullPath() + "' is read-only");
}
checkDerived(file);
List<ReplaceEdit> textEdits = change.getReplacements().stream().map(replacement -> {
return new ReplaceEdit(replacement.getOffset(), replacement.getLength(), replacement.getReplacementText());
}).collect(Collectors.toList());
MultiTextEdit textEdit = new MultiTextEdit();
textEdit.addChildren(textEdits.toArray(new TextEdit[textEdits.size()]));
ITextEditor openEditor = findOpenEditor(file);
final TextChange ltkChange;
if (openEditor == null) {
TextFileChange textFileChange = new TextFileChange(change.getOldURI().lastSegment(), file);
textFileChange.setSaveMode(TextFileChange.FORCE_SAVE);
ltkChange = textFileChange;
} else {
ltkChange = new EditorDocumentChange(currentChange.getName(), openEditor, false);
}
ltkChange.setEdit(textEdit);
ltkChange.setTextType(change.getOldURI().fileExtension());
addChange(ltkChange);
}
}
use of org.eclipse.ltk.core.refactoring.TextChange in project che by eclipse.
the class CUCorrectionProposal method getAdditionalProposalInfo.
@Override
public Object getAdditionalProposalInfo(IProgressMonitor monitor) {
StringBuffer buf = new StringBuffer();
try {
TextChange change = getTextChange();
change.setKeepPreviewEdits(true);
IDocument previewDocument = change.getPreviewDocument(monitor);
TextEdit rootEdit = change.getPreviewEdit(change.getEdit());
EditAnnotator ea = new EditAnnotator(buf, previewDocument);
rootEdit.accept(ea);
// Final pre-existing region
ea.unchangedUntil(previewDocument.getLength());
} catch (CoreException e) {
JavaPlugin.log(e);
}
return buf.toString();
}
use of org.eclipse.ltk.core.refactoring.TextChange in project che by eclipse.
the class InlineMethodRefactoring method createChange.
@Override
public Change createChange(IProgressMonitor pm) throws CoreException {
if (fDeleteSource && fCurrentMode == Mode.INLINE_ALL) {
TextChange change = fChangeManager.get((ICompilationUnit) fSourceProvider.getTypeRoot());
TextEdit delete = fSourceProvider.getDeleteEdit();
TextEditGroup description = new TextEditGroup(RefactoringCoreMessages.InlineMethodRefactoring_edit_delete, new TextEdit[] { delete });
TextEdit root = change.getEdit();
if (root != null) {
// TODO instead of finding the right insert position the call inliner should
// reuse the AST & rewriter of the source provide and we should rewrite the
// whole AST at the end. However, since recursive calls aren't allowed there
// shouldn't be a text edit overlap.
// root.addChild(delete);
TextChangeCompatibility.insert(root, delete);
} else {
change.setEdit(delete);
}
change.addTextEditGroup(description);
}
final Map<String, String> arguments = new HashMap<String, String>();
String project = null;
IJavaProject javaProject = fInitialTypeRoot.getJavaProject();
if (javaProject != null)
project = javaProject.getElementName();
int flags = RefactoringDescriptor.STRUCTURAL_CHANGE | JavaRefactoringDescriptor.JAR_REFACTORING | JavaRefactoringDescriptor.JAR_SOURCE_ATTACHMENT;
final IMethodBinding binding = fSourceProvider.getDeclaration().resolveBinding();
final ITypeBinding declaring = binding.getDeclaringClass();
if (!Modifier.isPrivate(binding.getModifiers()))
flags |= RefactoringDescriptor.MULTI_CHANGE;
final String description = Messages.format(RefactoringCoreMessages.InlineMethodRefactoring_descriptor_description_short, BasicElementLabels.getJavaElementName(binding.getName()));
final String header = Messages.format(RefactoringCoreMessages.InlineMethodRefactoring_descriptor_description, new String[] { BindingLabelProvider.getBindingLabel(binding, JavaElementLabels.ALL_FULLY_QUALIFIED), BindingLabelProvider.getBindingLabel(declaring, JavaElementLabels.ALL_FULLY_QUALIFIED) });
final JDTRefactoringDescriptorComment comment = new JDTRefactoringDescriptorComment(project, this, header);
comment.addSetting(Messages.format(RefactoringCoreMessages.InlineMethodRefactoring_original_pattern, BindingLabelProvider.getBindingLabel(binding, JavaElementLabels.ALL_FULLY_QUALIFIED)));
if (fDeleteSource)
comment.addSetting(RefactoringCoreMessages.InlineMethodRefactoring_remove_method);
if (fCurrentMode == Mode.INLINE_ALL)
comment.addSetting(RefactoringCoreMessages.InlineMethodRefactoring_replace_references);
final InlineMethodDescriptor descriptor = RefactoringSignatureDescriptorFactory.createInlineMethodDescriptor(project, description, comment.asString(), arguments, flags);
arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT, JavaRefactoringDescriptorUtil.elementToHandle(project, fInitialTypeRoot));
//$NON-NLS-1$
arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION, new Integer(fSelectionStart).toString() + " " + new Integer(fSelectionLength).toString());
arguments.put(ATTRIBUTE_DELETE, Boolean.valueOf(fDeleteSource).toString());
arguments.put(ATTRIBUTE_MODE, new Integer(fCurrentMode == Mode.INLINE_ALL ? 1 : 0).toString());
return new DynamicValidationRefactoringChange(descriptor, RefactoringCoreMessages.InlineMethodRefactoring_edit_inlineCall, fChangeManager.getAllChanges());
}
use of org.eclipse.ltk.core.refactoring.TextChange in project che by eclipse.
the class RefactoringCorrectionProposal method createTextChange.
@Override
protected TextChange createTextChange() throws CoreException {
init(fRefactoring);
fRefactoringStatus = fRefactoring.checkFinalConditions(new NullProgressMonitor());
if (fRefactoringStatus.hasFatalError()) {
//$NON-NLS-1$
TextFileChange dummyChange = new TextFileChange("fatal error", (IFile) getCompilationUnit().getResource());
//$NON-NLS-1$
dummyChange.setEdit(new InsertEdit(0, ""));
return dummyChange;
}
return (TextChange) fRefactoring.createChange(new NullProgressMonitor());
}
use of org.eclipse.ltk.core.refactoring.TextChange in project che by eclipse.
the class RenameAnalyzeUtil method getCorrespondingEditChangeRange.
private static IRegion getCorrespondingEditChangeRange(SearchMatch searchResult, TextChangeManager manager) {
TextChange change = getTextChange(searchResult, manager);
if (change == null)
return null;
IRegion oldMatchRange = createTextRange(searchResult);
TextEditChangeGroup[] editChanges = change.getTextEditChangeGroups();
for (int i = 0; i < editChanges.length; i++) {
if (oldMatchRange.equals(editChanges[i].getRegion()))
return TextEdit.getCoverage(change.getPreviewEdits(editChanges[i].getTextEdits()));
}
return null;
}
Aggregations