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