summaryrefslogtreecommitdiff
path: root/platform/lang-impl/src/com/intellij/openapi/editor/actions
diff options
context:
space:
mode:
Diffstat (limited to 'platform/lang-impl/src/com/intellij/openapi/editor/actions')
-rw-r--r--platform/lang-impl/src/com/intellij/openapi/editor/actions/SelectAllOccurrencesAction.java53
-rw-r--r--platform/lang-impl/src/com/intellij/openapi/editor/actions/SelectNextOccurrenceAction.java30
-rw-r--r--platform/lang-impl/src/com/intellij/openapi/editor/actions/SelectOccurrencesActionHandler.java47
-rw-r--r--platform/lang-impl/src/com/intellij/openapi/editor/actions/UnselectPreviousOccurrenceAction.java10
4 files changed, 105 insertions, 35 deletions
diff --git a/platform/lang-impl/src/com/intellij/openapi/editor/actions/SelectAllOccurrencesAction.java b/platform/lang-impl/src/com/intellij/openapi/editor/actions/SelectAllOccurrencesAction.java
index d135b528f5e2..3e284ff6b2cc 100644
--- a/platform/lang-impl/src/com/intellij/openapi/editor/actions/SelectAllOccurrencesAction.java
+++ b/platform/lang-impl/src/com/intellij/openapi/editor/actions/SelectAllOccurrencesAction.java
@@ -15,9 +15,9 @@
*/
package com.intellij.openapi.editor.actions;
-import com.intellij.find.FindManager;
-import com.intellij.find.FindModel;
-import com.intellij.find.FindResult;
+import com.intellij.find.*;
+import com.intellij.find.editorHeaderActions.EditorHeaderAction;
+import com.intellij.find.editorHeaderActions.SelectAllAction;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.editor.Caret;
import com.intellij.openapi.editor.Editor;
@@ -27,6 +27,8 @@ import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import org.jetbrains.annotations.Nullable;
+import java.util.Iterator;
+
public class SelectAllOccurrencesAction extends EditorAction {
protected SelectAllOccurrencesAction() {
super(new Handler());
@@ -39,7 +41,9 @@ public class SelectAllOccurrencesAction extends EditorAction {
}
@Override
- public void doExecute(Editor editor, @Nullable Caret c, DataContext dataContext) {
+ public void doExecute(final Editor editor, @Nullable Caret c, DataContext dataContext) {
+ if (executeEquivalentFindPanelAction(editor, dataContext)) return;
+
Caret caret = c == null ? editor.getCaretModel().getPrimaryCaret() : c;
boolean wholeWordsSearch = false;
@@ -58,25 +62,36 @@ public class SelectAllOccurrencesAction extends EditorAction {
}
int caretShiftFromSelectionStart = caret.getOffset() - caret.getSelectionStart();
- FindManager findManager = FindManager.getInstance(project);
+ final FindManager findManager = FindManager.getInstance(project);
+
+ final FindModel model = getFindModel(selectedText, wholeWordsSearch);
- FindModel model = new FindModel();
- model.setStringToFind(selectedText);
- model.setCaseSensitive(true);
- model.setWholeWordsOnly(wholeWordsSearch);
+ FindUtil.selectSearchResultsInEditor(editor, new Iterator<FindResult>() {
+ FindResult findResult = findManager.findString(editor.getDocument().getCharsSequence(), 0, model);
- int searchStartOffset = 0;
- FindResult findResult = findManager.findString(editor.getDocument().getCharsSequence(), searchStartOffset, model);
- while (findResult.isStringFound()) {
- int newCaretOffset = caretShiftFromSelectionStart + findResult.getStartOffset();
- EditorActionUtil.makePositionVisible(editor, newCaretOffset);
- Caret newCaret = editor.getCaretModel().addCaret(editor.offsetToVisualPosition(newCaretOffset));
- if (newCaret != null) {
- setSelection(editor, newCaret, findResult);
+ @Override
+ public boolean hasNext() {
+ return findResult.isStringFound();
}
- findResult = findManager.findString(editor.getDocument().getCharsSequence(), findResult.getEndOffset(), model);
- }
+
+ @Override
+ public FindResult next() {
+ FindResult result = findResult;
+ findResult = findManager.findString(editor.getDocument().getCharsSequence(), findResult.getEndOffset(), model);
+ return result;
+ }
+
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+ }, caretShiftFromSelectionStart);
editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
}
+
+ @Override
+ protected EditorHeaderAction getEquivalentFindPanelAction(EditorSearchComponent searchComponent) {
+ return new SelectAllAction(searchComponent);
+ }
}
}
diff --git a/platform/lang-impl/src/com/intellij/openapi/editor/actions/SelectNextOccurrenceAction.java b/platform/lang-impl/src/com/intellij/openapi/editor/actions/SelectNextOccurrenceAction.java
index cc4783b4d4db..7d3c408ee152 100644
--- a/platform/lang-impl/src/com/intellij/openapi/editor/actions/SelectNextOccurrenceAction.java
+++ b/platform/lang-impl/src/com/intellij/openapi/editor/actions/SelectNextOccurrenceAction.java
@@ -15,9 +15,9 @@
*/
package com.intellij.openapi.editor.actions;
-import com.intellij.find.FindManager;
-import com.intellij.find.FindModel;
-import com.intellij.find.FindResult;
+import com.intellij.find.*;
+import com.intellij.find.editorHeaderActions.AddOccurrenceAction;
+import com.intellij.find.editorHeaderActions.EditorHeaderAction;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.editor.Caret;
import com.intellij.openapi.editor.Editor;
@@ -40,6 +40,8 @@ public class SelectNextOccurrenceAction extends EditorAction {
@Override
public void doExecute(Editor editor, @Nullable Caret c, DataContext dataContext) {
+ if (executeEquivalentFindPanelAction(editor, dataContext)) return;
+
Caret caret = c == null ? editor.getCaretModel().getPrimaryCaret() : c;
TextRange wordSelectionRange = getSelectionRange(editor, caret);
boolean notFoundPreviously = getAndResetNotFoundStatus(editor);
@@ -52,26 +54,21 @@ public class SelectNextOccurrenceAction extends EditorAction {
}
FindManager findManager = FindManager.getInstance(project);
- FindModel model = new FindModel();
- model.setStringToFind(selectedText);
- model.setCaseSensitive(true);
- model.setWholeWordsOnly(wholeWordSearch);
+ FindModel model = getFindModel(selectedText, wholeWordSearch);
+
+ findManager.setFindWasPerformed();
+ findManager.setFindNextModel(model);
int searchStartOffset = notFoundPreviously ? 0 : caret.getSelectionEnd();
FindResult findResult = findManager.findString(editor.getDocument().getCharsSequence(), searchStartOffset, model);
if (findResult.isStringFound()) {
- int newCaretOffset = caret.getOffset() - caret.getSelectionStart() + findResult.getStartOffset();
- EditorActionUtil.makePositionVisible(editor, newCaretOffset);
- Caret newCaret = editor.getCaretModel().addCaret(editor.offsetToVisualPosition(newCaretOffset));
- if (newCaret == null) {
+ boolean caretAdded = FindUtil.selectSearchResultInEditor(editor, findResult, caret.getOffset() - caret.getSelectionStart());
+ if (!caretAdded) {
// this means that the found occurence is already selected
if (notFoundPreviously) {
setNotFoundStatus(editor); // to make sure we won't show hint anymore if there are no more occurrences
}
}
- else {
- setSelection(editor, newCaret, findResult);
- }
}
else {
setNotFoundStatus(editor);
@@ -87,5 +84,10 @@ public class SelectNextOccurrenceAction extends EditorAction {
}
editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
}
+
+ @Override
+ protected EditorHeaderAction getEquivalentFindPanelAction(EditorSearchComponent searchComponent) {
+ return new AddOccurrenceAction(searchComponent);
+ }
}
}
diff --git a/platform/lang-impl/src/com/intellij/openapi/editor/actions/SelectOccurrencesActionHandler.java b/platform/lang-impl/src/com/intellij/openapi/editor/actions/SelectOccurrencesActionHandler.java
index c43eae703fd5..7b189281dfd0 100644
--- a/platform/lang-impl/src/com/intellij/openapi/editor/actions/SelectOccurrencesActionHandler.java
+++ b/platform/lang-impl/src/com/intellij/openapi/editor/actions/SelectOccurrencesActionHandler.java
@@ -19,8 +19,11 @@ import com.intellij.codeInsight.editorActions.SelectWordUtil;
import com.intellij.codeInsight.hint.HintManager;
import com.intellij.codeInsight.hint.HintManagerImpl;
import com.intellij.codeInsight.hint.HintUtil;
+import com.intellij.find.EditorSearchComponent;
import com.intellij.find.FindBundle;
-import com.intellij.openapi.actionSystem.IdeActions;
+import com.intellij.find.FindModel;
+import com.intellij.find.editorHeaderActions.EditorHeaderAction;
+import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.editor.Caret;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.EditorLastActionTracker;
@@ -28,13 +31,24 @@ import com.intellij.openapi.editor.actionSystem.EditorActionHandler;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.TextRange;
import com.intellij.ui.LightweightHint;
+import com.intellij.util.containers.HashSet;
import org.jetbrains.annotations.Nullable;
+import java.util.Arrays;
+import java.util.Set;
+
abstract public class SelectOccurrencesActionHandler extends EditorActionHandler {
private static final Key<Boolean> NOT_FOUND = Key.create("select.next.occurence.not.found");
private static final Key<Boolean> WHOLE_WORDS = Key.create("select.next.occurence.whole.words");
+ private static final Set<String> SELECT_ACTIONS = new HashSet<String>(Arrays.asList(
+ IdeActions.ACTION_SELECT_NEXT_OCCURENCE,
+ IdeActions.ACTION_UNSELECT_PREVIOUS_OCCURENCE,
+ IdeActions.ACTION_FIND_NEXT,
+ IdeActions.ACTION_FIND_PREVIOUS
+ ));
+
protected static void setSelection(Editor editor, Caret caret, TextRange selectionRange) {
EditorActionUtil.makePositionVisible(editor, selectionRange.getStartOffset());
EditorActionUtil.makePositionVisible(editor, selectionRange.getEndOffset());
@@ -83,6 +97,35 @@ abstract public class SelectOccurrencesActionHandler extends EditorActionHandler
protected static boolean isRepeatedActionInvocation() {
String lastActionId = EditorLastActionTracker.getInstance().getLastActionId();
- return IdeActions.ACTION_SELECT_NEXT_OCCURENCE.equals(lastActionId) || IdeActions.ACTION_UNSELECT_PREVIOUS_OCCURENCE.equals(lastActionId);
+ return SELECT_ACTIONS.contains(lastActionId);
+ }
+
+ protected static FindModel getFindModel(String text, boolean wholeWords) {
+ FindModel model = new FindModel();
+ model.setStringToFind(text);
+ model.setCaseSensitive(true);
+ model.setWholeWordsOnly(wholeWords);
+ return model;
+ }
+
+ protected boolean executeEquivalentFindPanelAction(Editor editor, DataContext context) {
+ if (editor.getHeaderComponent() instanceof EditorSearchComponent) {
+ EditorSearchComponent searchComponent = (EditorSearchComponent)editor.getHeaderComponent();
+ EditorHeaderAction action = getEquivalentFindPanelAction(searchComponent);
+ if (action != null) {
+ Presentation presentation = new Presentation();
+ AnActionEvent event = new AnActionEvent(null, context, ActionPlaces.MAIN_MENU, presentation, ActionManager.getInstance(), 0);
+ action.update(event);
+ if (presentation.isEnabled()) {
+ action.actionPerformed(event);
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+ protected EditorHeaderAction getEquivalentFindPanelAction(EditorSearchComponent searchComponent) {
+ return null;
}
}
diff --git a/platform/lang-impl/src/com/intellij/openapi/editor/actions/UnselectPreviousOccurrenceAction.java b/platform/lang-impl/src/com/intellij/openapi/editor/actions/UnselectPreviousOccurrenceAction.java
index a5657ff89b27..fffff56b12dc 100644
--- a/platform/lang-impl/src/com/intellij/openapi/editor/actions/UnselectPreviousOccurrenceAction.java
+++ b/platform/lang-impl/src/com/intellij/openapi/editor/actions/UnselectPreviousOccurrenceAction.java
@@ -15,6 +15,9 @@
*/
package com.intellij.openapi.editor.actions;
+import com.intellij.find.EditorSearchComponent;
+import com.intellij.find.editorHeaderActions.EditorHeaderAction;
+import com.intellij.find.editorHeaderActions.RemoveOccurrenceAction;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.editor.Caret;
import com.intellij.openapi.editor.Editor;
@@ -35,6 +38,8 @@ public class UnselectPreviousOccurrenceAction extends EditorAction {
@Override
public void doExecute(Editor editor, @Nullable Caret caret, DataContext dataContext) {
+ if (executeEquivalentFindPanelAction(editor, dataContext)) return;
+
if (editor.getCaretModel().getCaretCount() > 1) {
editor.getCaretModel().removeCaret(editor.getCaretModel().getPrimaryCaret());
}
@@ -44,5 +49,10 @@ public class UnselectPreviousOccurrenceAction extends EditorAction {
getAndResetNotFoundStatus(editor);
editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
}
+
+ @Override
+ protected EditorHeaderAction getEquivalentFindPanelAction(EditorSearchComponent searchComponent) {
+ return new RemoveOccurrenceAction(searchComponent);
+ }
}
}