Skip to content

Commit 55a5c7a

Browse files
committed
refactor: avoid eagerly creating service instances
1 parent 849c256 commit 55a5c7a

16 files changed

Lines changed: 76 additions & 154 deletions

src/main/kotlin/com/github/lppedd/cc/completion/LookupEnhancer.kt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,6 @@ internal class LookupEnhancer(private val lookup: LookupImpl) : LookupListener,
4848
private val robot = Robot()
4949
}
5050

51-
private val commandProcessor = CommandProcessor.getInstance()
52-
private val actionManager = ActionManagerEx.getInstanceEx()
53-
private val config = CCConfigService.getInstance(lookup.project)
54-
5551
@Volatile private var lookupPopupMenuListenerDisposable: Disposable? = null
5652
@Volatile private var allActions = emptyList<AnAction>()
5753
@Volatile private var filterActions = emptyList<FilterAction>()
@@ -78,15 +74,17 @@ internal class LookupEnhancer(private val lookup: LookupImpl) : LookupListener,
7874
reopenMenu = true
7975
}
8076

81-
fun filterSelected(filterAction: FilterAction): Boolean =
82-
if (config.providerFilterType == KEEP_SELECTED) {
77+
fun filterSelected(filterAction: FilterAction): Boolean {
78+
val configService = CCConfigService.getInstance(lookup.project)
79+
return if (configService.providerFilterType == KEEP_SELECTED) {
8380
keepOnlySelectedOrReset(filterAction)
8481
menuButton?.click()
8582
false
8683
} else {
8784
reopenMenu = true
8885
true
8986
}
87+
}
9088

9189
override fun lookupShown(event: LookupEvent) {
9290
try {
@@ -157,7 +155,7 @@ internal class LookupEnhancer(private val lookup: LookupImpl) : LookupListener,
157155
var disposable = lookupPopupMenuListenerDisposable
158156
disposable?.dispose()
159157
disposable = Disposer.newDisposable(lookup, "LookupEnhancer popup menu listener")
160-
actionManager.addActionPopupMenuListener(LookupPopupMenuListener(disposable), disposable)
158+
ActionManagerEx.getInstanceEx().addActionPopupMenuListener(LookupPopupMenuListener(disposable), disposable)
161159
lookupPopupMenuListenerDisposable = disposable
162160
closeMenu = true
163161
}
@@ -185,7 +183,7 @@ internal class LookupEnhancer(private val lookup: LookupImpl) : LookupListener,
185183
.invokeCompletion(project, editor, 1)
186184
}
187185

188-
commandProcessor.executeCommand(project, command, "Invoke completion", CC.AppName)
186+
CommandProcessor.getInstance().executeCommand(project, command, "Invoke completion", CC.AppName)
189187
}
190188

191189
private inner class LookupPopupMenuListener(private val disposable: Disposable) : ActionPopupMenuListener {

src/main/kotlin/com/github/lppedd/cc/completion/menu/SettingsActions.kt

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ internal class SettingsActions(
2525
private val enhancer: LookupEnhancer,
2626
private val lookup: LookupImpl,
2727
) : ActionGroup("", false), DumbAware {
28-
private val config = CCConfigService.getInstance(lookup.project)
2928
private val actions = arrayOf(
3029
Separator.create(),
3130
CompletionModeChangeAction(),
@@ -47,20 +46,16 @@ internal class SettingsActions(
4746
ActionUpdateThread.EDT
4847

4948
override fun actionPerformed(e: AnActionEvent) {
50-
config.completionType =
51-
if (config.completionType == TEMPLATE) {
52-
POPUP
53-
} else {
54-
TEMPLATE
55-
}
56-
49+
val configService = CCConfigService.getInstance(lookup.project)
50+
configService.completionType = if (configService.completionType == TEMPLATE) POPUP else TEMPLATE
5751
enhancer.settingChanged()
5852
}
5953

6054
override fun update(event: AnActionEvent) {
55+
val configService = CCConfigService.getInstance(lookup.project)
6156
event.presentation.also {
6257
it.updateIcons(AllIcons.General.Settings)
63-
it.text = if (config.completionType == TEMPLATE) {
58+
it.text = if (configService.completionType == TEMPLATE) {
6459
"Template ${UIUtil.rightArrow()} Standard"
6560
} else {
6661
"Standard ${UIUtil.rightArrow()} Template"
@@ -74,23 +69,18 @@ internal class SettingsActions(
7469
ActionUpdateThread.EDT
7570

7671
override fun actionPerformed(e: AnActionEvent) {
77-
config.providerFilterType =
78-
if (config.providerFilterType == HIDE_SELECTED) {
79-
KEEP_SELECTED
80-
} else {
81-
HIDE_SELECTED
82-
}
83-
72+
val configService = CCConfigService.getInstance(lookup.project)
73+
configService.providerFilterType = if (configService.providerFilterType == HIDE_SELECTED) KEEP_SELECTED else HIDE_SELECTED
8474
enhancer.settingChanged()
8575
}
8676

8777
override fun update(event: AnActionEvent) {
8878
val hideSelected = CCBundle["cc.completion.menu.filter.hideSelected"]
8979
val keepSelected = CCBundle["cc.completion.menu.filter.keepSelected"]
90-
80+
val configService = CCConfigService.getInstance(lookup.project)
9181
event.presentation.also {
9282
it.updateIcons(AllIcons.General.Filter)
93-
it.text = if (config.providerFilterType == HIDE_SELECTED) {
83+
it.text = if (configService.providerFilterType == HIDE_SELECTED) {
9484
"$hideSelected ${UIUtil.rightArrow()} $keepSelected"
9585
} else {
9686
"$keepSelected ${UIUtil.rightArrow()} $hideSelected"

src/main/kotlin/com/github/lppedd/cc/configuration/CCMainConfigurable.kt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ internal class CCMainConfigurable(private val project: Project) : SearchableConf
1616
private val logger = logger<CCMainConfigurable>()
1717
}
1818

19-
private val tokensService = CCTokensService.getInstance(project)
20-
private val configService = CCConfigService.getInstance(project)
2119
private val disposable = Disposer.newDisposable("CCMainConfigurable")
2220
private lateinit var gui: CCMainConfigurableGui
2321

@@ -27,7 +25,9 @@ internal class CCMainConfigurable(private val project: Project) : SearchableConf
2725
override fun getDisplayName(): String =
2826
CCBundle["cc.plugin.name"]
2927

28+
@Suppress("LoggingSimilarMessage")
3029
override fun createComponent(): JComponent {
30+
val configService = CCConfigService.getInstance(project)
3131
gui = CCMainConfigurableGui(project, disposable)
3232
gui.completionType = configService.completionType
3333
gui.isEnableLanguageSupport = configService.enableLanguageSupport
@@ -36,7 +36,7 @@ internal class CCMainConfigurable(private val project: Project) : SearchableConf
3636
gui.customTokensFilePath = configService.customFilePath
3737
gui.customCoAuthorsFilePath = configService.customCoAuthorsFilePath
3838

39-
@Suppress("LoggingSimilarMessage")
39+
val tokensService = CCTokensService.getInstance(project)
4040
val tokens = when (val result = tokensService.getTokens()) {
4141
is TokensResult.Success -> result.tokens
4242
is TokensResult.FileError -> {
@@ -53,24 +53,28 @@ internal class CCMainConfigurable(private val project: Project) : SearchableConf
5353
return gui.rootPanel
5454
}
5555

56-
override fun isModified(): Boolean =
57-
gui.isValid && (
56+
override fun isModified(): Boolean {
57+
val configService = CCConfigService.getInstance(project)
58+
return gui.isValid && (
5859
gui.completionType != configService.completionType ||
5960
gui.isEnableLanguageSupport != configService.enableLanguageSupport ||
6061
gui.isPrioritizeRecentlyUsed != configService.prioritizeRecentlyUsed ||
6162
gui.isAutoInsertSpaceAfterColon != configService.autoInsertSpaceAfterColon ||
6263
gui.customCoAuthorsFilePath != configService.customCoAuthorsFilePath ||
6364
gui.customTokensFilePath != configService.customFilePath)
65+
}
6466

67+
@Suppress("LoggingSimilarMessage")
6568
override fun apply() {
69+
val configService = CCConfigService.getInstance(project)
6670
configService.completionType = gui.completionType
6771
configService.enableLanguageSupport = gui.isEnableLanguageSupport
6872
configService.prioritizeRecentlyUsed = gui.isPrioritizeRecentlyUsed
6973
configService.autoInsertSpaceAfterColon = gui.isAutoInsertSpaceAfterColon
7074
configService.customCoAuthorsFilePath = gui.customCoAuthorsFilePath
7175
configService.customFilePath = gui.customTokensFilePath
7276

73-
@Suppress("LoggingSimilarMessage")
77+
val tokensService = CCTokensService.getInstance(project)
7478
when (val result = tokensService.getTokens()) {
7579
is TokensResult.Success -> gui.setTokens(result.tokens.types)
7680
is TokensResult.FileError -> {
@@ -89,6 +93,7 @@ internal class CCMainConfigurable(private val project: Project) : SearchableConf
8993
}
9094

9195
override fun reset() {
96+
val configService = CCConfigService.getInstance(project)
9297
gui.completionType = configService.completionType
9398
gui.isEnableLanguageSupport = configService.enableLanguageSupport
9499
gui.isPrioritizeRecentlyUsed = configService.prioritizeRecentlyUsed

src/main/kotlin/com/github/lppedd/cc/configuration/CCProvidersConfigurable.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ import javax.swing.JPanel
1010
/**
1111
* @author Edoardo Luppi
1212
*/
13-
internal class CCProvidersConfigurable(project: Project) : SearchableConfigurable {
14-
private val configService = CCConfigService.getInstance(project)
15-
private val providerService = CommitTokenProviderService.getInstance(project)
13+
internal class CCProvidersConfigurable(private val project: Project) : SearchableConfigurable {
1614
private lateinit var gui: CCProvidersConfigurableGui
1715

1816
override fun getId(): String =
@@ -22,6 +20,7 @@ internal class CCProvidersConfigurable(project: Project) : SearchableConfigurabl
2220
CCBundle["cc.config.providers"]
2321

2422
override fun createComponent(): JPanel {
23+
val providerService = CommitTokenProviderService.getInstance(project)
2524
gui = CCProvidersConfigurableGui()
2625
gui.setProviders(
2726
providerService.getTypeProviders(),
@@ -39,6 +38,7 @@ internal class CCProvidersConfigurable(project: Project) : SearchableConfigurabl
3938
gui.isModified
4039

4140
override fun apply() {
41+
val configService = CCConfigService.getInstance(project)
4242
configService.setTypeProvidersOrder(
4343
gui.typeProviders
4444
.mapIndexed { index, provider -> provider.getId() to index }

src/main/kotlin/com/github/lppedd/cc/configuration/component/DefaultTokensFilePickerPanel.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ import javax.swing.event.DocumentEvent
2828
* @author Edoardo Luppi
2929
*/
3030
internal class DefaultTokensFilePickerPanel(
31-
project: Project,
31+
private val project: Project,
3232
private val disposable: Disposable,
3333
) : JPanel(GridLayoutManager(2, 1, JBUI.emptyInsets(), 0, 5.scaled)) {
34-
private val tokensService = CCTokensService.getInstance(project)
3534
private val isCustomFile = JBCheckBox(CCBundle["cc.config.customFile"]).also {
3635
it.addItemListener { event ->
3736
when (event.stateChange) {
@@ -142,6 +141,7 @@ internal class DefaultTokensFilePickerPanel(
142141
return ValidationInfo(CCBundle["cc.config.filePicker.error.path"], customFile)
143142
}
144143

144+
val tokensService = CCTokensService.getInstance(project)
145145
val result = tokensService.validateTokensFile(file)
146146

147147
if (result != null) {

src/main/kotlin/com/github/lppedd/cc/inspection/CommitNamingConventionInspectionOptions.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,23 +51,23 @@ internal class CommitNamingConventionInspectionOptions : ConfigurableUi<Project>
5151
)
5252

5353
override fun reset(project: Project) {
54-
val config = CCConfigService.getInstance(project)
55-
typePatternTextField.text = config.typeNamingPattern
56-
scopePatternTextField.text = config.scopeNamingPattern
54+
val configService = CCConfigService.getInstance(project)
55+
typePatternTextField.text = configService.typeNamingPattern
56+
scopePatternTextField.text = configService.scopeNamingPattern
5757
}
5858

5959
override fun isModified(project: Project): Boolean {
60-
val config = CCConfigService.getInstance(project)
60+
val configService = CCConfigService.getInstance(project)
6161
return typePatternTextField.isContentValid &&
6262
scopePatternTextField.isContentValid && (
63-
typePatternTextField.text != config.typeNamingPattern ||
64-
scopePatternTextField.text != config.scopeNamingPattern)
63+
typePatternTextField.text != configService.typeNamingPattern ||
64+
scopePatternTextField.text != configService.scopeNamingPattern)
6565
}
6666

6767
override fun apply(project: Project) {
68-
val config = CCConfigService.getInstance(project)
69-
config.typeNamingPattern = typePatternTextField.text
70-
config.scopeNamingPattern = scopePatternTextField.text
68+
val configService = CCConfigService.getInstance(project)
69+
configService.typeNamingPattern = typePatternTextField.text
70+
configService.scopeNamingPattern = scopePatternTextField.text
7171
}
7272

7373
override fun getComponent(): JComponent =

src/main/kotlin/com/github/lppedd/cc/language/ConventionalCommitSyntaxHighlighter.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import com.intellij.openapi.editor.colors.TextAttributesKey.createTextAttributes
1515
/**
1616
* @author Edoardo Luppi
1717
*/
18-
internal class ConventionalCommitSyntaxHighlighter(project: Project?) : SyntaxHighlighterBase() {
18+
internal class ConventionalCommitSyntaxHighlighter(private val project: Project?) : SyntaxHighlighterBase() {
1919
companion object {
2020
@JvmField val TYPE: TextAttributesKey = attrs("CONVENTIONAL_COMMIT_TYPE", TEXT)
2121
@JvmField val SCOPE: TextAttributesKey = attrs("CONVENTIONAL_COMMIT_SCOPE", TEXT)
@@ -45,13 +45,11 @@ internal class ConventionalCommitSyntaxHighlighter(project: Project?) : SyntaxHi
4545
}
4646
}
4747

48-
private val configService = project?.let { CCConfigService.getInstance(it) }
49-
5048
override fun getHighlightingLexer(): Lexer =
5149
ConventionalCommitLexer()
5250

5351
override fun getTokenHighlights(tokenType: IElementType): Array<TextAttributesKey> =
54-
if (configService == null || configService.enableLanguageSupport) {
52+
if (project == null || CCConfigService.getInstance(project).enableLanguageSupport) {
5553
attrsMap.getOrDefault(tokenType, attrsText)
5654
} else {
5755
TextAttributesKey.EMPTY_ARRAY

src/main/kotlin/com/github/lppedd/cc/lookupElement/CommitTokenLookupElement.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ import com.intellij.codeInsight.lookup.LookupElementPresentation
1212
* @author Edoardo Luppi
1313
*/
1414
internal sealed class CommitTokenLookupElement : LookupElement() {
15-
@Volatile
16-
private var isVisible: Boolean = true
15+
@Volatile private var isVisible: Boolean = true
1716

1817
fun setVisible(isVisible: Boolean) {
1918
this.isVisible = isVisible

src/main/kotlin/com/github/lppedd/cc/lookupElement/TemplateCommitTypeLookupElement.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,13 @@ internal class TemplateCommitTypeLookupElement(
2121
psiElement: CommitTypePsiElement,
2222
commitType: CommitType,
2323
) : CommitTypeLookupElement(psiElement, commitType) {
24-
private val templateSettings = TemplateSettings.getInstance()
25-
2624
/**
2725
* When the user selects the commit type, a new template has to be initiated.
2826
* The template should start with the commit type already inserted and with the
2927
* caret positioned in the commit scope context, with an active completion popup.
3028
*/
3129
override fun handleInsert(context: InsertionContext) {
32-
val template = templateSettings.getTemplateById("ConventionalCommit-cs") as? TemplateImpl ?: return
30+
val template = TemplateSettings.getInstance().getTemplateById("ConventionalCommit-cs") as? TemplateImpl ?: return
3331
val project = context.project
3432
val editor = context.editor
3533

src/main/kotlin/com/github/lppedd/cc/vcs/InternalVcsService.kt

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ import com.intellij.openapi.vcs.ProjectLevelVcsManager
99
import com.intellij.openapi.vcs.VcsException
1010
import com.intellij.openapi.vfs.VirtualFile
1111
import com.intellij.vcs.log.*
12+
import com.intellij.vcs.log.data.VcsLogMultiRepoJoiner
1213
import com.intellij.vcs.log.graph.PermanentGraph
1314
import com.intellij.vcs.log.impl.VcsLogManager
1415
import com.intellij.vcs.log.impl.VcsProjectLog
1516
import com.intellij.vcs.log.visible.filters.VcsLogFilterObject
17+
import fleet.multiplatform.shims.ConcurrentHashSet
1618
import java.util.*
1719
import java.util.Collections.newSetFromMap
1820
import kotlin.coroutines.cancellation.CancellationException
@@ -26,18 +28,12 @@ internal class InternalVcsService(private val project: Project) : VcsService {
2628
private val logger = logger<InternalVcsService>()
2729
}
2830

29-
private val refreshListeners = mutableSetOf<VcsListener>()
31+
private val refreshListeners = ConcurrentHashSet<VcsListener>()
3032
private val vcsLogRefresher = MyVcsLogRefresher()
31-
private val projectVcsManager = ProjectLevelVcsManager.getInstance(project)
32-
private val vcsRepositoryManager = VcsRepositoryManager.getInstance(project)
33-
private val vcsLogMultiRepoJoiner = VcsLogMultiRepoJoiner<Hash, VcsCommitMetadata>()
3433
private val subscribedVcsLogProviders = newSetFromMap<VcsLogProvider>(IdentityHashMap(16))
3534

36-
@Volatile
37-
private var cachedCurrentUser: Collection<VcsUser> = emptyList()
38-
39-
@Volatile
40-
private var cachedCommits: Collection<VcsCommitMetadata> = emptyList()
35+
@Volatile private var cachedCurrentUser: Set<VcsUser> = emptySet()
36+
@Volatile private var cachedCommits: List<VcsCommitMetadata> = emptyList()
4137

4238
override fun refresh() {
4339
val vcsLogProviders = getVcsLogProviders()
@@ -87,11 +83,12 @@ internal class InternalVcsService(private val project: Project) : VcsService {
8783
}
8884
}.toSet()
8985

86+
@Suppress("UnstableApiUsage")
9087
private fun <T : Comparable<T>> fetchCommits(sortBy: (VcsCommitMetadata) -> T): List<VcsCommitMetadata> =
9188
getVcsLogProviders()
9289
.map { (root, vcsLogProvider) -> fetchCommitsFromLogProvider(root, vcsLogProvider) }
9390
.toList()
94-
.let(vcsLogMultiRepoJoiner::join)
91+
.let { VcsLogMultiRepoJoiner<Hash, VcsCommitMetadata>().join(it) }
9592
.sortedByDescending(sortBy)
9693

9794
private fun fetchCommitsFromLogProvider(root: VirtualFile, vscLogProvider: VcsLogProvider): List<VcsCommitMetadata> {
@@ -103,6 +100,7 @@ internal class InternalVcsService(private val project: Project) : VcsService {
103100
return emptyList()
104101
}
105102

103+
val vcsRepositoryManager = VcsRepositoryManager.getInstance(project)
106104
val repository = vcsRepositoryManager.getRepositoryForRoot(root)
107105

108106
// If the repository is fresh, it means it doesn't have commits yet, and so no branches.
@@ -144,6 +142,7 @@ internal class InternalVcsService(private val project: Project) : VcsService {
144142
}
145143

146144
private fun getVcsLogProviders(): Map<VirtualFile, VcsLogProvider> {
145+
val projectVcsManager = ProjectLevelVcsManager.getInstance(project)
147146
val activeVcsRoots = projectVcsManager.getAllVcsRoots().toList()
148147
return VcsLogManager.findLogProviders(activeVcsRoots, project)
149148
}

0 commit comments

Comments
 (0)