Mark folder as source or test root

Answered

Hi, I am new on these topics, but I should write a plugin which listen on file changes (already done with success) and set a specific folder as source or test root, based upon on a set of rules.

What's the API to mark a folder as source or test root as can be done by the IDE UI?

Thank you so much!

Antonio

3 comments
Comment actions Permalink

Thank you so much Karol Lewandowski, I am going to try it..

0
Comment actions Permalink

It worked as expected, thank you!

I published my plugin at https://plugins.jetbrains.com/plugin/18410-maven-templating-for-ij/ .

Unfortunately now I have a new issue which not prevents it from working, but it logs a boring exception into IntelliJ log: 

ERROR - ts.impl.ProjectRootManagerImpl - Assertion failed: Do not use API that changes roots from roots events. Try using invoke later or something else.
java.lang.Throwable: Assertion failed: Do not use API that changes roots from roots events. Try using invoke later or something else.
at com.intellij.openapi.diagnostic.Logger.assertTrue(Logger.java:201)
at com.intellij.openapi.roots.impl.ProjectRootManagerImpl.fireBeforeRootsChanged(ProjectRootManagerImpl.java:440)
at com.intellij.openapi.roots.impl.ProjectRootManagerImpl$BatchSession.beforeRootsChanged(ProjectRootManagerImpl.java:102)
at com.intellij.workspaceModel.ide.impl.legacyBridge.project.ProjectRootsChangeListener.beforeChanged(ProjectRootsChangeListener.kt:23)
at com.intellij.workspaceModel.ide.impl.legacyBridge.module.ModuleManagerComponentBridge$2.beforeChanged(ModuleManagerComponentBridge.kt:129)
at com.intellij.workspaceModel.ide.WorkspaceModelTopics$EventsDispatcher.beforeChanged(WorkspaceModelTopics.kt:98)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at com.intellij.util.messages.impl.MessageBusImpl.invokeListener(MessageBusImpl.java:652)
at com.intellij.util.messages.impl.MessageBusImpl.deliverMessage(MessageBusImpl.java:426)
at com.intellij.util.messages.impl.MessageBusImpl.pumpWaitingBuses(MessageBusImpl.java:401)
at com.intellij.util.messages.impl.MessageBusImpl.pumpMessages(MessageBusImpl.java:383)
at com.intellij.util.messages.impl.MessageBusImpl.access$100(MessageBusImpl.java:33)
at com.intellij.util.messages.impl.MessageBusImpl$MessagePublisher.invoke(MessageBusImpl.java:189)
at com.sun.proxy.$Proxy53.beforeChanged(Unknown Source)
at com.intellij.workspaceModel.ide.impl.WorkspaceModelImpl.onBeforeChanged(WorkspaceModelImpl.kt:84)
at com.intellij.workspaceModel.ide.impl.WorkspaceModelImpl.access$onBeforeChanged(WorkspaceModelImpl.kt:22)
at com.intellij.workspaceModel.ide.impl.WorkspaceModelImpl$updateProjectModel$1.invoke(WorkspaceModelImpl.kt:68)
at com.intellij.workspaceModel.ide.impl.WorkspaceModelImpl$updateProjectModel$1.invoke(WorkspaceModelImpl.kt:22)
at com.intellij.workspaceModel.storage.impl.VersionedEntityStorageImpl.replace(VersionedEntityStorageImpl.kt:158)
at com.intellij.workspaceModel.ide.impl.WorkspaceModelImpl.updateProjectModel(WorkspaceModelImpl.kt:68)
at com.intellij.workspaceModel.ide.impl.legacyBridge.module.roots.ModifiableRootModelBridgeImpl.commit(ModifiableRootModelBridgeImpl.kt:502)

It happens because I manipulate source roots into a virtual file system before event.

How can I prevent it?

Is there a way provided by the plugin to defer the execution of my methods after the before event exited?

Thank you so much in advance.

0

Please sign in to leave a comment.