Skip to content

Commit ea4833f

Browse files
committed
Fixing compilation after platform update.
1 parent 9500596 commit ea4833f

4 files changed

Lines changed: 181 additions & 167 deletions

File tree

python-impl/src/main/java/com/jetbrains/python/impl/codeInsight/highlighting/PyHighlightExitPointsHandler.java

Lines changed: 77 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
1716
package com.jetbrains.python.impl.codeInsight.highlighting;
1817

1918
import com.jetbrains.python.impl.codeInsight.controlflow.ControlFlowCache;
2019
import com.jetbrains.python.psi.*;
2120
import com.jetbrains.python.psi.impl.PyPsiUtils;
21+
import consulo.annotation.access.RequiredReadAction;
2222
import consulo.codeEditor.Editor;
2323
import consulo.externalService.statistic.FeatureUsageTracker;
2424
import consulo.ide.impl.idea.codeInsight.highlighting.HighlightUsagesHandler;
2525
import consulo.language.controlFlow.ControlFlow;
2626
import consulo.language.controlFlow.Instruction;
27-
import consulo.language.editor.CodeInsightBundle;
2827
import consulo.language.editor.highlight.usage.HighlightUsagesHandlerBase;
28+
import consulo.language.editor.localize.CodeInsightLocalize;
2929
import consulo.language.editor.util.ProductivityFeatureNames;
3030
import consulo.language.psi.PsiElement;
3131
import consulo.language.psi.PsiFile;
@@ -42,87 +42,93 @@
4242
* @author oleg
4343
*/
4444
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;
5546

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+
}
6251

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);
6656
}
6757

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);
7161
}
7262

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);
7567

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);
9079
}
91-
return null;
92-
}
9380

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;
10097
}
10198

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+
);
107117
}
108-
myStatusText = CodeInsightBundle.message("status.bar.exit.points.highlighted.message",
109-
exitStatements.size(),
110-
HighlightUsagesHandler.getShortcutText());
111-
}
112118

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;
125133
}
126-
return statements;
127-
}
128134
}

python-impl/src/main/java/com/jetbrains/python/impl/codeInsight/liveTemplates/CollectionElementNameMacro.java

Lines changed: 58 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
1716
package com.jetbrains.python.impl.codeInsight.liveTemplates;
1817

1918
import consulo.annotation.component.ExtensionImpl;
@@ -24,6 +23,7 @@
2423
import consulo.language.editor.template.Result;
2524
import consulo.language.editor.template.TextResult;
2625
import consulo.language.editor.template.macro.Macro;
26+
import consulo.localize.LocalizeValue;
2727
import consulo.util.lang.StringUtil;
2828

2929
import java.util.ArrayList;
@@ -33,65 +33,69 @@
3333
* @author yole
3434
*/
3535
@ExtensionImpl
36-
public class CollectionElementNameMacro extends Macro
37-
{
38-
public String getName() {
39-
return "collectionElementName";
40-
}
41-
42-
public String getPresentableName() {
43-
return "collectionElementName()";
44-
}
45-
46-
public String getDefaultValue() {
47-
return "a";
48-
}
49-
50-
public Result calculateResult(Expression[] params, ExpressionContext context) {
51-
if (params.length != 1) {
52-
return null;
36+
public class CollectionElementNameMacro extends Macro {
37+
@Override
38+
public String getName() {
39+
return "collectionElementName";
5340
}
54-
Result paramResult = params[0].calculateResult(context);
55-
if (paramResult == null) {
56-
return null;
57-
}
58-
String param = paramResult.toString();
59-
int lastDot = param.lastIndexOf('.');
60-
if (lastDot >= 0) {
61-
param = param.substring(lastDot+1);
41+
42+
@Override
43+
public LocalizeValue getPresentableName() {
44+
return LocalizeValue.of("collectionElementName()");
6245
}
63-
if (param.endsWith(")")) {
64-
int lastParen = param.lastIndexOf('(');
65-
if (lastParen > 0) {
66-
param = param.substring(0, lastParen);
67-
}
46+
47+
@Override
48+
public String getDefaultValue() {
49+
return "a";
6850
}
69-
String result = smartUnpluralize(param);
70-
return new TextResult(result);
71-
}
7251

73-
private static String smartUnpluralize(String param) {
74-
if (param.endsWith("_list")) {
75-
return param.substring(0, param.length()-5);
52+
@Override
53+
public Result calculateResult(Expression[] params, ExpressionContext context) {
54+
if (params.length != 1) {
55+
return null;
56+
}
57+
Result paramResult = params[0].calculateResult(context);
58+
if (paramResult == null) {
59+
return null;
60+
}
61+
String param = paramResult.toString();
62+
int lastDot = param.lastIndexOf('.');
63+
if (lastDot >= 0) {
64+
param = param.substring(lastDot + 1);
65+
}
66+
if (param.endsWith(")")) {
67+
int lastParen = param.lastIndexOf('(');
68+
if (lastParen > 0) {
69+
param = param.substring(0, lastParen);
70+
}
71+
}
72+
String result = smartUnpluralize(param);
73+
return new TextResult(result);
7674
}
77-
String result = StringUtil.unpluralize(param);
78-
return result == null ? param : result;
79-
}
8075

81-
public LookupElement[] calculateLookupItems(Expression[] params, ExpressionContext context) {
82-
Result result = calculateResult(params, context);
83-
if (result == null) {
84-
return null;
76+
private static String smartUnpluralize(String param) {
77+
if (param.endsWith("_list")) {
78+
return param.substring(0, param.length() - 5);
79+
}
80+
String result = StringUtil.unpluralize(param);
81+
return result == null ? param : result;
8582
}
86-
String[] words = result.toString().split("_");
87-
if (words.length > 1) {
88-
List<LookupElement> lookup = new ArrayList<LookupElement>();
89-
for(int i=0; i<words.length; i++) {
90-
String element = StringUtil.join(words, i, words.length, "_");
91-
lookup.add(LookupElementBuilder.create(element));
92-
}
93-
return lookup.toArray(new LookupElement[lookup.size()]);
83+
84+
@Override
85+
public LookupElement[] calculateLookupItems(Expression[] params, ExpressionContext context) {
86+
Result result = calculateResult(params, context);
87+
if (result == null) {
88+
return null;
89+
}
90+
String[] words = result.toString().split("_");
91+
if (words.length > 1) {
92+
List<LookupElement> lookup = new ArrayList<>();
93+
for (int i = 0; i < words.length; i++) {
94+
String element = StringUtil.join(words, i, words.length, "_");
95+
lookup.add(LookupElementBuilder.create(element));
96+
}
97+
return lookup.toArray(new LookupElement[lookup.size()]);
98+
}
99+
return null;
94100
}
95-
return null;
96-
}
97101
}

python-impl/src/main/java/com/jetbrains/python/impl/codeInsight/liveTemplates/PyClassNameMacro.java

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
1716
package com.jetbrains.python.impl.codeInsight.liveTemplates;
1817

1918
import com.jetbrains.python.psi.PyClass;
19+
import consulo.annotation.access.RequiredReadAction;
2020
import consulo.annotation.component.ExtensionImpl;
2121
import consulo.language.editor.template.Expression;
2222
import consulo.language.editor.template.ExpressionContext;
@@ -27,37 +27,39 @@
2727
import consulo.language.psi.PsiElement;
2828
import consulo.language.psi.util.PsiTreeUtil;
2929

30+
import consulo.localize.LocalizeValue;
3031
import org.jspecify.annotations.Nullable;
3132

3233
/**
3334
* @author yole
3435
*/
3536
@ExtensionImpl
3637
public class PyClassNameMacro extends Macro {
37-
@Override
38-
public String getName() {
39-
return "pyClassName";
40-
}
38+
@Override
39+
public String getName() {
40+
return "pyClassName";
41+
}
4142

42-
@Override
43-
public String getPresentableName() {
44-
return "pyClassName()";
45-
}
43+
@Override
44+
public LocalizeValue getPresentableName() {
45+
return LocalizeValue.of("pyClassName()");
46+
}
4647

47-
@Nullable
48-
@Override
49-
public Result calculateResult(Expression[] params, ExpressionContext context) {
50-
PsiElement place = context.getPsiElementAtStartOffset();
51-
PyClass pyClass = PsiTreeUtil.getParentOfType(place, PyClass.class);
52-
if (pyClass == null) {
53-
return null;
48+
@Nullable
49+
@Override
50+
@RequiredReadAction
51+
public Result calculateResult(Expression[] params, ExpressionContext context) {
52+
PsiElement place = context.getPsiElementAtStartOffset();
53+
PyClass pyClass = PsiTreeUtil.getParentOfType(place, PyClass.class);
54+
if (pyClass == null) {
55+
return null;
56+
}
57+
String name = pyClass.getName();
58+
return name == null ? null : new TextResult(name);
5459
}
55-
String name = pyClass.getName();
56-
return name == null ? null : new TextResult(name);
57-
}
5860

59-
@Override
60-
public boolean isAcceptableInContext(TemplateContextType context) {
61-
return context instanceof PythonTemplateContextType;
62-
}
61+
@Override
62+
public boolean isAcceptableInContext(TemplateContextType context) {
63+
return context instanceof PythonTemplateContextType;
64+
}
6365
}

0 commit comments

Comments
 (0)