use of org.eclipse.text.edits.ReplaceEdit in project xtext-eclipse by eclipse.
the class TextChangeCombinerTest method createTextFileChange.
protected Change createTextFileChange(IFile file, int offset, int length, String replacement) {
ReplaceEdit edit = new ReplaceEdit(offset, length, replacement);
TextFileChange textFileChange = new TextFileChange("text change", file);
textFileChange.setEdit(edit);
textFileChange.setTextType(TEXT_TYPE);
return textFileChange;
}
use of org.eclipse.text.edits.ReplaceEdit in project xtext-eclipse by eclipse.
the class DefaultRenameElementStrategyTest method testRenameElementStrategy.
@Test
public void testRenameElementStrategy() throws Exception {
final XtextResource resource = getResourceFromString("A { B { C } }");
EObject targetElement = resource.getContents().get(0).eContents().get(0);
assertNotNull(targetElement);
assertTrue(targetElement instanceof Element);
assertEquals("A", ((Element) targetElement).getName());
IRenameStrategy renameElementStrategy = strategyProvider.get(targetElement, null);
assertNotNull(renameElementStrategy);
assertEquals("A", renameElementStrategy.getOriginalName());
RefactoringStatus validateNewNameStatus = renameElementStrategy.validateNewName("A");
assertTrue(validateNewNameStatus.isOK());
validateNewNameStatus = renameElementStrategy.validateNewName("}");
assertTrue(validateNewNameStatus.hasFatalError());
validateNewNameStatus = renameElementStrategy.validateNewName("ref");
assertTrue(validateNewNameStatus.hasError());
renameElementStrategy.applyDeclarationChange("D", resource.getResourceSet());
assertEquals("D", ((Element) targetElement).getName());
renameElementStrategy.createDeclarationUpdates("D", resource.getResourceSet(), this);
assertEquals(0, changes.size());
assertEquals(1, textEdits.size());
assertTrue(textEdits.get(0) instanceof ReplaceEdit);
ReplaceEdit renameEdit = (ReplaceEdit) textEdits.get(0);
assertEquals(0, renameEdit.getOffset());
assertEquals(1, renameEdit.getLength());
assertEquals("D", renameEdit.getText());
}
use of org.eclipse.text.edits.ReplaceEdit in project xtext-eclipse by eclipse.
the class ChangeSerializerTextEditComposer method collectChanges.
protected void collectChanges(IEmfResourceChange e, List<TextEdit> edits) {
if (e instanceof ITextDocumentChange && e.getResource() == recording) {
ITextDocumentChange change = (ITextDocumentChange) e;
for (ITextReplacement c : change.getReplacements()) {
ReplaceEdit edit = new ReplaceEdit(c.getOffset(), c.getLength(), c.getReplacementText());
edits.add(edit);
}
}
}
use of org.eclipse.text.edits.ReplaceEdit in project xtext-eclipse by eclipse.
the class DefaultLinkedPositionGroupCalculator method getLinkedPositionGroup.
@Override
public Provider<LinkedPositionGroup> getLinkedPositionGroup(IRenameElementContext renameElementContext, IProgressMonitor monitor) {
final SubMonitor progress = SubMonitor.convert(monitor, 100);
final XtextEditor editor = (XtextEditor) renameElementContext.getTriggeringEditor();
IProject project = projectUtil.getProject(renameElementContext.getContextResourceURI());
if (project == null)
throw new IllegalStateException("Could not determine project for context resource " + renameElementContext.getContextResourceURI());
RefactoringResourceSetProvider resourceSetProvider = new CachingResourceSetProvider(DefaultLinkedPositionGroupCalculator.this.resourceSetProvider);
ResourceSet resourceSet = resourceSetProvider.get(project);
EObject targetElement = resourceSet.getEObject(renameElementContext.getTargetElementURI(), true);
if (targetElement == null)
throw new IllegalStateException("Target element could not be loaded");
if (monitor.isCanceled()) {
throw new OperationCanceledException();
}
IRenameStrategy.Provider strategyProvider = globalServiceProvider.findService(targetElement, IRenameStrategy.Provider.class);
IRenameStrategy renameStrategy = null;
try {
renameStrategy = strategyProvider.get(targetElement, renameElementContext);
} catch (NoSuchStrategyException exc) {
// handle in next line
}
if (renameStrategy == null)
throw new IllegalArgumentException("Cannot find a rename strategy for " + notNull(renameElementContext.getTargetElementURI()));
String newName = renameStrategy.getOriginalName();
IResourceServiceProvider resourceServiceProvider = resourceServiceProviderRegistry.getResourceServiceProvider(renameElementContext.getTargetElementURI());
IDependentElementsCalculator dependentElementsCalculator = resourceServiceProvider.get(IDependentElementsCalculator.class);
Iterable<URI> dependentElementURIs = dependentElementsCalculator.getDependentElementURIs(targetElement, progress.newChild(10));
if (monitor.isCanceled()) {
throw new OperationCanceledException();
}
LocalResourceRefactoringUpdateAcceptor updateAcceptor = updateAcceptorProvider.get();
updateAcceptor.setLocalResourceURI(renameElementContext.getContextResourceURI());
renameStrategy.createDeclarationUpdates(newName, resourceSet, updateAcceptor);
if (monitor.isCanceled()) {
throw new OperationCanceledException();
}
Map<URI, URI> original2newEObjectURI = renamedElementTracker.renameAndTrack(concat(Collections.singleton(renameElementContext.getTargetElementURI()), dependentElementURIs), newName, resourceSet, renameStrategy, progress.newChild(10));
if (monitor.isCanceled()) {
throw new OperationCanceledException();
}
ElementRenameArguments elementRenameArguments = new ElementRenameArguments(renameElementContext.getTargetElementURI(), newName, renameStrategy, original2newEObjectURI, resourceSetProvider);
final List<IReferenceDescription> referenceDescriptions = newArrayList();
IReferenceFinder.Acceptor referenceAcceptor = new IReferenceFinder.Acceptor() {
@Override
public void accept(IReferenceDescription referenceDescription) {
referenceDescriptions.add(referenceDescription);
}
@Override
public void accept(EObject source, URI sourceURI, EReference eReference, int index, EObject targetOrProxy, URI targetURI) {
referenceDescriptions.add(new DefaultReferenceDescription(EcoreUtil2.getFragmentPathURI(source), targetURI, eReference, index, null));
}
};
if (monitor.isCanceled()) {
throw new OperationCanceledException();
}
referenceFinder.findReferences(targetURIConverter.fromIterable(elementRenameArguments.getRenamedElementURIs()), resourceSet.getResource(renameElementContext.getContextResourceURI(), true), referenceAcceptor, progress.newChild(10));
if (monitor.isCanceled()) {
throw new OperationCanceledException();
}
referenceUpdater.createReferenceUpdates(elementRenameArguments, referenceDescriptions, updateAcceptor, progress.newChild(60));
final List<ReplaceEdit> textEdits = updateAcceptor.getTextEdits();
if (monitor.isCanceled()) {
throw new OperationCanceledException();
}
final IRenameStrategy renameStrategy2 = renameStrategy;
return new Provider<LinkedPositionGroup>() {
@Override
public LinkedPositionGroup get() {
LinkedPositionGroup linkedGroup = createLinkedGroupFromReplaceEdits(textEdits, editor, renameStrategy2.getOriginalName(), progress.newChild(10));
return linkedGroup;
}
};
}
use of org.eclipse.text.edits.ReplaceEdit 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);
}
}
Aggregations