aboutsummaryrefslogtreecommitdiff
path: root/src/Analysis
diff options
context:
space:
mode:
authorDmitry Jemerov <yole@jetbrains.com>2015-05-27 18:30:57 +0200
committerDmitry Jemerov <yole@jetbrains.com>2015-05-27 18:30:57 +0200
commit70a70ca9dad31477877251b271a329d048f7873f (patch)
tree8fd0af9a420cc7a2e489fa6b417391379f23dfe4 /src/Analysis
parentd9f135d0a783461cd8650440230a5c0c8613ea29 (diff)
downloaddokka-70a70ca9dad31477877251b271a329d048f7873f.tar.gz
dokka-70a70ca9dad31477877251b271a329d048f7873f.tar.bz2
dokka-70a70ca9dad31477877251b271a329d048f7873f.zip
update to M12 compiler API; store dependency jars in VCS instead of depending on Kotlin plugin via path variable
Diffstat (limited to 'src/Analysis')
-rw-r--r--src/Analysis/AnalysisEnvironment.kt111
-rw-r--r--src/Analysis/CompilerAPI.kt9
-rw-r--r--src/Analysis/CoreProjectFileIndex.kt97
3 files changed, 156 insertions, 61 deletions
diff --git a/src/Analysis/AnalysisEnvironment.kt b/src/Analysis/AnalysisEnvironment.kt
index 1813a4b0..1d0631bb 100644
--- a/src/Analysis/AnalysisEnvironment.kt
+++ b/src/Analysis/AnalysisEnvironment.kt
@@ -1,17 +1,31 @@
package org.jetbrains.dokka
-import org.jetbrains.kotlin.cli.common.messages.*
-import com.intellij.openapi.*
-import org.jetbrains.kotlin.cli.jvm.compiler.*
-import org.jetbrains.kotlin.resolve.*
-import org.jetbrains.kotlin.psi.*
-import java.io.File
+import com.intellij.core.CoreApplicationEnvironment
+import com.intellij.core.CoreModuleManager
+import com.intellij.mock.MockComponentManager
+import com.intellij.openapi.Disposable
+import com.intellij.openapi.module.Module
+import com.intellij.openapi.module.ModuleManager
+import com.intellij.openapi.roots.ContentIterator
+import com.intellij.openapi.roots.OrderEntry
+import com.intellij.openapi.roots.ProjectFileIndex
+import com.intellij.openapi.util.Disposer
+import com.intellij.openapi.vfs.VirtualFile
+import org.jetbrains.jps.model.module.JpsModuleSourceRootType
+import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys
+import org.jetbrains.kotlin.cli.common.messages.MessageCollector
+import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles
+import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
+import org.jetbrains.kotlin.cli.jvm.config.addJavaSourceRoot
+import org.jetbrains.kotlin.cli.jvm.config.addJvmClasspathRoot
+import org.jetbrains.kotlin.cli.jvm.config.addJvmClasspathRoots
+import org.jetbrains.kotlin.cli.jvm.config.jvmClasspathRoots
import org.jetbrains.kotlin.config.*
-import org.jetbrains.kotlin.cli.common.*
-import org.jetbrains.kotlin.cli.jvm.*
-import com.intellij.openapi.util.*
-import org.jetbrains.kotlin.descriptors.ModuleDescriptor
+import org.jetbrains.kotlin.idea.caches.resolve.KotlinCacheService
+import org.jetbrains.kotlin.idea.caches.resolve.LibraryModificationTracker
+import org.jetbrains.kotlin.idea.caches.resolve.ResolutionFacade
import org.jetbrains.kotlin.resolve.lazy.ResolveSession
+import java.io.File
/**
* Kotlin as a service entry point
@@ -33,65 +47,41 @@ public class AnalysisEnvironment(val messageCollector: MessageCollector, body: A
* Executes [processor] when analysis is complete.
* $processor: function to receive compiler environment, module and context for symbol resolution
*/
- public fun withContext<T>(processor: (JetCoreEnvironment, ResolveSession) -> T): T {
- val environment = JetCoreEnvironment.createForProduction(this, configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES)
- val resolveSession = environment.analyze()
- if (environment.getSourceFiles().isNotEmpty()) {
- resolveSession.forceResolveAll()
- }
- return processor(environment, resolveSession)
- }
+ public fun withContext<T>(processor: (KotlinCoreEnvironment, ResolutionFacade, ResolveSession) -> T): T {
+ val environment = KotlinCoreEnvironment.createForProduction(this, configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES)
+ val projectComponentManager = environment.project as MockComponentManager
- /**
- * Executes [processor] when analysis is complete.
- * $processor: function to receive compiler module and context for symbol resolution
- */
- public fun withContext<T>(processor: (ResolveSession) -> T): T {
- return withContext { environment, session -> processor(session) }
- }
+ val moduleManager = CoreModuleManager(environment.project, this)
+ CoreApplicationEnvironment.registerComponentInstance(projectComponentManager.getPicoContainer(),
+ javaClass<ModuleManager>(), moduleManager)
- /**
- * Streams files into [processor] and returns a stream of its results
- * $processor: function to receive context for symbol resolution and file for processing
- */
- public fun streamFiles<T>(processor: (ResolveSession, JetFile) -> T): Stream<T> {
- return withContext { environment, session ->
- environment.getSourceFiles().stream().map { file -> processor(session, file) }
- }
- }
+ projectComponentManager.registerService(javaClass<ProjectFileIndex>(),
+ CoreProjectFileIndex())
+ projectComponentManager.registerService(javaClass<LibraryModificationTracker>(),
+ LibraryModificationTracker(environment.project))
+ projectComponentManager.registerService(javaClass<KotlinCacheService>(),
+ KotlinCacheService(environment.project))
- /**
- * Runs [processor] for each file and collects its results into single list
- * $processor: function to receive context for symbol resolution and file for processing
- */
- public fun processFiles<T>(processor: (ResolveSession, JetFile) -> T): List<T> {
- return withContext { environment, session ->
- environment.getSourceFiles().map { file -> processor(session, file) }
- }
- }
- /**
- * Runs [processor] for each file and collects its results into single list
- * $processor: is a function to receive context for symbol resolution and file for processing
- */
- public fun processFilesFlat<T>(processor: (ResolveSession, JetFile) -> List<T>): List<T> {
- return withContext { environment, session ->
- environment.getSourceFiles().flatMap { file -> processor(session, file) }
- }
+ val sourceFiles = environment.getSourceFiles()
+ val facade = KotlinCacheService.getInstance(environment.project).getResolutionFacade(sourceFiles)
+ // TODO get rid of resolveSession once we have all necessary APIs in ResolutionFacade
+ val resolveSession = environment.analyze()
+ return processor(environment, facade, resolveSession)
}
/**
* Classpath for this environment.
*/
public val classpath: List<File>
- get() = configuration.get(JVMConfigurationKeys.CLASSPATH_KEY) ?: listOf()
+ get() = configuration.jvmClasspathRoots
/**
* Adds list of paths to classpath.
* $paths: collection of files to add
*/
public fun addClasspath(paths: List<File>) {
- configuration.addAll(JVMConfigurationKeys.CLASSPATH_KEY, paths)
+ configuration.addJvmClasspathRoots(paths)
}
/**
@@ -99,21 +89,30 @@ public class AnalysisEnvironment(val messageCollector: MessageCollector, body: A
* $path: path to add
*/
public fun addClasspath(path: File) {
- configuration.add(JVMConfigurationKeys.CLASSPATH_KEY, path)
+ configuration.addJvmClasspathRoot(path)
}
/**
* List of source roots for this environment.
*/
public val sources: List<String>
- get() = configuration.get(CommonConfigurationKeys.SOURCE_ROOTS_KEY) ?: listOf()
+ get() = configuration.get(CommonConfigurationKeys.CONTENT_ROOTS)
+ ?.filterIsInstance<KotlinSourceRoot>()
+ ?.map { it.path } ?: emptyList()
/**
* Adds list of paths to source roots.
* $list: collection of files to add
*/
public fun addSources(list: List<String>) {
- configuration.addAll(CommonConfigurationKeys.SOURCE_ROOTS_KEY, list)
+ list.forEach {
+ val file = File(it)
+ if (file.extension == "java") {
+ configuration.addJavaSourceRoot(file)
+ } else {
+ configuration.addKotlinSourceRoot(it)
+ }
+ }
}
/**
diff --git a/src/Analysis/CompilerAPI.kt b/src/Analysis/CompilerAPI.kt
index abba0afa..8b877204 100644
--- a/src/Analysis/CompilerAPI.kt
+++ b/src/Analysis/CompilerAPI.kt
@@ -10,12 +10,12 @@ import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.resolve.scopes.*
import org.jetbrains.kotlin.context.GlobalContext
import com.intellij.psi.search.GlobalSearchScope
+import org.jetbrains.kotlin.context.ProjectContext
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.lazy.ResolveSession
-fun JetCoreEnvironment.analyze(): ResolveSession {
- val globalContext = GlobalContext()
- val project = getProject()
+fun KotlinCoreEnvironment.analyze(): ResolveSession {
+ val projectContext = ProjectContext(project)
val sourceFiles = getSourceFiles()
val module = object : ModuleInfo {
@@ -23,8 +23,7 @@ fun JetCoreEnvironment.analyze(): ResolveSession {
override fun dependencies(): List<ModuleInfo> = listOf(this)
}
val resolverForProject = JvmAnalyzerFacade.setupResolverForProject(
- globalContext,
- project,
+ projectContext,
listOf(module),
{ ModuleContent(sourceFiles, GlobalSearchScope.allScope(project)) },
JvmPlatformParameters { module }
diff --git a/src/Analysis/CoreProjectFileIndex.kt b/src/Analysis/CoreProjectFileIndex.kt
new file mode 100644
index 00000000..916eab73
--- /dev/null
+++ b/src/Analysis/CoreProjectFileIndex.kt
@@ -0,0 +1,97 @@
+package org.jetbrains.dokka
+
+import com.intellij.openapi.module.Module
+import com.intellij.openapi.roots.ContentIterator
+import com.intellij.openapi.roots.OrderEntry
+import com.intellij.openapi.roots.ProjectFileIndex
+import com.intellij.openapi.vfs.VirtualFile
+import org.jetbrains.jps.model.module.JpsModuleSourceRootType
+
+/**
+ * Workaround for the lack of ability to create a ProjectFileIndex implementation using only
+ * classes from projectModel-{api,impl}.
+ */
+class CoreProjectFileIndex(): ProjectFileIndex {
+ override fun getContentRootForFile(p0: VirtualFile): VirtualFile? {
+ throw UnsupportedOperationException()
+ }
+
+ override fun getContentRootForFile(p0: VirtualFile, p1: Boolean): VirtualFile? {
+ throw UnsupportedOperationException()
+ }
+
+ override fun getPackageNameByDirectory(p0: VirtualFile): String? {
+ throw UnsupportedOperationException()
+ }
+
+ override fun isInLibrarySource(p0: VirtualFile): Boolean {
+ throw UnsupportedOperationException()
+ }
+
+ override fun getClassRootForFile(p0: VirtualFile): VirtualFile? {
+ throw UnsupportedOperationException()
+ }
+
+ override fun getOrderEntriesForFile(p0: VirtualFile): List<OrderEntry> = emptyList()
+
+ override fun isInLibraryClasses(p0: VirtualFile): Boolean {
+ throw UnsupportedOperationException()
+ }
+
+ override fun isExcluded(p0: VirtualFile): Boolean {
+ throw UnsupportedOperationException()
+ }
+
+ override fun getSourceRootForFile(p0: VirtualFile): VirtualFile? {
+ throw UnsupportedOperationException()
+ }
+
+ override fun isUnderIgnored(p0: VirtualFile): Boolean {
+ throw UnsupportedOperationException()
+ }
+
+ override fun isLibraryClassFile(p0: VirtualFile): Boolean {
+ throw UnsupportedOperationException()
+ }
+
+ override fun getModuleForFile(p0: VirtualFile): Module? = null
+
+ override fun getModuleForFile(p0: VirtualFile, p1: Boolean): Module? {
+ throw UnsupportedOperationException()
+ }
+
+ override fun isInSource(p0: VirtualFile): Boolean {
+ throw UnsupportedOperationException()
+ }
+
+ override fun isIgnored(p0: VirtualFile): Boolean {
+ throw UnsupportedOperationException()
+ }
+
+ override fun isContentSourceFile(p0: VirtualFile): Boolean {
+ throw UnsupportedOperationException()
+ }
+
+ override fun isInSourceContent(p0: VirtualFile): Boolean = false
+
+ override fun iterateContent(p0: ContentIterator): Boolean {
+ throw UnsupportedOperationException()
+ }
+
+ override fun isInContent(p0: VirtualFile): Boolean {
+ throw UnsupportedOperationException()
+ }
+
+ override fun iterateContentUnderDirectory(p0: VirtualFile, p1: ContentIterator): Boolean {
+ throw UnsupportedOperationException()
+ }
+
+ override fun isInTestSourceContent(p0: VirtualFile): Boolean {
+ throw UnsupportedOperationException()
+ }
+
+ override fun isUnderSourceRootOfType(p0: VirtualFile, p1: MutableSet<out JpsModuleSourceRootType<*>>): Boolean {
+ throw UnsupportedOperationException()
+ }
+}
+