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

Camelcade / Perl5-IDEA / #525521551

26 May 2025 11:24AM UTC coverage: 82.32% (+0.001%) from 82.319%
#525521551

push

github

hurricup
Migrated to the presentation property

1 of 1 new or added line in 1 file covered. (100.0%)

148 existing lines in 15 files now uncovered.

30897 of 37533 relevant lines covered (82.32%)

0.82 hits per line

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

64.36
/plugin/core/src/main/java/com/perl5/lang/perl/psi/impl/PerlImplicitVariableDeclaration.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.psi.impl;
18

19
import com.intellij.openapi.editor.Document;
20
import com.intellij.psi.*;
21
import com.intellij.psi.stubs.IStubElementType;
22
import com.intellij.util.IncorrectOperationException;
23
import com.perl5.lang.perl.idea.codeInsight.typeInference.value.PerlScalarValue;
24
import com.perl5.lang.perl.idea.codeInsight.typeInference.value.PerlValue;
25
import com.perl5.lang.perl.idea.codeInsight.typeInference.value.PerlValues;
26
import com.perl5.lang.perl.psi.*;
27
import com.perl5.lang.perl.psi.mixins.PerlMethodDefinitionMixin;
28
import com.perl5.lang.perl.psi.stubs.variables.PerlVariableDeclarationStub;
29
import com.perl5.lang.perl.psi.utils.PerlVariableAnnotations;
30
import com.perl5.lang.perl.psi.utils.PerlVariableType;
31
import com.perl5.lang.perl.util.PerlPackageUtil;
32
import org.jetbrains.annotations.NonNls;
33
import org.jetbrains.annotations.NotNull;
34
import org.jetbrains.annotations.Nullable;
35

36
import javax.swing.*;
37
import java.util.Collections;
38
import java.util.List;
39
import java.util.Objects;
40

41

42
public class PerlImplicitVariableDeclaration extends PerlImplicitElement
43
  implements PerlVariable, PerlVariableNameElement, PerlVariableDeclarationElement {
44
  protected final PerlVariableType myVariableType;
45
  protected final @NotNull String myVariableName;
46
  protected final @NotNull PerlValue myDeclaredValue;
47
  protected final @Nullable String myNamespaceName;
48

49
  protected final boolean myIsLexical;
50
  protected final boolean myIsLocal;
51
  protected final boolean myIsInvocant;
52

53
  protected PerlImplicitVariableDeclaration(@NotNull PsiManager manager,
54
                                            @NotNull String variableNameWithSigil,
55
                                            @Nullable String namespaceName,
56
                                            @NotNull PerlValue variableValue,
57
                                            boolean isLexical,
58
                                            boolean isLocal,
59
                                            boolean isInvocant,
60
                                            @Nullable PsiElement parent) {
61
    this(manager,
1✔
62
         computeTypeFromNameWithSigil(variableNameWithSigil),
1✔
63
         variableNameWithSigil.substring(1),
1✔
64
         namespaceName,
65
         variableValue,
66
         isLexical,
67
         isLocal,
68
         isInvocant,
69
         parent);
70
  }
1✔
71

72
  protected PerlImplicitVariableDeclaration(@NotNull PsiManager manager,
73
                                            @NotNull PerlVariableType type,
74
                                            @NotNull String variableName,
75
                                            @Nullable String namespaceName,
76
                                            @NotNull PerlValue variableValue,
77
                                            boolean isLexical,
78
                                            boolean isLocal,
79
                                            boolean isInvocant,
80
                                            @Nullable PsiElement parent) {
81
    super(manager, parent);
1✔
82
    myVariableType = type;
1✔
83
    myVariableName = variableName;
1✔
84
    myDeclaredValue = variableValue;
1✔
85
    myIsLexical = isLexical;
1✔
86
    myIsLocal = isLocal;
1✔
87
    myIsInvocant = isInvocant;
1✔
88
    myNamespaceName = namespaceName;
1✔
89
  }
1✔
90

91
  private static PerlVariableType computeTypeFromNameWithSigil(@NotNull String variableNameWithSigil) {
92
    if (variableNameWithSigil.startsWith("$")) {
1✔
93
      return PerlVariableType.SCALAR;
1✔
94
    }
95
    else if (variableNameWithSigil.startsWith("@")) {
1✔
96
      return PerlVariableType.ARRAY;
1✔
97
    }
98
    else if (variableNameWithSigil.startsWith("%")) {
1✔
99
      return PerlVariableType.HASH;
1✔
100
    }
101
    else if (variableNameWithSigil.startsWith("*")) {
1✔
102
      return PerlVariableType.GLOB;
1✔
103
    }
UNCOV
104
    throw new RuntimeException("Incorrect variable name, should start from sigil: " + variableNameWithSigil);
×
105
  }
106

107
  @Override
108
  public void accept(@NotNull PsiElementVisitor visitor) {
109
    if (visitor instanceof PerlVisitor perlVisitor) {
1✔
110
      perlVisitor.visitPerlVariableDeclarationElement(this);
1✔
111
    }
112
    else {
UNCOV
113
      visitor.visitElement(this);
×
114
    }
115
  }
1✔
116

117
  @Override
118
  public final @NotNull String getNamespaceName() {
119
    String explicitPackageName = this.getExplicitNamespaceName();
1✔
120
    return explicitPackageName != null ? explicitPackageName : PerlPackageUtil.getContextNamespaceName(this);
1✔
121
  }
122

123
  @Override
124
  public @Nullable String getExplicitNamespaceName() {
125
    return myNamespaceName;
1✔
126
  }
127

128
  @Override
129
  public String toString() {
130
    return "Implicit variable: " + getVariableType().getSigil() + getCanonicalName() + '@' + getDeclaredValue();
1✔
131
  }
132

133
  @Override
134
  public @NotNull PerlValue getDeclaredValue() {
135
    return myDeclaredValue;
1✔
136
  }
137

138
  @Override
139
  public @NotNull PerlVariableType getActualType() {
140
    return getVariableType();
1✔
141
  }
142

143
  @Override
144
  public PerlVariableDeclarationElement getLexicalDeclaration() {
UNCOV
145
    return null;
×
146
  }
147

148
  @Override
149
  public @NotNull List<PerlVariableDeclarationElement> getGlobalDeclarations() {
150
    return Collections.emptyList();
1✔
151
  }
152

153
  @Override
154
  public @NotNull List<PerlGlobVariableElement> getRelatedGlobs() {
155
    return Collections.emptyList();
1✔
156
  }
157

158
  @Override
159
  public int getLineNumber() {
160
    PsiFile file = getContainingFile();
1✔
161
    if (file == null) {
1✔
162
      return 0;
1✔
163
    }
164
    Document document = PsiDocumentManager.getInstance(getProject()).getCachedDocument(file);
×
UNCOV
165
    return document == null ? 0 : document.getLineNumber(getTextOffset()) + 1;
×
166
  }
167

168
  @Override
169
  public PerlVariableNameElement getVariableNameElement() {
UNCOV
170
    return this;
×
171
  }
172

173
  @Override
174
  public boolean isBuiltIn() {
UNCOV
175
    return false;
×
176
  }
177

178
  @Override
179
  public boolean isDeprecated() {
180
    return false;
1✔
181
  }
182

183
  public PerlVariableType getVariableType() {
184
    return myVariableType;
1✔
185
  }
186

187
  @Override
188
  public @NotNull String getVariableName() {
189
    return myVariableName;
1✔
190
  }
191

192
  @Override
193
  public @NotNull PerlVariable getVariable() {
194
    return this;
1✔
195
  }
196

197
  @Override
198
  public boolean isLexicalDeclaration() {
UNCOV
199
    return myIsLexical;
×
200
  }
201

202
  @Override
203
  public boolean isLocalDeclaration() {
UNCOV
204
    return myIsLocal;
×
205
  }
206

207
  @Override
208
  public boolean isGlobalDeclaration() {
209
    return !myIsLexical;
1✔
210
  }
211

212
  @Override
213
  public boolean isInvocantDeclaration() {
UNCOV
214
    return myIsInvocant;
×
215
  }
216

217
  @Override
218
  public String getPresentableName() {
UNCOV
219
    return isLocalDeclaration() ? getName() : getCanonicalName();
×
220
  }
221

222
  @Override
223
  public @Nullable Icon getIcon(int flags) {
224
    Icon iconByType = getIconByType(getActualType());
×
UNCOV
225
    return iconByType == null ? super.getIcon(flags) : iconByType;
×
226
  }
227

228
  @Override
229
  public @Nullable PsiElement getNameIdentifier() {
UNCOV
230
    return null;
×
231
  }
232

233
  @Override
234
  public PsiElement setName(@NonNls @NotNull String name) throws IncorrectOperationException {
UNCOV
235
    return this;
×
236
  }
237

238
  @SuppressWarnings("deprecation")
239
  @Override
240
  public IStubElementType<?, ?> getElementType() {
241
    return null;
1✔
242
  }
243

244
  @Override
245
  public PerlVariableDeclarationStub getStub() {
246
    return null;
1✔
247
  }
248

249
  @Override
250
  public @NotNull String getName() {
251
    return getVariableName();
1✔
252
  }
253

254
  @Override
255
  public boolean isDeclaration() {
UNCOV
256
    return true;
×
257
  }
258

259
  @Override
260
  public @Nullable PerlVariableAnnotations getVariableAnnotations() {
UNCOV
261
    return null;
×
262
  }
263

264
  @Override
265
  public @NotNull PerlVariableAnnotations getLocalVariableAnnotations() {
UNCOV
266
    return PerlVariableAnnotations.empty();
×
267
  }
268

269
  @Override
270
  public boolean equals(Object o) {
271
    if (this == o) {
1✔
UNCOV
272
      return true;
×
273
    }
274
    if (o == null || getClass() != o.getClass()) {
1✔
275
      return false;
1✔
276
    }
277
    if (!super.equals(o)) {
×
UNCOV
278
      return false;
×
279
    }
280

UNCOV
281
    PerlImplicitVariableDeclaration that = (PerlImplicitVariableDeclaration)o;
×
282

283
    if (myIsLexical != that.myIsLexical) {
×
UNCOV
284
      return false;
×
285
    }
286
    if (myIsLocal != that.myIsLocal) {
×
UNCOV
287
      return false;
×
288
    }
289
    if (myIsInvocant != that.myIsInvocant) {
×
UNCOV
290
      return false;
×
291
    }
292
    if (myVariableType != that.myVariableType) {
×
UNCOV
293
      return false;
×
294
    }
295
    if (!myVariableName.equals(that.myVariableName)) {
×
UNCOV
296
      return false;
×
297
    }
298
    if (!myDeclaredValue.equals(that.myDeclaredValue)) {
×
UNCOV
299
      return false;
×
300
    }
UNCOV
301
    return Objects.equals(myNamespaceName, that.myNamespaceName);
×
302
  }
303

304
  @Override
305
  public int hashCode() {
306
    int result = super.hashCode();
1✔
307
    result = 31 * result + (myVariableType != null ? myVariableType.hashCode() : 0);
1✔
308
    result = 31 * result + myVariableName.hashCode();
1✔
309
    result = 31 * result + myDeclaredValue.hashCode();
1✔
310
    result = 31 * result + (myNamespaceName != null ? myNamespaceName.hashCode() : 0);
1✔
311
    result = 31 * result + (myIsLexical ? 1 : 0);
1✔
312
    result = 31 * result + (myIsLocal ? 1 : 0);
1✔
313
    result = 31 * result + (myIsInvocant ? 1 : 0);
1✔
314
    return result;
1✔
315
  }
316

317
  public static @NotNull PerlImplicitVariableDeclaration createGlobal(@NotNull PsiElement parent,
318
                                                                      @NotNull String variableName,
319
                                                                      @Nullable String variableClass
320
  ) {
321
    return create(parent, variableName, variableClass, false, false, false);
1✔
322
  }
323

324
  public static @NotNull PerlImplicitVariableDeclaration createGlobal(@NotNull PsiManager psiManager,
325
                                                                      @NotNull String variableName,
326
                                                                      @Nullable String packageName) {
327
    return create(psiManager, variableName, packageName, null, false, false, false, null);
1✔
328
  }
329

330
  public static @NotNull PerlImplicitVariableDeclaration createLexical(@NotNull PsiElement parent,
331
                                                                       @NotNull String variableName
332
  ) {
333
    return createLexical(parent, variableName, null);
1✔
334
  }
335

336
  public static @NotNull PerlImplicitVariableDeclaration createLexical(@NotNull PsiElement parent,
337
                                                                       @NotNull String variableName,
338
                                                                       @Nullable String variableClass
339
  ) {
340
    return create(parent, variableName, variableClass, true, false, false);
1✔
341
  }
342

343
  public static @NotNull PerlImplicitVariableDeclaration createInvocant(@NotNull PsiElement parent) {
344
    return new PerlImplicitVariableDeclaration(
1✔
345
      parent.getManager(),
1✔
346
      PerlVariableType.SCALAR,
347
      PerlMethodDefinitionMixin.getDefaultInvocantName().substring(1),
1✔
348
      null,
349
      PerlValues.FIRST_ARGUMENT_VALUE,
350
      true,
351
      false,
352
      true,
353
      parent
354
    );
355
  }
356

357
  private static @NotNull PerlImplicitVariableDeclaration create(@NotNull PsiElement parent,
358
                                                                 @NotNull String variableName,
359
                                                                 @Nullable String variableClass,
360
                                                                 boolean isLexical,
361
                                                                 boolean isLocal,
362
                                                                 boolean isInvocant
363
  ) {
364
    return create(parent.getManager(), variableName, null, variableClass, isLexical, isLocal, isInvocant, parent);
1✔
365
  }
366

367
  private static @NotNull PerlImplicitVariableDeclaration create(@NotNull PsiManager psiManager,
368
                                                                 @NotNull String variableNameWithSigil,
369
                                                                 @Nullable String packageName,
370
                                                                 @Nullable String variableClass,
371
                                                                 boolean isLexical,
372
                                                                 boolean isLocal,
373
                                                                 boolean isInvocant,
374
                                                                 @Nullable PsiElement parent
375
  ) {
376
    return new PerlImplicitVariableDeclaration(
1✔
377
      psiManager, variableNameWithSigil, packageName, PerlScalarValue.create(variableClass), isLexical, isLocal, isInvocant, parent);
1✔
378
  }
379

380
  public @NotNull List<PsiPerlExpr> getExprList() {
UNCOV
381
    return Collections.emptyList();
×
382
  }
383
}
384

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