Search in sources :

Example 31 with CompilationUnitChange

use of org.eclipse.jdt.core.refactoring.CompilationUnitChange in project che by eclipse.

the class UnimplementedCodeFix method createAddUnimplementedMethodsFix.

public static IProposableFix createAddUnimplementedMethodsFix(final CompilationUnit root, IProblemLocation problem) {
    ASTNode typeNode = getSelectedTypeNode(root, problem);
    if (typeNode == null)
        return null;
    if (isTypeBindingNull(typeNode))
        return null;
    AddUnimplementedMethodsOperation operation = new AddUnimplementedMethodsOperation(typeNode);
    if (operation.getMethodsToImplement().length > 0) {
        return new UnimplementedCodeFix(CorrectionMessages.UnimplementedMethodsCorrectionProposal_description, root, new CompilationUnitRewriteOperation[] { operation });
    } else {
        return new IProposableFix() {

            public CompilationUnitChange createChange(IProgressMonitor progressMonitor) throws CoreException {
                CompilationUnitChange change = new CompilationUnitChange(CorrectionMessages.UnimplementedMethodsCorrectionProposal_description, (ICompilationUnit) root.getJavaElement()) {

                    @Override
                    public Change perform(IProgressMonitor pm) throws CoreException {
                        //TODO
                        return new NullChange();
                    }
                };
                change.setEdit(new MultiTextEdit());
                return change;
            }

            public String getAdditionalProposalInfo() {
                return new String();
            }

            public String getDisplayString() {
                return CorrectionMessages.UnimplementedMethodsCorrectionProposal_description;
            }

            public IStatus getStatus() {
                return new Status(IStatus.ERROR, JavaPlugin.ID_PLUGIN, CorrectionMessages.UnimplementedCodeFix_DependenciesStatusMessage);
            }
        };
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) NullChange(org.eclipse.ltk.core.refactoring.NullChange) ASTNode(org.eclipse.jdt.core.dom.ASTNode) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange)

Example 32 with CompilationUnitChange

use of org.eclipse.jdt.core.refactoring.CompilationUnitChange in project che by eclipse.

the class InlineTempRefactoring method createChange.

//----- changes
@Override
public Change createChange(IProgressMonitor pm) throws CoreException {
    try {
        pm.beginTask(RefactoringCoreMessages.InlineTempRefactoring_preview, 2);
        final Map<String, String> arguments = new HashMap<String, String>();
        String project = null;
        IJavaProject javaProject = fCu.getJavaProject();
        if (javaProject != null)
            project = javaProject.getElementName();
        final IVariableBinding binding = getVariableDeclaration().resolveBinding();
        String text = null;
        final IMethodBinding method = binding.getDeclaringMethod();
        if (method != null)
            text = BindingLabelProvider.getBindingLabel(method, JavaElementLabels.ALL_FULLY_QUALIFIED);
        else
            text = BasicElementLabels.getJavaElementName('{' + JavaElementLabels.ELLIPSIS_STRING + '}');
        final String description = Messages.format(RefactoringCoreMessages.InlineTempRefactoring_descriptor_description_short, BasicElementLabels.getJavaElementName(binding.getName()));
        final String header = Messages.format(RefactoringCoreMessages.InlineTempRefactoring_descriptor_description, new String[] { BindingLabelProvider.getBindingLabel(binding, JavaElementLabels.ALL_FULLY_QUALIFIED), text });
        final JDTRefactoringDescriptorComment comment = new JDTRefactoringDescriptorComment(project, this, header);
        comment.addSetting(Messages.format(RefactoringCoreMessages.InlineTempRefactoring_original_pattern, BindingLabelProvider.getBindingLabel(binding, JavaElementLabels.ALL_FULLY_QUALIFIED)));
        final InlineLocalVariableDescriptor descriptor = RefactoringSignatureDescriptorFactory.createInlineLocalVariableDescriptor(project, description, comment.asString(), arguments, RefactoringDescriptor.NONE);
        arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT, JavaRefactoringDescriptorUtil.elementToHandle(project, fCu));
        arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION, String.valueOf(fSelectionStart) + ' ' + String.valueOf(fSelectionLength));
        CompilationUnitRewrite cuRewrite = new CompilationUnitRewrite(fCu, fASTRoot);
        inlineTemp(cuRewrite);
        removeTemp(cuRewrite);
        final CompilationUnitChange result = cuRewrite.createChange(RefactoringCoreMessages.InlineTempRefactoring_inline, false, new SubProgressMonitor(pm, 1));
        result.setDescriptor(new RefactoringChangeDescriptor(descriptor));
        return result;
    } finally {
        pm.done();
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) CompilationUnitRewrite(org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite) IJavaProject(org.eclipse.jdt.core.IJavaProject) HashMap(java.util.HashMap) InlineLocalVariableDescriptor(org.eclipse.jdt.core.refactoring.descriptors.InlineLocalVariableDescriptor) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) JDTRefactoringDescriptorComment(org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange) RefactoringChangeDescriptor(org.eclipse.ltk.core.refactoring.RefactoringChangeDescriptor)

Example 33 with CompilationUnitChange

use of org.eclipse.jdt.core.refactoring.CompilationUnitChange in project che by eclipse.

the class RenameLocalVariableProcessor method createEdits.

private void createEdits() {
    TextEdit declarationEdit = createRenameEdit(fTempDeclarationNode.getName().getStartPosition());
    TextEdit[] allRenameEdits = getAllRenameEdits(declarationEdit);
    TextEdit[] allUnparentedRenameEdits = new TextEdit[allRenameEdits.length];
    TextEdit unparentedDeclarationEdit = null;
    fChange = new CompilationUnitChange(RefactoringCoreMessages.RenameTempRefactoring_rename, fCu);
    MultiTextEdit rootEdit = new MultiTextEdit();
    fChange.setEdit(rootEdit);
    fChange.setKeepPreviewEdits(true);
    for (int i = 0; i < allRenameEdits.length; i++) {
        if (fIsComposite) {
            // Add a copy of the text edit (text edit may only have one
            // parent) to keep problem reporting code clean
            TextChangeCompatibility.addTextEdit(fChangeManager.get(fCu), RefactoringCoreMessages.RenameTempRefactoring_changeName, allRenameEdits[i].copy(), fCategorySet);
            // Add a separate copy for problem reporting
            allUnparentedRenameEdits[i] = allRenameEdits[i].copy();
            if (allRenameEdits[i].equals(declarationEdit))
                unparentedDeclarationEdit = allUnparentedRenameEdits[i];
        }
        rootEdit.addChild(allRenameEdits[i]);
        fChange.addTextEditGroup(new TextEditGroup(RefactoringCoreMessages.RenameTempRefactoring_changeName, allRenameEdits[i]));
    }
    // store information for analysis
    if (fIsComposite) {
        fLocalAnalyzePackage = new RenameAnalyzeUtil.LocalAnalyzePackage(unparentedDeclarationEdit, allUnparentedRenameEdits);
    } else
        fLocalAnalyzePackage = new RenameAnalyzeUtil.LocalAnalyzePackage(declarationEdit, allRenameEdits);
}
Also used : LocalAnalyzePackage(org.eclipse.jdt.internal.corext.refactoring.rename.RenameAnalyzeUtil.LocalAnalyzePackage) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) LocalAnalyzePackage(org.eclipse.jdt.internal.corext.refactoring.rename.RenameAnalyzeUtil.LocalAnalyzePackage) TextEditGroup(org.eclipse.text.edits.TextEditGroup) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange)

Example 34 with CompilationUnitChange

use of org.eclipse.jdt.core.refactoring.CompilationUnitChange in project che by eclipse.

the class UnresolvedElementsSubProcessor method createAddImportChange.

//	private static void addCopyAnnotationsJarProposal(final ICompilationUnit cu, final Name name, final String fullyQualifiedName, Bundle annotationsBundle, Collection<ICommandAccess> proposals) {
//		final IJavaProject javaProject= cu.getJavaProject();
//		final File bundleFile;
//		try {
//			bundleFile= FileLocator.getBundleFile(annotationsBundle);
//		} catch (IOException e) {
//			JavaPlugin.log(e);
//			return;
//		}
//		if (!bundleFile.isFile() || !bundleFile.canRead())
//			return; // we only support a JAR'd bundle, so this won't work in the runtime if you have org.eclipse.jdt.annotation in source.
//
//		final String changeName= CorrectionMessages.UnresolvedElementsSubProcessor_copy_annotation_jar_description;
//		ChangeCorrectionProposal proposal= new ChangeCorrectionProposal(changeName, null, IProposalRelevance.COPY_ANNOTATION_JAR) {
//			@Override
//			protected Change createChange() throws CoreException {
//				final IFile file= javaProject.getProject().getFile(bundleFile.getName());
//				ResourceChange copyFileChange= new ResourceChange() {
//					@Override
//					public Change perform(IProgressMonitor pm) throws CoreException {
//						try {
//							if (file.exists())
//								file.delete(false, pm);
//							file.create(new BufferedInputStream(new FileInputStream(bundleFile)), false, pm);
//							return new DeleteResourceChange(file.getFullPath(), false);
//						} catch (FileNotFoundException e) {
//							throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), e.getMessage()));
//						}
//					}
//					@Override
//					public String getName() {
//						return changeName;
//					}
//					@Override
//					protected IResource getModifiedResource() {
//						return javaProject.getProject();
//					}
//				};
//				ClasspathChange addEntryChange= ClasspathChange.addEntryChange(javaProject, JavaCore
//						.newLibraryEntry(file.getFullPath(), null, null));
//				CompilationUnitChange addImportChange= createAddImportChange(cu, name, fullyQualifiedName);
//				return new CompositeChange(changeName, new Change[] { copyFileChange, addEntryChange, addImportChange});
//			}
//
//			@Override
//			public Object getAdditionalProposalInfo(IProgressMonitor monitor) {
//				return CorrectionMessages.UnresolvedElementsSubProcessor_copy_annotation_jar_info;
//			}
//		};
//		proposals.add(proposal);
//	}
static CompilationUnitChange createAddImportChange(ICompilationUnit cu, Name name, String fullyQualifiedName) throws CoreException {
    String[] args = { BasicElementLabels.getJavaElementName(Signature.getSimpleName(fullyQualifiedName)), BasicElementLabels.getJavaElementName(Signature.getQualifier(fullyQualifiedName)) };
    String label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_importtype_description, args);
    CompilationUnitChange cuChange = new CompilationUnitChange(label, cu);
    ImportRewrite importRewrite = StubUtility.createImportRewrite((CompilationUnit) name.getRoot(), true);
    importRewrite.addImport(fullyQualifiedName);
    cuChange.setEdit(importRewrite.rewriteImports(null));
    return cuChange;
}
Also used : ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange)

Example 35 with CompilationUnitChange

use of org.eclipse.jdt.core.refactoring.CompilationUnitChange in project eclipse.jdt.ls by eclipse.

the class CompilationUnitRewriteOperationsFix method createChange.

@Override
public CompilationUnitChange createChange(IProgressMonitor progressMonitor) throws CoreException {
    CompilationUnitRewrite cuRewrite = new CompilationUnitRewrite((ICompilationUnit) fCompilationUnit.getJavaElement(), fCompilationUnit);
    fLinkedProposalModel.clear();
    for (int i = 0; i < fOperations.length; i++) {
        CompilationUnitRewriteOperation operation = fOperations[i];
        operation.rewriteAST(cuRewrite, fLinkedProposalModel);
    }
    CompilationUnitChange result = cuRewrite.createChange(getDisplayString(), true, null);
    if (result == null) {
        throw new CoreException(new Status(IStatus.ERROR, JavaLanguageServerPlugin.PLUGIN_ID, Messages.format(FixMessages.CompilationUnitRewriteOperationsFix_nullChangeError, getDisplayString())));
    }
    return result;
}
Also used : CompilationUnitRewrite(org.eclipse.jdt.ls.core.internal.corext.refactoring.structure.CompilationUnitRewrite) Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) CoreException(org.eclipse.core.runtime.CoreException) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange)

Aggregations

CompilationUnitChange (org.eclipse.jdt.core.refactoring.CompilationUnitChange)45 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)14 TextEdit (org.eclipse.text.edits.TextEdit)14 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)13 CoreException (org.eclipse.core.runtime.CoreException)9 CompilationUnitRewrite (org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite)8 TextEditGroup (org.eclipse.text.edits.TextEditGroup)8 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)5 ASTNode (org.eclipse.jdt.core.dom.ASTNode)5 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)5 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)4 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)4 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)4 ImportRewrite (org.eclipse.jdt.core.dom.rewrite.ImportRewrite)4 IDocument (org.eclipse.jface.text.IDocument)4 TextChange (org.eclipse.ltk.core.refactoring.TextChange)4 HashMap (java.util.HashMap)3 IFile (org.eclipse.core.resources.IFile)3 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)3 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)3