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

Camelcade / Perl5-IDEA / #525521616

07 Jul 2025 01:01PM UTC coverage: 82.387% (+0.03%) from 82.356%
#525521616

push

github

hurricup
J2K: PerlMethodDefinitionElementType

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

22 existing lines in 5 files now uncovered.

30915 of 37524 relevant lines covered (82.39%)

0.82 hits per line

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

94.12
/plugin/backend/src/main/java/com/perl5/lang/perl/psi/stubs/PerlPolyNamedElementType.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.stubs;
18

19
import com.intellij.lang.ASTNode;
20
import com.intellij.openapi.diagnostic.Logger;
21
import com.intellij.psi.PsiElement;
22
import com.intellij.psi.stubs.*;
23
import com.intellij.psi.tree.IElementType;
24
import com.intellij.util.containers.ContainerUtil;
25
import com.perl5.lang.perl.PerlLanguage;
26
import com.perl5.lang.perl.parser.elementTypes.PsiElementProvider;
27
import com.perl5.lang.perl.psi.impl.PerlPolyNamedElement;
28
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
29
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
30
import org.jetbrains.annotations.NotNull;
31

32
import java.io.IOException;
33
import java.util.ArrayList;
34
import java.util.List;
35

36
import static com.perl5.lang.perl.parser.Class.Accessor.ClassAccessorElementTypes.CLASS_ACCESSOR_METHOD;
37
import static com.perl5.lang.perl.psi.stubs.PerlStubElementTypes.*;
38

39
public abstract class PerlPolyNamedElementType<Stub extends PerlPolyNamedElementStub<Psi>, Psi extends PerlPolyNamedElement<Stub>>
40
  extends IStubElementType<Stub, Psi> implements PsiElementProvider {
41
  private static final Logger LOG = Logger.getInstance(PerlPolyNamedElementType.class);
1✔
42
  private static final Object2IntOpenHashMap<IElementType> DIRECT_MAP = new Object2IntOpenHashMap<>();
1✔
43
  private static final Int2ObjectOpenHashMap<IElementType> REVERSE_MAP = new Int2ObjectOpenHashMap<>();
1✔
44

45
  static {
46
    // 0 is reserved for n/a
47
    DIRECT_MAP.put(LIGHT_SUB_DEFINITION, 1);
1✔
48
    DIRECT_MAP.put(LIGHT_NAMESPACE_DEFINITION, 2);
1✔
49
    DIRECT_MAP.put(LIGHT_METHOD_DEFINITION, 3);
1✔
50
    DIRECT_MAP.put(CLASS_ACCESSOR_METHOD, 4);
1✔
51
    DIRECT_MAP.put(LIGHT_ATTRIBUTE_DEFINITION, 5);
1✔
52
    LOG.assertTrue(DIRECT_MAP.size() == 5);
1✔
53

54
    DIRECT_MAP.forEach((type, id) -> REVERSE_MAP.put(id.intValue(), type));
1✔
55
  }
1✔
56

57
  public PerlPolyNamedElementType(@NotNull String debugName) {
58
    super(debugName, PerlLanguage.INSTANCE);
1✔
59
  }
1✔
60

61
  @SuppressWarnings("rawtypes")
62
  @Override
63
  public @NotNull Stub createStub(@NotNull Psi psi, StubElement<?> parentStub) {
64
    List<StubElement<?>> lightNamedElements = new ArrayList<>();
1✔
65
    Stub result = createStub(psi, parentStub, lightNamedElements);
1✔
66

67
    psi.getLightElements().forEach(lightPsi -> {
1✔
68
      var lightElementType = lightPsi.getElementType();
1✔
69
      StubElement<?> lightStubElement;
70
      if (lightElementType instanceof IStubElementType stubElementType) {
1✔
71
        lightStubElement = stubElementType.createStub(lightPsi, result);
1✔
72
      }
73
      else {
74
        lightStubElement = ((StubElementFactory)getStubSerializer(lightElementType)).createStub(lightPsi, result);
1✔
75
      }
76

77
      if (lightStubElement instanceof PerlLightElementStub lightElementStub && lightPsi.isImplicit()) {
1✔
UNCOV
78
        lightElementStub.setImplicit(true);
×
79
      }
80
      lightNamedElements.add(lightStubElement);
1✔
81
    });
1✔
82

83
    return result;
1✔
84
  }
85

86
  protected abstract @NotNull Stub createStub(@NotNull Psi psi,
87
                                              StubElement<?> parentStub,
88
                                              @NotNull List<StubElement<?>> lightElementsStubs);
89

90
  @Override
91
  public final @NotNull String getExternalId() {
92
    return "perl.poly." + super.toString();
1✔
93
  }
94

95
  @Override
96
  public final void serialize(@NotNull Stub stub, @NotNull StubOutputStream dataStream) throws IOException {
97
    List<StubElement<?>> childrenStubs = ContainerUtil.filter(stub.getLightNamedElementsStubs(), it ->
1✔
98
      !(it instanceof PerlLightElementStub elementStub) || !elementStub.isImplicit());
1✔
99
    dataStream.writeVarInt(childrenStubs.size());
1✔
100
    serializeStub(stub, dataStream);
1✔
101
    //noinspection rawtypes
102
    for (StubElement childStub : childrenStubs) {
1✔
103
      dataStream.writeVarInt(getSerializationId(childStub)); // serialization id
1✔
104
      getStubSerializer(childStub).serialize(childStub, dataStream);
1✔
105
    }
1✔
106
  }
1✔
107

108
  @SuppressWarnings("rawtypes")
109
  private static ObjectStubSerializer getStubSerializer(@NotNull StubElement<?> childStub) {
110
    var elementType = childStub.getElementType();
1✔
111
    if (elementType instanceof IStubElementType<?, ?> stubElementType) {
1✔
112
      return stubElementType;
1✔
113
    }
114
    ObjectStubSerializer<?, com.intellij.psi.stubs.Stub> serializer =
115
      StubElementRegistryService.getInstance().getStubSerializer(elementType);
1✔
116
    LOG.assertTrue(serializer != null,
1✔
117
                   "Don't know how to serialize:" + elementType + "; " + elementType.getClass());
1✔
118
    return serializer;
1✔
119
  }
120

121
  @SuppressWarnings("rawtypes")
122
  private static @NotNull StubSerializer getStubSerializer(@NotNull IElementType elementType) {
123
    if (elementType instanceof IStubElementType<?, ?> stubElementType) {
1✔
124
      return stubElementType;
1✔
125
    }
126
    else {
127
      return (StubSerializer)StubElementRegistryService.getInstance().getStubSerializer(elementType);
1✔
128
    }
129
  }
130

131
  protected void serializeStub(@NotNull Stub stub, @NotNull StubOutputStream dataStream) throws IOException {
132
    // to save additional data in subclasses
UNCOV
133
  }
×
134

135
  @Override
136
  public final @NotNull Stub deserialize(@NotNull StubInputStream dataStream, StubElement parentStub) throws IOException {
137
    int size = dataStream.readVarInt();
1✔
138
    List<StubElement<?>> childStubs = new ArrayList<>(size);
1✔
139
    Stub result = deserialize(dataStream, parentStub, childStubs);
1✔
140

141
    for (int i = 0; i < size; i++) {
1✔
142
      childStubs.add((StubElement<?>)getStubSerializer(getElementTypeById(dataStream.readVarInt())).deserialize(dataStream, result));
1✔
143
    }
144

145
    return result;
1✔
146
  }
147

148
  protected abstract @NotNull Stub deserialize(@NotNull StubInputStream dataStream,
149
                                               StubElement<?> parentStub,
150
                                               @NotNull List<StubElement<?>> lightElementsStubs) throws IOException;
151

152
  @Override
153
  public final void indexStub(@NotNull Stub stub, @NotNull IndexSink sink) {
154
    stub.getLightNamedElementsStubs().forEach(childStub -> getStubSerializer(childStub).indexStub(childStub, sink));
1✔
155
    doIndexStub(stub, sink);
1✔
156
  }
1✔
157

158
  protected void doIndexStub(@NotNull Stub stub, @NotNull IndexSink sink) {
159
  }
1✔
160

161
  @Override
162
  public boolean shouldCreateStub(ASTNode node) {
163
    PsiElement psi = node.getPsi();
1✔
164
    assert psi instanceof PerlPolyNamedElement;
1✔
165
    return !((PerlPolyNamedElement<?>)psi).getLightElements().isEmpty();
1✔
166
  }
167

168
  private static int getSerializationId(@NotNull StubElement<?> stubElement) {
169
    int id = DIRECT_MAP.getInt(stubElement.getElementType());
1✔
170
    if (id > 0) {
1✔
171
      return id;
1✔
172
    }
UNCOV
173
    throw new IllegalArgumentException("Unregistered stub element class:" + stubElement.getElementType());
×
174
  }
175

176
  private static @NotNull IElementType getElementTypeById(int id) {
177
    assert id > 0;
1✔
178
    var type = REVERSE_MAP.get(id);
1✔
179
    if (type != null) {
1✔
180
      return type;
1✔
181
    }
UNCOV
182
    throw new IllegalArgumentException("Unregistered stub element id:" + id);
×
183
  }
184
}
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