• 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

78.95
/mason/mason2/backend/src/main/java/com/perl5/lang/mason2/psi/impl/MasonNamespaceDefinitionImpl.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.mason2.psi.impl;
18

19
import com.intellij.lang.ASTNode;
20
import com.intellij.openapi.vfs.VfsUtil;
21
import com.intellij.openapi.vfs.VfsUtilCore;
22
import com.intellij.openapi.vfs.VirtualFile;
23
import com.intellij.psi.tree.IElementType;
24
import com.perl5.lang.htmlmason.MasonCoreUtil;
25
import com.perl5.lang.mason2.Mason2Util;
26
import com.perl5.lang.mason2.idea.configuration.MasonSettings;
27
import com.perl5.lang.mason2.psi.MasonNamespaceDefinition;
28
import com.perl5.lang.perl.psi.PerlNamespaceElement;
29
import com.perl5.lang.perl.psi.PerlVariableDeclarationElement;
30
import com.perl5.lang.perl.psi.impl.PsiPerlNamespaceDefinitionImpl;
31
import com.perl5.lang.perl.psi.stubs.namespaces.PerlNamespaceDefinitionStub;
32
import com.perl5.lang.perl.util.PerlFileUtil;
33
import com.perl5.lang.perl.util.PerlPackageUtilCore;
34
import org.jetbrains.annotations.NotNull;
35
import org.jetbrains.annotations.Nullable;
36

37
import java.util.ArrayList;
38
import java.util.List;
39

40

41
public class MasonNamespaceDefinitionImpl extends PsiPerlNamespaceDefinitionImpl implements MasonNamespaceDefinition {
42
  protected List<PerlVariableDeclarationElement> myImplicitVariables = null;
1✔
43
  protected int mySettingsChangeCounter;
44

45
  public MasonNamespaceDefinitionImpl(ASTNode node) {
46
    super(node);
1✔
47
  }
1✔
48

49
  public MasonNamespaceDefinitionImpl(PerlNamespaceDefinitionStub stub, IElementType nodeType) {
50
    super(stub, nodeType);
1✔
51
  }
1✔
52

53
  protected @NotNull List<PerlVariableDeclarationElement> buildImplicitVariables(MasonSettings masonSettings) {
54
    List<PerlVariableDeclarationElement> newImplicitVariables = new ArrayList<>();
1✔
55

56
    if (isValid()) {
1✔
57
      MasonCoreUtil.fillVariablesList(this, newImplicitVariables, masonSettings.globalVariables);
1✔
58
    }
59
    return newImplicitVariables;
1✔
60
  }
61

62
  @Override
63
  public PerlNamespaceElement getNamespaceElement() {
64
    return null;
1✔
65
  }
66

67
  @Override
68
  public @Nullable String getNamespaceName() {
69
    String absoluteComponentPath = getAbsoluteComponentPath();
1✔
70
    if (absoluteComponentPath != null) {
1✔
71
      return Mason2Util.getClassnameFromPath(absoluteComponentPath);
1✔
72
    }
73
    return null;
×
74
  }
75

76
  @Override
77
  protected @Nullable String computeNamespaceName() {
78
    String packageName = Mason2Util.getVirtualFileClassName(getProject(), MasonCoreUtil.getContainingVirtualFile(getContainingFile()));
×
79
    return packageName == null ? PerlPackageUtilCore.MAIN_NAMESPACE_NAME : packageName;
×
80
  }
81

82
  @Override
83
  public @Nullable String getAbsoluteComponentPath() {
84
    VirtualFile containingFile = MasonCoreUtil.getContainingVirtualFile(getContainingFile());
1✔
85
    if (containingFile != null) {
1✔
86
      return PerlFileUtil.getPathRelativeToContentRoot(containingFile, getProject());
1✔
87
    }
88

89
    return null;
×
90
  }
91

92
  @Override
93
  public @Nullable String getComponentPath() {
94
    VirtualFile containingFile = MasonCoreUtil.getContainingVirtualFile(getContainingFile());
1✔
95
    if (containingFile != null) {
1✔
96
      VirtualFile containingRoot = Mason2Util.getComponentRoot(getProject(), containingFile);
1✔
97
      if (containingRoot != null) {
1✔
98
        return VfsUtilCore.getRelativePath(containingFile, containingRoot);
1✔
99
      }
100
    }
101
    return null;
×
102
  }
103

104
  protected @Nullable String getParentNamespaceFromAutobase() {
105
    // autobase
106
    VirtualFile componentRoot = getContainingFile().getComponentRoot();
1✔
107
    VirtualFile containingFile = MasonCoreUtil.getContainingVirtualFile(getContainingFile());
1✔
108

109
    if (componentRoot != null && containingFile != null) {
1✔
110
      VirtualFile parentComponentFile = getParentComponentFile(componentRoot, containingFile.getParent(), containingFile);
1✔
111
      if (parentComponentFile != null) // found autobase class
1✔
112
      {
113
        String componentPath = PerlFileUtil.getPathRelativeToContentRoot(parentComponentFile, getProject());
1✔
114

115
        return componentPath;
1✔
116
      }
117
    }
118
    return null;
1✔
119
  }
120

121
  /**
122
   * Recursively traversing paths and looking for autobase
123
   *
124
   * @param componentRoot    component root we are search in
125
   * @param currentDirectory directory we are currently in         *
126
   * @param childFile        current file (just to speed things up)
127
   * @return parent component virtual file or null if not found
128
   */
129
  private @Nullable VirtualFile getParentComponentFile(VirtualFile componentRoot, VirtualFile currentDirectory, VirtualFile childFile) {
130
    // check in current dir
131
    List<String> autobaseNames = new ArrayList<>(MasonSettings.getInstance(getProject()).autobaseNames);
1✔
132

133
    if (childFile.getParent().equals(currentDirectory) && autobaseNames.contains(childFile.getName())) // avoid cyclic inheritance
1✔
134
    {
135
      autobaseNames = autobaseNames.subList(0, autobaseNames.lastIndexOf(childFile.getName()));
1✔
136
    }
137

138
    for (int i = autobaseNames.size() - 1; i >= 0; i--) {
1✔
139
      VirtualFile potentialParent = VfsUtil.findRelativeFile(currentDirectory, autobaseNames.get(i));
1✔
140
      if (potentialParent != null && potentialParent.exists() && !potentialParent.equals(childFile)) {
1✔
141
        return potentialParent;
1✔
142
      }
143
    }
144

145
    // move up or exit
146
    if (!componentRoot.equals(currentDirectory)) {
1✔
147
      return getParentComponentFile(componentRoot, currentDirectory.getParent(), childFile);
1✔
148
    }
149
    return null;
1✔
150
  }
151

152
  @Override
153
  public String getPresentableName() {
154
    VirtualFile componentRoot = getContainingFile().getComponentRoot();
×
155
    VirtualFile containingFile = MasonCoreUtil.getContainingVirtualFile(getContainingFile());
×
156

157
    if (componentRoot != null && containingFile != null) {
×
158
      String componentPath = VfsUtilCore.getRelativePath(containingFile, componentRoot);
×
159

160
      if (componentPath != null) {
×
161
        return VfsUtil.VFS_SEPARATOR_CHAR + componentPath;
×
162
      }
163
    }
164

165
    return super.getPresentableName();
×
166
  }
167

168
  @Override
169
  public @NotNull MasonFileImpl getContainingFile() {
170
    return (MasonFileImpl)super.getContainingFile();
1✔
171
  }
172

173
  @Override
174
  public @NotNull List<PerlVariableDeclarationElement> getImplicitVariables() {
175
    MasonSettings settings = MasonSettings.getInstance(getProject());
1✔
176
    if (myImplicitVariables == null || mySettingsChangeCounter != settings.getChangeCounter()) {
1✔
177
      myImplicitVariables = buildImplicitVariables(settings);
1✔
178
      mySettingsChangeCounter = settings.getChangeCounter();
1✔
179
    }
180
    return myImplicitVariables;
1✔
181
  }
182
}
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