[xtext 2.0] protected region support? [message #690241] |
Wed, 29 June 2011 05:00  |
Eclipse User |
|
|
|
Hello,
in my Xtext 1 generator written in Xpand, I used the protected region functionality.
I can't seem to find how to migrate this functionality into Xtext 2 (or Xtend 2, for that matter).
Can anyone help?
Thanks
Mio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Re: (no subject) [message #696647 is a reply to message #693402] |
Thu, 14 July 2011 09:39   |
Eclipse User |
|
|
|
Hi
i run into the same problem. My generator should only generate a file if the file does not exists. After reading this post i was a little bit disappointed, but solved/hacked it as follows:
public class FileSystemUtils {
public void generateFileIfNotExists(IFileSystemAccess fsa, String fileName,
CharSequence contents) {
File tFile = new File(getBasePath(fsa).getAbsolutePath()
+ File.separator + fileName.substring(3));
if (!tFile.exists()) {
fsa.generateFile(fileName, contents);
}
}
public File getBasePath(IFileSystemAccess fsa) {
try {
Field rootField = fsa.getClass().getDeclaredField("root");
rootField.setAccessible(true);
Object rootObject = rootField.get(fsa);
Method folderMethod = rootObject.getClass().getMethod("getFolder",
String.class);
Object folder = folderMethod.invoke(rootObject, getProjectName(fsa)
+ File.separator + "tmp");
URI locationURI = (URI) folder.getClass()
.getMethod("getLocationURI").invoke(folder);
String tPath = locationURI.getPath();
File basePath = new File(tPath);
return basePath.getParentFile();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public String getProjectName(IFileSystemAccess fsa) {
try {
Field pathesField = AbstractFileSystemAccess.class
.getDeclaredField("pathes");
pathesField.setAccessible(true);
@SuppressWarnings("unchecked")
Map<String, String> pathes = (Map<String, String>) pathesField
.get(fsa);
return pathes.get(IFileSystemAccess.DEFAULT_OUTPUT).split(
File.separator)[1];
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
@Inject extension FileSystemUtils fsu
def doGenerate(Entity entity, IFileSystemAccess fsa){
var basePath = fsa.basePath;
fsa.generateFileIfNotExists("myProject/" + entity.name + ".h", entity.compileHeader)
It's not nice and works only with the EclipseResourceFileSystemAccess implementation, so i also would really be happy about an getBasePath method in IFileSystemAccess.
David
|
|
|
(no subject) [message #696651 is a reply to message #693402] |
Thu, 14 July 2011 09:39   |
Eclipse User |
|
|
|
Hi
i run into the same problem. My generator should only generate a file if the file does not exists. After reading this post i was a little bit disappointed, but solved/hacked it as follows:
public class FileSystemUtils {
public void generateFileIfNotExists(IFileSystemAccess fsa, String fileName,
CharSequence contents) {
File tFile = new File(getBasePath(fsa).getAbsolutePath()
+ File.separator + fileName.substring(3));
if (!tFile.exists()) {
fsa.generateFile(fileName, contents);
}
}
public File getBasePath(IFileSystemAccess fsa) {
try {
Field rootField = fsa.getClass().getDeclaredField("root");
rootField.setAccessible(true);
Object rootObject = rootField.get(fsa);
Method folderMethod = rootObject.getClass().getMethod("getFolder",
String.class);
Object folder = folderMethod.invoke(rootObject, getProjectName(fsa)
+ File.separator + "tmp");
URI locationURI = (URI) folder.getClass()
.getMethod("getLocationURI").invoke(folder);
String tPath = locationURI.getPath();
File basePath = new File(tPath);
return basePath.getParentFile();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public String getProjectName(IFileSystemAccess fsa) {
try {
Field pathesField = AbstractFileSystemAccess.class
.getDeclaredField("pathes");
pathesField.setAccessible(true);
@SuppressWarnings("unchecked")
Map<String, String> pathes = (Map<String, String>) pathesField
.get(fsa);
return pathes.get(IFileSystemAccess.DEFAULT_OUTPUT).split(
File.separator)[1];
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
@Inject extension FileSystemUtils fsu
def doGenerate(Entity entity, IFileSystemAccess fsa){
var basePath = fsa.basePath;
fsa.generateFileIfNotExists("myProject/" + entity.name + ".h", entity.compileHeader)
It's not nice and works only with the EclipseResourceFileSystemAccess implementation, so i also would really be happy about an getBasePath method in IFileSystemAccess.
David
|
|
|
|
|
|
|
|
|
|
|
|
|
Re: [xtext 2.0] protected region support? [message #760099 is a reply to message #760097] |
Thu, 01 December 2011 05:21  |
Eclipse User |
|
|
|
Thanks Hendy!
Christian: maybe... I'am new to modeling and code generation stuff (half year experience), but I found PR very useful. Can you tell me other methods for extending generated code (without inheritance or APO, i already mentioned)?
|
|
|
Powered by
FUDForum. Page generated in 0.29315 seconds