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

Camelcade / Perl5-IDEA / #525521637

21 Jul 2025 12:07PM UTC coverage: 82.347% (+0.08%) from 82.266%
#525521637

push

github

hurricup
#2842 Moved embedded perl extensions

30964 of 37602 relevant lines covered (82.35%)

0.82 hits per line

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

96.72
/plugin/backend/src/main/java/com/perl5/lang/perl/util/PerlNamespaceUtil.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.util;
18

19
import com.intellij.openapi.diagnostic.Logger;
20
import com.intellij.openapi.project.Project;
21
import com.intellij.psi.PsiElement;
22
import com.intellij.psi.PsiReference;
23
import com.intellij.psi.ResolveResult;
24
import com.intellij.psi.search.GlobalSearchScope;
25
import com.intellij.util.Processor;
26
import com.perl5.lang.perl.extensions.packageprocessor.PerlExportDescriptor;
27
import com.perl5.lang.perl.psi.PerlNamespaceDefinitionElement;
28
import com.perl5.lang.perl.psi.PerlNamespaceElement;
29
import com.perl5.lang.perl.psi.impl.PerlFileImpl;
30
import com.perl5.lang.perl.psi.impl.PerlUseStatementElement;
31
import com.perl5.lang.perl.psi.references.PerlNamespaceFileReference;
32
import com.perl5.lang.perl.psi.references.PerlNamespaceReference;
33
import com.perl5.lang.perl.psi.stubs.imports.PerlUseStatementsIndex;
34
import com.perl5.lang.perl.psi.stubs.namespaces.PerlLightNamespaceIndex;
35
import com.perl5.lang.perl.psi.stubs.namespaces.PerlNamespaceIndex;
36
import com.perl5.lang.perl.util.processors.PerlNamespaceEntityProcessor;
37
import org.jetbrains.annotations.NotNull;
38

39
import java.util.*;
40

41
public final class PerlNamespaceUtil {
42
  public static final Logger LOG_STUBS = Logger.getInstance("#perl5.namespce.stubs");
1✔
43

44
  private PerlNamespaceUtil() {
45
  }
46

47
  public static @NotNull Set<PerlExportDescriptor> getExportDescriptors(@NotNull Project project,
48
                                                                        @NotNull GlobalSearchScope searchScope,
49
                                                                        @NotNull String namespaceName) {
50
    Set<PerlExportDescriptor> result = new HashSet<>();
1✔
51
    processExportDescriptors(
1✔
52
      project, searchScope, namespaceName, (__, it) -> {
53
        result.add(it);
1✔
54
        return true;
1✔
55
      });
56
    return result;
1✔
57
  }
58

59
  public static boolean processExportDescriptors(@NotNull Project project,
60
                                                 @NotNull GlobalSearchScope searchScope,
61
                                                 @NotNull String namespaceName,
62
                                                 @NotNull PerlNamespaceEntityProcessor<? super PerlExportDescriptor> processor) {
63
    PerlTimeLogger logger = PerlTimeLogger.create(LOG_STUBS);
1✔
64
    PerlTimeLogger.Counter useStatementsCounter = logger.getCounter("use");
1✔
65
    PerlTimeLogger.Counter exportsCounter = logger.getCounter("export");
1✔
66

67
    boolean result = PerlUseStatementsIndex.getInstance().processElements(
1✔
68
      project, namespaceName, searchScope, createUseStatementsProcessor(processor, useStatementsCounter, exportsCounter));
1✔
69

70
    logger.debug("Processed: ", useStatementsCounter.get(), " use statements; ",
1✔
71
                 exportsCounter.get(), " exports");
1✔
72

73
    return result;
1✔
74
  }
75

76
  private static @NotNull Processor<PerlUseStatementElement> createUseStatementsProcessor(@NotNull PerlNamespaceEntityProcessor<? super PerlExportDescriptor> processor,
77
                                                                                          @NotNull PerlTimeLogger.Counter useStatementsCounter,
78
                                                                                          @NotNull PerlTimeLogger.Counter exportsCounter) {
79
    return it -> {
1✔
80
      useStatementsCounter.inc();
1✔
81
      String packageName = it.getPackageName();
1✔
82

83
      if (packageName == null) {
1✔
84
        return true;
×
85
      }
86
      for (PerlExportDescriptor entry : it.getPackageProcessor().getImports(it)) {
1✔
87
        exportsCounter.inc();
1✔
88
        if (!processor.process(packageName, entry)) {
1✔
89
          return false;
×
90
        }
91
      }
1✔
92
      return true;
1✔
93
    };
94
  }
95

96
  public static @NotNull List<PerlNamespaceDefinitionElement> collectNamespaceDefinitions(@NotNull Project project,
97
                                                                                          @NotNull List<String> packageNames) {
98
    ArrayList<PerlNamespaceDefinitionElement> namespaceDefinitions = new ArrayList<>();
1✔
99
    for (String packageName : packageNames) {
1✔
100
      Collection<PerlNamespaceDefinitionElement> list =
1✔
101
        getNamespaceDefinitions(project, GlobalSearchScope.projectScope(project), packageName);
1✔
102

103
      if (list.isEmpty()) {
1✔
104
        list = getNamespaceDefinitions(project, GlobalSearchScope.allScope(project), packageName);
1✔
105
      }
106

107
      namespaceDefinitions.addAll(list);
1✔
108
    }
1✔
109
    return namespaceDefinitions;
1✔
110
  }
111

112
  /**
113
   * Searching project files for namespace definitions by specific package name
114
   *
115
   * @see #processNamespaces(String, Project, GlobalSearchScope, Processor)
116
   */
117
  public static Collection<PerlNamespaceDefinitionElement> getNamespaceDefinitions(@NotNull Project project,
118
                                                                                   @NotNull GlobalSearchScope scope,
119
                                                                                   @NotNull String canonicalPackageName) {
120
    List<PerlNamespaceDefinitionElement> result = new ArrayList<>();
1✔
121
    processNamespaces(canonicalPackageName, project, scope, result::add);
1✔
122
    return result;
1✔
123
  }
124

125
  /**
126
   * Processes all global packages names with specific processor
127
   *
128
   * @param scope     search scope
129
   * @param processor string processor for suitable strings
130
   * @return collection of constants names
131
   */
132
  @SuppressWarnings("UnusedReturnValue")
133
  public static boolean processNamespaces(@NotNull String packageName,
134
                                          @NotNull Project project,
135
                                          @NotNull GlobalSearchScope scope,
136
                                          @NotNull Processor<? super PerlNamespaceDefinitionElement> processor) {
137
    return PerlNamespaceIndex.getInstance().processElements(project, packageName, scope, processor) &&
1✔
138
           PerlLightNamespaceIndex.getInstance().processLightElements(project, packageName, scope, processor);
1✔
139
  }
140

141
  /**
142
   * Returns list of files suitable for this namespace, works only if namespace is in use or require statement
143
   *
144
   * @return list of PerlNameSpaceDefitions
145
   */
146
  public static @NotNull List<PerlFileImpl> getNamespaceFiles(@NotNull PerlNamespaceElement namespaceElement) {
147
    List<PerlFileImpl> namespaceFiles = new ArrayList<>();
1✔
148

149
    PsiReference[] references = namespaceElement.getReferences();
1✔
150

151
    for (PsiReference reference : references) {
1✔
152
      if (reference instanceof PerlNamespaceFileReference namespaceFileReference) {
1✔
153
        ResolveResult[] results = namespaceFileReference.multiResolve(false);
1✔
154

155
        for (ResolveResult result : results) {
1✔
156
          PsiElement targetElement = result.getElement();
1✔
157
          assert targetElement != null;
1✔
158

159
          if (targetElement instanceof PerlFileImpl perlFile) {
1✔
160
            namespaceFiles.add(perlFile);
1✔
161
          }
162
        }
163
      }
164
    }
165
    return namespaceFiles;
1✔
166
  }
167

168
  /**
169
   * Returns list of definitions of current namespace
170
   *
171
   * @return list of PerlNameSpaceDefitions
172
   */
173
  public static @NotNull List<PerlNamespaceDefinitionElement> getNamespaceDefinitions(@NotNull PerlNamespaceElement namespaceElement) {
174
    List<PerlNamespaceDefinitionElement> namespaceDefinitions = new ArrayList<>();
1✔
175

176
    PsiReference[] references = namespaceElement.getReferences();
1✔
177

178
    for (PsiReference reference : references) {
1✔
179
      if (reference instanceof PerlNamespaceReference namespaceReference) {
1✔
180
        ResolveResult[] results = namespaceReference.multiResolve(false);
1✔
181

182
        for (ResolveResult result : results) {
1✔
183
          PsiElement targetElement = result.getElement();
1✔
184
          assert targetElement != null;
1✔
185
          assert targetElement instanceof PerlNamespaceDefinitionElement;
1✔
186

187
          namespaceDefinitions.add((PerlNamespaceDefinitionElement)targetElement);
1✔
188
        }
189
      }
190
    }
191
    return namespaceDefinitions;
1✔
192
  }
193
}
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