[debugger] Refactor: introduce NO_LAMBDA=-1 constant to represent basic line breakpoint type

GitOrigin-RevId: d3457ef7340d7b72ad8d2a889c2ee90c8d15dc60
This commit is contained in:
Maksim Zuev
2023-12-06 18:03:12 +01:00
committed by intellij-monorepo-bot
parent f14990ea5e
commit 2b291a0613
5 changed files with 46 additions and 14 deletions

View File

@@ -141,7 +141,7 @@ public class JavaLineBreakpointType extends JavaLineBreakpointTypeBase<JavaLineB
boolean mainMethodAdded = false;
int lambdaCount = 0;
if (!(startMethod instanceof PsiLambdaExpression)) {
res.add(new LineJavaBreakpointVariant(position, startMethod, -1));
res.add(new LineJavaBreakpointVariant(position, startMethod, JavaLineBreakpointProperties.NO_LAMBDA));
mainMethodAdded = true;
}
@@ -264,9 +264,10 @@ public class JavaLineBreakpointType extends JavaLineBreakpointTypeBase<JavaLineB
if (position == null) return null;
JavaBreakpointProperties properties = breakpoint.getProperties();
if (properties instanceof JavaLineBreakpointProperties && !(breakpoint instanceof RunToCursorBreakpoint)) {
Integer ordinal = ((JavaLineBreakpointProperties)properties).getLambdaOrdinal();
if (ordinal != null && ordinal != -1) {
if (properties instanceof JavaLineBreakpointProperties javaProperties && !(breakpoint instanceof RunToCursorBreakpoint)) {
if (javaProperties.isInLambda()) {
Integer ordinal = javaProperties.getLambdaOrdinal();
assert ordinal != null;
List<PsiLambdaExpression> lambdas = DebuggerUtilsEx.collectLambdas(position, true);
if (ordinal < lambdas.size()) {
return lambdas.get(ordinal);
@@ -363,11 +364,11 @@ public class JavaLineBreakpointType extends JavaLineBreakpointTypeBase<JavaLineB
@Override
public TextRange getHighlightRange() {
if (myElement != null && myEncodedInlinePosition != -1) {
TextRange textRange = getTextRangeWithoutTrailingComments(myElement);
return DebuggerUtilsEx.getHighlightingRangeInsideLine(textRange, myElement.getContainingFile(), mySourcePosition.getLine());
if (myElement == null || JavaLineBreakpointProperties.isLinePosition(myEncodedInlinePosition)) {
return null;
}
return null;
TextRange textRange = getTextRangeWithoutTrailingComments(myElement);
return DebuggerUtilsEx.getHighlightingRangeInsideLine(textRange, myElement.getContainingFile(), mySourcePosition.getLine());
}
@NotNull
@@ -450,8 +451,7 @@ public class JavaLineBreakpointType extends JavaLineBreakpointTypeBase<JavaLineB
if (properties == null) return null;
boolean condRet = properties.isConditionalReturn();
Integer lambdaOrdinal = properties.getLambdaOrdinal();
boolean isLambda = lambdaOrdinal != null && lambdaOrdinal != -1;
boolean isLambda = properties.isInLambda();
if (!condRet && !isLambda) return null;
return ReadAction.compute(() -> {
@@ -462,6 +462,8 @@ public class JavaLineBreakpointType extends JavaLineBreakpointTypeBase<JavaLineB
return XSourcePositionImpl.createByElement(theReturn);
}
else if (isLambda) {
Integer lambdaOrdinal = properties.getLambdaOrdinal();
assert lambdaOrdinal != null;
return DebuggerUtilsEx.toXSourcePosition(new PositionManagerImpl.JavaSourcePosition(linePosition, lambdaOrdinal));
}
}

View File

@@ -17,12 +17,30 @@ public class JavaLineBreakpointProperties extends JavaBreakpointProperties<JavaL
private static final int COND_RET_CODE = -10;
/**
* Represents a position for breakpoint in the current method (not in lambda).
* @see #getLambdaOrdinal()
*/
public static final int NO_LAMBDA = -1;
/**
* Encoded inline position for the case when we want to stop on the first statement on the line.
* @see #encodeInlinePosition
*/
private static final int BASIC_LINE_POSITION = encodeInlinePosition(NO_LAMBDA, false);
public static int encodeInlinePosition(int lambdaOrdinal, boolean conditionalReturn) {
return !conditionalReturn
? lambdaOrdinal
: COND_RET_CODE - lambdaOrdinal - 1;
}
/**
* @return <code>null</code>, if it should suspend on all lambdas and basic line;<br>
* {@link #NO_LAMBDA}, if it should suspend only on the basic line;<br>
* positive value, if it should suspend inside the lambda with the ordinal
*/
@Transient
public @Nullable Integer getLambdaOrdinal() {
if (encodedInlinePosition == null) {
@@ -36,6 +54,18 @@ public class JavaLineBreakpointProperties extends JavaBreakpointProperties<JavaL
return encodedInlinePosition != null && encodedInlinePosition <= COND_RET_CODE;
}
/**
* @return true iff suspends on the basic line position (including 'all' variants)
*/
public static boolean isLinePosition(Integer encodedInlinePosition) {
return encodedInlinePosition == null || encodedInlinePosition == BASIC_LINE_POSITION;
}
public boolean isInLambda() {
Integer lambdaOrdinal = getLambdaOrdinal();
return lambdaOrdinal != null && lambdaOrdinal != NO_LAMBDA;
}
@OptionTag("lambda-ordinal") // naming is a historic accident
public @Nullable Integer getEncodedInlinePosition() {
return encodedInlinePosition;

View File

@@ -430,8 +430,8 @@ public abstract class ExecutionWithDebuggerToolsTestCase extends ExecutionTestCa
}
case "ConditionalReturn" -> {
breakpoint = breakpointManager.addLineBreakpoint(document, commentLine + 1, p -> {
int lambdaOrdinal = -1; // Note that we don't support `return` inside of lambda in unit tests.
p.setEncodedInlinePosition(JavaLineBreakpointProperties.encodeInlinePosition(lambdaOrdinal, true));
// Note that we don't support `return` inside of lambda in unit tests.
p.setEncodedInlinePosition(JavaLineBreakpointProperties.encodeInlinePosition(JavaLineBreakpointProperties.NO_LAMBDA, true));
});
if (breakpoint != null) {
systemPrintln("ConditionalReturnBreakpoint created at " + breakpointLocation);

View File

@@ -121,7 +121,7 @@ class KotlinLineBreakpointType :
val isLambdaResult = bodyExpression is KtLambdaExpression && bodyExpression.functionLiteral in lambdas
if (!isLambdaResult) {
result.add(LineKotlinBreakpointVariant(position, mainMethod, -1))
result.add(LineKotlinBreakpointVariant(position, mainMethod, JavaLineBreakpointProperties.NO_LAMBDA))
lineBreakpointAdded = true
}
}

View File

@@ -187,7 +187,7 @@ internal class BreakpointCreator(
condition: String?
) {
val kotlinLineBreakpointType = findBreakpointType(KotlinLineBreakpointType::class.java)
val updatedLambdaOrdinal = lambdaOrdinal?.let { if (it != -1) it - 1 else it }
val updatedLambdaOrdinal = lambdaOrdinal?.let { if (it != JavaLineBreakpointProperties.NO_LAMBDA) it - 1 else it }
val javaBreakpoint = createBreakpointOfType(
breakpointManager,