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

Camelcade / Perl5-IDEA / #525521635

20 Jul 2025 03:45PM UTC coverage: 82.266% (-0.09%) from 82.355%
#525521635

push

github

hurricup
Build 252.23892.248

30936 of 37605 relevant lines covered (82.27%)

0.82 hits per line

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

89.83
/plugin/backend/src/main/java/com/perl5/lang/perl/util/PerlVariableUtil.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.progress.ProgressManager;
20
import com.intellij.openapi.project.Project;
21
import com.intellij.openapi.util.Pair;
22
import com.intellij.psi.PsiElement;
23
import com.intellij.psi.search.GlobalSearchScope;
24
import com.intellij.psi.stubs.StubIndex;
25
import com.intellij.psi.stubs.StubIndexKey;
26
import com.intellij.util.ObjectUtils;
27
import com.intellij.util.Processor;
28
import com.perl5.lang.perl.psi.PerlGlobVariableElement;
29
import com.perl5.lang.perl.psi.PerlVariable;
30
import com.perl5.lang.perl.psi.PerlVariableDeclaration;
31
import com.perl5.lang.perl.psi.PerlVariableDeclarationElement;
32
import com.perl5.lang.perl.psi.impl.PerlImplicitVariableDeclaration;
33
import com.perl5.lang.perl.psi.stubs.variables.PerlArrayStubIndex;
34
import com.perl5.lang.perl.psi.stubs.variables.PerlHashStubIndex;
35
import com.perl5.lang.perl.psi.stubs.variables.PerlScalarStubIndex;
36
import com.perl5.lang.perl.psi.utils.PerlVariableType;
37
import org.jetbrains.annotations.NotNull;
38
import org.jetbrains.annotations.Nullable;
39

40
import java.util.*;
41

42

43
public final class PerlVariableUtil {
1✔
44
  private PerlVariableUtil() {
45
  }
46

47
  /**
48
   * Compares two variables by actual type, packageName and name. They can be from different declarations
49
   * builds AST
50
   */
51
  public static boolean equals(@Nullable PerlVariable v1, @Nullable PerlVariable v2) {
52
    if (Objects.equals(v1, v2)) {
1✔
53
      return true;
×
54
    }
55
    assert v1 != null && v2 != null;
1✔
56
    return v1.getActualType().equals(v2.getActualType()) &&
1✔
57
           Objects.equals(v1.getExplicitNamespaceName(), v2.getExplicitNamespaceName()) &&
1✔
58
           Objects.equals(v1.getName(), v2.getName());
1✔
59
  }
60

61
  /**
62
   * Processes variables indexed with {@code indexKey}
63
   *
64
   * @param onePerName if true, only one variable per name is going to the processor
65
   */
66
  public static boolean processGlobalVariables(@NotNull StubIndexKey<? super String, PerlVariableDeclarationElement> indexKey,
67
                                               @NotNull Project project,
68
                                               @NotNull GlobalSearchScope scope,
69
                                               @NotNull Processor<? super PerlVariableDeclarationElement> processor,
70
                                               @NotNull String textKey,
71
                                               boolean onePerName) {
72
    Set<String> uniqueNames = onePerName ? new HashSet<>() : null;
1✔
73
    return StubIndex.getInstance().processElements(indexKey, textKey, project, scope, PerlVariableDeclarationElement.class, element -> {
1✔
74
      ProgressManager.checkCanceled();
1✔
75
      if (!onePerName || uniqueNames.add(element.getCanonicalName())) {
1✔
76
        return processor.process(element);
1✔
77
      }
78
      return true;
×
79
    });
80
  }
81

82
  /**
83
   * Processes all variables indexed with {@code indexKey}
84
   *
85
   * @param processAll if false, only one entry per name going to be processed. May be need when filling completion
86
   */
87
  public static boolean processGlobalVariables(@NotNull StubIndexKey<String, PerlVariableDeclarationElement> indexKey,
88
                                               @NotNull Project project,
89
                                               @NotNull GlobalSearchScope scope,
90
                                               @NotNull Processor<? super PerlVariableDeclarationElement> processor,
91
                                               boolean processAll) {
92
    for (String variableName : PerlStubUtil.getAllKeys(indexKey, scope)) {
1✔
93
      if (!processGlobalVariables(indexKey, project, scope, processor, variableName, !processAll)) {
1✔
94
        return false;
×
95
      }
96
    }
1✔
97
    return true;
1✔
98
  }
99

100
  /**
101
   * Looking for global variable declarations sutable for current variable
102
   *
103
   * @return list of global declarations
104
   */
105
  public static @NotNull List<PerlVariableDeclarationElement> getGlobalDeclarations(@NotNull PerlVariable perlVariable) {
106
    if (perlVariable instanceof PerlImplicitVariableDeclaration) {
1✔
107
      return Collections.emptyList();
1✔
108
    }
109
    String variableName = perlVariable.getName();
1✔
110
    if (variableName == null) {
1✔
111
      return Collections.emptyList();
×
112
    }
113

114
    List<PerlVariableDeclarationElement> result = new ArrayList<>();
1✔
115
    PerlVariableType myType = perlVariable.getActualType();
1✔
116

117
    PsiElement parent = perlVariable.getParent(); // wrapper if any
1✔
118
    String namespaceName =
1✔
119
      ObjectUtils.notNull(perlVariable.getExplicitNamespaceName(), PerlPackageUtilCore.getContextNamespaceName(perlVariable));
1✔
120
    String fullQualifiedName = PerlPackageUtilCore.join(namespaceName, variableName);
1✔
121

122
    Processor<PerlVariableDeclarationElement> variableProcessor = it -> {
1✔
123
      if (!it.equals(parent)) {
1✔
124
        result.add(it);
1✔
125
      }
126
      return true;
1✔
127
    };
128
    if (myType == PerlVariableType.SCALAR) {
1✔
129
      PerlScalarUtil.processGlobalScalarsByName(perlVariable.getProject(), perlVariable.getResolveScope(), fullQualifiedName, true,
1✔
130
                                                variableProcessor);
131
    }
132
    else if (myType == PerlVariableType.ARRAY) {
1✔
133
      PerlArrayUtil.processGlobalArraysByName(perlVariable.getProject(), perlVariable.getResolveScope(), fullQualifiedName, true,
1✔
134
                                              variableProcessor);
135
    }
136
    else if (myType == PerlVariableType.HASH) {
1✔
137
      PerlHashUtil.processGlobalHashesByName(perlVariable.getProject(), perlVariable.getResolveScope(), fullQualifiedName, true,
1✔
138
                                             variableProcessor);
139
    }
140

141
    return result;
1✔
142
  }
143

144
  public static Pair<StubIndexKey<String, PerlVariableDeclarationElement>, StubIndexKey<String, PerlVariableDeclarationElement>> getIndexKey(
145
    @NotNull PerlVariableDeclaration perlVariableDeclaration) {
146
    var myVariableType = perlVariableDeclaration.getActualType();
1✔
147
    if (myVariableType == PerlVariableType.ARRAY) {
1✔
148
      return PerlArrayStubIndex.ARRAY_KEYS;
1✔
149
    }
150
    else if (myVariableType == PerlVariableType.SCALAR) {
1✔
151
      return PerlScalarStubIndex.SCALAR_KEYS;
1✔
152
    }
153
    else if (myVariableType == PerlVariableType.HASH) {
1✔
154
      return PerlHashStubIndex.HASH_KEYS;
1✔
155
    }
156
    throw new RuntimeException("Don't have key for " + myVariableType);
×
157
  }
158

159
  public static @NotNull List<PerlGlobVariableElement> getRelatedGlobs(PerlVariable perlVariable) {
160
    if (perlVariable instanceof PerlImplicitVariableDeclaration) {
1✔
161
      return Collections.emptyList();
1✔
162
    }
163
    String variableName = perlVariable.getName();
1✔
164
    if (variableName == null) {
1✔
165
      return Collections.emptyList();
×
166
    }
167
    String namespaceName =
1✔
168
      ObjectUtils.notNull(perlVariable.getExplicitNamespaceName(), PerlPackageUtilCore.getContextNamespaceName(perlVariable));
1✔
169
    String fullQualifiedName = PerlPackageUtilCore.join(namespaceName, variableName);
1✔
170

171
    List<PerlGlobVariableElement> result = new ArrayList<>();
1✔
172
    PerlGlobUtil.processGlobsByName(perlVariable.getProject(), perlVariable.getResolveScope(), fullQualifiedName, result::add, true);
1✔
173
    return result;
1✔
174
  }
175
}
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