From 5213391b564e0c3004988dbc7cfc8258ea603223 Mon Sep 17 00:00:00 2001 From: Sergey Evdokimov Date: Wed, 25 Jan 2012 15:32:48 +0300 Subject: [PATCH] IDEA-80153 (Broken Introduce Property refactoring in pom.xml) --- .../introduce/IntroducePropertyAction.java | 47 ++++++++++--------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/plugins/maven/src/main/java/org/jetbrains/idea/maven/dom/refactorings/introduce/IntroducePropertyAction.java b/plugins/maven/src/main/java/org/jetbrains/idea/maven/dom/refactorings/introduce/IntroducePropertyAction.java index a8f1db45a43e..97dc4afa9243 100644 --- a/plugins/maven/src/main/java/org/jetbrains/idea/maven/dom/refactorings/introduce/IntroducePropertyAction.java +++ b/plugins/maven/src/main/java/org/jetbrains/idea/maven/dom/refactorings/introduce/IntroducePropertyAction.java @@ -12,7 +12,6 @@ import com.intellij.openapi.command.WriteCommandAction; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.project.Project; import com.intellij.openapi.ui.DialogWrapper; -import com.intellij.openapi.util.Computable; import com.intellij.openapi.util.Factory; import com.intellij.openapi.util.Pair; import com.intellij.openapi.util.TextRange; @@ -40,7 +39,7 @@ import org.jetbrains.idea.maven.dom.MavenDomUtil; import org.jetbrains.idea.maven.dom.model.MavenDomProjectModel; import org.jetbrains.idea.maven.dom.model.MavenDomProperties; -import java.util.Set; +import java.util.*; public class IntroducePropertyAction extends BaseRefactoringAction { private static String PREFIX = "${"; @@ -126,7 +125,7 @@ public class IntroducePropertyAction extends BaseRefactoringAction { final MavenDomProjectModel model = MavenDomUtil.getMavenDomModel(file, MavenDomProjectModel.class); final String selectedString = editor.getDocument().getText(range); - Set ranges = getPropertiesTextRanges(stringValue); + List ranges = getPropertiesTextRanges(stringValue); int offsetInElement = range.getStartOffset() - selectedElement.getTextOffset(); if (model == null || @@ -302,27 +301,33 @@ public class IntroducePropertyAction extends BaseRefactoringAction { @NotNull private Set getUsages(@NotNull XmlElement xmlElement) { String s = xmlElement.getText(); - Set usages = new HashSet(); - if (!StringUtil.isEmptyOrSpaces(s)) { - Set ranges = getPropertiesTextRanges(s); + if (StringUtil.isEmptyOrSpaces(s)) return Collections.emptySet(); - int start = s.indexOf(mySelectedString); - while (start >= 0) { - int end = start + mySelectedString.length(); - boolean isInsideProperty = isInsideTextRanges(ranges, start, end); - if (!isInsideProperty) { - usages.add(new UsageInfo(xmlElement, start, end)); - } - start = s.indexOf(mySelectedString, end); + int start = s.indexOf(mySelectedString); + if (start == -1) return Collections.emptySet(); + + Set usages = new HashSet(); + + List ranges = getPropertiesTextRanges(s); + TextRange elementTextRange = xmlElement.getTextRange(); + PsiFile containingFile = xmlElement.getContainingFile(); + + do { + int end = start + mySelectedString.length(); + boolean isInsideProperty = isInsideTextRanges(ranges, start, end); + if (!isInsideProperty) { + usages.add(new UsageInfo(containingFile, elementTextRange.getStartOffset() + start, elementTextRange.getStartOffset() + end)); } - } + start = s.indexOf(mySelectedString, end); + } while (start != -1); + return usages; } } } - private static Set getPropertiesTextRanges(String s) { - Set ranges = new HashSet(); + private static List getPropertiesTextRanges(String s) { + List ranges = new ArrayList(); int startOffset = s.indexOf(PREFIX); while (startOffset >= 0) { int endOffset = s.indexOf(SUFFIX, startOffset); @@ -343,15 +348,13 @@ public class IntroducePropertyAction extends BaseRefactoringAction { return ranges; } - private static boolean isInsideTextRanges(@NotNull Set ranges, int start, int end) { - boolean isInsideProperty = false; + private static boolean isInsideTextRanges(@NotNull Collection ranges, int start, int end) { for (TextRange range : ranges) { if ((start >= range.getStartOffset() && (end <= range.getEndOffset() || start <= range.getEndOffset())) || (end <= range.getEndOffset() && (end > range.getStartOffset()))) { - isInsideProperty = true; - break; + return true; } } - return isInsideProperty; + return false; } }