I can create a file but I persist it.

Hello Guys,

I suppose this is a very basic question, I'm trying to create a new class from text. And my code runs without any problem. But i can't find my file, neither in intellij nor in the file system.

Here is my code:

public class DelegateTInserter implements Runnable
{

private DataContext m_dataContext = null;

public DelegateTInserter ( final DataContext dataContext )
{
m_dataContext = dataContext;
}

public void run ()
{
try
{
Document doc = null;
Project project = ( Project ) m_dataContext.getData ( DataConstants.PROJECT );
Editor editor = ( Editor ) m_dataContext.getData ( DataConstants.EDITOR );
PsiClass currentClass = PluginUtil.getCurrentClass ( m_dataContext );
if ( editor != null ) doc = editor.getDocument ();

PsiManager manager = currentClass.getManager ();
PsiElementFactory factory = manager.getElementFactory ();

final PsiFile psiFile = factory.createFileFromText ( "AClass.java", "package com.psychotropics.tSilc; \n public class AClass{}" );
PsiDocumentManager documentManager = PsiDocumentManager.getInstance ( project );
Document document = documentManager.getDocument ( psiFile );
documentManager.commitDocument ( document );
}
catch ( Exception e )
{
String error = "The class could not be created.]]>";
throw new RuntimeException ( error, e );
}

}
}

Actually I already check the forums and that's how I obtained the knowledge on how to get this far. I think that it seems to be a big effort to write this pluggin. Still I think this is the best IDE around and I want to congratulate you.

Thank you for your help, ha and for your IDE.

Message was edited by:
Charly

3 comments
Comment actions Permalink

Hello Charly,

To create a file on disk, you need to find the directory where the file needs
to be located and call psiDirectory.add(currentClass).

I suppose this is a very basic question, I'm trying to create a new
class from text. And my code runs without any problem. But i can't
find my file, neither in intellij nor in the file system.

Here is my code:

public class DelegateTInserter implements Runnable {

private DataContext m_dataContext = null;

public DelegateTInserter ( final DataContext dataContext )
{
m_dataContext = dataContext;
}
public void run ()
{
try
{
Document doc = null;
Project project = ( Project ) m_dataContext.getData (
DataConstants.PROJECT );
Editor editor = ( Editor ) m_dataContext.getData (
DataConstants.EDITOR );
PsiClass currentClass = PluginUtil.getCurrentClass (
m_dataContext );
if ( editor != null ) doc = editor.getDocument ();
PsiManager manager = currentClass.getManager ();
PsiElementFactory factory = manager.getElementFactory ();
final PsiFile psiFile = factory.createFileFromText (
"AClass.java", "package com.psychotropics.tSilc; \n public class
AClass{}" );
PsiDocumentManager documentManager =
PsiDocumentManager.getInstance ( project );
Document document = documentManager.getDocument ( psiFile
);
documentManager.commitDocument ( document );
}
catch ( Exception e )
{
String error = "<error><msg>The class could not be
created.</msg></error>";
throw new RuntimeException ( error, e );
}
}
}
Actually I already check the forums and that's how I obtained the
knowledge on how to get this far. I think that it seems to be a big
effort to write this pluggin. Still I think this is the best IDE
around and I want to congratulate you.

Thank you for your help, ha and for your IDE.

--
Dmitry Jemerov
Software Developer
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Comment actions Permalink

find the directory where the file needs to be located ]]>

Thank you... :( but how can I do that?

0
Comment actions Permalink

I tried to find how but I still got no wonder.

this what I got until now



//factory
assert project != null;
PsiManager manager = PsiManager.getInstance ( project );
PsiElementFactory factory = manager.getElementFactory ();

final PsiFile psiFile = factory.createFileFromText ( "AClass.java", "package com.psychotropics.tSilc; \n public class AClass{}" );
PsiDocumentManager documentManager = PsiDocumentManager.getInstance ( project );
Document document = documentManager.getDocument ( psiFile );
psiFile.navigate ( true );

//final PsiPackage psiPackage = manager.findPackage ( "com.psychotropics.tSilc" );
//assert psiPackage != null;
//psiPackage.add ( psiFile );
//final PsiDirectory psiDirectory = manager.findDirectory ( psiPackage. );
//psiDirectory.add ( psiFile );
//psiFile.navigate ( true );

//documentManager.commitDocument ( document );
//psiDirectory.add ( psiFile );


thank you for continuing helping :)

0

Please sign in to leave a comment.