Best way for creating file in the IDE

Hi ! I'm trying to make an action for copying and creating some files.
And it's working fine in Java, but I think is wrong in jIDEA.
For example, the files are created ok, but the IDE doesn't show the prompt for adding those files to Git (or the current VCS).

What is the correct way for creating files in jIdea?

Thanks !

10 comments
Comment actions Permalink

Hi Enrique,

Please check the following settings - 'Project Settings | Version Control | Confirmation | When files are created' and ensure that it's not set to the 'Do not add'.

Denis

0
Comment actions Permalink

No, I don't have "do not add" selected, and the git dialog is working fine, the problem is with files created using my plugin, so I'm sure I'm doing it wrong, I want to know how the "jIDEA way" for creating files.
This is what I'm using right now:

// inside an Action:

ApplicationManager.getApplication().runWriteAction(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                FileUtil.copy(originalFile, newFile);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    });


The file (and directories) is created, but the git dialog doesn't appear, also the IDE doesn't refresh automatically the VCS changes and the project view (I need to try to open the new file for getting the changes in the IDE).

Thanks !

0
Comment actions Permalink

You should make VFS aware of your operations ==> use VfsUtilCore.copyFile() insread of FileUtil.copy()

Denis

0
Comment actions Permalink

OK, but that method requires two virtual files.
What happens if I don't have the parent directories for the destination file?
I've tried this:


              if( FileUtil.createParentDirs(newFile)){

                    final VirtualFile newParent = LocalFileSystem.getInstance().findFileByIoFile(newFile.getParentFile());
                    ApplicationManager.getApplication().runWriteAction(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                VfsUtilCore.copyFile(null, file, newParent);
                           } catch (IOException e) {

                                e.printStackTrace();

                            }

                        }

                    });

            }


But it only works if the parent folders exists before using the action (not when creating the directories using "createParentDirs", even when that method is working fine and newFile.getParentFile().exists() returns true.

How can I get the VF for a folder created a line before?

0
Comment actions Permalink

You shoud create the folder also via VFS - VfsUtil.createDirectories()

Denis

0
Comment actions Permalink

Thanks Denis, now it's adding the new file to the VCS changes and also the project view is updated instantly, but it doesn't show the dialog for adding the file to GIT, it just mark the file as unversioned.
This is the new code:


                        final VirtualFile newParent = VfsUtil.createDirectories(newFile.getParent());
                        if(newParent != null){
                            ApplicationManager.getApplication().runWriteAction(new Runnable() {
                                @Override
                                public void run()
                                {
                                    try {
                                        VfsUtilCore.copyFile(this, file, newParent);
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                }
                            });
                        }


Please tell me what I'm doing wrong.

0
Comment actions Permalink

Try to create parent directories inside the write action as well.

Denis

0
Comment actions Permalink

No, it doesn't show the add to git dialog

ApplicationManager.getApplication().runWriteAction(new Runnable() {
                        @Override
                        public void run()
                        {
                            try {
                                final VirtualFile newParent = VfsUtil.createDirectories(newFile.getParent());
                                if(newParent != null){
                                    VfsUtilCore.copyFile(this, file, newParent);
                                }
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    });


Do you think it should work automatically or do I need to execute something else for showing the "add to git" dialog?

0
Comment actions Permalink

I'll ask our VCS guys to have a look into this.

Denis

0
Comment actions Permalink

Don't worry Denis, it's working now, the git dialog doesn't appear if we don't open the new file.
Thanks for your help !

0

Please sign in to leave a comment.