1 package org.jetbrains.dokka
2
3 import com.intellij.openapi.Disposable
4 import com.intellij.openapi.components.BaseComponent
5 import com.intellij.openapi.extensions.ExtensionPointName
6 import com.intellij.openapi.module.Module
7 import com.intellij.openapi.project.Project
8 import com.intellij.openapi.projectRoots.Sdk
9 import com.intellij.openapi.projectRoots.SdkAdditionalData
10 import com.intellij.openapi.projectRoots.SdkModificator
11 import com.intellij.openapi.projectRoots.SdkTypeId
12 import com.intellij.openapi.roots.*
13 import com.intellij.openapi.roots.impl.ProjectOrderEnumerator
14 import com.intellij.openapi.util.Condition
15 import com.intellij.openapi.util.Key
16 import com.intellij.openapi.util.UserDataHolderBase
17 import com.intellij.openapi.vfs.StandardFileSystems
18 import com.intellij.openapi.vfs.VirtualFile
19 import com.intellij.openapi.vfs.VirtualFileFilter
20 import com.intellij.psi.search.GlobalSearchScope
21 import com.intellij.util.messages.MessageBus
22 import org.jetbrains.jps.model.module.JpsModuleSourceRootType
23 import org.jetbrains.kotlin.cli.common.config.ContentRoot
24 import org.jetbrains.kotlin.cli.common.config.KotlinSourceRoot
25 import org.jetbrains.kotlin.cli.jvm.config.JvmClasspathRoot
26 import org.jetbrains.kotlin.cli.jvm.config.JvmContentRoot
27 import org.picocontainer.PicoContainer
28 import java.io.File
29
30 /**
31 * Workaround for the lack of ability to create a ProjectFileIndex implementation using only
32 * classes from projectModel-{api,impl}.
33 */
34 class CoreProjectFileIndex(private val project: Project, contentRoots: List<ContentRoot>) : ProjectFileIndex, ModuleFileIndex {
iterateContentnull35 override fun iterateContent(p0: ContentIterator, p1: VirtualFileFilter?): Boolean {
36 throw UnsupportedOperationException()
37 }
38
iterateContentUnderDirectorynull39 override fun iterateContentUnderDirectory(p0: VirtualFile, p1: ContentIterator, p2: VirtualFileFilter?): Boolean {
40 throw UnsupportedOperationException()
41 }
42
isInLibrarynull43 override fun isInLibrary(p0: VirtualFile): Boolean {
44 throw UnsupportedOperationException()
45 }
46
<lambda>null47 val sourceRoots = contentRoots.filter { it !is JvmClasspathRoot }
48 val classpathRoots = contentRoots.filterIsInstance<JvmClasspathRoot>()
49
50 val module: Module = object : UserDataHolderBase(), Module {
isDisposednull51 override fun isDisposed(): Boolean {
52 throw UnsupportedOperationException()
53 }
54
getOptionValuenull55 override fun getOptionValue(p0: String): String? {
56 throw UnsupportedOperationException()
57 }
58
clearOptionnull59 override fun clearOption(p0: String) {
60 throw UnsupportedOperationException()
61 }
62
getNamenull63 override fun getName(): String = "<Dokka module>"
64
65 override fun getModuleWithLibrariesScope(): GlobalSearchScope {
66 throw UnsupportedOperationException()
67 }
68
getModuleWithDependentsScopenull69 override fun getModuleWithDependentsScope(): GlobalSearchScope {
70 throw UnsupportedOperationException()
71 }
72
getModuleContentScopenull73 override fun getModuleContentScope(): GlobalSearchScope {
74 throw UnsupportedOperationException()
75 }
76
isLoadednull77 override fun isLoaded(): Boolean {
78 throw UnsupportedOperationException()
79 }
80
setOptionnull81 override fun setOption(p0: String, p1: String?) {
82 throw UnsupportedOperationException()
83 }
84
getModuleWithDependenciesScopenull85 override fun getModuleWithDependenciesScope(): GlobalSearchScope {
86 throw UnsupportedOperationException()
87 }
88
getModuleWithDependenciesAndLibrariesScopenull89 override fun getModuleWithDependenciesAndLibrariesScope(p0: Boolean): GlobalSearchScope {
90 throw UnsupportedOperationException()
91 }
92
getProjectnull93 override fun getProject(): Project = this@CoreProjectFileIndex.project
94
95 override fun getModuleContentWithDependenciesScope(): GlobalSearchScope {
96 throw UnsupportedOperationException()
97 }
98
getModuleFilePathnull99 override fun getModuleFilePath(): String {
100 throw UnsupportedOperationException()
101 }
102
getModuleTestsWithDependentsScopenull103 override fun getModuleTestsWithDependentsScope(): GlobalSearchScope {
104 throw UnsupportedOperationException()
105 }
106
getModuleScopenull107 override fun getModuleScope(): GlobalSearchScope {
108 throw UnsupportedOperationException()
109 }
110
getModuleScopenull111 override fun getModuleScope(p0: Boolean): GlobalSearchScope {
112 throw UnsupportedOperationException()
113 }
114
getModuleRuntimeScopenull115 override fun getModuleRuntimeScope(p0: Boolean): GlobalSearchScope {
116 throw UnsupportedOperationException()
117 }
118
getModuleFilenull119 override fun getModuleFile(): VirtualFile? {
120 throw UnsupportedOperationException()
121 }
122
getExtensionsnull123 override fun <T : Any?> getExtensions(p0: ExtensionPointName<T>): Array<out T> {
124 throw UnsupportedOperationException()
125 }
126
getComponentnull127 override fun getComponent(p0: String): BaseComponent? {
128 throw UnsupportedOperationException()
129 }
130
getComponentnull131 override fun <T : Any?> getComponent(p0: Class<T>, p1: T): T {
132 throw UnsupportedOperationException()
133 }
134
getComponentnull135 override fun <T : Any?> getComponent(interfaceClass: Class<T>): T? {
136 if (interfaceClass == ModuleRootManager::class.java) {
137 return moduleRootManager as T
138 }
139 throw UnsupportedOperationException()
140 }
141
getDisposednull142 override fun getDisposed(): Condition<*> {
143 throw UnsupportedOperationException()
144 }
145
getComponentsnull146 override fun <T : Any?> getComponents(p0: Class<T>): Array<out T> {
147 throw UnsupportedOperationException()
148 }
149
getPicoContainernull150 override fun getPicoContainer(): PicoContainer {
151 throw UnsupportedOperationException()
152 }
153
hasComponentnull154 override fun hasComponent(p0: Class<*>): Boolean {
155 throw UnsupportedOperationException()
156 }
157
getMessageBusnull158 override fun getMessageBus(): MessageBus {
159 throw UnsupportedOperationException()
160 }
161
disposenull162 override fun dispose() {
163 throw UnsupportedOperationException()
164 }
165 }
166
167 private val sdk: Sdk = object : Sdk, RootProvider {
getFilesnull168 override fun getFiles(rootType: OrderRootType): Array<out VirtualFile> = classpathRoots
169 .map { StandardFileSystems.local().findFileByPath(it.file.path) }
170 .filterNotNull()
171 .toTypedArray()
172
addRootSetChangedListenernull173 override fun addRootSetChangedListener(p0: RootProvider.RootSetChangedListener) {
174 throw UnsupportedOperationException()
175 }
176
addRootSetChangedListenernull177 override fun addRootSetChangedListener(p0: RootProvider.RootSetChangedListener, p1: Disposable) {
178 throw UnsupportedOperationException()
179 }
180
getUrlsnull181 override fun getUrls(p0: OrderRootType): Array<out String> {
182 throw UnsupportedOperationException()
183 }
184
removeRootSetChangedListenernull185 override fun removeRootSetChangedListener(p0: RootProvider.RootSetChangedListener) {
186 throw UnsupportedOperationException()
187 }
188
getSdkModificatornull189 override fun getSdkModificator(): SdkModificator {
190 throw UnsupportedOperationException()
191 }
192
getNamenull193 override fun getName(): String = "<dokka SDK>"
194
195 override fun getRootProvider(): RootProvider = this
196
197 override fun getHomePath(): String? {
198 throw UnsupportedOperationException()
199 }
200
getVersionStringnull201 override fun getVersionString(): String? {
202 throw UnsupportedOperationException()
203 }
204
getSdkAdditionalDatanull205 override fun getSdkAdditionalData(): SdkAdditionalData? {
206 throw UnsupportedOperationException()
207 }
208
clonenull209 override fun clone(): Any {
210 throw UnsupportedOperationException()
211 }
212
getSdkTypenull213 override fun getSdkType(): SdkTypeId {
214 throw UnsupportedOperationException()
215 }
216
getHomeDirectorynull217 override fun getHomeDirectory(): VirtualFile? {
218 throw UnsupportedOperationException()
219 }
220
getUserDatanull221 override fun <T : Any?> getUserData(p0: Key<T>): T? {
222 throw UnsupportedOperationException()
223 }
224
putUserDatanull225 override fun <T : Any?> putUserData(p0: Key<T>, p1: T?) {
226 throw UnsupportedOperationException()
227 }
228 }
229
230 private val moduleSourceOrderEntry = object : ModuleSourceOrderEntry {
getFilesnull231 override fun getFiles(p0: OrderRootType): Array<VirtualFile> {
232 throw UnsupportedOperationException()
233 }
234
getUrlsnull235 override fun getUrls(p0: OrderRootType): Array<String> {
236 throw UnsupportedOperationException()
237 }
238
acceptnull239 override fun <R : Any?> accept(p0: RootPolicy<R>, p1: R?): R {
240 throw UnsupportedOperationException()
241 }
242
243
getPresentableNamenull244 override fun getPresentableName(): String {
245 throw UnsupportedOperationException()
246 }
247
getOwnerModulenull248 override fun getOwnerModule(): Module = module
249
250
251 override fun isValid(): Boolean {
252 throw UnsupportedOperationException()
253 }
254
compareTonull255 override fun compareTo(other: OrderEntry?): Int {
256 throw UnsupportedOperationException()
257 }
258
getRootModelnull259 override fun getRootModel(): ModuleRootModel = moduleRootManager
260
261 override fun isSynthetic(): Boolean {
262 throw UnsupportedOperationException()
263 }
264 }
265
266 private val sdkOrderEntry = object : JdkOrderEntry {
getFilesnull267 override fun getFiles(p0: OrderRootType): Array<VirtualFile> {
268 throw UnsupportedOperationException()
269 }
270
getUrlsnull271 override fun getUrls(p0: OrderRootType): Array<String> {
272 throw UnsupportedOperationException()
273 }
274
acceptnull275 override fun <R : Any?> accept(p0: RootPolicy<R>, p1: R?): R {
276 throw UnsupportedOperationException()
277 }
278
getJdkNamenull279 override fun getJdkName(): String? {
280 throw UnsupportedOperationException()
281 }
282
getJdknull283 override fun getJdk(): Sdk = sdk
284
285 override fun getPresentableName(): String {
286 throw UnsupportedOperationException()
287 }
288
getOwnerModulenull289 override fun getOwnerModule(): Module {
290 throw UnsupportedOperationException()
291 }
292
isValidnull293 override fun isValid(): Boolean {
294 throw UnsupportedOperationException()
295 }
296
getRootFilesnull297 override fun getRootFiles(p0: OrderRootType): Array<out VirtualFile> {
298 throw UnsupportedOperationException()
299 }
300
getRootUrlsnull301 override fun getRootUrls(p0: OrderRootType): Array<out String> {
302 throw UnsupportedOperationException()
303 }
304
compareTonull305 override fun compareTo(other: OrderEntry?): Int {
306 throw UnsupportedOperationException()
307 }
308
isSyntheticnull309 override fun isSynthetic(): Boolean {
310 throw UnsupportedOperationException()
311 }
312
313 }
314
315 inner class MyModuleRootManager : ModuleRootManager() {
getExternalSourcenull316 override fun getExternalSource(): ProjectModelExternalSource? {
317 throw UnsupportedOperationException()
318 }
319
getExcludeRootsnull320 override fun getExcludeRoots(): Array<out VirtualFile> {
321 throw UnsupportedOperationException()
322 }
323
getContentEntriesnull324 override fun getContentEntries(): Array<out ContentEntry> {
325 throw UnsupportedOperationException()
326 }
327
getExcludeRootUrlsnull328 override fun getExcludeRootUrls(): Array<out String> {
329 throw UnsupportedOperationException()
330 }
331
processOrdernull332 override fun <R : Any?> processOrder(p0: RootPolicy<R>?, p1: R): R {
333 throw UnsupportedOperationException()
334 }
335
getSourceRootsnull336 override fun getSourceRoots(p0: Boolean): Array<out VirtualFile> {
337 throw UnsupportedOperationException()
338 }
339
getSourceRootsnull340 override fun getSourceRoots(): Array<out VirtualFile> {
341 throw UnsupportedOperationException()
342 }
343
getSourceRootsnull344 override fun getSourceRoots(p0: JpsModuleSourceRootType<*>): MutableList<VirtualFile> {
345 throw UnsupportedOperationException()
346 }
347
getSourceRootsnull348 override fun getSourceRoots(p0: MutableSet<out JpsModuleSourceRootType<*>>): MutableList<VirtualFile> {
349 throw UnsupportedOperationException()
350 }
351
getContentRootsnull352 override fun getContentRoots(): Array<out VirtualFile> {
353 throw UnsupportedOperationException()
354 }
355
orderEntriesnull356 override fun orderEntries(): OrderEnumerator =
357 ProjectOrderEnumerator(project, null).using(object : RootModelProvider {
358 override fun getModules(): Array<out Module> = arrayOf(module)
359
360 override fun getRootModel(p0: Module): ModuleRootModel = this@MyModuleRootManager
361 })
362
getModuleExtensionnull363 override fun <T : Any?> getModuleExtension(p0: Class<T>): T {
364 throw UnsupportedOperationException()
365 }
366
getDependencyModuleNamesnull367 override fun getDependencyModuleNames(): Array<out String> {
368 throw UnsupportedOperationException()
369 }
370
getModulenull371 override fun getModule(): Module = this@CoreProjectFileIndex.module
372
373 override fun isSdkInherited(): Boolean {
374 throw UnsupportedOperationException()
375 }
376
getOrderEntriesnull377 override fun getOrderEntries(): Array<out OrderEntry> = arrayOf(moduleSourceOrderEntry, sdkOrderEntry)
378
379 override fun getSourceRootUrls(): Array<out String> {
380 throw UnsupportedOperationException()
381 }
382
getSourceRootUrlsnull383 override fun getSourceRootUrls(p0: Boolean): Array<out String> {
384 throw UnsupportedOperationException()
385 }
386
getSdknull387 override fun getSdk(): Sdk? {
388 throw UnsupportedOperationException()
389 }
390
getContentRootUrlsnull391 override fun getContentRootUrls(): Array<out String> {
392 throw UnsupportedOperationException()
393 }
394
getModuleDependenciesnull395 override fun getModuleDependencies(): Array<out Module> {
396 throw UnsupportedOperationException()
397 }
398
getModuleDependenciesnull399 override fun getModuleDependencies(p0: Boolean): Array<out Module> {
400 throw UnsupportedOperationException()
401 }
402
getModifiableModelnull403 override fun getModifiableModel(): ModifiableRootModel {
404 throw UnsupportedOperationException()
405 }
406
isDependsOnnull407 override fun isDependsOn(p0: Module?): Boolean {
408 throw UnsupportedOperationException()
409 }
410
getFileIndexnull411 override fun getFileIndex(): ModuleFileIndex {
412 return this@CoreProjectFileIndex
413 }
414
getDependenciesnull415 override fun getDependencies(): Array<out Module> {
416 throw UnsupportedOperationException()
417 }
418
getDependenciesnull419 override fun getDependencies(p0: Boolean): Array<out Module> {
420 throw UnsupportedOperationException()
421 }
422 }
423
424 val moduleRootManager = MyModuleRootManager()
425
getContentRootForFilenull426 override fun getContentRootForFile(p0: VirtualFile): VirtualFile? {
427 throw UnsupportedOperationException()
428 }
429
getContentRootForFilenull430 override fun getContentRootForFile(p0: VirtualFile, p1: Boolean): VirtualFile? {
431 throw UnsupportedOperationException()
432 }
433
getPackageNameByDirectorynull434 override fun getPackageNameByDirectory(p0: VirtualFile): String? {
435 throw UnsupportedOperationException()
436 }
437
isInLibrarySourcenull438 override fun isInLibrarySource(file: VirtualFile): Boolean = false
439
440 override fun getClassRootForFile(file: VirtualFile): VirtualFile? =
441 classpathRoots.firstOrNull { it.contains(file) }?.let { StandardFileSystems.local().findFileByPath(it.file.path) }
442
getOrderEntriesForFilenull443 override fun getOrderEntriesForFile(file: VirtualFile): List<OrderEntry> =
444 if (classpathRoots.contains(file)) listOf(sdkOrderEntry) else emptyList()
445
446 override fun isInLibraryClasses(file: VirtualFile): Boolean = classpathRoots.contains(file)
447
448 override fun isExcluded(p0: VirtualFile): Boolean {
449 throw UnsupportedOperationException()
450 }
451
getSourceRootForFilenull452 override fun getSourceRootForFile(p0: VirtualFile): VirtualFile? {
453 throw UnsupportedOperationException()
454 }
455
isUnderIgnorednull456 override fun isUnderIgnored(p0: VirtualFile): Boolean {
457 throw UnsupportedOperationException()
458 }
459
isLibraryClassFilenull460 override fun isLibraryClassFile(p0: VirtualFile): Boolean {
461 throw UnsupportedOperationException()
462 }
463
getModuleForFilenull464 override fun getModuleForFile(file: VirtualFile): Module? =
465 if (sourceRoots.contains(file)) module else null
466
467 private fun List<ContentRoot>.contains(file: VirtualFile): Boolean = any { it.contains(file) }
468
getModuleForFilenull469 override fun getModuleForFile(p0: VirtualFile, p1: Boolean): Module? {
470 throw UnsupportedOperationException()
471 }
472
isInSourcenull473 override fun isInSource(p0: VirtualFile): Boolean {
474 throw UnsupportedOperationException()
475 }
476
isIgnorednull477 override fun isIgnored(p0: VirtualFile): Boolean {
478 throw UnsupportedOperationException()
479 }
480
isContentSourceFilenull481 override fun isContentSourceFile(p0: VirtualFile): Boolean {
482 throw UnsupportedOperationException()
483 }
484
isInSourceContentnull485 override fun isInSourceContent(file: VirtualFile): Boolean = sourceRoots.contains(file)
486
487 override fun iterateContent(p0: ContentIterator): Boolean {
488 throw UnsupportedOperationException()
489 }
490
isInContentnull491 override fun isInContent(p0: VirtualFile): Boolean {
492 throw UnsupportedOperationException()
493 }
494
iterateContentUnderDirectorynull495 override fun iterateContentUnderDirectory(p0: VirtualFile, p1: ContentIterator): Boolean {
496 throw UnsupportedOperationException()
497 }
498
isInTestSourceContentnull499 override fun isInTestSourceContent(file: VirtualFile): Boolean = false
500
501 override fun isUnderSourceRootOfType(p0: VirtualFile, p1: MutableSet<out JpsModuleSourceRootType<*>>): Boolean {
502 throw UnsupportedOperationException()
503 }
504
getOrderEntryForFilenull505 override fun getOrderEntryForFile(p0: VirtualFile): OrderEntry? {
506 throw UnsupportedOperationException()
507 }
508 }
509
510 class CoreProjectRootManager(val projectFileIndex: CoreProjectFileIndex) : ProjectRootManager() {
orderEntriesnull511 override fun orderEntries(): OrderEnumerator {
512 throw UnsupportedOperationException()
513 }
514
orderEntriesnull515 override fun orderEntries(p0: MutableCollection<out Module>): OrderEnumerator {
516 throw UnsupportedOperationException()
517 }
518
getContentRootsFromAllModulesnull519 override fun getContentRootsFromAllModules(): Array<out VirtualFile>? {
520 throw UnsupportedOperationException()
521 }
522
setProjectSdknull523 override fun setProjectSdk(p0: Sdk?) {
524 throw UnsupportedOperationException()
525 }
526
setProjectSdkNamenull527 override fun setProjectSdkName(p0: String?) {
528 throw UnsupportedOperationException()
529 }
530
getModuleSourceRootsnull531 override fun getModuleSourceRoots(p0: MutableSet<out JpsModuleSourceRootType<*>>): MutableList<VirtualFile> {
532 throw UnsupportedOperationException()
533 }
534
getContentSourceRootsnull535 override fun getContentSourceRoots(): Array<out VirtualFile> {
536 throw UnsupportedOperationException()
537 }
538
getFileIndexnull539 override fun getFileIndex(): ProjectFileIndex = projectFileIndex
540
541 override fun getProjectSdkName(): String? {
542 throw UnsupportedOperationException()
543 }
544
getProjectSdknull545 override fun getProjectSdk(): Sdk? {
546 throw UnsupportedOperationException()
547 }
548
getContentRootsnull549 override fun getContentRoots(): Array<out VirtualFile> {
550 throw UnsupportedOperationException()
551 }
552
getContentRootUrlsnull553 override fun getContentRootUrls(): MutableList<String> {
554 throw UnsupportedOperationException()
555 }
556
557 }
558
containsnull559 fun ContentRoot.contains(file: VirtualFile) = when (this) {
560 is JvmContentRoot -> {
561 val path = if (file.fileSystem.protocol == StandardFileSystems.JAR_PROTOCOL)
562 StandardFileSystems.getVirtualFileForJar(file)?.path ?: file.path
563 else
564 file.path
565 File(path).startsWith(this.file.absoluteFile)
566 }
567 is KotlinSourceRoot -> File(file.path).startsWith(File(this.path).absoluteFile)
568 else -> false
569 }
570