Search in sources :

Example 26 with TextChange

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);
    }
}
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)

Example 27 with TextChange

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();
}
Also used : EditAnnotator(org.eclipse.jdt.internal.ui.text.correction.proposals.EditAnnotator) CoreException(org.eclipse.core.runtime.CoreException) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) TextChange(org.eclipse.ltk.core.refactoring.TextChange) IDocument(org.eclipse.jface.text.IDocument)

Example 28 with TextChange

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());
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) HashMap(java.util.HashMap) DynamicValidationRefactoringChange(org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationRefactoringChange) TextChange(org.eclipse.ltk.core.refactoring.TextChange) JDTRefactoringDescriptorComment(org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment) IJavaProject(org.eclipse.jdt.core.IJavaProject) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) InlineMethodDescriptor(org.eclipse.jdt.core.refactoring.descriptors.InlineMethodDescriptor) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 29 with TextChange

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

Example 30 with TextChange

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;
}
Also used : TextEditChangeGroup(org.eclipse.ltk.core.refactoring.TextEditChangeGroup) TextChange(org.eclipse.ltk.core.refactoring.TextChange) IRegion(org.eclipse.jface.text.IRegion)

Aggregations

TextChange (org.eclipse.ltk.core.refactoring.TextChange)44 TextEdit (org.eclipse.text.edits.TextEdit)14 TextFileChange (org.eclipse.ltk.core.refactoring.TextFileChange)13 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)13 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)10 CompositeChange (org.eclipse.ltk.core.refactoring.CompositeChange)10 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)8 IFile (org.eclipse.core.resources.IFile)7 IDocument (org.eclipse.jface.text.IDocument)7 ArrayList (java.util.ArrayList)6 JavaModelException (org.eclipse.jdt.core.JavaModelException)6 CompilationUnitChange (org.eclipse.jdt.core.refactoring.CompilationUnitChange)6 Change (org.eclipse.ltk.core.refactoring.Change)6 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)6 Iterator (java.util.Iterator)5 DynamicValidationRefactoringChange (org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationRefactoringChange)5 HashMap (java.util.HashMap)4 IJavaProject (org.eclipse.jdt.core.IJavaProject)4 EditorDocumentChange (org.eclipse.xtext.ui.refactoring.impl.EditorDocumentChange)4 IOException (java.io.IOException)3