From ce3dcaa816d45e05790801122d9d80e9e44c14ea Mon Sep 17 00:00:00 2001 From: Dmitry Batrak Date: Tue, 25 Apr 2017 20:30:09 +0300 Subject: [PATCH] IDEA-170592 Choose Content to Paste Dialog (Ctrl+Shift+V) only highlights the first match per line following IDEA-CR-20578 --- .../psi/codeStyle/AllOccurrencesMatcher.java | 58 +++++++++++++++++++ .../psi/codeStyle/FixingLayoutMatcher.java | 7 +-- .../psi/codeStyle/MinusculeMatcher.java | 29 +--------- .../com/intellij/psi/codeStyle/NameUtil.java | 3 +- 4 files changed, 65 insertions(+), 32 deletions(-) create mode 100644 platform/util/src/com/intellij/psi/codeStyle/AllOccurrencesMatcher.java diff --git a/platform/util/src/com/intellij/psi/codeStyle/AllOccurrencesMatcher.java b/platform/util/src/com/intellij/psi/codeStyle/AllOccurrencesMatcher.java new file mode 100644 index 000000000000..4d4b6f5bc31b --- /dev/null +++ b/platform/util/src/com/intellij/psi/codeStyle/AllOccurrencesMatcher.java @@ -0,0 +1,58 @@ +/* + * Copyright 2000-2017 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.psi.codeStyle; + +import com.intellij.openapi.util.TextRange; +import com.intellij.util.containers.ContainerUtil; +import com.intellij.util.containers.FList; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; +import java.util.List; + +class AllOccurrencesMatcher extends FixingLayoutMatcher { + AllOccurrencesMatcher(@NotNull String pattern, @NotNull NameUtil.MatchingCaseSensitivity options, String hardSeparators) { + super(pattern, options, hardSeparators); + } + + @Nullable + @Override + public FList matchingFragments(@NotNull String name) { + FList match = super.matchingFragments(name); + if (!ContainerUtil.isEmpty(match)) { + List> allMatchesReversed = new ArrayList>(); + int lastOffset = 0; + while (!ContainerUtil.isEmpty(match)) { + FList reversedWithAbsoluteOffsets = FList.emptyList(); + for (TextRange r : match) { + reversedWithAbsoluteOffsets = reversedWithAbsoluteOffsets.prepend(r.shiftRight(lastOffset)); + } + allMatchesReversed.add(reversedWithAbsoluteOffsets); + lastOffset = reversedWithAbsoluteOffsets.get(0).getEndOffset(); + match = super.matchingFragments(name.substring(lastOffset)); + + } + match = FList.emptyList(); + for (int i = allMatchesReversed.size() - 1; i >= 0; i--) { + for (TextRange range : allMatchesReversed.get(i)) { + match = match.prepend(range); + } + } + } + return match; + } +} diff --git a/platform/util/src/com/intellij/psi/codeStyle/FixingLayoutMatcher.java b/platform/util/src/com/intellij/psi/codeStyle/FixingLayoutMatcher.java index fda723a40b74..1b9b1920dc0b 100644 --- a/platform/util/src/com/intellij/psi/codeStyle/FixingLayoutMatcher.java +++ b/platform/util/src/com/intellij/psi/codeStyle/FixingLayoutMatcher.java @@ -31,11 +31,10 @@ public class FixingLayoutMatcher extends MinusculeMatcher { @Nullable private final MinusculeMatcher myFixedMatcher; - FixingLayoutMatcher(@NotNull String pattern, @NotNull NameUtil.MatchingCaseSensitivity options, String hardSeparators, - boolean matchAllOccurrences) { - super(pattern, options, hardSeparators, matchAllOccurrences); + FixingLayoutMatcher(@NotNull String pattern, @NotNull NameUtil.MatchingCaseSensitivity options, String hardSeparators) { + super(pattern, options, hardSeparators); String s = fixPattern(pattern); - myFixedMatcher = s == null ? null : new MinusculeMatcher(s, options, hardSeparators, matchAllOccurrences); + myFixedMatcher = s == null ? null : new MinusculeMatcher(s, options, hardSeparators); } @Nullable diff --git a/platform/util/src/com/intellij/psi/codeStyle/MinusculeMatcher.java b/platform/util/src/com/intellij/psi/codeStyle/MinusculeMatcher.java index e2b98fcbc046..227e595c649e 100644 --- a/platform/util/src/com/intellij/psi/codeStyle/MinusculeMatcher.java +++ b/platform/util/src/com/intellij/psi/codeStyle/MinusculeMatcher.java @@ -17,7 +17,6 @@ package com.intellij.psi.codeStyle; import com.intellij.openapi.util.TextRange; import com.intellij.openapi.util.text.StringUtil; -import com.intellij.util.containers.ContainerUtil; import com.intellij.util.containers.FList; import com.intellij.util.io.IOUtil; import com.intellij.util.text.Matcher; @@ -25,10 +24,8 @@ import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.ArrayList; import java.util.BitSet; import java.util.Iterator; -import java.util.List; /** * Tells whether a string matches a specific pattern. Allows for lowercase camel-hump matching. @@ -59,7 +56,6 @@ public class MinusculeMatcher implements Matcher { private final char[] toLowerCase; private final char[] myMeaningfulCharacters; private final int myMinNameLength; - private final boolean myMatchAllOccurrences; /** * Constructs a matcher by a given pattern. @@ -67,11 +63,8 @@ public class MinusculeMatcher implements Matcher { * @param options case sensitivity settings * @param hardSeparators A string of characters (empty by default). Lowercase humps don't work for parts separated by any of these characters. * Need either an explicit uppercase letter or the same separator character in prefix - * @param matchAllOccurrences if true, all pattern matches will be reported by {@link #matchingFragments(String)} method, - * otherwise, only first match will be reported */ - MinusculeMatcher(@NotNull String pattern, @NotNull NameUtil.MatchingCaseSensitivity options, @NotNull String hardSeparators, - boolean matchAllOccurrences) { + MinusculeMatcher(@NotNull String pattern, @NotNull NameUtil.MatchingCaseSensitivity options, @NotNull String hardSeparators) { myOptions = options; myPattern = StringUtil.trimEnd(pattern, "* ").toCharArray(); myHardSeparators = hardSeparators; @@ -100,7 +93,6 @@ public class MinusculeMatcher implements Matcher { myHasDots = hasDots(i); myMeaningfulCharacters = meaningful.toString().toCharArray(); myMinNameLength = myMeaningfulCharacters.length / 2; - myMatchAllOccurrences = matchAllOccurrences; } private static boolean isWordSeparator(char c) { @@ -264,24 +256,7 @@ public class MinusculeMatcher implements Matcher { MatchingState state = myMatchingState.get(); state.initializeState(isAscii, length); try { - FList match = matchWildcards(name, 0, 0, state); - - if (myMatchAllOccurrences && !ContainerUtil.isEmpty(match)) { - List> allMatchesReversed = new ArrayList>(); - while (!ContainerUtil.isEmpty(match)) { - match = FList.createFromReversed(match); - allMatchesReversed.add(match); - int lastOffset = match.get(0).getEndOffset(); - match = matchWildcards(name, 0, lastOffset, state); - } - match = FList.emptyList(); - for (int i = allMatchesReversed.size() - 1; i >= 0; i--) { - for (TextRange range : allMatchesReversed.get(i)) { - match = match.prepend(range); - } - } - } - return match; + return matchWildcards(name, 0, 0, state); } finally { state.releaseState(); diff --git a/platform/util/src/com/intellij/psi/codeStyle/NameUtil.java b/platform/util/src/com/intellij/psi/codeStyle/NameUtil.java index 696424c50499..91369fed3ce4 100644 --- a/platform/util/src/com/intellij/psi/codeStyle/NameUtil.java +++ b/platform/util/src/com/intellij/psi/codeStyle/NameUtil.java @@ -436,7 +436,8 @@ public class NameUtil { } public MinusculeMatcher build() { - return new FixingLayoutMatcher(pattern, caseSensitivity, separators, matchAllOccurrences); + return matchAllOccurrences ? new AllOccurrencesMatcher(pattern, caseSensitivity, separators) + : new FixingLayoutMatcher(pattern, caseSensitivity, separators); } }