highlight continued/exiting statements on continue/break selection

IDEA-123328
This commit is contained in:
Anna Kozlova
2018-06-06 09:43:10 +03:00
parent bd0209bd69
commit 3cc9cdf88b
5 changed files with 93 additions and 0 deletions
@@ -0,0 +1,62 @@
// Copyright 2000-2018 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.highlighting;
import com.intellij.openapi.editor.Editor;
import com.intellij.psi.*;
import com.intellij.util.Consumer;
import java.util.Collections;
import java.util.List;
public class HighlightBreakOutsHandler extends HighlightUsagesHandlerBase<PsiElement> {
private final PsiElement myTarget;
public HighlightBreakOutsHandler(Editor editor, PsiFile file, PsiElement target) {
super(editor, file);
myTarget = target;
}
@Override
public List<PsiElement> getTargets() {
return Collections.singletonList(myTarget);
}
@Override
protected void selectTargets(List<PsiElement> targets, Consumer<List<PsiElement>> selectionConsumer) {
selectionConsumer.consume(targets);
}
@Override
public void computeUsages(List<PsiElement> targets) {
PsiElement parent = myTarget.getParent();
if (parent instanceof PsiContinueStatement) {
PsiStatement statement = ((PsiContinueStatement)parent).findContinuedStatement();
if (statement instanceof PsiLoopStatement) {
highlightLoopDeclaration((PsiLoopStatement)statement);
}
}
else if (parent instanceof PsiBreakStatement) {
PsiStatement exitedStatement = ((PsiBreakStatement)parent).findExitedStatement();
if (exitedStatement instanceof PsiLoopStatement) {
highlightLoopDeclaration((PsiLoopStatement)exitedStatement);
}
else if (exitedStatement instanceof PsiSwitchStatement) {
addOccurrence(exitedStatement.getFirstChild());
}
}
addOccurrence(myTarget);
}
private void highlightLoopDeclaration(PsiLoopStatement statement) {
if (statement instanceof PsiDoWhileStatement) {
PsiKeyword whileKeyword = ((PsiDoWhileStatement)statement).getWhileKeyword();
if (whileKeyword != null) {
addOccurrence(whileKeyword);
}
}
else {
addOccurrence(statement.getFirstChild());
}
}
}
@@ -31,6 +31,9 @@ public class HighlightExitPointsHandlerFactory extends HighlightUsagesHandlerFac
if (PsiKeyword.RETURN.equals(target.getText()) || PsiKeyword.THROW.equals(target.getText())) {
return new HighlightExitPointsHandler(editor, file, target);
}
if (PsiKeyword.CONTINUE.equals(target.getText()) || PsiKeyword.BREAK.equals(target.getText())) {
return new HighlightBreakOutsHandler(editor, file, target);
}
}
return null;
}
@@ -0,0 +1,7 @@
class Main {
public static void main(String[] args) {
do {
if (true) br<caret>eak;
} while (args != null);
}
}
@@ -0,0 +1,7 @@
class Main {
public static void main(String[] args) {
switch (args[0]) {
case "A" : br<caret>eak;
}
}
}
@@ -98,6 +98,20 @@ class HighlightUsagesHandlerTest extends LightCodeInsightFixtureTestCase {
checkUnselect()
}
void testBreakInSwitch() {
configureFile()
ctrlShiftF7()
assertRangeText 'switch', 'break'
checkUnselect()
}
void testBreakInDoWhile() {
configureFile()
ctrlShiftF7()
assertRangeText 'break', 'while'
checkUnselect()
}
void testIDEADEV28822() {
myFixture.configureByText 'Foo.java', '''
public class Foo {