aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin/Analysis/CoreProjectFileIndex.kt
blob: 319d85b14d78e70cbe96d49f1a1775542c238b6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
package org.jetbrains.dokka

import com.intellij.openapi.Disposable
import com.intellij.openapi.components.BaseComponent
import com.intellij.openapi.extensions.ExtensionPointName
import com.intellij.openapi.module.Module
import com.intellij.openapi.project.Project
import com.intellij.openapi.projectRoots.Sdk
import com.intellij.openapi.projectRoots.SdkAdditionalData
import com.intellij.openapi.projectRoots.SdkModificator
import com.intellij.openapi.projectRoots.SdkTypeId
import com.intellij.openapi.roots.*
import com.intellij.openapi.roots.impl.ProjectOrderEnumerator
import com.intellij.openapi.util.Condition
import com.intellij.openapi.util.Key
import com.intellij.openapi.util.UserDataHolderBase
import com.intellij.openapi.vfs.StandardFileSystems
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.VirtualFileFilter
import com.intellij.psi.search.GlobalSearchScope
import com.intellij.util.messages.MessageBus
import org.jetbrains.jps.model.module.JpsModuleSourceRootType
import org.jetbrains.kotlin.cli.common.config.ContentRoot
import org.jetbrains.kotlin.cli.common.config.KotlinSourceRoot
import org.jetbrains.kotlin.cli.jvm.config.JvmClasspathRoot
import org.jetbrains.kotlin.cli.jvm.config.JvmContentRoot
import org.picocontainer.PicoContainer
import java.io.File

/**
 * Workaround for the lack of ability to create a ProjectFileIndex implementation using only
 * classes from projectModel-{api,impl}.
 */
class CoreProjectFileIndex(private val project: Project, contentRoots: List<ContentRoot>) : ProjectFileIndex, ModuleFileIndex {
    override fun iterateContent(p0: ContentIterator, p1: VirtualFileFilter?): Boolean {
        throw UnsupportedOperationException()
    }

    override fun iterateContentUnderDirectory(p0: VirtualFile, p1: ContentIterator, p2: VirtualFileFilter?): Boolean {
        throw UnsupportedOperationException()
    }

    override fun isInLibrary(p0: VirtualFile): Boolean {
        throw UnsupportedOperationException()
    }

    val sourceRoots = contentRoots.filter { it !is JvmClasspathRoot }
    val classpathRoots = contentRoots.filterIsInstance<JvmClasspathRoot>()

    val module: Module = object : UserDataHolderBase(), Module {
        override fun isDisposed(): Boolean {
            throw UnsupportedOperationException()
        }

        override fun getOptionValue(p0: String): String? {
            throw UnsupportedOperationException()
        }

        override fun clearOption(p0: String) {
            throw UnsupportedOperationException()
        }

        override fun getName(): String = "<Dokka module>"

        override fun getModuleWithLibrariesScope(): GlobalSearchScope {
            throw UnsupportedOperationException()
        }

        override fun getModuleWithDependentsScope(): GlobalSearchScope {
            throw UnsupportedOperationException()
        }

        override fun getModuleContentScope(): GlobalSearchScope {
            throw UnsupportedOperationException()
        }

        override fun isLoaded(): Boolean {
            throw UnsupportedOperationException()
        }

        override fun setOption(p0: String, p1: String?) {
            throw UnsupportedOperationException()
        }

        override fun getModuleWithDependenciesScope(): GlobalSearchScope {
            throw UnsupportedOperationException()
        }

        override fun getModuleWithDependenciesAndLibrariesScope(p0: Boolean): GlobalSearchScope {
            throw UnsupportedOperationException()
        }

        override fun getProject(): Project = this@CoreProjectFileIndex.project

        override fun getModuleContentWithDependenciesScope(): GlobalSearchScope {
            throw UnsupportedOperationException()
        }

        override fun getModuleFilePath(): String {
            throw UnsupportedOperationException()
        }

        override fun getModuleTestsWithDependentsScope(): GlobalSearchScope {
            throw UnsupportedOperationException()
        }

        override fun getModuleScope(): GlobalSearchScope {
            throw UnsupportedOperationException()
        }

        override fun getModuleScope(p0: Boolean): GlobalSearchScope {
            throw UnsupportedOperationException()
        }

        override fun getModuleRuntimeScope(p0: Boolean): GlobalSearchScope {
            throw UnsupportedOperationException()
        }

        override fun getModuleFile(): VirtualFile? {
            throw UnsupportedOperationException()
        }

        override fun <T : Any?> getExtensions(p0: ExtensionPointName<T>): Array<out T> {
            throw UnsupportedOperationException()
        }

        override fun getComponent(p0: String): BaseComponent? {
            throw UnsupportedOperationException()
        }

        override fun <T : Any?> getComponent(p0: Class<T>, p1: T): T {
            throw UnsupportedOperationException()
        }

        override fun <T : Any?> getComponent(interfaceClass: Class<T>): T? {
            if (interfaceClass == ModuleRootManager::class.java) {
                return moduleRootManager as T
            }
            throw UnsupportedOperationException()
        }

        override fun getDisposed(): Condition<*> {
            throw UnsupportedOperationException()
        }

        override fun <T : Any?> getComponents(p0: Class<T>): Array<out T> {
            throw UnsupportedOperationException()
        }

        override fun getPicoContainer(): PicoContainer {
            throw UnsupportedOperationException()
        }

        override fun hasComponent(p0: Class<*>): Boolean {
            throw UnsupportedOperationException()
        }

        override fun getMessageBus(): MessageBus {
            throw UnsupportedOperationException()
        }

        override fun dispose() {
            throw UnsupportedOperationException()
        }
    }

    private val sdk: Sdk = object : Sdk, RootProvider {
        override fun getFiles(rootType: OrderRootType): Array<out VirtualFile> = classpathRoots
                .map { StandardFileSystems.local().findFileByPath(it.file.path) }
                .filterNotNull()
                .toTypedArray()

        override fun addRootSetChangedListener(p0: RootProvider.RootSetChangedListener) {
            throw UnsupportedOperationException()
        }

        override fun addRootSetChangedListener(p0: RootProvider.RootSetChangedListener, p1: Disposable) {
            throw UnsupportedOperationException()
        }

        override fun getUrls(p0: OrderRootType): Array<out String> {
            throw UnsupportedOperationException()
        }

        override fun removeRootSetChangedListener(p0: RootProvider.RootSetChangedListener) {
            throw UnsupportedOperationException()
        }

        override fun getSdkModificator(): SdkModificator {
            throw UnsupportedOperationException()
        }

        override fun getName(): String = "<dokka SDK>"

        override fun getRootProvider(): RootProvider = this

        override fun getHomePath(): String? {
            throw UnsupportedOperationException()
        }

        override fun getVersionString(): String? {
            throw UnsupportedOperationException()
        }

        override fun getSdkAdditionalData(): SdkAdditionalData? {
            throw UnsupportedOperationException()
        }

        override fun clone(): Any {
            throw UnsupportedOperationException()
        }

        override fun getSdkType(): SdkTypeId {
            throw UnsupportedOperationException()
        }

        override fun getHomeDirectory(): VirtualFile? {
            throw UnsupportedOperationException()
        }

        override fun <T : Any?> getUserData(p0: Key<T>): T? {
            throw UnsupportedOperationException()
        }

        override fun <T : Any?> putUserData(p0: Key<T>, p1: T?) {
            throw UnsupportedOperationException()
        }
    }

    private val moduleSourceOrderEntry = object : ModuleSourceOrderEntry {
        override fun getFiles(p0: OrderRootType): Array<VirtualFile> {
            throw UnsupportedOperationException()
        }

        override fun getUrls(p0: OrderRootType): Array<String> {
            throw UnsupportedOperationException()
        }

        override fun <R : Any?> accept(p0: RootPolicy<R>, p1: R?): R {
            throw UnsupportedOperationException()
        }


        override fun getPresentableName(): String {
            throw UnsupportedOperationException()
        }

        override fun getOwnerModule(): Module = module


        override fun isValid(): Boolean {
            throw UnsupportedOperationException()
        }

        override fun compareTo(other: OrderEntry?): Int {
            throw UnsupportedOperationException()
        }

        override fun getRootModel(): ModuleRootModel = moduleRootManager

        override fun isSynthetic(): Boolean {
            throw UnsupportedOperationException()
        }
    }

    private val sdkOrderEntry = object : JdkOrderEntry {
        override fun getFiles(p0: OrderRootType): Array<VirtualFile> {
            throw UnsupportedOperationException()
        }

        override fun getUrls(p0: OrderRootType): Array<String> {
            throw UnsupportedOperationException()
        }

        override fun <R : Any?> accept(p0: RootPolicy<R>, p1: R?): R {
            throw UnsupportedOperationException()
        }

        override fun getJdkName(): String? {
            throw UnsupportedOperationException()
        }

        override fun getJdk(): Sdk = sdk

        override fun getPresentableName(): String {
            throw UnsupportedOperationException()
        }

        override fun getOwnerModule(): Module {
            throw UnsupportedOperationException()
        }

        override fun isValid(): Boolean {
            throw UnsupportedOperationException()
        }

        override fun getRootFiles(p0: OrderRootType): Array<out VirtualFile> {
            throw UnsupportedOperationException()
        }

        override fun getRootUrls(p0: OrderRootType): Array<out String> {
            throw UnsupportedOperationException()
        }

        override fun compareTo(other: OrderEntry?): Int {
            throw UnsupportedOperationException()
        }

        override fun isSynthetic(): Boolean {
            throw UnsupportedOperationException()
        }

    }

    inner class MyModuleRootManager : ModuleRootManager() {
        override fun getExternalSource(): ProjectModelExternalSource? {
            throw UnsupportedOperationException()
        }

        override fun getExcludeRoots(): Array<out VirtualFile> {
            throw UnsupportedOperationException()
        }

        override fun getContentEntries(): Array<out ContentEntry> {
            throw UnsupportedOperationException()
        }

        override fun getExcludeRootUrls(): Array<out String> {
            throw UnsupportedOperationException()
        }

        override fun <R : Any?> processOrder(p0: RootPolicy<R>?, p1: R): R {
            throw UnsupportedOperationException()
        }

        override fun getSourceRoots(p0: Boolean): Array<out VirtualFile> {
            throw UnsupportedOperationException()
        }

        override fun getSourceRoots(): Array<out VirtualFile> {
            throw UnsupportedOperationException()
        }

        override fun getSourceRoots(p0: JpsModuleSourceRootType<*>): MutableList<VirtualFile> {
            throw UnsupportedOperationException()
        }

        override fun getSourceRoots(p0: MutableSet<out JpsModuleSourceRootType<*>>): MutableList<VirtualFile> {
            throw UnsupportedOperationException()
        }

        override fun getContentRoots(): Array<out VirtualFile> {
            throw UnsupportedOperationException()
        }

        override fun orderEntries(): OrderEnumerator =
                ProjectOrderEnumerator(project, null).using(object : RootModelProvider {
                    override fun getModules(): Array<out Module> = arrayOf(module)

                    override fun getRootModel(p0: Module): ModuleRootModel = this@MyModuleRootManager
                })

        override fun <T : Any?> getModuleExtension(p0: Class<T>): T {
            throw UnsupportedOperationException()
        }

        override fun getDependencyModuleNames(): Array<out String> {
            throw UnsupportedOperationException()
        }

        override fun getModule(): Module = this@CoreProjectFileIndex.module

        override fun isSdkInherited(): Boolean {
            throw UnsupportedOperationException()
        }

        override fun getOrderEntries(): Array<out OrderEntry> = arrayOf(moduleSourceOrderEntry, sdkOrderEntry)

        override fun getSourceRootUrls(): Array<out String> {
            throw UnsupportedOperationException()
        }

        override fun getSourceRootUrls(p0: Boolean): Array<out String> {
            throw UnsupportedOperationException()
        }

        override fun getSdk(): Sdk? {
            throw UnsupportedOperationException()
        }

        override fun getContentRootUrls(): Array<out String> {
            throw UnsupportedOperationException()
        }

        override fun getModuleDependencies(): Array<out Module> {
            throw UnsupportedOperationException()
        }

        override fun getModuleDependencies(p0: Boolean): Array<out Module> {
            throw UnsupportedOperationException()
        }

        override fun getModifiableModel(): ModifiableRootModel {
            throw UnsupportedOperationException()
        }

        override fun isDependsOn(p0: Module?): Boolean {
            throw UnsupportedOperationException()
        }

        override fun getFileIndex(): ModuleFileIndex {
            return this@CoreProjectFileIndex
        }

        override fun getDependencies(): Array<out Module> {
            throw UnsupportedOperationException()
        }

        override fun getDependencies(p0: Boolean): Array<out Module> {
            throw UnsupportedOperationException()
        }
    }

    val moduleRootManager = MyModuleRootManager()

    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(file: VirtualFile): Boolean = false

    override fun getClassRootForFile(file: VirtualFile): VirtualFile? =
        classpathRoots.firstOrNull { it.contains(file) }?.let { StandardFileSystems.local().findFileByPath(it.file.path) }

    override fun getOrderEntriesForFile(file: VirtualFile): List<OrderEntry> =
        if (classpathRoots.contains(file)) listOf(sdkOrderEntry) else emptyList()

    override fun isInLibraryClasses(file: VirtualFile): Boolean = classpathRoots.contains(file)

    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(file: VirtualFile): Module? =
            if (sourceRoots.contains(file)) module else null

    private fun List<ContentRoot>.contains(file: VirtualFile): Boolean = any { it.contains(file) }

    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(file: VirtualFile): Boolean = sourceRoots.contains(file)

    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(file: VirtualFile): Boolean = false

    override fun isUnderSourceRootOfType(p0: VirtualFile, p1: MutableSet<out JpsModuleSourceRootType<*>>): Boolean {
        throw UnsupportedOperationException()
    }

    override fun getOrderEntryForFile(p0: VirtualFile): OrderEntry? {
        throw UnsupportedOperationException()
    }
}

class CoreProjectRootManager(val projectFileIndex: CoreProjectFileIndex) : ProjectRootManager() {
    override fun orderEntries(): OrderEnumerator {
        throw UnsupportedOperationException()
    }

    override fun orderEntries(p0: MutableCollection<out Module>): OrderEnumerator {
        throw UnsupportedOperationException()
    }

    override fun getContentRootsFromAllModules(): Array<out VirtualFile>? {
        throw UnsupportedOperationException()
    }

    override fun setProjectSdk(p0: Sdk?) {
        throw UnsupportedOperationException()
    }

    override fun setProjectSdkName(p0: String?) {
        throw UnsupportedOperationException()
    }

    override fun getModuleSourceRoots(p0: MutableSet<out JpsModuleSourceRootType<*>>): MutableList<VirtualFile> {
        throw UnsupportedOperationException()
    }

    override fun getContentSourceRoots(): Array<out VirtualFile> {
        throw UnsupportedOperationException()
    }

    override fun getFileIndex(): ProjectFileIndex = projectFileIndex

    override fun getProjectSdkName(): String? {
        throw UnsupportedOperationException()
    }

    override fun getProjectSdk(): Sdk? {
        throw UnsupportedOperationException()
    }

    override fun getContentRoots(): Array<out VirtualFile> {
        throw UnsupportedOperationException()
    }

    override fun getContentRootUrls(): MutableList<String> {
        throw UnsupportedOperationException()
    }

}

fun ContentRoot.contains(file: VirtualFile) = when (this) {
    is JvmContentRoot -> {
        val path = if (file.fileSystem.protocol == StandardFileSystems.JAR_PROTOCOL)
            StandardFileSystems.getVirtualFileForJar(file)?.path ?: file.path
        else
            file.path
        File(path).startsWith(this.file.absoluteFile)
    }
    is KotlinSourceRoot -> File(file.path).startsWith(File(this.path).absoluteFile)
    else -> false
}