• 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

84.62
/plugin/backend/src/main/java/com/perl5/lang/perl/idea/intentions/ForeachToForIntention.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.idea.intentions;
18

19
import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction;
20
import com.intellij.openapi.editor.Editor;
21
import com.intellij.openapi.project.Project;
22
import com.intellij.psi.PsiElement;
23
import com.intellij.psi.PsiWhiteSpace;
24
import com.intellij.psi.util.PsiTreeUtil;
25
import com.intellij.util.IncorrectOperationException;
26
import com.perl5.PerlBundle;
27
import com.perl5.lang.perl.psi.*;
28
import com.perl5.lang.perl.psi.utils.PerlElementFactory;
29
import org.jetbrains.annotations.Nls;
30
import org.jetbrains.annotations.NotNull;
31
import org.jetbrains.annotations.Nullable;
32

33
import java.util.Objects;
34

35

36
public class ForeachToForIntention extends PsiElementBaseIntentionAction {
1✔
37
  @Override
38
  public @Nls @NotNull String getText() {
39
    return PerlBundle.message("perl.intention.foreach.to.for");
1✔
40
  }
41

42
  @Override
43
  public @Nls @NotNull String getFamilyName() {
44
    return getText();
1✔
45
  }
46

47
  private @Nullable PerlForeachCompound getForeachStatement(@NotNull PsiElement element) {
48
    PerlForeachCompound forCompound = PsiTreeUtil.getParentOfType(element, PerlForeachCompound.class);
1✔
49
    if (forCompound == null) {
1✔
50
      return null;
1✔
51
    }
52

53
    // fixme we should work without variable and without declaration
54
    PsiPerlForeachIterator foreachIterator = forCompound.getForeachIterator();
1✔
55
    return foreachIterator != null &&
1✔
56
           foreachIterator.getExpr() instanceof PsiPerlVariableDeclarationLexical ? forCompound : null;
1✔
57
  }
58

59
  @Override
60
  public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) {
61
    PerlForeachCompound foreachStatement = getForeachStatement(element);
1✔
62
    if (foreachStatement == null) {
1✔
63
      return false;
1✔
64
    }
65
    PsiPerlForeachIterator foreachIterator = foreachStatement.getForeachIterator();
1✔
66
    if( foreachIterator == null){
1✔
67
      return false;
×
68
    }
69

70
    PsiPerlExpr iterableList = foreachStatement.getConditionExpr();
1✔
71
    if( iterableList == null){
1✔
72
      return false;
×
73
    }
74

75
    return foreachStatement.getBlock() != null;
1✔
76
  }
77

78
  @Override
79
  public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
80
    PerlForeachCompound foreachStatement = Objects.requireNonNull(getForeachStatement(element));
1✔
81
    PsiPerlForeachIterator foreachIterator = Objects.requireNonNull(foreachStatement.getForeachIterator());
1✔
82
    PsiPerlExpr variableDeclaration = Objects.requireNonNull(foreachIterator.getExpr());
1✔
83
    PsiPerlExpr iterableList = Objects.requireNonNull(foreachStatement.getConditionExpr());
1✔
84
    PsiPerlBlock block = Objects.requireNonNull(foreachStatement.getBlock());
1✔
85

86
    PsiPerlForCompound indexedFor = createIndexedFor(project, (PsiPerlVariableDeclarationLexical)variableDeclaration, iterableList, block);
1✔
87
    foreachStatement.replace(indexedFor);
1✔
88
  }
1✔
89

90
  public static PsiPerlForCompound createIndexedFor(@NotNull Project project,
91
                                                    @NotNull PsiPerlVariableDeclarationLexical variableDeclaration,
92
                                                    @NotNull PsiPerlExpr iterableExpr,
93
                                                    @NotNull PsiPerlBlock forBlock) {
94
    // check if listExpr is a single array or a list expression
95
    // fixme how about cast?
96
    PsiElement[] children = iterableExpr.getChildren();
1✔
97
    boolean isSingleArray = children.length == 1 && children[0] instanceof PsiPerlArrayVariable;
1✔
98

99
    String arrayName;
100
    if (isSingleArray) {
1✔
101
      PsiPerlArrayVariable childArray = (PsiPerlArrayVariable)children[0];
1✔
102
      arrayName = childArray.getName();
1✔
103
    }
1✔
104
    else {
105
      arrayName = "list";
×
106
    }
107

108
    // Assign iterationVariable = iterationArray[$idx]
109
    String assignStatementStr = String.format("%s = $%s[$idx];",
1✔
110
                                              variableDeclaration.getText(),
1✔
111
                                              arrayName);
112
    PsiPerlStatement assignStatement = createPsiOfTypeFromSyntax(project, assignStatementStr, PsiPerlStatement.class);
1✔
113

114
    // Define where to insert the new statement
115
    PsiPerlStatement[] statementList = PsiTreeUtil.getChildrenOfType(forBlock, PsiPerlStatement.class);
1✔
116
    PsiElement anchorPoint = statementList == null ? forBlock.getLastChild() : statementList[0];
1✔
117
    forBlock.addBefore(assignStatement, anchorPoint);
1✔
118

119
    String indexedForSyntax = String.format("for (my $idx = 0; $idx < scalar(@%s); $idx++) %s",
1✔
120
                                            arrayName,
121
                                            forBlock.getText());
1✔
122

123
    PsiPerlForCompound result = createPsiOfTypeFromSyntax(project, indexedForSyntax, PsiPerlForCompound.class);
1✔
124

125
    if (!isSingleArray) {
1✔
126
      // declare and initialize the new array before the new for
127
      String newListSyntax = String.format("my @%s = %s;\n", arrayName, iterableExpr.getText());
×
128
      PsiPerlStatement newListStatement = createPsiOfTypeFromSyntax(project, newListSyntax, PsiPerlStatement.class);
×
129
      PsiWhiteSpace newLineElement = createPsiOfTypeFromSyntax(project, newListSyntax, PsiWhiteSpace.class);
×
130
      result.addBefore(newLineElement, result.getFirstChild());
×
131
      result.addBefore(newListStatement, result.getFirstChild());
×
132
    }
133

134
    assert result != null : "While creating PsiPerlForCompound";
1✔
135
    return result;
1✔
136
  }
137

138
  private static <T extends PsiElement> T createPsiOfTypeFromSyntax(Project project, String syntax, Class<T> type) {
139
    return PsiTreeUtil.findChildOfType(PerlElementFactory.createFile(project, syntax), type);
1✔
140
  }
141
}
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