use of org.eclipse.xtext.common.types.JvmIdentifiableElement in project xtext-xtend by eclipse.
the class CreateMemberQuickfixes method addQuickfixes.
@Override
public void addQuickfixes(Issue issue, IssueResolutionAcceptor issueResolutionAcceptor, IXtextDocument xtextDocument, XtextResource resource, EObject referenceOwner, EReference unresolvedReference) throws Exception {
if (referenceOwner instanceof XAbstractFeatureCall) {
XAbstractFeatureCall call = (XAbstractFeatureCall) referenceOwner;
String newMemberName = (issue.getData() != null && issue.getData().length > 0) ? issue.getData()[0] : null;
if (newMemberName != null) {
if (call instanceof XMemberFeatureCall) {
if (!call.isExplicitOperationCallOrBuilderSyntax()) {
newFieldQuickfix(newMemberName, call, issue, issueResolutionAcceptor);
newGetterQuickfixes(newMemberName, call, issue, issueResolutionAcceptor);
}
newMethodQuickfixes(newMemberName, call, issue, issueResolutionAcceptor);
} else if (call instanceof XFeatureCall) {
if (!call.isExplicitOperationCallOrBuilderSyntax()) {
if (logicalContainerProvider.getNearestLogicalContainer(call) instanceof JvmExecutable)
newLocalVariableQuickfix(newMemberName, call, issue, issueResolutionAcceptor);
newFieldQuickfix(newMemberName, call, issue, issueResolutionAcceptor);
newGetterQuickfixes(newMemberName, call, issue, issueResolutionAcceptor);
}
newMethodQuickfixes(newMemberName, call, issue, issueResolutionAcceptor);
} else if (call instanceof XAssignment) {
newSetterQuickfix(issue, issueResolutionAcceptor, newMemberName, call);
XAssignment assigment = (XAssignment) call;
if (assigment.getAssignable() == null) {
newLocalVariableQuickfix(newMemberName, call, issue, issueResolutionAcceptor);
newFieldQuickfix(newMemberName, call, issue, issueResolutionAcceptor);
} else if (isThis(assigment)) {
newFieldQuickfix(newMemberName, call, issue, issueResolutionAcceptor);
}
}
}
if (call.isOperation()) {
JvmIdentifiableElement feature = call.getFeature();
if (feature.eIsProxy()) {
String operatorMethodName = getOperatorMethodName(call);
if (operatorMethodName != null)
newMethodQuickfixes(operatorMethodName, call, issue, issueResolutionAcceptor);
}
}
if (call instanceof XFeatureCall && call.getFeature() instanceof JvmConstructor) {
newConstructorQuickfix(issue, issueResolutionAcceptor, (XFeatureCall) call);
}
}
if (referenceOwner instanceof XConstructorCall) {
newConstructorQuickfix(issue, issueResolutionAcceptor, (XConstructorCall) referenceOwner);
}
}
use of org.eclipse.xtext.common.types.JvmIdentifiableElement in project xtext-xtend by eclipse.
the class ExtractMethodRefactoring method calculateExpressions.
protected List<XExpression> calculateExpressions(final List<XExpression> expressions) {
final XExpression firstExpression = expressions.get(0);
// instead of the complete XVariableDeclaration
if (expressions.size() == 1 && firstExpression instanceof XVariableDeclaration) {
final XtendFunction originalMethod = EcoreUtil2.getContainerOfType(firstExpression, XtendFunction.class);
for (final EObject element : Iterables.filter(EcoreUtil2.eAllContents(originalMethod.getExpression()), new Predicate<EObject>() {
@Override
public boolean apply(EObject input) {
return !EcoreUtil.isAncestor(expressions, input);
}
})) {
if (element instanceof XFeatureCall) {
XFeatureCall featureCall = (XFeatureCall) element;
JvmIdentifiableElement feature = featureCall.getFeature();
if (EcoreUtil.isAncestor(expressions, feature)) {
return singletonList(((XVariableDeclaration) firstExpression).getRight());
}
}
}
}
return expressions;
}
use of org.eclipse.xtext.common.types.JvmIdentifiableElement in project xtext-xtend by eclipse.
the class ExtractMethodRefactoring method createMethodCallEdit.
protected void createMethodCallEdit(DocumentRewriter.Section methodCallSection, ITextRegion expressionRegion) throws BadLocationException {
if (firstExpression.eContainer() instanceof RichString) {
methodCallSection.append("�");
}
if (isNeedsReturnExpression()) {
JvmIdentifiableElement returnFeature = ((XFeatureCall) returnExpression).getFeature();
if (isFinalFeature(returnFeature))
methodCallSection.append("val ");
else
methodCallSection.append("var ");
methodCallSection.append(returnFeature.getSimpleName()).append(" = ");
}
boolean needsSurroundingParentheses = false;
if (firstExpression.eContainer() instanceof XMemberFeatureCall) {
if (((XMemberFeatureCall) firstExpression.eContainer()).getMemberCallArguments().size() == 1) {
String expressionExpanded = document.get(expressionRegion.getOffset() - 1, expressionRegion.getLength() + 2);
if (!expressionExpanded.startsWith("(") || !expressionExpanded.endsWith(")")) {
needsSurroundingParentheses = true;
methodCallSection.append("(");
}
}
}
methodCallSection.append(methodName).append("(");
boolean isFirst = true;
for (ParameterInfo parameterInfo : getParameterInfos()) {
if (!isFirst)
methodCallSection.append(", ");
isFirst = false;
methodCallSection.append(parameterInfo.getOldName());
}
methodCallSection.append(")");
if (needsSurroundingParentheses)
methodCallSection.append(")");
if (lastExpression.eContainer() instanceof RichString) {
methodCallSection.append("�");
}
}
use of org.eclipse.xtext.common.types.JvmIdentifiableElement 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.common.types.JvmIdentifiableElement in project xtext-xtend by eclipse.
the class JvmElementAtOffsetHelperTest method testRefOtherClass.
@Test
public void testRefOtherClass() {
try {
StringConcatenation _builder = new StringConcatenation();
_builder.append("package testPackage");
_builder.newLine();
_builder.append("class TestCase {");
_builder.newLine();
_builder.append("\t");
_builder.append("def foo(String a){");
_builder.newLine();
_builder.append("\t\t");
_builder.append("a.toStr|ing");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.append("def bar(String a){}");
_builder.newLine();
_builder.append("}");
_builder.newLine();
final String content = _builder.toString();
final XtendFile file = this.file(content.replace("|", ""));
Resource _eResource = file.eResource();
JvmIdentifiableElement jvmIdentifiableElement = this.jvmElementAtOffsetHelper.getJvmIdentifiableElement(((XtextResource) _eResource), content.indexOf("|"));
Assert.assertTrue((jvmIdentifiableElement instanceof JvmOperation));
Assert.assertEquals("java.lang.String.toString", jvmIdentifiableElement.getQualifiedName());
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
Aggregations