How to create complete code suggestions in Xtext Eclipse Plugin? [message #1799648] |
Tue, 11 December 2018 22:26  |
Eclipse User |
|
|
|
Hi,
I'm creating an Eclipse language plugin using Xtext. As of now, when I run the plugin, I get keyword suggestions when I press Ctrl+space. However, the keywords are suggestion one-by-one. i.e, I need to press Ctrl+space four times if the syntax has four keywords.
Quote:
eg: - if the complete syntax is 'entityset <entityname> for <entityunit>' then, when I pressed Ctrl+space once, it will suggest 'entity', then, again when I entered Ctrl+space it will suggest '<entityname> and so on. Instead I want it to suggest the complete syntax by just one Ctrl+space.
My question is, how do I make it in a way, that when I press Ctrl+space, the entire syntax will be suggested?
Below is my code: -
Domainmodel:
(elements+=MainElement)*
;
MainElement:
FileName | Type
;
Type:
Component | Layer | Description | Category | Entity | Comment
;
FileName:
'projection' name=ID ';'
;
Component:
'component' name=ID ';'
;
Layer:
'layer' name=ID ';'
;
Description:
'description' string=STRING ';'
;
Entity:
'entityset' name=ID 'for' name2=ID ';'
;
Comment:
'----------' comment=ID '----------'
;
[Updated on: Tue, 11 December 2018 22:27] by Moderator
|
|
|
|
Re: How to create complete code suggestions in Xtext Eclipse Plugin? [message #1800033 is a reply to message #1799656] |
Wed, 19 December 2018 04:06   |
Eclipse User |
|
|
|
Hi,
I looked for examples on template proposals, but I couldn't quite find a simple example. I found this posthttps://kthoms.wordpress.com/2012/05/22/xtext-content-assist-filtering-keyword-proposals/ from which I copied the first given code sample and my MyDslProposalProvider.xtend file looks like below: -
/*
* generated by Xtext 2.15.0
*/
package org.xtext.example.mydsl.ui.contentassist
import org.eclipse.xtext.Keyword
/**
* See https://www.eclipse.org/Xtext/documentation/304_ide_concepts.html#content-assist
* on how to customize the content assistant.
*/
class MyDslProposalProvider extends AbstractMyDslProposalProvider {
private static final Set<String> FILTERED_KEYWORDS = Sets.newHashSet("text", "line", "class", "behavior", "style", "custom");
@Override
public void completeKeyword(Keyword keyword, ContentAssistContext contentAssistContext, ICompletionProposalAcceptor acceptor) {
if (FILTERED_KEYWORDS.contains(keyword.getValue())) {
// don't propose keyword
return;
}
super.completeKeyword(keyword, contentAssistContext, acceptor);
}
}
However this does not appear to be correct. get errors for the below listed keywords (keyword: "error") : -
Set< : "no viable alternative at input '&'", "lt cannot be resolved to a type.", "no viable alternative at input ';'"
String> : "no viable alternative at input '&'", "gt cannot be resolved to a type.", "no viable alternative at input ';'"
FILTERED_KEYWORDS = : "FILTERED_KEYWORDS cannot be resolved to a type.", "no viable alternative at input '='"
@Override : "The annotation @Override is disallowed for this location."
void : "Primitive void cannot be a dependency."
Keyword keyword : "mismatched input 'keyword' expecting ')'"
Could you please let me know whether this is the correct approach and why these errors occur? I'll explain my question again.
Instead of code suggestions appearing for each keypress of Ctrl+space, I need the complete code to be suggested.
Eg:- If the full expression is : Hello from Xext !
I want the whole sentence to be suggested instead of having Ctrl+space= Hello, Ctrl+space= from, Ctrl+space= Xtext, Ctrl+space= !
Regards!
[Updated on: Wed, 19 December 2018 04:07] by Moderator
|
|
|
|
Re: How to create complete code suggestions in Xtext Eclipse Plugin? [message #1800036 is a reply to message #1800035] |
Wed, 19 December 2018 04:29   |
Eclipse User |
|
|
|
It is working. But now when I go to the runtime and press Ctrl+space, nothing happens. However, after I manually typed the first keyword 'Hello', keywords are suggested by each press of Ctrl+space. It is the same as before. The only thing that has happened is 'Hello' keyword not being suggested at all.
Below is my ProposalProvider file: -
package org.xtext.example.mydsl.ui.contentassist
import org.eclipse.xtext.Keyword
import com.google.inject.Inject
import org.xtext.example.mydsl.services.MyDslGrammarAccess
import org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext
import org.eclipse.xtext.ui.editor.contentassist.ICompletionProposalAcceptor
class MyDslProposalProvider extends AbstractMyDslProposalProvider {
@Inject package MyDslGrammarAccess grammarAccess
override void completeKeyword(Keyword keyword, ContentAssistContext contentAssistContext, ICompletionProposalAcceptor acceptor) {
if (!grammarAccess.getGreetingAccess().getHelloKeyword_0().equals(keyword)) {
super.completeKeyword(keyword, contentAssistContext, acceptor)
}
}
}
I read the explanation in the Doc, but I don't exactly understand the procedure that I should follow or where to actually start. Is it required to write a .xml file configuring the proposal templates that I need to implement, or can I directly do it using the ProposalProvider file? Could you kindly explain?
|
|
|
|
|
|
Re: How to create complete code suggestions in Xtext Eclipse Plugin? [message #1800054 is a reply to message #1800051] |
Wed, 19 December 2018 06:14   |
Eclipse User |
|
|
|
Behaviour without customization: In runtime, when I press Ctrl+space in a blank .dsl file, I first get 'Hello' keyword suggested. After Hello keyword, when I again press Ctrl+space, I get 'name-ID' suggested. Finally, when I again press Ctrl+space after 'name-ID', eclipse suggests '!'
Expected Behaviour: I need the complete code (Hello name !) to appear by just pressing Ctrl+space only one time. That is, I want the complete expression's template to appear in my file when I select the suggestion so that I will only need to change the variables. In my expected behavior, I do not have to press Ctrl+space 3 times to get the whole expression, but only once.
Actual Behaviour with customization: When I press Ctrl+space once in the blank .dsl file, nothing happens. If I manually typed 'Hello' and then pressed Ctrl+space, Eclipse suggests 'name-ID'. Then again when I press Ctrl+space, it suggests '!'
[Updated on: Wed, 19 December 2018 06:15] by Moderator
|
|
|
|
Powered by
FUDForum. Page generated in 0.08351 seconds