Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Variable Resolver for content assist
Variable Resolver for content assist [message #645888] Tue, 21 December 2010 00:11 Go to next message
No real name is currently offline No real nameFriend
Messages: 101
Registered: August 2010
Senior Member
Say I have a grammar fule for a Function Call. I can provide content assist by displaying a list of functions, like foo(int a), foo1(int a, int b), etc.

However, for the arguments, I want to provide a drop down list of possible values (as the user presses tab to cycle through it, I've setup a simpleLinkedMode). For this, I need to write a VariableResolver to get a list of possible values.

I'm not sure how to add this variable resolver to my grammar rule and tell it to use it to resolve the values for each argument. For example, the content assist method prototype for my function rule is:

public void complete_Function(EObject model, RuleCall ruleCall, ContentAssistContext context, ICompletionProposalAcceptor acceptor


I looked at ContentAssistContext to see if it had some addResolver() method but it doesn't. Anyone have any ideas how I can do this?

Unfortunately, I can't use code templates since the legal values for the arguments is come from an Excel spreadsheet. I can read the spreadsheet and store the values in a data structure. Thank you for any ideas.


Re: Variable Resolver for content assist [message #645897 is a reply to message #645888] Tue, 21 December 2010 02:51 Go to previous messageGo to next message
No real name is currently offline No real nameFriend
Messages: 101
Registered: August 2010
Senior Member
I think this is what I want:
http://help.eclipse.org/helios/index.jsp?topic=/org.eclipse. platform.doc.isv/reference/api/org/eclipse/jface/text/link/P roposalPosition.html

I'll try it out.

http://dev.eclipse.org/viewcvs/viewvc.cgi/org.eclipse.jdt.ui /ui/org/eclipse/jdt/internal/ui/text/java/ParameterGuessingP roposal.java?revision=1.60&view=markup
Re: Variable Resolver for content assist [message #645911 is a reply to message #645888] Tue, 21 December 2010 07:45 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

there were some threads recently on dynamic templates. I don't know whether it is easier to adapt the regular content proposal framework to allow for variables or rather to hook into the template creation. For the latter there is a blog post from yesterday which may serve as inspiration

http://blogs.itemis.de/stundzig/archives/750

Alex
Re: Variable Resolver for content assist [message #646085 is a reply to message #645911] Tue, 21 December 2010 23:06 Go to previous messageGo to next message
No real name is currently offline No real nameFriend
Messages: 101
Registered: August 2010
Senior Member
Hi,

Thanks for your help. I seem to have most of it working. The only problem is the replacement of the text. I create a dummy proposal called "sheet.getlist". For both "sheet" and "getlist", I provide three possibilities "this", "that", "which". It shows up fine on content assist. However, when I select "this" for "sheet", sheet.getlist gets replaced by only "this". I was expecting this.getlist. Similarly, if I select say "which" for getlist, sheet.getlist gets replaced by "which" (should be sheet.which).

I can't seem to figure out what is going wrong. Seems like the problem could be in setting the replacement offset / length but I think I've done that OK.
Would anyone have any ideas?

Thanks.


	@Override
	public void complete_DataStructure(EObject model, RuleCall ruleCall, ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
		super.complete_DataStructure(model, ruleCall, context, acceptor);

		String prop = "sheet.getlist";
		StyledString displayString = new StyledString("sheet.getlist");
		String proposalText = getValueConverter().toString(prop, ruleCall.getRule().getName());
		ICompletionProposal proposal = createCompletionProposal(proposalText, displayString, null, context);
		if (proposal instanceof MyHLMConfigurableCompletionProposal) {
			MyHLMConfigurableCompletionProposal configurable = (MyHLMConfigurableCompletionProposal) proposal;
			
			Position[] positions = new Position[2];
			positions[0] = new Position(0,0);
			positions[1] = new Position(0,0);
			
			String[] values = new String[]{"this", "that", "which"};
			ICompletionProposal[][] choices = new ICompletionProposal[2][3];
			for (int i = 0; i < 2; i++) {
				for (int j = 0; j < 3; j++) {
					choices[i][j] = createCompletionProposal(values[j], context);
				}
			}
						
			int count = choices.length;
			int offset = configurable.getReplacementOffset();
			
			String[] params = new String[]{"sheet", "getlist"};
			for (int i = 0; i < count; i++) {
				ICompletionProposal p = choices[i][0];;
				Position position = positions[i];
				position.setOffset(offset);
				position.setLength(params[i].length());
				
				if (p instanceof MyHLMConfigurableCompletionProposal) {
					MyHLMConfigurableCompletionProposal t = (MyHLMConfigurableCompletionProposal) p;
					t.setReplacementOffset(offset);
					t.setReplacementLength(params[i].length());
				}
					
				offset += params[i].length() + 1;
			}
			
			configurable.setSimpleLinkedMode(context.getViewer(), positions, choices, ' ');
		}
		acceptor.accept(proposal);
	}

	@Override
	protected ConfigurableCompletionProposal doCreateProposal(String proposal, StyledString displayString, Image image, int priority, ContentAssistContext context) {
		int replacementOffset = context.getReplaceRegion().getOffset();
		int replacementLength = context.getReplaceRegion().getLength();
		MyHLMConfigurableCompletionProposal result = doCreateProposal(proposal, displayString, image, replacementOffset, replacementLength);
		result.setPriority(priority);
		result.setMatcher(context.getMatcher());
		result.setReplaceContextLength(context.getReplaceContextLength());
		return result;
	}
	
	@Override
	protected MyHLMConfigurableCompletionProposal doCreateProposal(String proposal, StyledString displayString, Image image, int replacementOffset, int replacementLength) {
		return new MyHLMConfigurableCompletionProposal(proposal, replacementOffset, replacementLength, proposal.length(), image, displayString, null, null);
	}
Re: Variable Resolver for content assist [message #646088 is a reply to message #646085] Wed, 22 December 2010 01:01 Go to previous message
No real name is currently offline No real nameFriend
Messages: 101
Registered: August 2010
Senior Member
I was able to solve my problem. Two issues:

1. Note including a InclusivePositionUpdated in the document to updated the position offsets for replacements.

2. Not overridding the getSelection() method to return the selected reigion.

Now things seems to work.
Previous Topic:Comma separated features
Next Topic:Xtext generated code missing
Goto Forum:
  


Current Time: Fri Apr 26 19:08:31 GMT 2024

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

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

Back to the top