Initialise Project, must read PSI-Files
Hey,
i am currently working on https://github.com/carymrobbins/intellij-haskforce, and i want to improve the project-import experience, by adding modules and marking the source-code/tests-directories correctly
The only sane way is to use the PSI-Files to get the relevant information from the cabal-files (custom format). But i am getting multiple error:
- Project is not yet initialized: Project '/Users/LeanderK/Documents/Haskell/XY' XY
- Assertion failed: Access to psi files should be performed only after startup activity
- Project is not yet initialized: Project '/Users/LeanderK/Documents/Haskell/XY' XY
Is there a way to achieve what i want to do? For example a callback where i call the setup code only after i can get access to PSI-Files?
Please sign in to leave a comment.
postStartupActivity extension point
in your StartupActivity implementation, in runActivity, add this:
hmm ok, thank you. But then i have another problem. How can i add source-paths to existing modules?
Modules don't have the method and i don't see how i can get a ModuleBuilder out of an Module.
ProjectStructureDetector may help.
Override this class to provide automatic detection of modules and libraries for 'Create from existing sources' mode of the new project/module wizard.
I never used it so can't suggest more than what the comments say. It seems to be made for this purpose.
I solved it in another way,it is possible to create PSI-files nonetheless. You just have to do it "from scratch":
val text = try {new String(virtualFile.contentsToByteArray(), StandardCharsets.UTF_8)
} catch {
case e: IOException =>
LOG.warn(s"Could not read CabalFile $virtualFile: $e", e)
return None
}
PsiFileFactory.getInstance(defaultProject).createFileFromText(
virtualFile.getName, CabalLanguage.INSTANCE, text
) match {
case psiFile: CabalFile => Some(psiFile)
case other =>
LOG.warn(new AssertionError(s"Expected CabalFile, got: ${other.getClass}"))
None
}
as long as it works
;)
To work with existing files' PSI, postStartupActivity is indeed the easiest way.
To modify module roots, you can use ModuleRootModificationUtil or ModuleRootManager.getModifiableModel directly.