Limiting Proposed Java types to implementers of Interface does not work with Package [message #1739947] |
Fri, 05 August 2016 19:59  |
Eclipse User |
|
|
|
I wanted to limit the proposed Java Types to implementers of a certain interface. Thanks to the Domain Model Xbase example and Karsten's Blog "Limiting Proposed Java types to implementers of Interface" , I was able to combine these 2 examples and make the java types limited to a Interface type without package
Proposal Provider:
public class DomainmodelProposalProvider extends AbstractDomainmodelProposalProvider {
@Inject IJvmTypeProvider.Factory jvmTypeProviderFactory;
@Inject ITypesProposalProvider typeProposalProvider;
@Override
public void completeProperty_Type(EObject model, Assignment assignment, ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
IJvmTypeProvider jvmTypeProvider = jvmTypeProviderFactory.createTypeProvider(
model.eResource().getResourceSet());
JvmType interfaceToImplement = jvmTypeProvider.findTypeByName(IEntity.class.getName());
typeProposalProvider.createSubTypeProposals(interfaceToImplement, this, context,
DomainmodelPackage.Literals.FEATURE__TYPE, TypeMatchFilters.canInstantiate(), acceptor);
}
}
Proposal with filtered types working properly without packages :
entity Bar extends IEntity {
name : String
}
entity Foo extends IEntity{
bar : Bar //Control + Space Proposal and accept works
op doStuff(String x) : String {
return x + ' ' + bar.getName()
}
}
ISSUE After Adding packages to DSL , the Proposal completion for the type IEntity in line bar:Bar doesn't work properly.
With Package - Proposals are displayed correctly but when we select a proposal I get error Error:org.eclipse.xtext.xbase.ui.contentassist.ImportingTypesProposalProvider$FQNImporter - Could not find unique type named 'com.pkg1.Bar' in scope
package com.pkg1{
entity Bar extends IEntity {
name : String
}
}
package com.pkg1 {
entity Foo extends IEntity{
bar : Bar // Proposal is ok , but selection of proposed option results in error
op doStuff(String x) : String {
return x + ' ' + bar.getName()
}
}
}
Debugging inside XBase Code :
Class : ImportingTypesProposalProvider
public void apply(IDocument document, ConfigurableCompletionProposal proposal) throws BadLocationException {
.......
IEObjectDescription typeToImport = scope.getSingleElement(qualifiedName); //Name resolution does not happen.
|
|
|
|
|
Re: Limiting Proposed Java types to implementers of Interface does not work with Package [message #1739954 is a reply to message #1739950] |
Fri, 05 August 2016 22:19   |
Eclipse User |
|
|
|
Thanks Christian . Overriding JdtTypesProposalProvider works 
However I get the complete fully qualified name with the package for the JvmTypeReference ( com.pkg1.Bar instead of Bar)
import org.eclipse.xtext.example.domainmodel.interfaces.IEntity
package com.pkg1 {
entity Foo extends IEntity{
bar : com.pkg1.Bar
op doStuff(String x) : String {
return x + ' ' + bar.getName()
}
}
}
Is there a way, I get only the simple Class Name without the package for my references so that the DSL will look cleaner.
|
|
|
|
Re: Limiting Proposed Java types to implementers of Interface does not work with Package [message #1739962 is a reply to message #1739958] |
Sat, 06 August 2016 06:00   |
Eclipse User |
|
|
|
My imports work using XImportSelection. I still use it to access java libraries from XExpression.
case 1(Using Proposal):
In the above example , the JdtTypesProposalProvider provides the fully qualified type name as com.pkg1.Bar. After selecting the proposal, even if i remove the package part com.pkg1 and just have Bar, it still works the same.
XBase is still able to recognize the type correctly and generates the java files correctly.
case 2 (without implementing proposal):
If i don't use use Type Proposal provider explicitly, all the java reference types are listed. When i select Bar from this very long list. Then i get only Bar with simple class name as type.
Based on the above 2 cases, I wanted to confirm whether I will be able to implement cross reference only with the simple type name. So that i get the cleaner code reference as case 2 and better usability as in case 1 .
|
|
|
|
Re: Limiting Proposed Java types to implementers of Interface does not work with Package [message #1739964 is a reply to message #1739963] |
Sat, 06 August 2016 06:28   |
Eclipse User |
|
|
|
and did i get it right you want to have split packages in the model? and what is your grammar? it indicates you dont use the domain model 1:1 that would mean having implemented
public class DomainmodelProposalProvider extends AbstractDomainmodelProposalProvider {
@Inject
IJvmTypeProvider.Factory jvmTypeProviderFactory;
@Inject
ITypesProposalProvider typeProposalProvider;
@Override // why not completeJvmParameterizedTypeReference_Type
public void completeProperty_Type(EObject model, Assignment assignment, ContentAssistContext context,
ICompletionProposalAcceptor acceptor) {
IJvmTypeProvider jvmTypeProvider = jvmTypeProviderFactory.createTypeProvider(
model.eResource().getResourceSet());
JvmType interfaceToImplement = jvmTypeProvider.findTypeByName("test.IEntity");
typeProposalProvider.createSubTypeProposals(interfaceToImplement, this, context,
TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE__TYPE, TypeMatchFilters.canInstantiate(), acceptor);
}
@Override
public void completeJvmParameterizedTypeReference_Type(EObject model, Assignment assignment, ContentAssistContext context,
ICompletionProposalAcceptor acceptor) {
// TODO Auto-generated method stub
//super.completeJvmParameterizedTypeReference_Type(model, assignment, context, acceptor);
}
}
|
|
|
|
Re: Limiting Proposed Java types to implementers of Interface does not work with Package [message #1739971 is a reply to message #1739965] |
Sat, 06 August 2016 08:15   |
Eclipse User |
|
|
|
Hi Christian,
Yes I use Import section.
ImportingTypesProposalProvider doesn't work . If i Use ImportingTypesProposalProvider, I still get the old error message
org.eclipse.xtext.xbase.ui.contentassist.ImportingTypesProposalProvider$FQNImporter - Could not find unique type named 'com.pkg1.Bar' in scope
Grammar:
grammar org.eclipse.xtext.example.domainmodel.Domainmodel with org.eclipse.xtext.xbase.Xbase
generate domainmodel "http://www.xtext.org/example/Domainmodel"
DomainModel:
importSection=XImportSection?
elements+=AbstractElement*;
AbstractElement:
PackageDeclaration | Entity;
PackageDeclaration:
'package' name=QualifiedName '{'
elements+=AbstractElement*
'}';
Entity:
'entity' name=ValidID ('extends' superType=JvmParameterizedTypeReference)? '{'
features+=Feature*
'}';
Feature:
Property | Operation;
Property:
name=ValidID ':' type=JvmTypeReference;
Operation:
'op' name=ValidID '(' (params+=FullJvmFormalParameter (',' params+=FullJvmFormalParameter)*)? ')' (':' type=JvmTypeReference)?
body=XBlockExpression;
Also Attached the complete sample source here which has the Proposal implementation.
Attachment: Sample.zip
(Size: 1.21MB, Downloaded 120 times)
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04385 seconds