From ec90754f4a36121cd0541240248e167e77e1c31f Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Wed, 22 Dec 2010 13:12:03 +0300 Subject: [PATCH] IDEA-63384 --- .../src/com/intellij/find/FindModel.java | 24 ++++++++++++++ .../intellij/find/EditorSearchComponent.java | 31 ++++++++++--------- .../intellij/find/impl/FindManagerImpl.java | 14 ++------- 3 files changed, 42 insertions(+), 27 deletions(-) diff --git a/platform/lang-api/src/com/intellij/find/FindModel.java b/platform/lang-api/src/com/intellij/find/FindModel.java index c3c191568012..88f946e98fa2 100644 --- a/platform/lang-api/src/com/intellij/find/FindModel.java +++ b/platform/lang-api/src/com/intellij/find/FindModel.java @@ -22,6 +22,9 @@ import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; + /** * Represents the settings of a Find, Replace, Find in Path or Replace in Path * operations. @@ -132,6 +135,7 @@ public class FindModel extends UserDataHolderBase implements Cloneable { public void setStringToFind(@NotNull String s) { LOG.assertTrue(s.length() > 0); myStringToFind = s; + myPattern = NO_PATTERN; } /** @@ -618,4 +622,24 @@ public class FindModel extends UserDataHolderBase implements Cloneable { public void setInCommentsOnly(boolean inCommentsOnly) { isInCommentsOnly = inCommentsOnly; } + + private static final Pattern NO_PATTERN = Pattern.compile(""); + private Pattern myPattern = NO_PATTERN; + public Pattern compileRegExp() { + String toFind = getStringToFind(); + + Pattern pattern = myPattern; + if (pattern == NO_PATTERN) { + try { + myPattern = pattern = Pattern.compile(toFind, isCaseSensitive() ? Pattern.MULTILINE : Pattern.MULTILINE | Pattern.CASE_INSENSITIVE); + } + catch(PatternSyntaxException e){ + LOG.error(e); + myPattern = null; + return null; + } + } + + return pattern; + } } \ No newline at end of file diff --git a/platform/lang-impl/src/com/intellij/find/EditorSearchComponent.java b/platform/lang-impl/src/com/intellij/find/EditorSearchComponent.java index 5d3e20c99caf..6d43f2d82806 100644 --- a/platform/lang-impl/src/com/intellij/find/EditorSearchComponent.java +++ b/platform/lang-impl/src/com/intellij/find/EditorSearchComponent.java @@ -146,8 +146,8 @@ public class EditorSearchComponent extends JPanel implements DataProvider { DefaultActionGroup group = new DefaultActionGroup("search bar", false); group.add(new ShowHistoryAction()); - group.add(new PrevOccurenceAction()); - group.add(new NextOccurenceAction()); + group.add(new PrevOccurrenceAction()); + group.add(new NextOccurrenceAction()); group.add(new FindAllAction()); final ActionToolbar tb = ActionManager.getInstance().createActionToolbar("SearchBar", group, true); @@ -250,7 +250,7 @@ public class EditorSearchComponent extends JPanel implements DataProvider { setSmallerFontAndOpaque(myClickToHighlightLabel); myClickToHighlightLabel.setVisible(false); - JLabel closeLabel = new JLabel(" ", IconLoader.getIcon("/actions/cross.png"), JLabel.RIGHT); + JLabel closeLabel = new JLabel(" ", IconLoader.getIcon("/actions/cross.png"), SwingConstants.RIGHT); closeLabel.addMouseListener(new MouseAdapter() { public void mousePressed(final MouseEvent e) { close(); @@ -297,7 +297,7 @@ public class EditorSearchComponent extends JPanel implements DataProvider { addCurrentTextToRecents(); } } - }, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, SystemInfo.isMac ? KeyEvent.META_DOWN_MASK : KeyEvent.CTRL_DOWN_MASK), JComponent.WHEN_FOCUSED); + }, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, SystemInfo.isMac ? InputEvent.META_DOWN_MASK : InputEvent.CTRL_DOWN_MASK), JComponent.WHEN_FOCUSED); final String initialText = myEditor.getSelectionModel().getSelectedText(); @@ -459,7 +459,9 @@ public class EditorSearchComponent extends JPanel implements DataProvider { while (true) { FindResult result = findManager.findString(myEditor.getDocument().getCharsSequence(), offset, model, virtualFile); if (!result.isStringFound()) break; - offset = result.getEndOffset(); + int newOffset = result.getEndOffset(); + if (offset == newOffset) break; + offset = newOffset; results.add(result); if (results.size() > myMatchesLimit) break; @@ -597,16 +599,15 @@ public class EditorSearchComponent extends JPanel implements DataProvider { updateResults(true); } - private class PrevOccurenceAction extends AnAction implements DumbAware { - public PrevOccurenceAction() { + private class PrevOccurrenceAction extends AnAction implements DumbAware { + public PrevOccurrenceAction() { copyFrom(ActionManager.getInstance().getAction(IdeActions.ACTION_PREVIOUS_OCCURENCE)); ArrayList shortcuts = new ArrayList(); - ContainerUtil - .addAll(shortcuts, ActionManager.getInstance().getAction(IdeActions.ACTION_FIND_PREVIOUS).getShortcutSet().getShortcuts()); - ContainerUtil - .addAll(shortcuts, ActionManager.getInstance().getAction(IdeActions.ACTION_EDITOR_MOVE_CARET_UP).getShortcutSet().getShortcuts()); - shortcuts.add(new KeyboardShortcut(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, KeyEvent.SHIFT_DOWN_MASK), null)); + ContainerUtil.addAll(shortcuts,ActionManager.getInstance().getAction(IdeActions.ACTION_FIND_PREVIOUS).getShortcutSet().getShortcuts()); + ContainerUtil.addAll(shortcuts, + ActionManager.getInstance().getAction(IdeActions.ACTION_EDITOR_MOVE_CARET_UP).getShortcutSet().getShortcuts()); + shortcuts.add(new KeyboardShortcut(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, InputEvent.SHIFT_DOWN_MASK), null)); registerCustomShortcutSet( new CustomShortcutSet(shortcuts.toArray(new Shortcut[shortcuts.size()])), @@ -626,8 +627,8 @@ public class EditorSearchComponent extends JPanel implements DataProvider { return myOkToSearch && myHasMatches; } - private class NextOccurenceAction extends AnAction implements DumbAware { - public NextOccurenceAction() { + private class NextOccurrenceAction extends AnAction implements DumbAware { + public NextOccurrenceAction() { copyFrom(ActionManager.getInstance().getAction(IdeActions.ACTION_NEXT_OCCURENCE)); ArrayList shortcuts = new ArrayList(); ContainerUtil.addAll(shortcuts, ActionManager.getInstance().getAction(IdeActions.ACTION_FIND_NEXT).getShortcutSet().getShortcuts()); @@ -657,7 +658,7 @@ public class EditorSearchComponent extends JPanel implements DataProvider { ArrayList shortcuts = new ArrayList(); ContainerUtil.addAll(shortcuts, ActionManager.getInstance().getAction(IdeActions.ACTION_FIND).getShortcutSet().getShortcuts()); - shortcuts.add(new KeyboardShortcut(KeyStroke.getKeyStroke(KeyEvent.VK_H, KeyEvent.CTRL_DOWN_MASK), null)); + shortcuts.add(new KeyboardShortcut(KeyStroke.getKeyStroke(KeyEvent.VK_H, InputEvent.CTRL_DOWN_MASK), null)); ContainerUtil.addAll(shortcuts, ActionManager.getInstance().getAction("IncrementalSearch").getShortcutSet().getShortcuts()); registerCustomShortcutSet( diff --git a/platform/lang-impl/src/com/intellij/find/impl/FindManagerImpl.java b/platform/lang-impl/src/com/intellij/find/impl/FindManagerImpl.java index 0f924d4274a4..e264af03df3c 100644 --- a/platform/lang-impl/src/com/intellij/find/impl/FindManagerImpl.java +++ b/platform/lang-impl/src/com/intellij/find/impl/FindManagerImpl.java @@ -508,18 +508,8 @@ public class FindManagerImpl extends FindManager implements PersistentStateCompo } private static Matcher compileRegExp(FindModel model, CharSequence text) { - String toFind = model.getStringToFind(); - - Pattern pattern; - try { - pattern = Pattern.compile(toFind, model.isCaseSensitive() ? Pattern.MULTILINE : Pattern.MULTILINE | Pattern.CASE_INSENSITIVE); - } - catch(PatternSyntaxException e){ - LOG.error(e); - return null; - } - - return pattern.matcher(text); + Pattern pattern = model.compileRegExp(); + return pattern == null ? null : pattern.matcher(text); } public String getStringToReplace(@NotNull String foundString, @NotNull FindModel model) {