From 33bfed7c94e79615ac9577487cab532ddf7bc43d Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Mon, 23 Jan 2012 10:39:23 +0100 Subject: [PATCH] IDEA-80185 (Replace with For-Each with Indexed Loop bug) --- ...orEachLoopWithIndexedForLoopIntention.java | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/forloop/ReplaceForEachLoopWithIndexedForLoopIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/forloop/ReplaceForEachLoopWithIndexedForLoopIntention.java index 53ac57b7a48a..cf41ae7358ab 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/forloop/ReplaceForEachLoopWithIndexedForLoopIntention.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/forloop/ReplaceForEachLoopWithIndexedForLoopIntention.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2011 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2012 Dave Griffith, Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -35,10 +35,8 @@ public class ReplaceForEachLoopWithIndexedForLoopIntention extends Intention { } @Override - public void processIntention(@NotNull PsiElement element) - throws IncorrectOperationException { - final PsiForeachStatement statement = - (PsiForeachStatement)element.getParent(); + public void processIntention(@NotNull PsiElement element) throws IncorrectOperationException { + final PsiForeachStatement statement = (PsiForeachStatement)element.getParent(); if (statement == null) { return; } @@ -54,18 +52,20 @@ public class ReplaceForEachLoopWithIndexedForLoopIntention extends Intention { return; } final boolean isArray = iteratedValueType instanceof PsiArrayType; - final String iteratedValueText = - getReferenceToIterate(iteratedValue, statement); + final PsiElement grandParent = statement.getParent(); + final PsiStatement context; + if (grandParent instanceof PsiLabeledStatement) { + context = (PsiStatement)grandParent; + } else { + context = statement; + } + final String iteratedValueText = getReferenceToIterate(iteratedValue, context); final String lengthText; if (isArray) { - lengthText = - createVariableName(iteratedValueText + "Length", - PsiType.INT, statement); + lengthText = createVariableName(iteratedValueText + "Length", PsiType.INT, statement); } else { - lengthText = - createVariableName(iteratedValueText + "Size", - PsiType.INT, statement); + lengthText = createVariableName(iteratedValueText + "Size", PsiType.INT, statement); } @NonNls final StringBuilder newStatement = new StringBuilder(); newStatement.append("for(int ");