• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Camelcade / Perl5-IDEA / #525521663

29 Aug 2025 11:08AM UTC coverage: 75.815% (-0.07%) from 75.88%
#525521663

push

github

hurricup
Release 2025.2.1

14734 of 22639 branches covered (65.08%)

Branch coverage included in aggregate %.

31063 of 37767 relevant lines covered (82.25%)

0.82 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.79
/plugin/backend/src/main/java/com/perl5/lang/perl/idea/project/PerlNamesBackendCache.java
1
/*
2
 * Copyright 2015-2025 Alexandr Evstigneev
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 * http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16

17
package com.perl5.lang.perl.idea.project;
18

19
import com.intellij.ide.lightEdit.LightEdit;
20
import com.intellij.openapi.application.ApplicationManager;
21
import com.intellij.openapi.application.ReadAction;
22
import com.intellij.openapi.application.WriteAction;
23
import com.intellij.openapi.diagnostic.Logger;
24
import com.intellij.openapi.progress.ProgressManager;
25
import com.intellij.openapi.project.DumbService;
26
import com.intellij.openapi.project.Project;
27
import com.intellij.openapi.roots.ModuleRootEvent;
28
import com.intellij.openapi.roots.ModuleRootListener;
29
import com.intellij.psi.PsiManager;
30
import com.intellij.psi.PsiTreeChangeAdapter;
31
import com.intellij.psi.PsiTreeChangeEvent;
32
import com.intellij.util.messages.MessageBusConnection;
33
import com.intellij.util.ui.update.MergingUpdateQueue;
34
import com.intellij.util.ui.update.Update;
35
import com.perl5.lang.perl.psi.stubs.namespaces.PerlLightNamespaceIndex;
36
import com.perl5.lang.perl.psi.stubs.namespaces.PerlNamespaceIndex;
37
import com.perl5.lang.perl.psi.stubs.subsdeclarations.PerlSubDeclarationIndex;
38
import com.perl5.lang.perl.psi.stubs.subsdefinitions.PerlLightSubDefinitionsIndex;
39
import com.perl5.lang.perl.psi.stubs.subsdefinitions.PerlSubDefinitionsIndex;
40
import com.perl5.lang.perl.util.PerlPackageUtilCore;
41
import com.perl5.lang.perl.util.PerlTimeLogger;
42
import org.jetbrains.annotations.NotNull;
43
import org.jetbrains.annotations.TestOnly;
44

45
import java.util.Collection;
46
import java.util.Collections;
47
import java.util.HashSet;
48
import java.util.Set;
49
import java.util.concurrent.atomic.AtomicBoolean;
50

51

52
public class PerlNamesBackendCache implements PerlNamesCache {
53
  private static final Logger LOG = Logger.getInstance(PerlNamesBackendCache.class);
1✔
54
  private final MergingUpdateQueue myQueue = new MergingUpdateQueue("Perl names cache updater", 1000, true, null, this, null, false);
1✔
55
  private final Project myProject;
56
  private final AtomicBoolean myIsUpdating = new AtomicBoolean(false);
1✔
57
  private volatile Set<String> myKnownSubs = Collections.emptySet();
1✔
58
  private volatile Set<String> myKnownNamespaces = Collections.emptySet();
1✔
59
  private final AtomicBoolean myIsDisposed = new AtomicBoolean(false);
1✔
60

61
  public PerlNamesBackendCache(Project project) {
1✔
62
    myProject = project;
1✔
63
    if (LightEdit.owns(myProject) || project.isDefault()) {
1!
64
      return;
×
65
    }
66
    MessageBusConnection connection = project.getMessageBus().connect(this);
1✔
67
    connection.subscribe(ModuleRootListener.TOPIC, new ModuleRootListener() {
1✔
68
      @Override
69
      public void rootsChanged(@NotNull ModuleRootEvent event) {
70
        queueUpdate();
1✔
71
      }
1✔
72
    });
73
    connection.subscribe(DumbService.DUMB_MODE, new DumbService.DumbModeListener() {
1✔
74
      @Override
75
      public void exitDumbMode() {
76
        queueUpdate();
1✔
77
      }
1✔
78
    });
79
    PsiManager.getInstance(myProject).addPsiTreeChangeListener(new PsiTreeChangeAdapter() {
1✔
80
      @Override
81
      public void childAdded(@NotNull PsiTreeChangeEvent event) {
82
        queueUpdate();
1✔
83
      }
1✔
84

85
      @Override
86
      public void childRemoved(@NotNull PsiTreeChangeEvent event) {
87
        queueUpdate();
1✔
88
      }
1✔
89

90
      @Override
91
      public void childReplaced(@NotNull PsiTreeChangeEvent event) {
92
        queueUpdate();
1✔
93
      }
1✔
94

95
      @Override
96
      public void childMoved(@NotNull PsiTreeChangeEvent event) {
97
        queueUpdate();
×
98
      }
×
99

100
      @Override
101
      public void childrenChanged(@NotNull PsiTreeChangeEvent event) {
102
        queueUpdate();
1✔
103
      }
1✔
104

105
      @Override
106
      public void propertyChanged(@NotNull PsiTreeChangeEvent event) {
107
        queueUpdate();
1✔
108
      }
1✔
109
    }, this);
110
  }
1✔
111

112
  private void queueUpdate() {
113
    if (!myProject.isDefault()) {
1!
114
      myQueue.queue(Update.create(this, this::doUpdateSingleThread));
1✔
115
    }
116
  }
1✔
117

118
  private void doUpdateSingleThread() {
119
    if (!DumbService.isDumb(myProject) && myIsUpdating.compareAndSet(false, true)) {
1!
120
      try {
121
        doUpdateCache();
1✔
122
      }
123
      finally {
124
        myIsUpdating.set(false);
1✔
125
      }
1✔
126
    }
127
    else {
128
      queueUpdate();
1✔
129
    }
130
  }
1✔
131

132
  private void doUpdateCache() {
133
    if (LightEdit.owns(myProject)) {
1!
134
      return;
×
135
    }
136
    ReadAction.nonBlocking(() -> {
1✔
137
      PerlTimeLogger logger = PerlTimeLogger.create(LOG);
1✔
138
      logger.debug("Starting to update names cache at");
1✔
139

140
      PerlSubDeclarationIndex subDeclarationIndex = PerlSubDeclarationIndex.getInstance();
1✔
141
      Collection<String> declarationsNames = subDeclarationIndex.getAllNames(myProject);
1✔
142
      Set<String> subsSet = new HashSet<>(declarationsNames);
1✔
143
      logger.debug("Got declarations names: ", declarationsNames.size());
1✔
144
      ProgressManager.checkCanceled();
1✔
145

146
      PerlSubDefinitionsIndex subDefinitionsIndex = PerlSubDefinitionsIndex.getInstance();
1✔
147
      Collection<String> definitionsNames = subDefinitionsIndex.getAllNames(myProject);
1✔
148
      subsSet.addAll(definitionsNames);
1✔
149
      logger.debug("Got definitions names: ", definitionsNames.size());
1✔
150
      ProgressManager.checkCanceled();
1✔
151

152
      PerlLightSubDefinitionsIndex lightSubDefinitionsIndex = PerlLightSubDefinitionsIndex.getInstance();
1✔
153
      Collection<String> lightDefinitionsNames = lightSubDefinitionsIndex.getAllNames(myProject);
1✔
154
      subsSet.addAll(lightDefinitionsNames);
1✔
155
      logger.debug("Got light definitions names: ", lightDefinitionsNames.size());
1✔
156
      ProgressManager.checkCanceled();
1✔
157
      myKnownSubs = Collections.unmodifiableSet(subsSet);
1✔
158

159
      Set<String> namespacesSet = new HashSet<>(PerlPackageUtilCore.CORE_PACKAGES_ALL);
1✔
160

161
      PerlNamespaceIndex namespaceIndex = PerlNamespaceIndex.getInstance();
1✔
162
      Collection<String> namespacesNames = namespaceIndex.getAllNames(myProject);
1✔
163
      namespacesSet.addAll(namespacesNames);
1✔
164
      logger.debug("Got namespaces names: ", namespacesNames.size());
1✔
165
      ProgressManager.checkCanceled();
1✔
166

167
      PerlLightNamespaceIndex lightNamespaceIndex = PerlLightNamespaceIndex.getInstance();
1✔
168
      Collection<String> lightNamespacesNames = lightNamespaceIndex.getAllNames(myProject);
1✔
169
      namespacesSet.addAll(lightNamespacesNames);
1✔
170
      logger.debug("Got light namespaces names: ", lightNamespacesNames.size());
1✔
171
      myKnownNamespaces = Collections.unmodifiableSet(namespacesSet);
1✔
172

173
      logger.debug("Names cache updated");
1✔
174
      return null;
1✔
175
    }).inSmartMode(myProject).expireWith(this).executeSynchronously();
1✔
176
  }
1✔
177

178
  @Override
179
  public boolean isDisposed() {
180
    return myIsDisposed.get();
×
181
  }
182

183
  @Override
184
  public void forceCacheUpdate() {
185
    var application = ApplicationManager.getApplication();
1✔
186
    application.assertIsNonDispatchThread();
1✔
187
    doUpdateCache();
1✔
188
  }
1✔
189

190
  @Override
191
  @TestOnly
192
  public void cleanCache() {
193
    myKnownSubs = Collections.emptySet();
1✔
194
    myKnownNamespaces = Collections.emptySet();
1✔
195
  }
1✔
196

197
  @Override
198
  public void dispose() {
199
    WriteAction.run(() -> myIsDisposed.set(true));
1✔
200
  }
1✔
201

202
  @Override
203
  public @NotNull Set<String> getSubsNamesSet() {
204
    return myKnownSubs;
1!
205
  }
206

207
  @Override
208
  public @NotNull Set<String> getNamespacesNamesSet() {
209
    return myKnownNamespaces;
1!
210
  }
211

212
  @TestOnly
213
  public void stopQueue() {
214
    myQueue.dispose();
1✔
215
  }
1✔
216

217
  @TestOnly
218
  public boolean isUpdating() { return myIsUpdating.get(); }
1✔
219
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc