org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReferenceFactory Java Examples

The following examples show how to use org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReferenceFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CompilationUnitImpl.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
public void before(final ActiveAnnotationContexts.AnnotationCallback phase) {
  this.lastPhase = phase;
  final StandardTypeReferenceOwner standardTypeReferenceOwner = new StandardTypeReferenceOwner(this.services, this.xtendFile);
  boolean _equals = Objects.equal(ActiveAnnotationContexts.AnnotationCallback.INDEXING, phase);
  if (_equals) {
    IndexingLightweightTypeReferenceFactory _indexingLightweightTypeReferenceFactory = new IndexingLightweightTypeReferenceFactory(standardTypeReferenceOwner);
    this.typeRefFactory = _indexingLightweightTypeReferenceFactory;
  } else {
    LightweightTypeReferenceFactory _lightweightTypeReferenceFactory = new LightweightTypeReferenceFactory(standardTypeReferenceOwner);
    this.typeRefFactory = _lightweightTypeReferenceFactory;
  }
  boolean _equals_1 = Objects.equal(ActiveAnnotationContexts.AnnotationCallback.VALIDATION, phase);
  if (_equals_1) {
    this.problemSupport.validationPhaseStarted();
  }
}
 
Example #2
Source File: XtypeProposalProvider.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected void appendParameters(StyledString result, JvmExecutable executable, int insignificantParameters, LightweightTypeReferenceFactory ownedConverter) {
	List<JvmFormalParameter> declaredParameters = executable.getParameters();
	List<JvmFormalParameter> relevantParameters = declaredParameters.subList(Math.min(insignificantParameters, declaredParameters.size()), declaredParameters.size());
	for(int i = 0; i < relevantParameters.size(); i++) {
		JvmFormalParameter parameter = relevantParameters.get(i);
		if (i != 0)
			result.append(", ");
		if (i == relevantParameters.size() - 1 && executable.isVarArgs() && parameter.getParameterType() instanceof JvmGenericArrayTypeReference) {
			JvmGenericArrayTypeReference parameterType = (JvmGenericArrayTypeReference) parameter.getParameterType();
			result.append(ownedConverter.toLightweightReference(parameterType.getComponentType()).getHumanReadableName());
			result.append("...");
		} else {
			if (parameter.getParameterType()!= null) {
				String simpleName = ownedConverter.toLightweightReference(parameter.getParameterType()).getHumanReadableName();
				if (simpleName != null) // is null if the file is not on the class path
					result.append(simpleName);
			}
		}
		result.append(' ');
		result.append(notNull(parameter.getName()));
	}
}
 
Example #3
Source File: DeferredTypeParameterHintCollector.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected LightweightTypeReference getStricterConstraint(final UnboundTypeReference typeParameter, LightweightTypeReference hint) {
	final JvmTypeParameter parameter = typeParameter.getTypeParameter();
	List<JvmTypeConstraint> constraints = parameter.getConstraints();
	for(JvmTypeConstraint constraint: constraints) {
		JvmTypeReference constraintReference = constraint.getTypeReference();
		if (constraintReference != null) {
			final boolean[] recursive = new boolean[] { false };
			LightweightTypeReferenceFactory factory = new LightweightTypeReferenceFactory(hint.getOwner()) {
				@Override
				public LightweightTypeReference doVisitParameterizedTypeReference(JvmParameterizedTypeReference reference) {
					JvmType type = reference.getType();
					if (type == parameter) {// recursively bound
						recursive[0] = true;
					}
					return super.doVisitParameterizedTypeReference(reference);
				}
			};
			LightweightTypeReference lightweightReference = factory.toLightweightReference(constraintReference);
			if (!recursive[0]) {
				if (hint.isAssignableFrom(lightweightReference)) {
					hint = lightweightReference;	
				} else if (hint.isResolved() && !lightweightReference.getRawTypeReference().isAssignableFrom(hint, TypeConformanceComputationArgument.RAW)) {
					return null;
				}
			}
		}
	}
	return hint;
}
 
Example #4
Source File: Utils.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Convert a type to a lightweight type reference.
 *
 * @param type - type to convert.
 * @param services - services used for the conversion
 * @param keepUnboundWildcardInformation - indicates if the unbound wild card
 *        information must be keeped in the lightweight reference.
 * @return the lightweight type reference.
 */
public static LightweightTypeReference toLightweightTypeReference(
		JvmType type, CommonTypeComputationServices services,
		boolean keepUnboundWildcardInformation) {
	if (type == null) {
		return null;
	}
	final StandardTypeReferenceOwner owner = new StandardTypeReferenceOwner(services, type);
	final LightweightTypeReferenceFactory factory = new LightweightTypeReferenceFactory(owner,
			keepUnboundWildcardInformation);
	final LightweightTypeReference reference = factory.toLightweightReference(type);
	return reference;
}
 
Example #5
Source File: Utils.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Convert a type reference to a lightweight type reference.
 *
 * @param typeRef - reference to convert.
 * @param services - services used for the conversion
 * @param keepUnboundWildcardInformation - indicates if the unbound wild card
 *        information must be keeped in the lightweight reference.
 * @return the lightweight type reference.
 */
public static LightweightTypeReference toLightweightTypeReference(
		JvmTypeReference typeRef, CommonTypeComputationServices services,
		boolean keepUnboundWildcardInformation) {
	if (typeRef == null) {
		return null;
	}
	final StandardTypeReferenceOwner owner = new StandardTypeReferenceOwner(services, typeRef);
	final LightweightTypeReferenceFactory factory = new LightweightTypeReferenceFactory(owner,
			keepUnboundWildcardInformation);
	final LightweightTypeReference reference = factory.toLightweightReference(typeRef);
	return reference;
}
 
Example #6
Source File: AbstractBuilder.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Replies if the first parameter is a subtype of the second parameter.
 *
 * @param context the context.
 * @param subType the subtype to test.
 * @param superType the expected super type.
 * @return the type reference.
 */
@Pure
protected boolean isSubTypeOf(EObject context, JvmTypeReference subType, JvmTypeReference superType) {
	if (isTypeReference(superType) && isTypeReference(subType)) {
		StandardTypeReferenceOwner owner = new StandardTypeReferenceOwner(services, context);
		LightweightTypeReferenceFactory factory = new LightweightTypeReferenceFactory(owner, false);
		LightweightTypeReference reference = factory.toLightweightReference(subType);
		return reference.isSubtypeOf(superType.getType());
	}
	return false;
}
 
Example #7
Source File: CompilationUnitImpl.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
public void setXtendFile(final XtendFile xtendFile) {
  this.xtendFile = xtendFile;
  StandardTypeReferenceOwner _standardTypeReferenceOwner = new StandardTypeReferenceOwner(this.services, xtendFile);
  LightweightTypeReferenceFactory _lightweightTypeReferenceFactory = new LightweightTypeReferenceFactory(_standardTypeReferenceOwner);
  this.typeRefFactory = _lightweightTypeReferenceFactory;
  this.fileSystemSupport.setContext(xtendFile.eResource().getResourceSet());
  this.fileLocations.setContext(xtendFile.eResource());
}
 
Example #8
Source File: XtypeProposalProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected LightweightTypeReferenceFactory getTypeConverter(XtextResource context) {
	return new LightweightTypeReferenceFactory(new StandardTypeReferenceOwner(services, context)) {
		@Override
		public LightweightTypeReference doVisitParameterizedTypeReference(JvmParameterizedTypeReference reference) {
			LightweightTypeReference result = super.doVisitParameterizedTypeReference(reference);
			if (result.isFunctionType()) {
				FunctionTypeReference functionTypeReference = result.tryConvertToFunctionTypeReference(false);
				return functionTypeReference;
			}
			return result;
		}
	};
}
 
Example #9
Source File: XtypeProposalProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void completeXImportDeclaration_MemberName(EObject model, Assignment assignment,
		ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
	if (model instanceof XImportDeclaration) {
		XImportDeclaration importDeclaration = (XImportDeclaration) model;
		for (JvmFeature feature : staticallyImportedMemberProvider.findAllFeatures(importDeclaration)) {
			Image image = getImage(feature);
			LightweightTypeReferenceFactory typeConverter = getTypeConverter(context.getResource());
			StyledString displayString = getStyledDisplayString(feature, false, 0, feature.getQualifiedName(), feature.getSimpleName(), typeConverter);
			acceptor.accept(createCompletionProposal(feature.getSimpleName(), displayString, image, context));
		}
	}
}
 
Example #10
Source File: XbaseIdeCrossrefProposalProvider.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void appendParameters(StringBuilder result, JvmExecutable executable, int insignificantParameters,
		LightweightTypeReferenceFactory ownedConverter) {
	List<JvmFormalParameter> declaredParameters = executable.getParameters();
	List<JvmFormalParameter> relevantParameters = declaredParameters
			.subList(Math.min(insignificantParameters, declaredParameters.size()), declaredParameters.size());
	for (int i = 0; i < relevantParameters.size(); i++) {
		JvmFormalParameter parameter = relevantParameters.get(i);
		if (i != 0) {
			result.append(", ");
		}
		if (i == relevantParameters.size() - 1 && executable.isVarArgs()
				&& parameter.getParameterType() instanceof JvmGenericArrayTypeReference) {
			JvmGenericArrayTypeReference parameterType = (JvmGenericArrayTypeReference) parameter
					.getParameterType();
			result.append(
					ownedConverter.toLightweightReference(parameterType.getComponentType()).getHumanReadableName());
			result.append("...");
		} else {
			if (parameter.getParameterType() != null) {
				String simpleName = ownedConverter.toLightweightReference(parameter.getParameterType())
						.getHumanReadableName();
				if (simpleName != null) {
					result.append(simpleName);
				}
			}
		}
		result.append(" ");
		result.append(String.valueOf(parameter.getName()));
	}
}
 
Example #11
Source File: XbaseIdeCrossrefProposalProvider.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected LightweightTypeReferenceFactory getTypeConverter(XtextResource context) {
	return new LightweightTypeReferenceFactory(new StandardTypeReferenceOwner(typeComputationServices, context)) {
		@Override
		public LightweightTypeReference doVisitParameterizedTypeReference(JvmParameterizedTypeReference reference) {
			LightweightTypeReference result = super.doVisitParameterizedTypeReference(reference);
			if (result.isFunctionType()) {
				return result.tryConvertToFunctionTypeReference(false);
			}
			return result;
		}
	};
}
 
Example #12
Source File: XtypeProposalProvider.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
protected StyledString getStyledDisplayString(JvmFeature feature, boolean withParents, int insignificantParameters, String qualifiedNameAsString, String shortName,
		LightweightTypeReferenceFactory converter) {
	StyledString result = new StyledString(shortName);
	if (feature instanceof JvmOperation) {
		JvmOperation operation = (JvmOperation) feature;
		if (withParents) {
			result.append('(');
			appendParameters(result, (JvmExecutable)feature, insignificantParameters, converter);
			result.append(')');
		}
		JvmTypeReference returnType = operation.getReturnType();
		if (returnType != null && returnType.getSimpleName() != null) {
			result.append(" : ");
			result.append(converter.toLightweightReference(returnType).getHumanReadableName());
		}
		result.append(" - ", StyledString.QUALIFIER_STYLER);
		result.append(converter.toPlainTypeReference(feature.getDeclaringType()).getHumanReadableName(), StyledString.QUALIFIER_STYLER);
		if (!withParents) {
			result.append(".", StyledString.QUALIFIER_STYLER);
			result.append(feature.getSimpleName(), StyledString.QUALIFIER_STYLER);
			result.append("()", StyledString.QUALIFIER_STYLER);
		}
	} else if (feature instanceof JvmField) {
		JvmField field = (JvmField) feature;
		result.append(" : ");
		if (field.getType() != null) {
			String fieldType = converter.toLightweightReference(field.getType()).getHumanReadableName();
			if (fieldType != null)
				result.append(fieldType);
		}
		result.append(" - ", StyledString.QUALIFIER_STYLER);
		result.append(converter.toPlainTypeReference(feature.getDeclaringType()).getHumanReadableName(), StyledString.QUALIFIER_STYLER);
	} else if (feature instanceof JvmConstructor) {
		if (withParents) {
			result.append('(');
			appendParameters(result, (JvmExecutable)feature, insignificantParameters, converter);
			result.append(')');
		}
	}
	return result;
}
 
Example #13
Source File: ParameterContextInformationProvider.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void getContextInformation(ContentAssistContext context, IContextInformationAcceptor acceptor) {
	XExpression containerCall = getContainerCall(eObjectAtOffsetHelper.resolveContainedElementAt(context.getResource(), context.getOffset()));
	LightweightTypeReferenceFactory factory = proposalProvider.getTypeConverter(context.getResource());
	if (containerCall != null) {
		ICompositeNode containerCallNode = NodeModelUtils.findActualNodeFor(containerCall);
		ITextRegion containerCallRegion = containerCallNode.getTextRegion();
		if(containerCallRegion.getOffset() > context.getOffset()
				|| containerCallRegion.getOffset() + containerCallRegion.getLength() < context.getOffset()) 
			return;
		JvmIdentifiableElement calledFeature = getCalledFeature(containerCall);
		if (calledFeature instanceof JvmExecutable) {
			if(getParameterListOffset(containerCall) > context.getOffset()) 
				return;
			ParameterData parameterData = new ParameterData();
			IScope scope = getScope(containerCall);
			QualifiedName qualifiedName = QualifiedName.create(getCalledFeatureName(containerCall));
			boolean candidatesFound = false;
			for (IEObjectDescription element : scope.getElements(qualifiedName)) {
				if (element instanceof IIdentifiableElementDescription) {
					IIdentifiableElementDescription featureDescription = (IIdentifiableElementDescription) element;
					JvmIdentifiableElement featureCandidate = featureDescription.getElementOrProxy();
					if (featureCandidate instanceof JvmExecutable) {
						JvmExecutable executable = (JvmExecutable) featureCandidate;
						if(!executable.getParameters().isEmpty()) {
							StyledString styledString = new StyledString();
							proposalProvider.appendParameters(styledString, executable,
									featureDescription.getNumberOfIrrelevantParameters(), factory);
							parameterData.addOverloaded(styledString.toString(), executable.isVarArgs());
							candidatesFound = true;
						}
					}
				}
			}
			if (candidatesFound) {
				StyledString displayString = proposalProvider.getStyledDisplayString((JvmExecutable) calledFeature, true, 0, 
						qualifiedNameConverter.toString(qualifiedNameProvider.getFullyQualifiedName(calledFeature)), 
						calledFeature.getSimpleName(), factory);
				ParameterContextInformation parameterContextInformation = new ParameterContextInformation(
						parameterData, displayString.toString(), getParameterListOffset(containerCall), context.getOffset());
				acceptor.accept(parameterContextInformation);
			}
		}
	}
}
 
Example #14
Source File: XbaseIdeCrossrefProposalProvider.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected void addNameAndDescription(ContentAssistEntry entry, JvmFeature feature, boolean withParents,
		int insignificantParameters, String shortName, LightweightTypeReferenceFactory converter) {
	StringBuilder labelBuilder = new StringBuilder(shortName);
	StringBuilder descriptionBuilder = new StringBuilder();
	if (feature instanceof JvmOperation) {
		if (withParents) {
			labelBuilder.append("(");
			appendParameters(labelBuilder, (JvmExecutable) feature, insignificantParameters, converter);
			labelBuilder.append(")");
		}
		JvmOperation jvmOperation = (JvmOperation) feature;
		JvmTypeReference returnType = jvmOperation.getReturnType();
		if (returnType != null && returnType.getSimpleName() != null) {
			labelBuilder.append(" : ");
			labelBuilder.append(converter.toLightweightReference(returnType).getHumanReadableName());
		}
		descriptionBuilder.append(
				converter.toPlainTypeReference(jvmOperation.getDeclaringType()).getHumanReadableName());
		if (!withParents) {
			descriptionBuilder.append(".");
			descriptionBuilder.append(jvmOperation.getSimpleName());
			descriptionBuilder.append("()");
		}
	} else {
		if (feature instanceof JvmField) {
			labelBuilder.append(" : ");
			JvmField jvmField = (JvmField) feature;
			if (jvmField.getType() != null) {
				String fieldType = converter.toLightweightReference(jvmField.getType())
						.getHumanReadableName();
				if (fieldType != null) {
					labelBuilder.append(fieldType);
				}
			}
			descriptionBuilder.append(
					converter.toPlainTypeReference(jvmField.getDeclaringType()).getHumanReadableName());
		} else if (feature instanceof JvmConstructor) {
			if (withParents) {
				labelBuilder.append("(");
				appendParameters(labelBuilder, ((JvmExecutable) feature), insignificantParameters, converter);
				labelBuilder.append(")");
			}
		}
	}
	entry.setLabel(labelBuilder.toString());
	entry.setDescription(descriptionBuilder.toString());
}
 
Example #15
Source File: CompilationUnitImpl.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Pure
public LightweightTypeReferenceFactory getTypeRefFactory() {
  return this.typeRefFactory;
}
 
Example #16
Source File: XbaseIdeCrossrefProposalProvider.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public ContentAssistEntry createProposal(IEObjectDescription candidate, CrossReference crossRef,
		ContentAssistContext context) {
	if (hasIdRule(crossRef)) {
		ProposalBracketInfo bracketInfo = getProposalBracketInfo(candidate, context);
		String proposalString = getQualifiedNameConverter().toString(candidate.getName()) + bracketInfo.brackets;
		final int insignificantParameters;
		if (candidate instanceof IIdentifiableElementDescription) {
			insignificantParameters = ((IIdentifiableElementDescription) candidate)
					.getNumberOfIrrelevantParameters();
		} else {
			insignificantParameters = 0;
		}
		LightweightTypeReferenceFactory converter = getTypeConverter(context.getResource());
		EObject objectOrProxy = candidate.getEObjectOrProxy();
		return getProposalCreator().createProposal(proposalString, context, (ContentAssistEntry result) -> {
			result.setKind(ContentAssistEntry.KIND_REFERENCE);
			if (objectOrProxy instanceof JvmFeature) {
				if (bracketInfo.brackets.startsWith(" =")) {
					addNameAndDescription(result, (JvmFeature) objectOrProxy, false, insignificantParameters,
							getQualifiedNameConverter().toString(candidate.getName()) + bracketInfo.brackets,
							converter);
				} else {
					addNameAndDescription(result, (JvmFeature) objectOrProxy,
							!Strings.isNullOrEmpty(bracketInfo.brackets), insignificantParameters,
							getQualifiedNameConverter().toString(candidate.getName()), converter);
				}
			} else {
				addNameAndDescription(result, objectOrProxy,
						getQualifiedNameConverter().toString(candidate.getQualifiedName()),
						getQualifiedNameConverter().toString(candidate.getName()));
			}
			int offset = context.getOffset() - context.getPrefix().length() + proposalString.length();
			result.setEscapePosition(offset + bracketInfo.caretOffset);
			if (bracketInfo.selectionOffset != 0) {
				offset = offset + bracketInfo.selectionOffset;
				result.getEditPositions().add(new TextRegion(offset, bracketInfo.selectionLength));
			}
			if (objectOrProxy instanceof JvmExecutable) {
				StringBuilder parameterList = new StringBuilder();
				appendParameters(parameterList, (JvmExecutable) objectOrProxy, insignificantParameters, converter);
			}
		});
	}
	return super.createProposal(candidate, crossRef, context);
}
 
Example #17
Source File: ImportingStringConcatenation.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected String _getStringRepresentation(JvmTypeReference object) {
	return _getStringRepresentation(
			new LightweightTypeReferenceFactory(typeReferenceOwner, true).toLightweightReference(object));
}
 
Example #18
Source File: XbaseValidator.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected LightweightTypeReference toLightweightTypeReference(JvmTypeReference typeRef, boolean keepUnboundWildcardInformation) {
	StandardTypeReferenceOwner owner = new StandardTypeReferenceOwner(getServices(), typeRef);
	LightweightTypeReferenceFactory factory = new LightweightTypeReferenceFactory(owner, keepUnboundWildcardInformation);
	LightweightTypeReference reference = factory.toLightweightReference(typeRef);
	return reference;
}
 
Example #19
Source File: SARLValidator.java    From sarl with Apache License 2.0 2 votes vote down vote up
/** Create a lightweight type reference from the given type.
 *
 * @param type the type to point to.
 * @param context the context in which the reference is located.
 * @return the reference.
 */
protected LightweightTypeReference toLightweightTypeReference(JvmType type, EObject context) {
	final StandardTypeReferenceOwner owner = new StandardTypeReferenceOwner(getServices(), context);
	final LightweightTypeReferenceFactory factory = new LightweightTypeReferenceFactory(owner, false);
	return factory.toLightweightReference(type);
}