Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Autocomplete (suggest) java type
Autocomplete (suggest) java type [message #1734110] Fri, 03 June 2016 22:56 Go to next message
Mansour Al is currently offline Mansour AlFriend
Messages: 44
Registered: June 2016
Member
I have this rule:



Action:
	type=JvmTypeReference (component=JvmTypeReference)? (data+=Data)*;




I am looking to complete this grammar with "suggest/autocomplete" for specific Java classes:

For example, if I have classes that implements Action.class, and classes that implements or extends Component.class on the classpath, like Cook.class and Egg.class, so the action would be:

cook egg  with  "salt" "peper" "oil" 



How can I get the auto complete, to work with the correct class types, and auto import the correct classes (if they are not imported) ? And how can instruct the grammar to only accept specific types ?

Note: I am using XBase and my GenerateDSL.mwe2 workFlow has component = XtextGenerator


Thank you a lot
Re: Autocomplete (suggest) java type [message #1734147 is a reply to message #1734110] Sun, 05 June 2016 06:09 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
something like https://kthoms.wordpress.com/2012/03/14/how-to-limit-proposed-java-types-to-implementors-of-an-interface/ should still work, at least with some minor mods.

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Autocomplete (suggest) java type [message #1734162 is a reply to message #1734147] Sun, 05 June 2016 14:43 Go to previous messageGo to next message
Mansour Al is currently offline Mansour AlFriend
Messages: 44
Registered: June 2016
Member
Christian,

Thank you a lot for replying. I found my way around the proposal part. I figured out it has to be done in the UI module. The link you gave was very useful, and answered the first part of the question.

The second part is about making the imported class available. For example:

[color=red]import org.example.actions.Cook ;[/color]  // This is not recognized !

Cook egg  with  "salt" "peper" "oil"



Most of the articles and tutorials solve it using an IGenrator, which is not what I am looking for. The language is to be interrupted and not looking to generate Java file from it. Additionally, how can I allow the content assist to auto-import a class when used. For example, when I select a class from the content assist, and this class is not already imported, how can I add it to the import block ?

Thank you a lot for your help.
Re: Autocomplete (suggest) java type [message #1734163 is a reply to message #1734162] Sun, 05 June 2016 14:50 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
Is the jar containing the class on the class path of the dsl file? This is required

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Autocomplete (suggest) java type [message #1734170 is a reply to message #1734163] Sun, 05 June 2016 16:59 Go to previous messageGo to next message
Mansour Al is currently offline Mansour AlFriend
Messages: 44
Registered: June 2016
Member
Yes. It's on the classpath. For example:

import java.lang.annotation.Annotation ;
import java.lang.String;


Gives me an error by red-line the class. Those classes are available as they are included with the JRE. Here's the relevant part of my grammar (in case this is the issue).

ImportSection:
	importDeclarations+=ImportDeclaration+;
ImportDeclaration:
	'import' importedType=QualifiedName ';';


Thank you


Re: Autocomplete (suggest) java type [message #1734173 is a reply to message #1734170] Sun, 05 June 2016 17:02 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
Can you share a complete sample grammar and model project

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Autocomplete (suggest) java type [message #1734176 is a reply to message #1734173] Sun, 05 June 2016 17:23 Go to previous messageGo to next message
Mansour Al is currently offline Mansour AlFriend
Messages: 44
Registered: June 2016
Member
Yes, I can. I don't know what you mean by model project. But here is the whole project.

https://github.com/malakeel/myDsl

Thank you a lot Christian.
Re: Autocomplete (suggest) java type [message #1734181 is a reply to message #1734176] Sun, 05 June 2016 18:14 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
importSection=XImportSection?
as you have it now is the correct way to add imports in xbase. you should remove your stu
ff

then your CONFIG_FILE is bad. it destroys the lexer cause the lexer is greedy.

use

CONFIG_FILE:
(ID|INT) "." ("xml" | "properties" | "data");

instead

with model project i mean the project containing the test.mydsl file you try to di the imports to.
it has to be a java project or a plugin project (a general -> project is not enhough)


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Autocomplete (suggest) java type [message #1734183 is a reply to message #1734181] Sun, 05 June 2016 20:32 Go to previous messageGo to next message
Mansour Al is currently offline Mansour AlFriend
Messages: 44
Registered: June 2016
Member
Christian,
The definition of XImportSection from xbase allows "static" and "extension" which I don't want. However, let's get it to work now with XImportSection then I will try with different definition.
The module project can be found at https://github.com/malakeel/myDsl-demo
and it's a java project.
Thank you again.
Re: Autocomplete (suggest) java type [message #1734189 is a reply to message #1734183] Mon, 06 June 2016 02:57 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
Override the rules in xbase

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Autocomplete (suggest) java type [message #1734222 is a reply to message #1734189] Mon, 06 June 2016 11:20 Go to previous messageGo to next message
Mansour Al is currently offline Mansour AlFriend
Messages: 44
Registered: June 2016
Member
Christian,
The original problem is "import" statement is not recognized at all even without modification. imported classes are decorated with a red-line.
So how to get the import to be recognized in the model project ?

Thank you
Re: Autocomplete (suggest) java type [message #1734223 is a reply to message #1734222] Mon, 06 June 2016 11:28 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
Please Post your updated grammar following my suggestions and the model
project


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Autocomplete (suggest) java type [message #1734224 is a reply to message #1734223] Mon, 06 June 2016 11:32 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
and that is the error you get.

once again:

you have to have a java project
you have to fix CONFIG_FILE


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Autocomplete (suggest) java type [message #1734225 is a reply to message #1734224] Mon, 06 June 2016 12:02 Go to previous messageGo to next message
Mansour Al is currently offline Mansour AlFriend
Messages: 44
Registered: June 2016
Member
I have updated and uploaded the grammar, and the demo-dsl project model as per your advice
The grammar can be found here https://github.com/malakeel/myDsl/blob/master/org.xtext.example.mydsl/src/org/xtext/example/mydsl/MyDsl.xtext
For the model project. It is a java project. Yet the problem is still there. The model project can be found at https://github.com/malakeel/myDsl-demo.
The sample file https://github.com/malakeel/myDsl-demo/blob/master/src/MyFile.demo showing the imports errors with tooltip:

Multiple markers at this line
- no viable alternative at character 's'
- no viable alternative at character 'J'
- Frame cannot be resolved to a type.


Thank you
Re: Autocomplete (suggest) java type [message #1734226 is a reply to message #1734225] Mon, 06 June 2016 12:09 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
as i said: it is a lexing problem

BAD (terminal rule):
terminal CONFIG_FILE:
(ID | INT) "." ("xml" | "properties" | "data");
GOOD (datatype rule):
CONFIG_FILE:
(ID | INT) "." ("xml" | "properties" | "data");


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Autocomplete (suggest) java type [message #1734303 is a reply to message #1734226] Tue, 07 June 2016 01:50 Go to previous messageGo to next message
Mansour Al is currently offline Mansour AlFriend
Messages: 44
Registered: June 2016
Member
Ouch,
I am sorry Christian. You were right. I used CONFIG_FILE as a terminal rather than ParserRule. I updated the definition only : (ID | INT) "." ("xml" | "properties" | "data");
How now that I fixed it and removed the terminal, the main issue is gone.

Now it's working fine, however, I am still getting warnings about the imported classes not being used. Additionally, I would like to disable "import static" or "extension". This is exactly how I need the import to appear:

import javax.swing.JButton;

Thank you
Re: Autocomplete (suggest) java type [message #1734307 is a reply to message #1734303] Tue, 07 June 2016 03:24 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
As I said

You can override the import decl of xbase

And I am sure you can find the place where the unused imort is calculated and produces the warning


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Latest Xtext version: 2.9.2 or 2.10.0
Next Topic:How to include the enherited class attributes in the content assistant
Goto Forum:
  


Current Time: Thu Mar 28 13:03:18 GMT 2024

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

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

Back to the top