[ByteCodeViewer] IDEA-373600 Fix source<->bytecode matching being off by one when debug info is shown

GitOrigin-RevId: 57d6587afdbb97d966cf2c5460ccbc7f793fd0e4
This commit is contained in:
Bartek Pacia
2025-06-25 08:44:04 +00:00
committed by intellij-monorepo-bot
parent ca5906a4f7
commit 73af7f89bd
2 changed files with 14 additions and 10 deletions
@@ -36,7 +36,7 @@ internal fun removeDebugInfo(bytecodeWithDebugInfo: String): String = bytecodeWi
* @return A pair where the first element is the start line number in the bytecode, and the second element is the end line number in the bytecode. Returns (0, 0) if no valid mapping
* is found.
*/
internal fun mapLines(bytecodeWithDebugInfo: String, sourceStartLine: Int, sourceEndLine: Int, showDebugInfo: Boolean = true): IntRange {
internal fun mapLines(bytecodeWithDebugInfo: String, sourceStartLine: Int, sourceEndLine: Int, showDebugInfo: Boolean): IntRange {
var sourceStartLine = sourceStartLine // editor selection is 0-indexed
var currentBytecodeLine = 0
var bytecodeStartLine = -1
@@ -97,6 +97,10 @@ internal fun mapLines(bytecodeWithDebugInfo: String, sourceStartLine: Int, sourc
bytecodeStartLine -= linesToSkipBeforeStartLine
bytecodeEndLine -= linesToSkipBeforeEndLine
}
else {
bytecodeStartLine -= 1
bytecodeEndLine -= 1
}
return if (bytecodeStartLine == -1 || bytecodeEndLine == -1) IntRange(0, 0) else IntRange(bytecodeStartLine, bytecodeEndLine)
}
@@ -122,7 +122,7 @@ class BytecodeLineMappingTest : BasePlatformTestCase() {
fixture = simple1,
showDebugInfo = true,
expectedBytecodeSelection = """
| LINENUMBER 5 L0
| L0
""".trimMargin("|"),
)
}
@@ -168,12 +168,12 @@ class BytecodeLineMappingTest : BasePlatformTestCase() {
doTest(
sourceWithSelection = source, fixture = simple1, showDebugInfo = true,
expectedBytecodeSelection = """
| L0
| LINENUMBER 5 L0
| GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
| LDC "hello world"
| INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/String;)V
| L1
| LINENUMBER 6 L1
""".trimMargin("|"),
)
}
@@ -219,6 +219,7 @@ class BytecodeLineMappingTest : BasePlatformTestCase() {
doTest(
sourceWithSelection = source, fixture = simple1, showDebugInfo = true,
expectedBytecodeSelection = """
| L0
| LINENUMBER 5 L0
| GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
| LDC "hello world"
@@ -227,7 +228,6 @@ class BytecodeLineMappingTest : BasePlatformTestCase() {
| LINENUMBER 6 L1
| RETURN
| L2
| LOCALVARIABLE args [Ljava/lang/String; L0 L2 0
""".trimMargin("|"),
)
}
@@ -270,10 +270,10 @@ class BytecodeLineMappingTest : BasePlatformTestCase() {
doTest(
sourceWithSelection = source, fixture = simple1, showDebugInfo = true,
expectedBytecodeSelection = """
| L1
| LINENUMBER 6 L1
| RETURN
| L2
| LOCALVARIABLE args [Ljava/lang/String; L0 L2 0
""".trimMargin("|"),
)
}
@@ -317,12 +317,12 @@ class BytecodeLineMappingTest : BasePlatformTestCase() {
doTest(
sourceWithSelection = source, fixture = simple1, showDebugInfo = true,
expectedBytecodeSelection = """
| L0
| LINENUMBER 3 L0
| ALOAD 0
| INVOKESPECIAL java/lang/Object.<init> ()V
| RETURN
| L1
| LOCALVARIABLE this Lsimple1/Main; L0 L1 0
""".trimMargin("|"),
)
}
@@ -376,11 +376,11 @@ class BytecodeLineMappingTest : BasePlatformTestCase() {
doTest(
sourceWithSelection = source, fixture = simple2, showDebugInfo = true,
expectedBytecodeSelection = """
| L0
| LINENUMBER 4 L0
| LDC "Charlie"
| PUTSTATIC simple2/Main.name : Ljava/lang/String;
| L1
| LINENUMBER 5 L1
""".trimMargin("|"),
)
}
@@ -439,6 +439,7 @@ class BytecodeLineMappingTest : BasePlatformTestCase() {
doTest(
sourceWithSelection = source, fixture = simple2, showDebugInfo = true,
expectedBytecodeSelection = """
| L0
| LINENUMBER 4 L0
| LDC "Charlie"
| PUTSTATIC simple2/Main.name : Ljava/lang/String;
@@ -449,7 +450,6 @@ class BytecodeLineMappingTest : BasePlatformTestCase() {
| INVOKESPECIAL java/lang/Object.<init> ()V
| PUTSTATIC simple2/Main.obj : Ljava/lang/Object;
| RETURN
| MAXSTACK = 2
""".trimMargin("|"),
)
}
@@ -508,6 +508,7 @@ class BytecodeLineMappingTest : BasePlatformTestCase() {
doTest(
sourceWithSelection = source, fixture = simple2, showDebugInfo = true,
expectedBytecodeSelection = """
| L0
| LINENUMBER 8 L0
| GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
| LDC "hello world"
@@ -518,7 +519,6 @@ class BytecodeLineMappingTest : BasePlatformTestCase() {
| ARRAYLENGTH
| ISTORE 1
| L2
| LINENUMBER 10 L2
""".trimMargin("|"),
)
}
@@ -622,6 +622,7 @@ class BytecodeLineMappingTest : BasePlatformTestCase() {
doTest(
sourceWithSelection = source, fixture = simple3, showDebugInfo = true,
expectedBytecodeSelection = """
| L0
| LINENUMBER 12 L0
| ILOAD 1
| GETSTATIC java/lang/Boolean.TRUE : Ljava/lang/Boolean;
@@ -632,7 +633,6 @@ class BytecodeLineMappingTest : BasePlatformTestCase() {
| LDC "bar"
| ARETURN
| L1
| LINENUMBER 15 L1
""".trimMargin("|"),
)
}