From 7c47e2728dd669c13d1b7205f013333eb1c6f155 Mon Sep 17 00:00:00 2001 From: Petr Kudriavtsev Date: Mon, 11 Dec 2017 20:02:13 +0300 Subject: [PATCH 1/5] Go to next/prev marked occurrence --- .../actions/GotoMarkedOccurrenceBase.java | 72 +++++++++++++++++++ .../GotoNextMarkedOccurrenceAction.java | 11 +++ .../GotoPrevMarkedOccurrenceAction.java | 11 +++ 3 files changed, 94 insertions(+) create mode 100644 platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/actions/GotoMarkedOccurrenceBase.java create mode 100644 platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/actions/GotoNextMarkedOccurrenceAction.java create mode 100644 platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/actions/GotoPrevMarkedOccurrenceAction.java diff --git a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/actions/GotoMarkedOccurrenceBase.java b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/actions/GotoMarkedOccurrenceBase.java new file mode 100644 index 000000000000..71942666b376 --- /dev/null +++ b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/actions/GotoMarkedOccurrenceBase.java @@ -0,0 +1,72 @@ +/* + * Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. + */ +package com.intellij.codeInsight.daemon.impl.actions; + +import com.intellij.codeInsight.CodeInsightActionHandler; +import com.intellij.codeInsight.actions.BaseCodeInsightAction; +import com.intellij.codeInsight.daemon.impl.DaemonCodeAnalyzerEx; +import com.intellij.codeInsight.daemon.impl.HighlightInfoType; +import com.intellij.openapi.editor.Caret; +import com.intellij.openapi.editor.Document; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.editor.EditorModificationUtil; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.Ref; +import com.intellij.psi.PsiFile; +import org.jetbrains.annotations.NotNull; + +import java.util.Comparator; + +public abstract class GotoMarkedOccurrenceBase extends BaseCodeInsightAction implements CodeInsightActionHandler { + + private final Comparator myComparator; + + /** + * @param comparator defines ordering of occurrences. + */ + public GotoMarkedOccurrenceBase(Comparator comparator) { + myComparator = comparator; + } + + @NotNull + @Override + protected CodeInsightActionHandler getHandler() { + return this; + } + + @Override + public void invoke(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) { + final int caretOffset = editor.getCaretModel().getOffset(); + final int startOffset = file.getTextRange().getStartOffset(); + final int endOffset = file.getTextRange().getEndOffset(); + final Ref first = new Ref<>(); + final Ref next = new Ref<>(); + DaemonCodeAnalyzerEx.processHighlights(editor.getDocument(), project, null, startOffset, endOffset, info -> { + if (info.type == HighlightInfoType.ELEMENT_UNDER_CARET_READ || info.type == HighlightInfoType.ELEMENT_UNDER_CARET_WRITE) { + if (myComparator.compare(info.startOffset, caretOffset) > 0) { + if (next.isNull() || myComparator.compare(next.get(), info.startOffset) > 0) { + next.set(info.startOffset); + } + } + if (first.isNull() || myComparator.compare(first.get(), info.startOffset) > 0) { + first.set(info.startOffset); + } + } + return true; + }); + if (!next.isNull()) { + moveCaret(editor, editor.getCaretModel().getCurrentCaret(), next.get()); + } else if (!first.isNull()) { + moveCaret(editor, editor.getCaretModel().getCurrentCaret(), first.get()); + } else { + // It's ok, do nothing. + } + } + + private static void moveCaret(Editor editor, Caret caret, int offset) { + caret.removeSelection(); + caret.moveToOffset(offset); + EditorModificationUtil.scrollToCaret(editor); + } +} diff --git a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/actions/GotoNextMarkedOccurrenceAction.java b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/actions/GotoNextMarkedOccurrenceAction.java new file mode 100644 index 000000000000..5b2ed55094c9 --- /dev/null +++ b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/actions/GotoNextMarkedOccurrenceAction.java @@ -0,0 +1,11 @@ +/* + * Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. + */ +package com.intellij.codeInsight.daemon.impl.actions; + + +public class GotoNextMarkedOccurrenceAction extends GotoMarkedOccurrenceBase { + public GotoNextMarkedOccurrenceAction() { + super((a, b) -> a - b); + } +} diff --git a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/actions/GotoPrevMarkedOccurrenceAction.java b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/actions/GotoPrevMarkedOccurrenceAction.java new file mode 100644 index 000000000000..0fc7e5d35fee --- /dev/null +++ b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/actions/GotoPrevMarkedOccurrenceAction.java @@ -0,0 +1,11 @@ +/* + * Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. + */ +package com.intellij.codeInsight.daemon.impl.actions; + + +public class GotoPrevMarkedOccurrenceAction extends GotoMarkedOccurrenceBase { + public GotoPrevMarkedOccurrenceAction() { + super((a, b) -> b - a); + } +} From ecae00f832024210f80206d40698059827402bb9 Mon Sep 17 00:00:00 2001 From: Petr Kudriavtsev Date: Mon, 11 Dec 2017 20:03:21 +0300 Subject: [PATCH 2/5] Registered actions to go to next/prev marked occurrence --- .../src/messages/ActionsBundle.properties | 4 ++++ platform/platform-resources/src/idea/PlatformActions.xml | 2 ++ 2 files changed, 6 insertions(+) diff --git a/platform/platform-resources-en/src/messages/ActionsBundle.properties b/platform/platform-resources-en/src/messages/ActionsBundle.properties index c8a8541544d8..ba04acabd937 100644 --- a/platform/platform-resources-en/src/messages/ActionsBundle.properties +++ b/platform/platform-resources-en/src/messages/ActionsBundle.properties @@ -512,6 +512,10 @@ action.GotoDeclaration.text=_Declaration action.GotoDeclaration.description=Navigate to the declaration of the symbol at caret action.GotoImplementation.text=I_mplementation(s) action.GotoImplementation.description=Navigate to the implementation(s) of the selected class or method +action.GotoNextMarkedOccurrence.text=Go to next marked occurrence +action.GotoNextMarkedOccurrence.description=Navigate to the next highlighted occurrence +action.GotoPrevMarkedOccurrence.text=Go to previous marked occurrence +action.GotoPrevMarkedOccurrence.description=Navigate to the previous highlighted occurrence action.GotoTypeDeclaration.text=_Type Declaration action.GotoTypeDeclaration.description=Navigate to the declaration of the symbol's type at caret action.GotoSuperMethod.text=S_uper Method diff --git a/platform/platform-resources/src/idea/PlatformActions.xml b/platform/platform-resources/src/idea/PlatformActions.xml index 10a0c10390a9..7e32b777f737 100644 --- a/platform/platform-resources/src/idea/PlatformActions.xml +++ b/platform/platform-resources/src/idea/PlatformActions.xml @@ -330,6 +330,8 @@ + + From 66792efc799a9cba6ec24983746b8deb381fbb0a Mon Sep 17 00:00:00 2001 From: Petr Kudriavtsev Date: Tue, 12 Dec 2017 22:34:35 +0300 Subject: [PATCH 3/5] Fixing CPP-11457: Navigation between marked occurrences - improvements after review --- ...va => GotoElementUnderCaretUsageBase.java} | 27 ++++++++++++++----- .../GotoNextElementUnderCaretUsageAction.java | 16 +++++++++++ .../GotoNextMarkedOccurrenceAction.java | 11 -------- .../GotoPrevElementUnderCaretUsageAction.java | 16 +++++++++++ .../GotoPrevMarkedOccurrenceAction.java | 11 -------- .../src/messages/ActionsBundle.properties | 8 +++--- .../src/idea/PlatformActions.xml | 4 +-- 7 files changed, 58 insertions(+), 35 deletions(-) rename platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/actions/{GotoMarkedOccurrenceBase.java => GotoElementUnderCaretUsageBase.java} (73%) create mode 100644 platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/actions/GotoNextElementUnderCaretUsageAction.java delete mode 100644 platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/actions/GotoNextMarkedOccurrenceAction.java create mode 100644 platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/actions/GotoPrevElementUnderCaretUsageAction.java delete mode 100644 platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/actions/GotoPrevMarkedOccurrenceAction.java diff --git a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/actions/GotoMarkedOccurrenceBase.java b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/actions/GotoElementUnderCaretUsageBase.java similarity index 73% rename from platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/actions/GotoMarkedOccurrenceBase.java rename to platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/actions/GotoElementUnderCaretUsageBase.java index 71942666b376..f7a6caf328f0 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/actions/GotoMarkedOccurrenceBase.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/actions/GotoElementUnderCaretUsageBase.java @@ -18,15 +18,16 @@ import org.jetbrains.annotations.NotNull; import java.util.Comparator; -public abstract class GotoMarkedOccurrenceBase extends BaseCodeInsightAction implements CodeInsightActionHandler { +/*package*/ abstract class GotoElementUnderCaretUsageBase extends BaseCodeInsightAction implements CodeInsightActionHandler { - private final Comparator myComparator; + @NotNull + private final Direction myDirection; /** * @param comparator defines ordering of occurrences. */ - public GotoMarkedOccurrenceBase(Comparator comparator) { - myComparator = comparator; + public GotoElementUnderCaretUsageBase(@NotNull Direction direction) { + myDirection = direction; } @NotNull @@ -37,6 +38,7 @@ public abstract class GotoMarkedOccurrenceBase extends BaseCodeInsightAction imp @Override public void invoke(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) { + final Comparator ordering = myDirection.ordering; final int caretOffset = editor.getCaretModel().getOffset(); final int startOffset = file.getTextRange().getStartOffset(); final int endOffset = file.getTextRange().getEndOffset(); @@ -44,12 +46,12 @@ public abstract class GotoMarkedOccurrenceBase extends BaseCodeInsightAction imp final Ref next = new Ref<>(); DaemonCodeAnalyzerEx.processHighlights(editor.getDocument(), project, null, startOffset, endOffset, info -> { if (info.type == HighlightInfoType.ELEMENT_UNDER_CARET_READ || info.type == HighlightInfoType.ELEMENT_UNDER_CARET_WRITE) { - if (myComparator.compare(info.startOffset, caretOffset) > 0) { - if (next.isNull() || myComparator.compare(next.get(), info.startOffset) > 0) { + if (ordering.compare(info.startOffset, caretOffset) > 0) { + if (next.isNull() || ordering.compare(next.get(), info.startOffset) > 0) { next.set(info.startOffset); } } - if (first.isNull() || myComparator.compare(first.get(), info.startOffset) > 0) { + if (first.isNull() || ordering.compare(first.get(), info.startOffset) > 0) { first.set(info.startOffset); } } @@ -69,4 +71,15 @@ public abstract class GotoMarkedOccurrenceBase extends BaseCodeInsightAction imp caret.moveToOffset(offset); EditorModificationUtil.scrollToCaret(editor); } + + protected enum Direction { + FORWARD((l, r) -> l - r), + BACKWARD((l, r) -> r - l); + + public final Comparator ordering; + + Direction(Comparator ordering) { + this.ordering = ordering; + } + }; } diff --git a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/actions/GotoNextElementUnderCaretUsageAction.java b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/actions/GotoNextElementUnderCaretUsageAction.java new file mode 100644 index 000000000000..b4b5b97c0006 --- /dev/null +++ b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/actions/GotoNextElementUnderCaretUsageAction.java @@ -0,0 +1,16 @@ +/* + * Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. + */ +package com.intellij.codeInsight.daemon.impl.actions; + +/** + * Action moves caret to the next highlighted element under caret. + * + * Please note, it works only if option "Highlight usages of element at caret" turned on. + * @see com.intellij.codeInsight.CodeInsightSettings#HIGHLIGHT_IDENTIFIER_UNDER_CARET highlight usages + */ +public class GotoNextElementUnderCaretUsageAction extends GotoElementUnderCaretUsageBase { + public GotoNextElementUnderCaretUsageAction() { + super(Direction.FORWARD); + } +} diff --git a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/actions/GotoNextMarkedOccurrenceAction.java b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/actions/GotoNextMarkedOccurrenceAction.java deleted file mode 100644 index 5b2ed55094c9..000000000000 --- a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/actions/GotoNextMarkedOccurrenceAction.java +++ /dev/null @@ -1,11 +0,0 @@ -/* - * Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. - */ -package com.intellij.codeInsight.daemon.impl.actions; - - -public class GotoNextMarkedOccurrenceAction extends GotoMarkedOccurrenceBase { - public GotoNextMarkedOccurrenceAction() { - super((a, b) -> a - b); - } -} diff --git a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/actions/GotoPrevElementUnderCaretUsageAction.java b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/actions/GotoPrevElementUnderCaretUsageAction.java new file mode 100644 index 000000000000..fd0eaea692ee --- /dev/null +++ b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/actions/GotoPrevElementUnderCaretUsageAction.java @@ -0,0 +1,16 @@ +/* + * Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. + */ +package com.intellij.codeInsight.daemon.impl.actions; + +/** + * Action moves caret to the previous highlighted element under caret. + * + * Please note, it works only if option "Highlight usages of element at caret" turned on. + * @see com.intellij.codeInsight.CodeInsightSettings#HIGHLIGHT_IDENTIFIER_UNDER_CARET highlight usages + */ +public class GotoPrevElementUnderCaretUsageAction extends GotoElementUnderCaretUsageBase { + public GotoPrevElementUnderCaretUsageAction() { + super(Direction.BACKWARD); + } +} diff --git a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/actions/GotoPrevMarkedOccurrenceAction.java b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/actions/GotoPrevMarkedOccurrenceAction.java deleted file mode 100644 index 0fc7e5d35fee..000000000000 --- a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/actions/GotoPrevMarkedOccurrenceAction.java +++ /dev/null @@ -1,11 +0,0 @@ -/* - * Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. - */ -package com.intellij.codeInsight.daemon.impl.actions; - - -public class GotoPrevMarkedOccurrenceAction extends GotoMarkedOccurrenceBase { - public GotoPrevMarkedOccurrenceAction() { - super((a, b) -> b - a); - } -} diff --git a/platform/platform-resources-en/src/messages/ActionsBundle.properties b/platform/platform-resources-en/src/messages/ActionsBundle.properties index ba04acabd937..e50478f47982 100644 --- a/platform/platform-resources-en/src/messages/ActionsBundle.properties +++ b/platform/platform-resources-en/src/messages/ActionsBundle.properties @@ -512,10 +512,10 @@ action.GotoDeclaration.text=_Declaration action.GotoDeclaration.description=Navigate to the declaration of the symbol at caret action.GotoImplementation.text=I_mplementation(s) action.GotoImplementation.description=Navigate to the implementation(s) of the selected class or method -action.GotoNextMarkedOccurrence.text=Go to next marked occurrence -action.GotoNextMarkedOccurrence.description=Navigate to the next highlighted occurrence -action.GotoPrevMarkedOccurrence.text=Go to previous marked occurrence -action.GotoPrevMarkedOccurrence.description=Navigate to the previous highlighted occurrence +action.GotoNextElementUnderCaretUsage.text=Go to next highlighted element usage +action.GotoNextElementUnderCaretUsage.description=Navigate to the next highlighted element usage +action.GotoPrevElementUnderCaretUsage.text=Go to previous highlighted element usage +action.GotoPrevElementUnderCaretUsage.description=Navigate to the previous highlighted element usage action.GotoTypeDeclaration.text=_Type Declaration action.GotoTypeDeclaration.description=Navigate to the declaration of the symbol's type at caret action.GotoSuperMethod.text=S_uper Method diff --git a/platform/platform-resources/src/idea/PlatformActions.xml b/platform/platform-resources/src/idea/PlatformActions.xml index 7e32b777f737..87f0bc8549ba 100644 --- a/platform/platform-resources/src/idea/PlatformActions.xml +++ b/platform/platform-resources/src/idea/PlatformActions.xml @@ -330,8 +330,8 @@ - - + + From 319ec0a552a4554617f8a510036b5d53f99b2062 Mon Sep 17 00:00:00 2001 From: Petr Kudriavtsev Date: Tue, 12 Dec 2017 22:44:21 +0300 Subject: [PATCH 4/5] Fixing CPP-11457: Navigation between marked occurrences - improved going to the previous occurrence if caret is inside or at the right border of current occurrence --- .../daemon/impl/actions/GotoElementUnderCaretUsageBase.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/actions/GotoElementUnderCaretUsageBase.java b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/actions/GotoElementUnderCaretUsageBase.java index f7a6caf328f0..7b74a9fa3012 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/actions/GotoElementUnderCaretUsageBase.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/actions/GotoElementUnderCaretUsageBase.java @@ -46,7 +46,7 @@ import java.util.Comparator; final Ref next = new Ref<>(); DaemonCodeAnalyzerEx.processHighlights(editor.getDocument(), project, null, startOffset, endOffset, info -> { if (info.type == HighlightInfoType.ELEMENT_UNDER_CARET_READ || info.type == HighlightInfoType.ELEMENT_UNDER_CARET_WRITE) { - if (ordering.compare(info.startOffset, caretOffset) > 0) { + if (ordering.compare(info.startOffset, caretOffset) > 0 && ordering.compare(info.endOffset, caretOffset) > 0) { if (next.isNull() || ordering.compare(next.get(), info.startOffset) > 0) { next.set(info.startOffset); } From 6e7a26e33ed7c693c7d8383a49d7cd10b97f1370 Mon Sep 17 00:00:00 2001 From: Petr Kudriavtsev Date: Wed, 13 Dec 2017 13:14:55 +0300 Subject: [PATCH 5/5] Fixing CPP-11457: Navigation between marked occurrences - comment --- .../daemon/impl/actions/GotoElementUnderCaretUsageBase.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/actions/GotoElementUnderCaretUsageBase.java b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/actions/GotoElementUnderCaretUsageBase.java index 7b74a9fa3012..17365b0380f3 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/actions/GotoElementUnderCaretUsageBase.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/actions/GotoElementUnderCaretUsageBase.java @@ -24,7 +24,7 @@ import java.util.Comparator; private final Direction myDirection; /** - * @param comparator defines ordering of occurrences. + * @param direction defines direction for next usage searching. */ public GotoElementUnderCaretUsageBase(@NotNull Direction direction) { myDirection = direction;