How to access project directory of a PsiFile?

My plugin injects an in-comment language in Java and Kotlin files, which is processed by the accompanying Gradle plugin. The Gradle plugin saves it’s state to the project’s build/stonecutter-cache, which is needed to provide code completion. How do I access that directory from the CompletionProvider?.

class StitcherCompletionContributor : CompletionContributor() {
    init {
        extend(
            CompletionType.BASIC,
            PlatformPatterns.psiElement(StitcherTokenType.CONSTANT_ID),
            StitcherCompletionProvider(StitcherTokenType.CONSTANT_ID)
        )
    }

    class StitcherCompletionProvider(val type: StitcherComponentType) : CompletionProvider<CompletionParameters>() {
        override fun addCompletions(
            parameters: CompletionParameters,
            context: ProcessingContext,
            result: CompletionResultSet
        ) {
            val file: PsiFile = parameters.originalFile
        }
    }
}

virtual-file-system, this page explains IDEA’s virtual file system, which should solve your problem

You can try to use (Java) file.getProject().getBaseDir(), but read the documentation for that method carefully. You may be better off trying ProjectUtil.guessProjectDir(file.getProject()).

It looks like the equivalent Kotlin would be file.project.baseDir and file.project.guessProjectDir() respectively.

There’s also BaseProjectDirectories, which provides top-level content roots in the default implementation.
If you’d like to get the build directory, then you may have to use the Gradle plugin or the workspace model to retrieve it.