Search in sources :

Example 1 with EditorDocumentChange

use of org.eclipse.xtext.ui.refactoring.impl.EditorDocumentChange in project xtext-xtend by eclipse.

the class ExtractMethodRefactoring method createChange.

@Override
public Change createChange(IProgressMonitor pm) throws CoreException, OperationCanceledException {
    EditorDocumentChange change = new EditorDocumentChange("Extract Method", editor, doSave);
    change.setEdit(textEdit);
    change.setTextType(resourceURI.fileExtension());
    return change;
}
Also used : EditorDocumentChange(org.eclipse.xtext.ui.refactoring.impl.EditorDocumentChange)

Example 2 with EditorDocumentChange

use of org.eclipse.xtext.ui.refactoring.impl.EditorDocumentChange in project xtext-eclipse by eclipse.

the class TextChangeCombiner method createTextChange.

protected TextChange createTextChange(Object key, String textType) {
    TextChange change = null;
    if (key instanceof ICompilationUnit)
        change = new CompilationUnitChange("Combined CompilationUnitChange", (ICompilationUnit) key);
    else if (key instanceof IFile)
        change = new TextFileChange("Combined TextFileChange", (IFile) key);
    else if (key instanceof ITextEditor)
        change = new EditorDocumentChange("Combined DocumentChange", (ITextEditor) key, false);
    else
        LOG.error("Error creating change for " + key.getClass().getName());
    if (change != null) {
        MultiTextEdit edits = new MultiTextEdit();
        change.setEdit(edits);
        change.setTextType(textType);
    }
    return change;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IFile(org.eclipse.core.resources.IFile) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) TextChange(org.eclipse.ltk.core.refactoring.TextChange) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) EditorDocumentChange(org.eclipse.xtext.ui.refactoring.impl.EditorDocumentChange) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange)

Example 3 with EditorDocumentChange

use of org.eclipse.xtext.ui.refactoring.impl.EditorDocumentChange in project xtext-eclipse by eclipse.

the class TextChangeCombinerTest method createMultiEditorDocumentChange.

protected Change createMultiEditorDocumentChange(ITextEditor editor, int offset0, int length0, String replacement0, int offset1, int length1, String replacement1) {
    MultiTextEdit multiTextEdit = createMultiTextEdit(offset0, length0, replacement0, offset1, length1, replacement1);
    EditorDocumentChange documentChange = new EditorDocumentChange("document change", editor, false);
    documentChange.setEdit(multiTextEdit);
    documentChange.setTextType(TEXT_TYPE);
    return documentChange;
}
Also used : MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) EditorDocumentChange(org.eclipse.xtext.ui.refactoring.impl.EditorDocumentChange)

Example 4 with EditorDocumentChange

use of org.eclipse.xtext.ui.refactoring.impl.EditorDocumentChange 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 5 with EditorDocumentChange

use of org.eclipse.xtext.ui.refactoring.impl.EditorDocumentChange in project xtext-eclipse by eclipse.

the class MultiImportOrganizer method organizeImports.

/**
 * @param files
 *            ordered by corresponding {@link IProject}
 * @param mon
 * @return Creates {@link Change}s for each {@link IFile} using {@link ImportOrganizer}
 * @throws InterruptedException
 */
public List<Change> organizeImports(Multimap<IProject, IFile> files, IProgressMonitor mon) throws InterruptedException {
    List<Change> result = Lists.newArrayList();
    for (IProject project : files.keySet()) {
        ResourceSet resSet = resSetProvider.get(project);
        for (IFile file : files.get(project)) {
            mon.subTask("Calculating imports in - " + file.getName());
            URI uri = storageMapper.getUri(file);
            if (uri != null) {
                XtextResource resource = (XtextResource) resSet.getResource(uri, true);
                List<ReplaceRegion> replace = importOrganizer.getOrganizedImportChanges(resource);
                // TODO - find out why \n\n changes are produced, even if there are any unused imports
                if (replace == null || replace.isEmpty()) {
                    continue;
                }
                TextEdit textEdit = replaceConverter.convertToTextEdit(replace);
                IRefactoringDocument iRefactoringDocument = provider.get(uri, status);
                if (iRefactoringDocument != null) {
                    Change change = iRefactoringDocument.createChange(Messages.OrganizeImports, textEdit);
                    if (change instanceof EditorDocumentChange) {
                        if (((EditorDocument) iRefactoringDocument).getEditor().isDirty()) {
                            ((EditorDocumentChange) change).setDoSave(false);
                        }
                    }
                    result.add(change);
                }
            }
            mon.worked(1);
            if (mon.isCanceled()) {
                throw new InterruptedException();
            }
        }
    }
    return result;
}
Also used : IFile(org.eclipse.core.resources.IFile) IRefactoringDocument(org.eclipse.xtext.ui.refactoring.impl.IRefactoringDocument) XtextResource(org.eclipse.xtext.resource.XtextResource) Change(org.eclipse.ltk.core.refactoring.Change) EditorDocumentChange(org.eclipse.xtext.ui.refactoring.impl.EditorDocumentChange) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) URI(org.eclipse.emf.common.util.URI) IProject(org.eclipse.core.resources.IProject) EditorDocumentChange(org.eclipse.xtext.ui.refactoring.impl.EditorDocumentChange) ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion) TextEdit(org.eclipse.text.edits.TextEdit)

Aggregations

EditorDocumentChange (org.eclipse.xtext.ui.refactoring.impl.EditorDocumentChange)12 Change (org.eclipse.ltk.core.refactoring.Change)6 TextFileChange (org.eclipse.ltk.core.refactoring.TextFileChange)6 CompositeChange (org.eclipse.ltk.core.refactoring.CompositeChange)4 TextChange (org.eclipse.ltk.core.refactoring.TextChange)4 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)4 TextEdit (org.eclipse.text.edits.TextEdit)4 IFile (org.eclipse.core.resources.IFile)3 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)3 IRefactoringDocument (org.eclipse.xtext.ui.refactoring.impl.IRefactoringDocument)3 Test (org.junit.Test)3 CompilationUnitChange (org.eclipse.jdt.core.refactoring.CompilationUnitChange)2 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)2 IEditorPart (org.eclipse.ui.IEditorPart)2 XtextEditor (org.eclipse.xtext.ui.editor.XtextEditor)2 IXtextDocument (org.eclipse.xtext.ui.editor.model.IXtextDocument)2 EditorDocument (org.eclipse.xtext.ui.refactoring.impl.DefaultRefactoringDocumentProvider.EditorDocument)2 AbstractEditorTest (org.eclipse.xtext.ui.testing.AbstractEditorTest)2 Predicate (com.google.common.base.Predicate)1 Inject (com.google.inject.Inject)1