Search in sources :

Example 1 with StatusWrapper

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

the class JdtRenameRefactoringParticipantProcessor method preCheckInitialConditions.

/**
 * Subclasses can decide to override this to allow specific multi-refactorings.
 *
 * @since 2.4
 */
protected RefactoringStatus preCheckInitialConditions(IProgressMonitor pm) throws CoreException {
    if (associations.getJvmElements(getTargetElement()).size() > 1) {
        StatusWrapper statusWrapper = getStatusProvider().get();
        statusWrapper.add(ERROR, "Rename from here will not be complete. Try to rename {0} instead.", getTargetElement());
        statusWrapper.merge(super.checkInitialConditions(pm));
        return statusWrapper.getRefactoringStatus();
    }
    return new RefactoringStatus();
}
Also used : StatusWrapper(org.eclipse.xtext.ui.refactoring.impl.StatusWrapper) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus)

Example 2 with StatusWrapper

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

the class ExtractMethodRefactoring method checkFinalConditions.

@Override
public RefactoringStatus checkFinalConditions(IProgressMonitor pm) throws CoreException, OperationCanceledException {
    StatusWrapper status = statusProvider.get();
    try {
        status.merge(validateMethodName(methodName));
        status.merge(validateParameters());
        ITextRegion expressionsRegion = getExpressionsRegion();
        ITextRegion predecessorRegion = locationInFileProvider.getFullTextRegion(originalMethod);
        if (pm.isCanceled()) {
            throw new OperationCanceledException();
        }
        Section expressionSection = rewriter.newSection(expressionsRegion.getOffset(), expressionsRegion.getLength());
        Section declarationSection = rewriter.newSection(predecessorRegion.getOffset() + predecessorRegion.getLength(), 0);
        createMethodCallEdit(expressionSection, expressionsRegion);
        if (pm.isCanceled()) {
            throw new OperationCanceledException();
        }
        createMethodDeclarationEdit(declarationSection, expressionSection.getBaseIndentLevel(), expressionsRegion);
        if (pm.isCanceled()) {
            throw new OperationCanceledException();
        }
        textEdit = replaceConverter.convertToTextEdit(rewriter.getChanges());
    } catch (OperationCanceledException e) {
        throw e;
    } catch (Exception exc) {
        handleException(exc, status);
    }
    return status.getRefactoringStatus();
}
Also used : ITextRegion(org.eclipse.xtext.util.ITextRegion) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) StatusWrapper(org.eclipse.xtext.ui.refactoring.impl.StatusWrapper) Section(org.eclipse.xtext.xbase.ui.document.DocumentRewriter.Section) CoreException(org.eclipse.core.runtime.CoreException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 3 with StatusWrapper

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

the class ExtractMethodRefactoring method checkInitialConditions.

@Override
public RefactoringStatus checkInitialConditions(final IProgressMonitor pm) throws CoreException, OperationCanceledException {
    StatusWrapper status = statusProvider.get();
    IResolvedTypes resolvedTypes = typeResolver.resolveTypes(firstExpression, new CancelIndicator() {

        @Override
        public boolean isCanceled() {
            return pm.isCanceled();
        }
    });
    try {
        Set<String> calledExternalFeatureNames = newHashSet();
        returnType = calculateReturnType(resolvedTypes);
        if (returnType != null && !equal("void", returnType.getIdentifier()))
            returnExpression = lastExpression;
        boolean isReturnAllowed = isEndOfOriginalMethod();
        for (EObject element : EcoreUtil2.eAllContents(originalMethod.getExpression())) {
            if (pm.isCanceled()) {
                throw new OperationCanceledException();
            }
            boolean isLocalExpression = EcoreUtil.isAncestor(expressions, element);
            if (element instanceof XFeatureCall) {
                XFeatureCall featureCall = (XFeatureCall) element;
                JvmIdentifiableElement feature = featureCall.getFeature();
                LightweightTypeReference featureType = resolvedTypes.getActualType(featureCall);
                boolean isLocalFeature = EcoreUtil.isAncestor(expressions, feature);
                if (!isLocalFeature && isLocalExpression) {
                    // call-out
                    if (feature instanceof JvmFormalParameter || feature instanceof XVariableDeclaration) {
                        if (!calledExternalFeatureNames.contains(feature.getSimpleName())) {
                            calledExternalFeatureNames.add(feature.getSimpleName());
                            ParameterInfo parameterInfo = new ParameterInfo(featureType.getIdentifier(), feature.getSimpleName(), parameterInfos.size());
                            parameterInfos.add(parameterInfo);
                            parameter2type.put(parameterInfo, featureType);
                        }
                        externalFeatureCalls.put(feature.getSimpleName(), featureCall);
                    }
                } else if (isLocalFeature && !isLocalExpression) {
                    // call-in
                    if (returnExpression != null) {
                        status.add(RefactoringStatus.FATAL, "Ambiguous return value: Multiple local variables are accessed in subsequent code.");
                        break;
                    }
                    returnExpression = featureCall;
                    returnType = featureType;
                }
            } else if (isLocalExpression) {
                if (element instanceof XReturnExpression && !isReturnAllowed) {
                    status.add(RefactoringStatus.FATAL, "Extracting method would break control flow due to return statements.");
                    break;
                } else if (element instanceof JvmTypeReference) {
                    JvmType type = ((JvmTypeReference) element).getType();
                    if (type instanceof JvmTypeParameter) {
                        JvmOperation operation = associations.getDirectlyInferredOperation(originalMethod);
                        if (operation != null) {
                            List<JvmTypeParameter> typeParameters = operation.getTypeParameters();
                            if (typeParameters.contains(type))
                                neededTypeParameters.add((JvmTypeParameter) type);
                        }
                    }
                } else if (element instanceof JvmFormalParameter)
                    localFeatureNames.add(((JvmFormalParameter) element).getName());
                else if (element instanceof XVariableDeclaration)
                    localFeatureNames.add(((XVariableDeclaration) element).getIdentifier());
            }
        }
    } catch (OperationCanceledException e) {
        throw e;
    } catch (Exception exc) {
        handleException(exc, status);
    }
    return status.getRefactoringStatus();
}
Also used : XVariableDeclaration(org.eclipse.xtext.xbase.XVariableDeclaration) LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) JvmIdentifiableElement(org.eclipse.xtext.common.types.JvmIdentifiableElement) IResolvedTypes(org.eclipse.xtext.xbase.typesystem.IResolvedTypes) XFeatureCall(org.eclipse.xtext.xbase.XFeatureCall) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) JvmTypeParameter(org.eclipse.xtext.common.types.JvmTypeParameter) StatusWrapper(org.eclipse.xtext.ui.refactoring.impl.StatusWrapper) RichString(org.eclipse.xtend.core.xtend.RichString) ParameterInfo(org.eclipse.jdt.internal.corext.refactoring.ParameterInfo) JvmType(org.eclipse.xtext.common.types.JvmType) CoreException(org.eclipse.core.runtime.CoreException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) BadLocationException(org.eclipse.jface.text.BadLocationException) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) EObject(org.eclipse.emf.ecore.EObject) CancelIndicator(org.eclipse.xtext.util.CancelIndicator) XReturnExpression(org.eclipse.xtext.xbase.XReturnExpression)

Example 4 with StatusWrapper

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

the class ExtractVariableRefactoring method checkFinalConditions.

@Override
public RefactoringStatus checkFinalConditions(IProgressMonitor pm) throws CoreException, OperationCanceledException {
    StatusWrapper status = statusProvider.get();
    try {
        status.merge(validateNewVariableName(variableName));
        ITextRegion expressionRegion = locationInFileProvider.getFullTextRegion(expression);
        ITextRegion successorRegion = locationInFileProvider.getFullTextRegion(successor);
        Section callerSection = rewriter.newSection(expressionRegion.getOffset(), expressionRegion.getLength());
        if (pm.isCanceled()) {
            throw new OperationCanceledException();
        }
        if (isNeedsNewBlock) {
            IRegion lineInformation = document.getLineInformationOfOffset(successorRegion.getOffset());
            int previousLineEnd = lineInformation.getOffset() - rewriter.getLineSeparator().length();
            Section blockStartSection = getDeclarationSection(Math.max(0, previousLineEnd), expressionRegion.getOffset(), callerSection);
            if (previousLineEnd > 0 && !Character.isWhitespace(document.getChar(previousLineEnd - 1)))
                blockStartSection.append(" ");
            blockStartSection.append("{").increaseIndentation().newLine();
            appendDeclaration(blockStartSection, expressionRegion);
            Section blockEndSection = rewriter.newSection(successorRegion.getOffset() + successorRegion.getLength(), 0);
            blockEndSection.decreaseIndentation().newLine().append("}");
        } else {
            Section declarationSection = getDeclarationSection(successorRegion.getOffset(), expressionRegion.getOffset(), callerSection);
            appendDeclaration(declarationSection, expressionRegion);
            declarationSection.newLine();
        }
        if (pm.isCanceled()) {
            throw new OperationCanceledException();
        }
        String callerText = variableName;
        // handle closure shortcut syntaxes
        if (expression.eContainer() instanceof XMemberFeatureCall) {
            if (((XMemberFeatureCall) expression.eContainer()).getMemberCallArguments().size() == 1) {
                String expressionExpanded = document.get(expressionRegion.getOffset() - 1, expressionRegion.getLength() + 2);
                if (!expressionExpanded.startsWith("(") || !expressionExpanded.endsWith(")"))
                    callerText = "(" + callerText + ")";
            }
        }
        callerSection.append(callerText);
    } catch (OperationCanceledException e) {
        throw e;
    } catch (Exception exc) {
        handleException(exc, status);
    }
    return status.getRefactoringStatus();
}
Also used : ITextRegion(org.eclipse.xtext.util.ITextRegion) XMemberFeatureCall(org.eclipse.xtext.xbase.XMemberFeatureCall) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) StatusWrapper(org.eclipse.xtext.ui.refactoring.impl.StatusWrapper) Section(org.eclipse.xtext.xbase.ui.document.DocumentRewriter.Section) IRegion(org.eclipse.jface.text.IRegion) CoreException(org.eclipse.core.runtime.CoreException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

StatusWrapper (org.eclipse.xtext.ui.refactoring.impl.StatusWrapper)4 CoreException (org.eclipse.core.runtime.CoreException)3 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)3 BadLocationException (org.eclipse.jface.text.BadLocationException)3 ITextRegion (org.eclipse.xtext.util.ITextRegion)2 Section (org.eclipse.xtext.xbase.ui.document.DocumentRewriter.Section)2 EObject (org.eclipse.emf.ecore.EObject)1 ParameterInfo (org.eclipse.jdt.internal.corext.refactoring.ParameterInfo)1 IRegion (org.eclipse.jface.text.IRegion)1 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)1 RichString (org.eclipse.xtend.core.xtend.RichString)1 JvmFormalParameter (org.eclipse.xtext.common.types.JvmFormalParameter)1 JvmIdentifiableElement (org.eclipse.xtext.common.types.JvmIdentifiableElement)1 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)1 JvmType (org.eclipse.xtext.common.types.JvmType)1 JvmTypeParameter (org.eclipse.xtext.common.types.JvmTypeParameter)1 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)1 CancelIndicator (org.eclipse.xtext.util.CancelIndicator)1 XFeatureCall (org.eclipse.xtext.xbase.XFeatureCall)1 XMemberFeatureCall (org.eclipse.xtext.xbase.XMemberFeatureCall)1