|
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | | - |
17 | 16 | package com.jetbrains.python.impl.codeInsight.highlighting; |
18 | 17 |
|
19 | 18 | import com.jetbrains.python.impl.codeInsight.controlflow.ControlFlowCache; |
20 | 19 | import com.jetbrains.python.psi.*; |
21 | 20 | import com.jetbrains.python.psi.impl.PyPsiUtils; |
| 21 | +import consulo.annotation.access.RequiredReadAction; |
22 | 22 | import consulo.codeEditor.Editor; |
23 | 23 | import consulo.externalService.statistic.FeatureUsageTracker; |
24 | 24 | import consulo.ide.impl.idea.codeInsight.highlighting.HighlightUsagesHandler; |
25 | 25 | import consulo.language.controlFlow.ControlFlow; |
26 | 26 | import consulo.language.controlFlow.Instruction; |
27 | | -import consulo.language.editor.CodeInsightBundle; |
28 | 27 | import consulo.language.editor.highlight.usage.HighlightUsagesHandlerBase; |
| 28 | +import consulo.language.editor.localize.CodeInsightLocalize; |
29 | 29 | import consulo.language.editor.util.ProductivityFeatureNames; |
30 | 30 | import consulo.language.psi.PsiElement; |
31 | 31 | import consulo.language.psi.PsiFile; |
|
42 | 42 | * @author oleg |
43 | 43 | */ |
44 | 44 | public class PyHighlightExitPointsHandler extends HighlightUsagesHandlerBase<PsiElement> { |
45 | | - private final PsiElement myTarget; |
46 | | - |
47 | | - public PyHighlightExitPointsHandler(Editor editor, PsiFile file, PsiElement target) { |
48 | | - super(editor, file); |
49 | | - myTarget = target; |
50 | | - } |
51 | | - |
52 | | - public List<PsiElement> getTargets() { |
53 | | - return Collections.singletonList(myTarget); |
54 | | - } |
| 45 | + private final PsiElement myTarget; |
55 | 46 |
|
56 | | - protected void selectTargets(List<PsiElement> targets, Consumer<List<PsiElement>> selectionConsumer) { |
57 | | - selectionConsumer.accept(targets); |
58 | | - } |
59 | | - |
60 | | - public void computeUsages(List<PsiElement> targets) { |
61 | | - FeatureUsageTracker.getInstance().triggerFeatureUsed(ProductivityFeatureNames.CODEASSISTS_HIGHLIGHT_RETURN); |
| 47 | + public PyHighlightExitPointsHandler(Editor editor, PsiFile file, PsiElement target) { |
| 48 | + super(editor, file); |
| 49 | + myTarget = target; |
| 50 | + } |
62 | 51 |
|
63 | | - PsiElement parent = myTarget.getParent(); |
64 | | - if (!(parent instanceof PyReturnStatement)) { |
65 | | - return; |
| 52 | + @Override |
| 53 | + @RequiredReadAction |
| 54 | + public List<PsiElement> getTargets() { |
| 55 | + return Collections.singletonList(myTarget); |
66 | 56 | } |
67 | 57 |
|
68 | | - PyFunction function = PsiTreeUtil.getParentOfType(myTarget, PyFunction.class); |
69 | | - if (function == null) { |
70 | | - return; |
| 58 | + @Override |
| 59 | + protected void selectTargets(List<PsiElement> targets, Consumer<List<PsiElement>> selectionConsumer) { |
| 60 | + selectionConsumer.accept(targets); |
71 | 61 | } |
72 | 62 |
|
73 | | - highlightExitPoints((PyReturnStatement)parent, function); |
74 | | - } |
| 63 | + @Override |
| 64 | + @RequiredReadAction |
| 65 | + public void computeUsages(List<PsiElement> targets) { |
| 66 | + FeatureUsageTracker.getInstance().triggerFeatureUsed(ProductivityFeatureNames.CODEASSISTS_HIGHLIGHT_RETURN); |
75 | 67 |
|
76 | | - @Nullable |
77 | | - private static PsiElement getExitTarget(PsiElement exitStatement) { |
78 | | - if (exitStatement instanceof PyReturnStatement) { |
79 | | - return PsiTreeUtil.getParentOfType(exitStatement, PyFunction.class); |
80 | | - } |
81 | | - else if (exitStatement instanceof PyBreakStatement) { |
82 | | - return ((PyBreakStatement)exitStatement).getLoopStatement(); |
83 | | - } |
84 | | - else if (exitStatement instanceof PyContinueStatement) { |
85 | | - return ((PyContinueStatement)exitStatement).getLoopStatement(); |
86 | | - } |
87 | | - else if (exitStatement instanceof PyRaiseStatement) { |
88 | | - // TODO[oleg]: Implement better logic here! |
89 | | - return null; |
| 68 | + PsiElement parent = myTarget.getParent(); |
| 69 | + if (!(parent instanceof PyReturnStatement)) { |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + PyFunction function = PsiTreeUtil.getParentOfType(myTarget, PyFunction.class); |
| 74 | + if (function == null) { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + highlightExitPoints((PyReturnStatement) parent, function); |
90 | 79 | } |
91 | | - return null; |
92 | | - } |
93 | 80 |
|
94 | | - private void highlightExitPoints(PyReturnStatement statement, |
95 | | - PyFunction function) { |
96 | | - ControlFlow flow = ControlFlowCache.getControlFlow(function); |
97 | | - Collection<PsiElement> exitStatements = findExitPointsAndStatements(flow); |
98 | | - if (!exitStatements.contains(statement)) { |
99 | | - return; |
| 81 | + @Nullable |
| 82 | + private static PsiElement getExitTarget(PsiElement exitStatement) { |
| 83 | + if (exitStatement instanceof PyReturnStatement returnStmt) { |
| 84 | + return PsiTreeUtil.getParentOfType(returnStmt, PyFunction.class); |
| 85 | + } |
| 86 | + else if (exitStatement instanceof PyBreakStatement breakStmt) { |
| 87 | + return breakStmt.getLoopStatement(); |
| 88 | + } |
| 89 | + else if (exitStatement instanceof PyContinueStatement continueStmt) { |
| 90 | + return continueStmt.getLoopStatement(); |
| 91 | + } |
| 92 | + else if (exitStatement instanceof PyRaiseStatement) { |
| 93 | + // TODO[oleg]: Implement better logic here! |
| 94 | + return null; |
| 95 | + } |
| 96 | + return null; |
100 | 97 | } |
101 | 98 |
|
102 | | - PsiElement originalTarget = getExitTarget(statement); |
103 | | - for (PsiElement exitStatement : exitStatements) { |
104 | | - if (getExitTarget(exitStatement) == originalTarget) { |
105 | | - addOccurrence(exitStatement); |
106 | | - } |
| 99 | + @RequiredReadAction |
| 100 | + private void highlightExitPoints(PyReturnStatement statement, PyFunction function) { |
| 101 | + ControlFlow flow = ControlFlowCache.getControlFlow(function); |
| 102 | + Collection<PsiElement> exitStatements = findExitPointsAndStatements(flow); |
| 103 | + if (!exitStatements.contains(statement)) { |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + PsiElement originalTarget = getExitTarget(statement); |
| 108 | + for (PsiElement exitStatement : exitStatements) { |
| 109 | + if (getExitTarget(exitStatement) == originalTarget) { |
| 110 | + addOccurrence(exitStatement); |
| 111 | + } |
| 112 | + } |
| 113 | + myStatusText = CodeInsightLocalize.statusBarExitPointsHighlightedMessage( |
| 114 | + exitStatements.size(), |
| 115 | + HighlightUsagesHandler.getShortcutText() |
| 116 | + ); |
107 | 117 | } |
108 | | - myStatusText = CodeInsightBundle.message("status.bar.exit.points.highlighted.message", |
109 | | - exitStatements.size(), |
110 | | - HighlightUsagesHandler.getShortcutText()); |
111 | | - } |
112 | 118 |
|
113 | | - private static Collection<PsiElement> findExitPointsAndStatements(ControlFlow flow) { |
114 | | - List<PsiElement> statements = new ArrayList<PsiElement>(); |
115 | | - Instruction[] instructions = flow.getInstructions(); |
116 | | - for (Instruction instruction : instructions[instructions.length - 1].allPred()){ |
117 | | - PsiElement element = instruction.getElement(); |
118 | | - if (element == null){ |
119 | | - continue; |
120 | | - } |
121 | | - PsiElement statement = PyPsiUtils.getStatement(element); |
122 | | - if (statement != null){ |
123 | | - statements.add(statement); |
124 | | - } |
| 119 | + private static Collection<PsiElement> findExitPointsAndStatements(ControlFlow flow) { |
| 120 | + List<PsiElement> statements = new ArrayList<>(); |
| 121 | + Instruction[] instructions = flow.getInstructions(); |
| 122 | + for (Instruction instruction : instructions[instructions.length - 1].allPred()) { |
| 123 | + PsiElement element = instruction.getElement(); |
| 124 | + if (element == null) { |
| 125 | + continue; |
| 126 | + } |
| 127 | + PsiElement statement = PyPsiUtils.getStatement(element); |
| 128 | + if (statement != null) { |
| 129 | + statements.add(statement); |
| 130 | + } |
| 131 | + } |
| 132 | + return statements; |
125 | 133 | } |
126 | | - return statements; |
127 | | - } |
128 | 134 | } |
0 commit comments