Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » [ContentAssistant] Propose type involving ImportManager
[ContentAssistant] Propose type involving ImportManager [message #1172044] Tue, 05 November 2013 17:10 Go to next message
Boris Brodski is currently offline Boris BrodskiFriend
Messages: 111
Registered: July 2009
Senior Member
Hello,


Short version:

In XxxProposalProvider, how to propose a fixed set of JvmTypes, so that (if chosen) import manager get involved?


Long version:

I have a grammar with "name=ID type=JvmTypeReference". In MyDSLProposalProvider I would like to propose a fixed list of types. When type selected, it should be imported using XImportSection of my grammar.

(I don't want to restrict the scope. So I can provide better validation errors.)

My idea was to use "JdtTypesProposalProvider.createTypeProposal()" but it's protected.

Finally, I found a very weird solution overriding ImportingTypesProposalProvider and calling protected method 'createTextApplier()' from there.

Is there a straight forward solution?

I think, this should be a common case.

Thanks!


Regards,
Boris


Re: [ContentAssistant] Propose type involving ImportManager [message #1173778 is a reply to message #1172044] Wed, 06 November 2013 19:04 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13493
Registered: July 2009
Senior Member
Hi,

do you use xbase?
overring org.eclipse.xtext.xbase.ui.contentassist.XbaseProposalProvider.createVisibilityFilter(ContentAssistContext, int) or preceeding methods doesnt work?

@Override
	protected Filter createVisibilityFilter(ContentAssistContext context, final int searchFor) {
		final char[] pn = "java.lang".toCharArray();
		final char[] cn = "String".toCharArray();
		return new Filter() {

			public boolean accept(int modifiers, char[] packageName, char[] simpleTypeName,
					char[][] enclosingTypeNames, String path) {
				return java.util.Arrays.equals(packageName, pn) && java.util.Arrays.equals(simpleTypeName, cn);
			}

			public int getSearchFor() {
				return searchFor;
			}
			
		};
	}


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: [ContentAssistant] Propose type involving ImportManager [message #1173900 is a reply to message #1173778] Wed, 06 November 2013 20:43 Go to previous messageGo to next message
Boris Brodski is currently offline Boris BrodskiFriend
Messages: 111
Registered: July 2009
Senior Member
Hello Christian,


thank you for reply.

Nice trick, but doesn't it has a performance issues?
You will iterate over all possible references to accept a single one you already have in the hand.

I thought, there should be some easy solution (like in Scope provider), where you just pass a list of JvmTypes and see it in the proposals.


Cheers,
Boris
Re: [ContentAssistant] Propose type involving ImportManager [message #1173973 is a reply to message #1173900] Wed, 06 November 2013 21:49 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13493
Registered: July 2009
Senior Member
Hi,

i have no idea on the impact/ performance, but the comparison has to be done somewhere. maybe there is a more efficient way to compare a char array to a string.

this filter thing is actually passed to the ITypesProposalProvider. JdtTypeProposalProvider does actually something similar in createSubTypeProposals

so if i understand you correct you do not want to use the typeprovider at all?!?

why dont you do this simply upfront
@Inject
	private JdtTypeProviderFactory jdtTypeProviderFatory;
	
	@Inject
	private IScopeProvider scopeProvider;
	
	@Inject
	private RewritableImportSection.Factory importSectionFactory;
	
	@Inject
	private ReplaceConverter replaceConverter;
	
	@Override
	public void completeJvmParameterizedTypeReference_Type(EObject model, Assignment assignment,
			ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
		IScope typeScope = null;
		if (context.getCurrentModel() != null) {
			typeScope = scopeProvider.getScope(context.getCurrentModel(), TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE__TYPE);
		}
		final IJvmTypeProvider jvmTypeProvider = jdtTypeProviderFatory.findOrCreateTypeProvider(context.getResource().getResourceSet());
		ICompletionProposal theProposal = createCompletionProposal("java.util.List", context);
		((ConfigurableCompletionProposal) theProposal).setAdditionalProposalInfo(new Provider<EObject>(){
			public EObject get() {
				return jvmTypeProvider.findTypeByName("java.util.List");
			}});
		((ConfigurableCompletionProposal) theProposal).setTextApplier(createTextApplier(context, typeScope, getQualifiedNameConverter(), getQualifiedNameValueConverter()));
		acceptor.accept(theProposal);
	}
	
	protected ConfigurableCompletionProposal.IReplacementTextApplier createTextApplier(
			ContentAssistContext context, IScope typeScope, 
			IQualifiedNameConverter qualifiedNameConverter, IValueConverter<String> valueConverter) {
		return new ImportingTypesProposalProvider.FQNImporter(context.getResource(), context.getViewer(), typeScope, qualifiedNameConverter,
				valueConverter, importSectionFactory, replaceConverter);
	}


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: [ContentAssistant] Propose type involving ImportManager [message #1174665 is a reply to message #1173973] Thu, 07 November 2013 08:39 Go to previous message
Boris Brodski is currently offline Boris BrodskiFriend
Messages: 111
Registered: July 2009
Senior Member
Hello Christian,

thank you very much! This is the missing peace of the puzzle I was looking for.

Here is my current solution:


public class XxxProposalProvider extends AbstractXxxProposalProvider {
  @Inject private RewritableImportSection.Factory importSectionFactory;
  @Inject private ReplaceConverter replaceConverter;
  @Inject private IScopeProvider scopeProvider;
  @Inject private IEObjectHover hover;

  @Override
  public void completeMethodDefinition_MitSperreClassList(
    final EObject model, final Assignment assignment,
    final ContentAssistContext context,
    final ICompletionProposalAcceptor acceptor) {
    List<JvmType> entities = ...; //Get list of allowed entities
    for (final JvmType jvmType : entities) {
      IScope typeScope = null;
      if (context.getCurrentModel() != null) {
        typeScope = scopeProvider.getScope(context.getCurrentModel(),
          TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE__TYPE);
      }

      ICompletionProposal theProposal = createCompletionProposal(jvmType.getQualifiedName(),
          getDisplayString(jvmType, jvmType.getQualifiedName(), jvmType.getSimpleName()), getImage(jvmType), context);
      if (theProposal != null && theProposal instanceof ConfigurableCompletionProposal) {
        ConfigurableCompletionProposal configurableCompletionProposal = (ConfigurableCompletionProposal) theProposal;
        // Use 'new Provider<EObject>() {}' if EObject should be found in the first place
        configurableCompletionProposal.setAdditionalProposalInfo(jvmType);
        configurableCompletionProposal.setHover(hover);
        configurableCompletionProposal.setTextApplier(
          createTextApplier(context, typeScope, getQualifiedNameConverter(), getQualifiedNameValueConverter()));
        acceptor.accept(theProposal);
      }
    }
  }

  protected ConfigurableCompletionProposal.IReplacementTextApplier createTextApplier(
      ContentAssistContext context, IScope typeScope, 
      IQualifiedNameConverter qualifiedNameConverter, IValueConverter<String> valueConverter) {
    return new ImportingTypesProposalProvider.FQNImporter(context.getResource(), context.getViewer(),
        typeScope, qualifiedNameConverter, valueConverter, importSectionFactory, replaceConverter);
  }
}




Cheers,
Boris

[Updated on: Thu, 07 November 2013 08:40]

Report message to a moderator

Previous Topic:(SOLVED) Cyclic resolution of lazy links problem
Next Topic:How to display contents of Xtext index (builder.state)?
Goto Forum:
  


Current Time: Fri Jul 10 14:01:08 GMT 2020

Powered by FUDForum. Page generated in 0.01550 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top