summaryrefslogtreecommitdiff
path: root/platform/lang-impl/src/com/intellij/codeInsight/codeVision/settings/CodeVisionSettingsLiveModel.kt
blob: 51e350cd91963576ff4b1227238fde0b26239c8e (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
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.codeInsight.codeVision.settings

import com.intellij.codeInsight.codeVision.CodeVisionAnchorKind
import com.intellij.openapi.rd.createNestedDisposable
import com.intellij.openapi.util.registry.Registry
import com.intellij.openapi.util.registry.RegistryValue
import com.intellij.openapi.util.registry.RegistryValueListener
import com.intellij.util.application
import com.jetbrains.rd.util.lifetime.Lifetime
import com.jetbrains.rd.util.reactive.Property
import com.jetbrains.rd.util.reactive.ViewableMap
import com.jetbrains.rd.util.reactive.ViewableSet

class CodeVisionSettingsLiveModel(lifetime: Lifetime) {

  val isRegistryEnabled: Property<Boolean> = Property(Registry.`is`("editor.codeVision.new")).also {
    val registry = Registry.get("editor.codeVision.new")
    registry.addListener(object : RegistryValueListener {
      override fun afterValueChanged(value: RegistryValue) {
        it.value = value.asBoolean()
      }
    }, lifetime.createNestedDisposable())
  }


  val isEnabled: Property<Boolean> = Property(CodeVisionSettings.instance().codeVisionEnabled)

  val isEnabledWithRegistry: Property<Boolean> = Property(isRegistryEnabled.value && isEnabled.value).also { property ->
    isRegistryEnabled.advise(lifetime) {
      property.value = it && isEnabled.value
    }

    isEnabled.advise(lifetime) {
      property.value = it && isRegistryEnabled.value
    }
  }

  val defaultPosition: Property<CodeVisionAnchorKind> = Property(CodeVisionSettings.instance().defaultPosition)
  val visibleMetricsAboveDeclarationCount: Property<Int> = Property(CodeVisionSettings.instance().visibleMetricsAboveDeclarationCount)
  val visibleMetricsNextToDeclarationCount: Property<Int> = Property(CodeVisionSettings.instance().visibleMetricsNextToDeclarationCount)
  val disabledCodeVisionProviderIds: ViewableSet<String> = ViewableSet(CodeVisionSettings.instance().state.disabledCodeVisionProviderIds.toMutableSet())
  val codeVisionGroupToPosition: ViewableMap<String, CodeVisionAnchorKind> = ViewableMap<String, CodeVisionAnchorKind>().apply {
    putAll(CodeVisionSettings.instance().state.codeVisionGroupToPosition.map { it.key to CodeVisionAnchorKind.valueOf(it.value) })
  }

  init {
    application.messageBus.connect(lifetime.createNestedDisposable())
      .subscribe(CodeVisionSettings.CODE_LENS_SETTINGS_CHANGED,
                 object : CodeVisionSettings.CodeVisionSettingsListener {
                   override fun globalEnabledChanged(newValue: Boolean) {
                     isEnabled.value = newValue
                   }

                   override fun defaultPositionChanged(newDefaultPosition: CodeVisionAnchorKind) {
                     defaultPosition.value = newDefaultPosition
                   }

                   override fun visibleMetricsAboveDeclarationCountChanged(newValue: Int) {
                     visibleMetricsAboveDeclarationCount.value = newValue
                   }

                   override fun visibleMetricsNextToDeclarationCountChanged(newValue: Int) {
                     visibleMetricsNextToDeclarationCount.value = newValue
                   }

                   override fun providerAvailabilityChanged(id: String, isEnabled: Boolean) {
                     if (isEnabled) {
                       disabledCodeVisionProviderIds.remove(id)
                     }
                     else {
                       disabledCodeVisionProviderIds.add(id)
                     }
                   }

                   override fun groupPositionChanged(id: String, position: CodeVisionAnchorKind) {
                     codeVisionGroupToPosition[id] = position
                   }
                 })
  }

  fun getAnchorLimit(anchor: CodeVisionAnchorKind): Int? {
    return when (anchor) {
      CodeVisionAnchorKind.Top -> visibleMetricsAboveDeclarationCount.value
      CodeVisionAnchorKind.Right -> visibleMetricsNextToDeclarationCount.value
      CodeVisionAnchorKind.Default -> getAnchorLimit(defaultPosition.value)

      else -> getAnchorLimit(defaultPosition.value)
    }
  }
}