Mark directory as sources root from plugin

Hello,


Is it possible to automatically mark a directory as 'Sources root' from the plugin?
The use case is like this: after the user selects the application type that is handled by the plugin, if it's a new project in a new directory, the directories are created to match a specific layout but I can't mark the 'src' directory as a 'Source root' from (or better said, I don't know how to do it).


Thanks,
Florin

2 comments
Comment actions Permalink

Hi there,

I haven't tested this; However -

ModifiableRootModel model = ModuleRootManager.getInstance(yourModule).getModifiableModel();

model.addContentEntry(yourVirtualFile).addSourceFolder(yourVirtualFile, false);

model.commit(); // Note - *Must* be called, or model.dispose()

Let me know if this works for you :)

Alan

0
Comment actions Permalink

Hi Alan,


Thanks for the quick reply.
It works like a charm, thank you!

There was a small trick, to call "getParent()" on the directory which I wanted marked as a source.

The code used to do this looks like this (in case someone else needs it (and I dont' do anything wrong with it):

ApplicationManager.getApplication().runWriteAction(new Runnable() {     @Override     public void run() {         Sdk goSdk = new GoSdk(project);         ProjectRootManager.getInstance(project).setProjectSdk(goSdk);         VirtualFile sourceDir;         try {             baseDir.createChildDirectory(this, "bin");             baseDir.createChildDirectory(this, "pkg");             sourceDir = baseDir.createChildDirectory(this, "src");         } catch (IOException ignored) {             return;         }         ModifiableRootModel model = ModuleRootManager.getInstance(module).getModifiableModel();         model.addContentEntry(sourceDir.getParent()).addSourceFolder(sourceDir, false);         model.commit();     } });



Kind regards,
Florin

Added workaround information. Message was edited by: Florin Patan

0

Please sign in to leave a comment.