Home » Modeling » TMF (Xtext) » Variable Resolver for content assist
Variable Resolver for content assist [message #645888] |
Tue, 21 December 2010 00:11 |
No real name 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 #646085 is a reply to message #645911] |
Tue, 21 December 2010 23:06 |
No real name 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);
}
|
|
| |
Goto Forum:
Current Time: Fri Apr 26 19:08:31 GMT 2024
Powered by FUDForum. Page generated in 0.03538 seconds
|