Scala Plugin that finds Path to Project Directory

Answered

Hi,

I'm writing a Scala Plugin for IntelliJ IDEA that uses the SyntheticMembersInjector API.

The Plugin should be able to read a configuration file from the currently opened Scala project, and inject members depending on this configuration file. However I have trouble finding a way to get the path to the project directory.

I have tried:

val currentDirectory = new java.io.File(".").getCanonicalPath

But this does not work.

To clarify, with project directory, I mean the directory of some opened project where for example build.sbt is stored, and not the project directory of the plugin itself.

Thank you!

0
4 comments

Please see https://plugins.jetbrains.com/docs/intellij/project.html and https://plugins.jetbrains.com/docs/intellij/module.html topics on how to access Project/Module roots/source roots and related information

0
Avatar
Fabian Bösiger

Thank you for the quick answer!

It seems that in order to interact with ProjectRootManager, I first need an instance of Project. However it is not clear to me how to get an instance of Project within SyntheticMembersInjector. To be more precise, my code looks something like this:

final class MyInjector extends SyntheticMembersInjector {
private val project = /* How do I get an instance of project here? */

override def injectMembers(source: ScTypeDefinition): Seq[String] = {
}
}
0

You can get it from parameter source: ScTypeDefinition -> PsiElement#getProject()

See also existing plugins using this EP https://plugins.jetbrains.com/intellij-platform-explorer?extensions=org.intellij.scala.syntheticMemberInjector

0
Avatar
Fabian Bösiger

Oh you are right thank you so much!

0

Please sign in to leave a comment.