Search in sources :

Example 61 with ReplaceEdit

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;
}
Also used : ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange)

Example 62 with ReplaceEdit

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());
}
Also used : IRenameStrategy(org.eclipse.xtext.ui.refactoring.IRenameStrategy) EObject(org.eclipse.emf.ecore.EObject) Element(org.eclipse.xtext.ui.tests.refactoring.refactoring.Element) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) XtextResource(org.eclipse.xtext.resource.XtextResource) Test(org.junit.Test)

Example 63 with ReplaceEdit

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);
        }
    }
}
Also used : ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) ITextDocumentChange(org.eclipse.xtext.ide.serializer.ITextDocumentChange)

Example 64 with ReplaceEdit

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;
        }
    };
}
Also used : IRenameStrategy(org.eclipse.xtext.ui.refactoring.IRenameStrategy) ElementRenameArguments(org.eclipse.xtext.ui.refactoring.ElementRenameArguments) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) URI(org.eclipse.emf.common.util.URI) IResourceServiceProvider(org.eclipse.xtext.resource.IResourceServiceProvider) EObject(org.eclipse.emf.ecore.EObject) IDependentElementsCalculator(org.eclipse.xtext.ui.refactoring.IDependentElementsCalculator) DefaultReferenceDescription(org.eclipse.xtext.resource.impl.DefaultReferenceDescription) CachingResourceSetProvider(org.eclipse.xtext.ui.refactoring.impl.CachingResourceSetProvider) NoSuchStrategyException(org.eclipse.xtext.ui.refactoring.IRenameStrategy.Provider.NoSuchStrategyException) EReference(org.eclipse.emf.ecore.EReference) IReferenceDescription(org.eclipse.xtext.resource.IReferenceDescription) IRefactoringUpdateAcceptor(org.eclipse.xtext.ui.refactoring.IRefactoringUpdateAcceptor) XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) SubMonitor(org.eclipse.core.runtime.SubMonitor) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) IProject(org.eclipse.core.resources.IProject) CachingResourceSetProvider(org.eclipse.xtext.ui.refactoring.impl.CachingResourceSetProvider) IGlobalServiceProvider(org.eclipse.xtext.resource.IGlobalServiceProvider) RefactoringResourceSetProvider(org.eclipse.xtext.ui.refactoring.impl.RefactoringResourceSetProvider) IResourceServiceProvider(org.eclipse.xtext.resource.IResourceServiceProvider) Provider(com.google.inject.Provider) LinkedPositionGroup(org.eclipse.jface.text.link.LinkedPositionGroup) IReferenceFinder(org.eclipse.xtext.findReferences.IReferenceFinder) RefactoringResourceSetProvider(org.eclipse.xtext.ui.refactoring.impl.RefactoringResourceSetProvider) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit)

Example 65 with ReplaceEdit

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);
    }
}
Also used : RefactoringIssueAcceptor(org.eclipse.xtext.ide.refactoring.RefactoringIssueAcceptor) Arrays(java.util.Arrays) IEmfResourceChange(org.eclipse.xtext.ide.serializer.IEmfResourceChange) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IAcceptor(org.eclipse.xtext.util.IAcceptor) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) Inject(com.google.inject.Inject) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange) IContainer(org.eclipse.core.resources.IContainer) Logger(org.apache.log4j.Logger) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) IWorkspace(org.eclipse.core.resources.IWorkspace) TextChange(org.eclipse.ltk.core.refactoring.TextChange) IFile(org.eclipse.core.resources.IFile) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) Change(org.eclipse.ltk.core.refactoring.Change) ITextDocumentChange(org.eclipse.xtext.ide.serializer.ITextDocumentChange) RenameResourceChange(org.eclipse.ltk.core.refactoring.resource.RenameResourceChange) IEditorPart(org.eclipse.ui.IEditorPart) FileEditorInput(org.eclipse.ui.part.FileEditorInput) Exceptions(org.eclipse.xtext.util.Exceptions) TextEdit(org.eclipse.text.edits.TextEdit) IOException(java.io.IOException) DisplayRunnableWithResult(org.eclipse.xtext.ui.util.DisplayRunnableWithResult) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) List(java.util.List) Predicate(com.google.common.base.Predicate) MoveResourceChange(org.eclipse.ltk.core.refactoring.resource.MoveResourceChange) IWorkbench(org.eclipse.ui.IWorkbench) EditorDocumentChange(org.eclipse.xtext.ui.refactoring.impl.EditorDocumentChange) Nullable(org.eclipse.xtext.util.internal.Nullable) IFile(org.eclipse.core.resources.IFile) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) TextChange(org.eclipse.ltk.core.refactoring.TextChange) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) EditorDocumentChange(org.eclipse.xtext.ui.refactoring.impl.EditorDocumentChange)

Aggregations

ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)131 TextEdit (org.eclipse.text.edits.TextEdit)53 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)43 Test (org.junit.Test)34 InsertEdit (org.eclipse.text.edits.InsertEdit)25 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)22 MalformedTreeException (org.eclipse.text.edits.MalformedTreeException)15 BadLocationException (org.eclipse.jface.text.BadLocationException)14 IDocument (org.eclipse.jface.text.IDocument)14 DeleteEdit (org.eclipse.text.edits.DeleteEdit)13 MoveSourceEdit (org.eclipse.text.edits.MoveSourceEdit)13 MoveTargetEdit (org.eclipse.text.edits.MoveTargetEdit)13 UndoEdit (org.eclipse.text.edits.UndoEdit)13 TextFileChange (org.eclipse.ltk.core.refactoring.TextFileChange)11 SearchMatch (org.eclipse.jdt.core.search.SearchMatch)10 ArrayList (java.util.ArrayList)9 SearchResultGroup (org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup)8 TextChange (org.eclipse.ltk.core.refactoring.TextChange)8 List (java.util.List)7 Document (org.eclipse.jface.text.Document)7