Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
feat: jump to result (#58)
Browse files Browse the repository at this point in the history
Co-authored-by: Maurice Van Wassenhove <mauricevanwassenhove@gmail.com>
  • Loading branch information
jitsedesmet and Mouwrice authored Jul 27, 2023
1 parent 9225426 commit dd89560
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/jvmMain/kotlin/ui/codeview/CodeViewer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ fun CodeViewer(viewModel: DebuggerViewModel) {
}
}

InstructionViewer(instruction, maxOffsetLength, color, inCatch)
InstructionViewer(viewModel, instruction, maxOffsetLength, color, inCatch)
}

// There is an error to display at the current instruction
Expand Down
49 changes: 47 additions & 2 deletions src/jvmMain/kotlin/ui/codeview/InstructionViewer.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
package ui.codeview

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.onClick
import androidx.compose.material3.Divider
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
Expand All @@ -18,16 +26,24 @@ import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import data.InstructionRecord
import ui.Colors
import viewmodel.DebuggerViewModel
import viewmodel.Display

/**
* Display the current instruction. Highlight it if it is the current one.
*/
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun InstructionViewer(instruction: InstructionRecord, maxOffsetLength: Int, color: Color, inCatch: Boolean) {
fun InstructionViewer(viewModel: DebuggerViewModel, instruction: InstructionRecord, maxOffsetLength: Int, color: Color, inCatch: Boolean) {
val dividerColor = if (inCatch) Colors.Red.value else MaterialTheme.colorScheme.onSurfaceVariant
var expandedSelection by remember {
mutableStateOf(false)
}

Row(
Modifier.fillMaxWidth().background(color),
Modifier.fillMaxWidth().background(color).onClick {
expandedSelection = true
},
verticalAlignment = Alignment.CenterVertically,
) {
// Pad with whitespaces to align the offsets
Expand All @@ -52,5 +68,34 @@ fun InstructionViewer(instruction: InstructionRecord, maxOffsetLength: Int, colo
fontFamily = FontFamily.Monospace,
modifier = Modifier.padding(start = 16.dp),
)

viewModel.codeAttribute?.let { codeAttribute ->
DropdownMenu(expandedSelection, { expandedSelection = false }) {
DropdownMenuItem({ Text("Result") }, onClick = {
expandedSelection = false
val instructionIndex = codeAttribute.instructions.indexOf(instruction)
viewModel.display = Display.RESULTS
viewModel.instructionIndex = instructionIndex
})

codeAttribute.blockEvaluations.withIndex().flatMap { blockIndex ->
// Filter: match instruction
blockIndex.value.evaluations.filter { evaluationIndex ->
evaluationIndex.instructionOffset == instruction.offset
}.withIndex().map { evaluationIndex ->
// Pair of <Block index; evaluation index> with a pair containing the evaluation
Pair(Pair(blockIndex.index, evaluationIndex.index), evaluationIndex.value)
}
}.forEach() {
val evalCount = it.second.evaluationCount + 1
DropdownMenuItem({ Text(evalCount.toString()) }, onClick = {
expandedSelection = false
viewModel.display = Display.EVALUATIONS
viewModel.updateEvaluationBlockIndex(it.first.first)
viewModel.evaluationIndex = it.first.second
})
}
}
}
}
}

0 comments on commit dd89560

Please sign in to comment.