Search in sources :

Example 1 with RenameCompilationUnitChange

use of org.eclipse.jdt.ls.core.internal.corext.refactoring.changes.RenameCompilationUnitChange in project eclipse.jdt.ls by eclipse.

the class RenameCompilationUnitProcessor method createChange.

@Override
public Change createChange(IProgressMonitor pm) throws CoreException {
    // renaming the file is taken care of in renameTypeRefactoring
    if (fWillRenameType) {
        return fRenameTypeProcessor.createChange(pm);
    }
    fRenameTypeProcessor = null;
    final String newName = getNewElementName();
    final IResource resource = fCu.getResource();
    if (resource != null && resource.isLinked()) {
        final IProject project = resource.getProject();
        final String name = project.getName();
        final String description = Messages.format(RefactoringCoreMessages.RenameCompilationUnitChange_descriptor_description_short, BasicElementLabels.getResourceName(resource.getName()));
        final String header = Messages.format(RefactoringCoreMessages.RenameCompilationUnitChange_descriptor_description, new String[] { BasicElementLabels.getPathLabel(resource.getFullPath(), false), BasicElementLabels.getResourceName(resource.getName()) });
        final String comment = new JDTRefactoringDescriptorComment(name, this, header).asString();
        final int flags = RefactoringDescriptor.STRUCTURAL_CHANGE;
        final RenameResourceDescriptor descriptor = (RenameResourceDescriptor) RefactoringCore.getRefactoringContribution(RenameResourceDescriptor.ID).createDescriptor();
        descriptor.setProject(name);
        descriptor.setDescription(description);
        descriptor.setComment(comment);
        descriptor.setFlags(flags);
        descriptor.setResourcePath(resource.getFullPath());
        descriptor.setNewName(newName);
        RenameResourceChange resourceChange = new RenameResourceChange(resource.getFullPath(), newName);
        resourceChange.setDescriptor(new RefactoringChangeDescriptor(descriptor));
        return new DynamicValidationStateChange(resourceChange);
    }
    String label = JavaElementLabels.getTextLabel(fCu, JavaElementLabels.ALL_FULLY_QUALIFIED);
    final String name = fCu.getJavaProject().getElementName();
    final String description = Messages.format(RefactoringCoreMessages.RenameCompilationUnitChange_descriptor_description_short, BasicElementLabels.getFileName(fCu));
    final String header = Messages.format(RefactoringCoreMessages.RenameCompilationUnitChange_descriptor_description, new String[] { label, BasicElementLabels.getResourceName(newName) });
    final String comment = new JDTRefactoringDescriptorComment(name, this, header).asString();
    final int flags = RefactoringDescriptor.STRUCTURAL_CHANGE;
    final RenameJavaElementDescriptor descriptor = RefactoringSignatureDescriptorFactory.createRenameJavaElementDescriptor(IJavaRefactorings.RENAME_COMPILATION_UNIT);
    descriptor.setProject(name);
    descriptor.setDescription(description);
    descriptor.setComment(comment);
    descriptor.setFlags(flags);
    descriptor.setJavaElement(fCu);
    descriptor.setNewName(newName);
    return new DynamicValidationRefactoringChange(descriptor, RefactoringCoreMessages.RenameCompilationUnitRefactoring_name, new Change[] { new RenameCompilationUnitChange(fCu, newName) });
}
Also used : RenameResourceChange(org.eclipse.ltk.core.refactoring.resource.RenameResourceChange) DynamicValidationRefactoringChange(org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationRefactoringChange) RenameResourceDescriptor(org.eclipse.ltk.core.refactoring.resource.RenameResourceDescriptor) RenameJavaElementDescriptor(org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor) IResource(org.eclipse.core.resources.IResource) IProject(org.eclipse.core.resources.IProject) DynamicValidationStateChange(org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationStateChange) JDTRefactoringDescriptorComment(org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment) RefactoringChangeDescriptor(org.eclipse.ltk.core.refactoring.RefactoringChangeDescriptor) RenameCompilationUnitChange(org.eclipse.jdt.ls.core.internal.corext.refactoring.changes.RenameCompilationUnitChange)

Example 2 with RenameCompilationUnitChange

use of org.eclipse.jdt.ls.core.internal.corext.refactoring.changes.RenameCompilationUnitChange in project eclipse.jdt.ls by eclipse.

the class ChangeUtilTest method testConvertRenameCompilationUnitChange.

// Resource Changes
@Test
public void testConvertRenameCompilationUnitChange() throws CoreException {
    IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", "", false, null);
    String newName = "ENew.java";
    RenameCompilationUnitChange change = new RenameCompilationUnitChange(cu, newName);
    String oldUri = JDTUtils.toURI(cu);
    String newUri = ResourceUtils.fixURI(URI.create(oldUri).resolve(newName));
    WorkspaceEdit edit = ChangeUtil.convertToWorkspaceEdit(change);
    assertEquals(edit.getDocumentChanges().size(), 1);
    ResourceOperation resourceOperation = edit.getDocumentChanges().get(0).getRight();
    assertTrue(resourceOperation instanceof RenameFile);
    assertEquals(((RenameFile) resourceOperation).getOldUri(), oldUri);
    assertEquals(((RenameFile) resourceOperation).getNewUri(), newUri);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) RenameFile(org.eclipse.lsp4j.RenameFile) RenameCompilationUnitChange(org.eclipse.jdt.ls.core.internal.corext.refactoring.changes.RenameCompilationUnitChange) ResourceOperation(org.eclipse.lsp4j.ResourceOperation) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Example 3 with RenameCompilationUnitChange

use of org.eclipse.jdt.ls.core.internal.corext.refactoring.changes.RenameCompilationUnitChange in project eclipse.jdt.ls by eclipse.

the class RenameTypeProcessor method createChange.

@Override
public Change createChange(IProgressMonitor monitor) throws CoreException {
    try {
        monitor.beginTask(RefactoringCoreMessages.RenameTypeRefactoring_creating_change, 4);
        String project = null;
        IJavaProject javaProject = fType.getJavaProject();
        if (javaProject != null) {
            project = javaProject.getElementName();
        }
        int flags = JavaRefactoringDescriptor.JAR_MIGRATION | JavaRefactoringDescriptor.JAR_REFACTORING | RefactoringDescriptor.STRUCTURAL_CHANGE;
        try {
            if (!Flags.isPrivate(fType.getFlags())) {
                flags |= RefactoringDescriptor.MULTI_CHANGE;
            }
            if (fType.isAnonymous() || fType.isLocal()) {
                flags |= JavaRefactoringDescriptor.JAR_SOURCE_ATTACHMENT;
            }
        } catch (JavaModelException exception) {
            JavaLanguageServerPlugin.log(exception);
        }
        final String description = Messages.format(RefactoringCoreMessages.RenameTypeProcessor_descriptor_description_short, BasicElementLabels.getJavaElementName(fType.getElementName()));
        final String header = Messages.format(RefactoringCoreMessages.RenameTypeProcessor_descriptor_description, new String[] { JavaElementLabels.getElementLabel(fType, JavaElementLabels.ALL_FULLY_QUALIFIED), getNewElementLabel() });
        final String comment = new JDTRefactoringDescriptorComment(project, this, header).asString();
        final RenameJavaElementDescriptor descriptor = RefactoringSignatureDescriptorFactory.createRenameJavaElementDescriptor(IJavaRefactorings.RENAME_TYPE);
        descriptor.setProject(project);
        descriptor.setDescription(description);
        descriptor.setComment(comment);
        descriptor.setFlags(flags);
        descriptor.setJavaElement(fType);
        descriptor.setNewName(getNewElementName());
        descriptor.setUpdateQualifiedNames(fUpdateQualifiedNames);
        descriptor.setUpdateTextualOccurrences(fUpdateTextualMatches);
        descriptor.setUpdateReferences(fUpdateReferences);
        if (fUpdateQualifiedNames && fFilePatterns != null && !"".equals(fFilePatterns)) {
            descriptor.setFileNamePatterns(fFilePatterns);
        }
        descriptor.setUpdateSimilarDeclarations(fUpdateSimilarElements);
        descriptor.setMatchStrategy(fRenamingStrategy);
        final DynamicValidationRefactoringChange result = new DynamicValidationRefactoringChange(descriptor, RefactoringCoreMessages.RenameTypeProcessor_change_name);
        if (fChangeManager.containsChangesIn(fType.getCompilationUnit())) {
            TextChange textChange = fChangeManager.get(fType.getCompilationUnit());
            if (textChange instanceof TextFileChange) {
                ((TextFileChange) textChange).setSaveMode(TextFileChange.FORCE_SAVE);
            }
        }
        if (willRenameCU()) {
            IResource resource = fType.getCompilationUnit().getResource();
            if (resource != null && resource.isLinked()) {
                result.addAll(fChangeManager.getAllChanges());
                String ext = resource.getFileExtension();
                String renamedResourceName;
                if (ext == null) {
                    renamedResourceName = getNewElementName();
                } else {
                    renamedResourceName = getNewElementName() + '.' + ext;
                }
                result.add(new RenameResourceChange(fType.getCompilationUnit().getPath(), renamedResourceName));
            } else {
                addTypeDeclarationUpdate(fChangeManager);
                addConstructorRenames(fChangeManager);
                result.addAll(fChangeManager.getAllChanges());
                String renamedCUName = JavaModelUtil.getRenamedCUName(fType.getCompilationUnit(), getNewElementName());
                result.add(new RenameCompilationUnitChange(fType.getCompilationUnit(), renamedCUName));
            }
        } else {
            result.addAll(fChangeManager.getAllChanges());
        }
        monitor.worked(1);
        return result;
    } finally {
        fChangeManager = null;
    }
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IJavaProject(org.eclipse.jdt.core.IJavaProject) DynamicValidationRefactoringChange(org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationRefactoringChange) RenameResourceChange(org.eclipse.ltk.core.refactoring.resource.RenameResourceChange) TextChange(org.eclipse.ltk.core.refactoring.TextChange) RenameJavaElementDescriptor(org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) IResource(org.eclipse.core.resources.IResource) JDTRefactoringDescriptorComment(org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment) RenameCompilationUnitChange(org.eclipse.jdt.ls.core.internal.corext.refactoring.changes.RenameCompilationUnitChange)

Example 4 with RenameCompilationUnitChange

use of org.eclipse.jdt.ls.core.internal.corext.refactoring.changes.RenameCompilationUnitChange in project eclipse.jdt.ls by eclipse.

the class ReorgCorrectionsSubProcessor method getWrongTypeNameProposals.

public static void getWrongTypeNameProposals(IInvocationContext context, IProblemLocationCore problem, Collection<ChangeCorrectionProposal> proposals) {
    ICompilationUnit cu = context.getCompilationUnit();
    boolean isLinked = cu.getResource().isLinked();
    IJavaProject javaProject = cu.getJavaProject();
    String sourceLevel = javaProject.getOption(JavaCore.COMPILER_SOURCE, true);
    String compliance = javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true);
    CompilationUnit root = context.getASTRoot();
    ASTNode coveredNode = problem.getCoveredNode(root);
    if (!(coveredNode instanceof SimpleName)) {
        return;
    }
    ASTNode parentType = coveredNode.getParent();
    if (!(parentType instanceof AbstractTypeDeclaration)) {
        return;
    }
    String currTypeName = ((SimpleName) coveredNode).getIdentifier();
    String newTypeName = JavaCore.removeJavaLikeExtension(cu.getElementName());
    boolean hasOtherPublicTypeBefore = false;
    boolean found = false;
    List<AbstractTypeDeclaration> types = root.types();
    for (int i = 0; i < types.size(); i++) {
        AbstractTypeDeclaration curr = types.get(i);
        if (parentType != curr) {
            if (newTypeName.equals(curr.getName().getIdentifier())) {
                return;
            }
            if (!found && Modifier.isPublic(curr.getModifiers())) {
                hasOtherPublicTypeBefore = true;
            }
        } else {
            found = true;
        }
    }
    if (!JavaConventions.validateJavaTypeName(newTypeName, sourceLevel, compliance).matches(IStatus.ERROR)) {
        proposals.add(new CorrectMainTypeNameProposal(cu, context, currTypeName, newTypeName, IProposalRelevance.RENAME_TYPE));
    }
    if (!hasOtherPublicTypeBefore && JavaLanguageServerPlugin.getPreferencesManager().getClientPreferences().isResourceOperationSupported()) {
        String newCUName = JavaModelUtil.getRenamedCUName(cu, currTypeName);
        ICompilationUnit newCU = ((IPackageFragment) (cu.getParent())).getCompilationUnit(newCUName);
        if (!newCU.exists() && !isLinked && !JavaConventions.validateCompilationUnitName(newCUName, sourceLevel, compliance).matches(IStatus.ERROR)) {
            RenameCompilationUnitChange change = new RenameCompilationUnitChange(cu, newCUName);
            // rename CU
            String label = Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_renamecu_description, BasicElementLabels.getResourceName(newCUName));
            proposals.add(new ChangeCorrectionProposal(label, CodeActionKind.QuickFix, change, IProposalRelevance.RENAME_CU));
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) SimpleName(org.eclipse.jdt.core.dom.SimpleName) RenameCompilationUnitChange(org.eclipse.jdt.ls.core.internal.corext.refactoring.changes.RenameCompilationUnitChange) IJavaProject(org.eclipse.jdt.core.IJavaProject) ASTNode(org.eclipse.jdt.core.dom.ASTNode) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 5 with RenameCompilationUnitChange

use of org.eclipse.jdt.ls.core.internal.corext.refactoring.changes.RenameCompilationUnitChange in project eclipse.jdt.ls by eclipse.

the class ChangeUtilTest method testConvertSimpleCompositeChange.

// Composite Changes
@Test
public void testConvertSimpleCompositeChange() throws CoreException {
    IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", "", false, null);
    CompositeChange change = new CompositeChange("simple composite change");
    RenameCompilationUnitChange resourceChange = new RenameCompilationUnitChange(cu, "ENew.java");
    change.add(resourceChange);
    CompilationUnitChange textChange = new CompilationUnitChange("insertText", cu);
    textChange.setEdit(new InsertEdit(0, "// some content"));
    change.add(textChange);
    WorkspaceEdit edit = ChangeUtil.convertToWorkspaceEdit(change);
    assertEquals(edit.getDocumentChanges().size(), 2);
    assertTrue(edit.getDocumentChanges().get(0).getRight() instanceof RenameFile);
    assertTrue(edit.getDocumentChanges().get(1).getLeft() instanceof TextDocumentEdit);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) InsertEdit(org.eclipse.text.edits.InsertEdit) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) TextDocumentEdit(org.eclipse.lsp4j.TextDocumentEdit) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange) RenameFile(org.eclipse.lsp4j.RenameFile) RenameCompilationUnitChange(org.eclipse.jdt.ls.core.internal.corext.refactoring.changes.RenameCompilationUnitChange) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange) RenameCompilationUnitChange(org.eclipse.jdt.ls.core.internal.corext.refactoring.changes.RenameCompilationUnitChange) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Aggregations

RenameCompilationUnitChange (org.eclipse.jdt.ls.core.internal.corext.refactoring.changes.RenameCompilationUnitChange)5 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)3 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)3 IResource (org.eclipse.core.resources.IResource)2 IJavaProject (org.eclipse.jdt.core.IJavaProject)2 RenameJavaElementDescriptor (org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor)2 JDTRefactoringDescriptorComment (org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment)2 DynamicValidationRefactoringChange (org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationRefactoringChange)2 AbstractProjectsManagerBasedTest (org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest)2 RenameFile (org.eclipse.lsp4j.RenameFile)2 WorkspaceEdit (org.eclipse.lsp4j.WorkspaceEdit)2 RenameResourceChange (org.eclipse.ltk.core.refactoring.resource.RenameResourceChange)2 Test (org.junit.Test)2 IProject (org.eclipse.core.resources.IProject)1 JavaModelException (org.eclipse.jdt.core.JavaModelException)1 ASTNode (org.eclipse.jdt.core.dom.ASTNode)1 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)1 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)1 SimpleName (org.eclipse.jdt.core.dom.SimpleName)1 CompilationUnitChange (org.eclipse.jdt.core.refactoring.CompilationUnitChange)1