Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Suggesting Java classes
Suggesting Java classes [message #1696175] Fri, 22 May 2015 14:26 Go to next message
Luis De Bello is currently offline Luis De BelloFriend
Messages: 95
Registered: January 2015
Member
Hi guys,

I am building a grammar which has to include the ability to "import" java classes, My current DSL looks like

header: {
customerName: "Cosme",
customerNumber: 1234
} as :object {class: "example.model.Header"}

So I want to be able to suggest the native java classes + the classes from the current project when the user use the autocomplete after "class:"

Do you know if there is something similar provided by xtext or I should build my own stuff for this?

Thanks in advance

Regards,
Luis

Re: Suggesting Java classes [message #1696177 is a reply to message #1696175] Fri, 22 May 2015 14:29 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
Hi,

did you consider to use xbase?

grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.xbase.Xbase

...

MyRule:
.... type=JvmParameterizedTypeReference


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Suggesting Java classes [message #1696178 is a reply to message #1696177] Fri, 22 May 2015 14:30 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
P.S: have a look at the following chapter in the docs: https://www.eclipse.org/Xtext/documentation/305_xbase.html

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Suggesting Java classes [message #1696194 is a reply to message #1696178] Fri, 22 May 2015 17:20 Go to previous messageGo to next message
Luis De Bello is currently offline Luis De BelloFriend
Messages: 95
Registered: January 2015
Member
Hi Christian,

Thanks for your answer but if I tried to use Xbase I got some error and I need to do several changes in my grammar, so I was looking for a way of reusing the logic for autocompletion when I use

javaType=[jvmTypes::JvmType|QualifiedName];

My first approach was to create my own class loader and suggest any of the classes from my project but I want to check another way to do this and I think Xbase could resolve this in a smarter way or maybe they are doing the same.

Do you have any clue about how Xtext solve this?

Regards,
Luis
Re: Suggesting Java classes [message #1696196 is a reply to message #1696194] Fri, 22 May 2015 17:59 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
Use common types only

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Suggesting Java classes [message #1696354 is a reply to message #1696196] Tue, 26 May 2015 04:04 Go to previous message
Luis De Bello is currently offline Luis De BelloFriend
Messages: 95
Registered: January 2015
Member
Hi Christian,

Thanks for pointing out to check the common types I was doing some debugging and I learnt that text ends up using the SearchEngine from eclipse adding some filters and hierarchy. As I didn't want to include Xbase to cover this simple case I have extracted some part of the code and then I use the result to generate some proposals. I left the code here in case someone need it


import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.search.IJavaSearchConstants;
import org.eclipse.jdt.core.search.IJavaSearchScope;
import org.eclipse.jdt.core.search.SearchEngine;
import org.eclipse.jdt.core.search.SearchPattern;
import org.eclipse.jdt.core.search.TypeNameRequestor;

public final class JDTResolver {

public JDTResolver() {
}

public List<JDTType> resolve(EObject model, String currentContent) {
List<JDTType> jdtTypes = new ArrayList<>();

String platformString = model.eResource().getURI().toPlatformString(true);
IFile resource = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(platformString));
IJavaSearchScope searchScope = SearchEngine.createJavaSearchScope(new IJavaElement[] { JavaCore.create(resource.getProject()) });

char[] typeName = null;
char[] packageName = null;

final int index = currentContent.lastIndexOf('.');

if (index == -1) {
// There is no package qualification perform the search only on the type name
typeName = currentContent.toCharArray();
} else if (index + 1 == currentContent.length()) {
// There is a package qualification and the last character is a dot perform the search for all types under the given package pattern for all types
typeName = "".toCharArray();

// Package name without the trailing dot
packageName = currentContent.substring(0, index).toCharArray();
} else {
// There is a package qualification, followed by a dot, and a type fragment type name without the package qualification
typeName = currentContent.substring(index + 1).toCharArray();

// Package name without the trailing dot
packageName = currentContent.substring(0, index).toCharArray();
}

JDTTypeNameRequestor requestor = new JDTTypeNameRequestor(jdtTypes);

try {
// Important: Do not use the search() method, its performance is bad compared to the searchAllTypeNames() method
new SearchEngine().searchAllTypeNames(packageName, SearchPattern.R_EXACT_MATCH, typeName, SearchPattern.R_PREFIX_MATCH, IJavaSearchConstants.CLASS_AND_INTERFACE,
searchScope, requestor, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, null);
} catch (JavaModelException e) {
// Log error here
}

return jdtTypes;
}

public static class JDTType {

private String simpleTypeName;

private String packageName;

private int modifiers;

public JDTType(String simpleTypeName, String packageName, int modifiers) {
super();
this.simpleTypeName = simpleTypeName;
this.packageName = packageName;
this.modifiers = modifiers;
}

public String getSimpleTypeName() {
return simpleTypeName;
}

public String getPackageName() {
return packageName;
}

public int getModifiers() {
return modifiers;
}

}

private static class JDTTypeNameRequestor extends TypeNameRequestor {

private List<JDTType> jdtTypes;

public JDTTypeNameRequestor(List<JDTType> jdtTypes) {
this.jdtTypes = jdtTypes;
}

@Override
public void acceptType(int modifiers, char[] packageName, char[] simpleTypeName, char[][] enclosingTypeNames, String path) {
jdtTypes.add(new JDTType(String.valueOf(simpleTypeName), String.valueOf(packageName), modifiers));
}
}
}

[Updated on: Tue, 26 May 2015 04:06]

Report message to a moderator

Previous Topic:xText as standalone lexer, parser, interpreter?
Next Topic:Qualified name for EObject without a name
Goto Forum:
  


Current Time: Thu Mar 28 09:23:44 GMT 2024

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

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

Back to the top