|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2026 Red Hat, Inc. |
| 3 | + * Distributed under license by Red Hat, Inc. All rights reserved. |
| 4 | + * This program is made available under the terms of the |
| 5 | + * Eclipse Public License v2.0 which accompanies this distribution, |
| 6 | + * and is available at https://www.eclipse.org/legal/epl-v20.html |
| 7 | + * |
| 8 | + * Contributors: |
| 9 | + * Red Hat, Inc. - initial API and implementation |
| 10 | + ******************************************************************************/ |
| 11 | +package com.redhat.devtools.lsp4ij.dap.stepping; |
| 12 | + |
| 13 | +import com.intellij.openapi.editor.Document; |
| 14 | +import com.intellij.openapi.progress.ProcessCanceledException; |
| 15 | +import com.intellij.xdebugger.XDebugSession; |
| 16 | +import com.intellij.xdebugger.XSourcePosition; |
| 17 | +import com.intellij.xdebugger.frame.XSuspendContext; |
| 18 | +import com.intellij.xdebugger.stepping.XSmartStepIntoHandler; |
| 19 | +import com.redhat.devtools.lsp4ij.LSPIJUtils; |
| 20 | +import com.redhat.devtools.lsp4ij.dap.client.DAPClient; |
| 21 | +import com.redhat.devtools.lsp4ij.dap.client.DAPStackFrame; |
| 22 | +import com.redhat.devtools.lsp4ij.dap.client.DAPSuspendContext; |
| 23 | +import com.redhat.devtools.lsp4ij.internal.CompletableFutures; |
| 24 | +import org.eclipse.lsp4j.debug.StepInTarget; |
| 25 | +import org.eclipse.lsp4j.debug.StepInTargetsResponse; |
| 26 | +import org.jetbrains.annotations.NotNull; |
| 27 | +import org.jetbrains.annotations.Nullable; |
| 28 | +import org.jetbrains.concurrency.AsyncPromise; |
| 29 | +import org.jetbrains.concurrency.Promise; |
| 30 | +import org.slf4j.Logger; |
| 31 | +import org.slf4j.LoggerFactory; |
| 32 | + |
| 33 | +import java.util.ArrayList; |
| 34 | +import java.util.Collections; |
| 35 | +import java.util.List; |
| 36 | +import java.util.Map; |
| 37 | +import java.util.concurrent.CompletableFuture; |
| 38 | +import java.util.concurrent.ExecutionException; |
| 39 | + |
| 40 | +/** |
| 41 | + * Handles "Smart Step Into" functionality for DAP debug sessions. |
| 42 | + * When multiple function calls exist on a single line, this handler allows the user |
| 43 | + * to choose which function to step into via the DAP stepInTargets request. |
| 44 | + */ |
| 45 | +public class DAPSmartStepIntoHandler extends XSmartStepIntoHandler<DAPStepIntoVariant> { |
| 46 | + |
| 47 | + private static final Logger LOGGER = LoggerFactory.getLogger(DAPSmartStepIntoHandler.class); |
| 48 | + |
| 49 | + private final @NotNull XDebugSession session; |
| 50 | + |
| 51 | + /** |
| 52 | + * Creates a new Smart Step Into handler. |
| 53 | + * |
| 54 | + * @param session the debug session |
| 55 | + */ |
| 56 | + public DAPSmartStepIntoHandler(@NotNull XDebugSession session) { |
| 57 | + this.session = session; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public @NotNull List<DAPStepIntoVariant> computeSmartStepVariants(@NotNull XSourcePosition position) { |
| 62 | + CompletableFuture<List<DAPStepIntoVariant>> future = getStepInTargetsFuture(position); |
| 63 | + |
| 64 | + try { |
| 65 | + CompletableFutures.waitUntilDone(future, (com.intellij.psi.PsiFile) null, 5000); |
| 66 | + } catch (ProcessCanceledException e) { |
| 67 | + throw e; |
| 68 | + } catch (ExecutionException e) { |
| 69 | + LOGGER.warn("Error getting smart step variants", e); |
| 70 | + return Collections.emptyList(); |
| 71 | + } catch (java.util.concurrent.TimeoutException e) { |
| 72 | + return Collections.emptyList(); |
| 73 | + } |
| 74 | + |
| 75 | + List<DAPStepIntoVariant> result = future.getNow(null); |
| 76 | + return result != null ? result : Collections.emptyList(); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Helper method to get the CompletableFuture for step-in targets. |
| 81 | + * Extracted to avoid code duplication between sync and async versions. |
| 82 | + */ |
| 83 | + private CompletableFuture<List<DAPStepIntoVariant>> getStepInTargetsFuture(@NotNull XSourcePosition position) { |
| 84 | + XSuspendContext suspendContext = session.getSuspendContext(); |
| 85 | + if (!(suspendContext instanceof DAPSuspendContext dapContext)) { |
| 86 | + return CompletableFuture.completedFuture(Collections.emptyList()); |
| 87 | + } |
| 88 | + |
| 89 | + var activeStack = dapContext.getActiveExecutionStack(); |
| 90 | + if (activeStack == null) { |
| 91 | + return CompletableFuture.completedFuture(Collections.emptyList()); |
| 92 | + } |
| 93 | + |
| 94 | + var topFrame = activeStack.getTopFrame(); |
| 95 | + if (!(topFrame instanceof DAPStackFrame dapFrame)) { |
| 96 | + return CompletableFuture.completedFuture(Collections.emptyList()); |
| 97 | + } |
| 98 | + |
| 99 | + DAPClient client = dapFrame.getClient(); |
| 100 | + |
| 101 | + // Check capability |
| 102 | + if (!client.isSupportsStepInTargetsRequest()) { |
| 103 | + return CompletableFuture.completedFuture(Collections.emptyList()); |
| 104 | + } |
| 105 | + |
| 106 | + int frameId = dapFrame.getFrameId(); |
| 107 | + CompletableFuture<StepInTargetsResponse> responseFuture = client.stepInTargets(frameId); |
| 108 | + |
| 109 | + // Transform the response into variants |
| 110 | + Document document = LSPIJUtils.getDocument(position.getFile()); |
| 111 | + int zeroBasedLine = position.getLine(); |
| 112 | + |
| 113 | + return responseFuture.handle((response, throwable) -> { |
| 114 | + if (throwable != null) { |
| 115 | + LOGGER.error("Error while fetching step-in targets from DAP server", throwable); |
| 116 | + return Collections.emptyList(); |
| 117 | + } |
| 118 | + |
| 119 | + if (response == null || response.getTargets() == null || response.getTargets().length == 0) { |
| 120 | + return Collections.emptyList(); |
| 121 | + } |
| 122 | + |
| 123 | + List<DAPStepIntoVariant> variants = new ArrayList<>(); |
| 124 | + |
| 125 | + // Track how many times we've seen each function name to handle duplicates |
| 126 | + Map<String, Integer> functionNameCounts = new java.util.HashMap<>(); |
| 127 | + |
| 128 | + for (StepInTarget target : response.getTargets()) { |
| 129 | + // Extract function name from label to count occurrences |
| 130 | + String label = target.getLabel(); |
| 131 | + String functionName = label; |
| 132 | + int parenIndex = label.indexOf('('); |
| 133 | + if (parenIndex > 0) { |
| 134 | + functionName = label.substring(0, parenIndex); |
| 135 | + } |
| 136 | + |
| 137 | + // Get the occurrence index for this function name |
| 138 | + int occurrenceIndex = functionNameCounts.getOrDefault(functionName, 0); |
| 139 | + functionNameCounts.put(functionName, occurrenceIndex + 1); |
| 140 | + |
| 141 | + DAPStepIntoVariant variant = new DAPStepIntoVariant(target, document, zeroBasedLine, occurrenceIndex); |
| 142 | + variants.add(variant); |
| 143 | + } |
| 144 | + return variants; |
| 145 | + }); |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public @NotNull Promise<List<DAPStepIntoVariant>> computeSmartStepVariantsAsync( |
| 150 | + @NotNull XSourcePosition position) { |
| 151 | + |
| 152 | + // Get the CompletableFuture and wrap it in an AsyncPromise |
| 153 | + CompletableFuture<List<DAPStepIntoVariant>> future = getStepInTargetsFuture(position); |
| 154 | + |
| 155 | + AsyncPromise<List<DAPStepIntoVariant>> promise = new AsyncPromise<>(); |
| 156 | + future.whenComplete((result, throwable) -> { |
| 157 | + if (throwable != null) { |
| 158 | + promise.setResult(Collections.emptyList()); |
| 159 | + } else { |
| 160 | + promise.setResult(result != null ? result : Collections.emptyList()); |
| 161 | + } |
| 162 | + }); |
| 163 | + |
| 164 | + return promise; |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public void startStepInto(@NotNull DAPStepIntoVariant variant, @Nullable XSuspendContext context) { |
| 169 | + if (!(context instanceof DAPSuspendContext dapContext)) { |
| 170 | + return; |
| 171 | + } |
| 172 | + |
| 173 | + Integer threadId = dapContext.getThreadId(); |
| 174 | + if (threadId == null) { |
| 175 | + return; |
| 176 | + } |
| 177 | + |
| 178 | + var activeStack = dapContext.getActiveExecutionStack(); |
| 179 | + if (activeStack == null) { |
| 180 | + return; |
| 181 | + } |
| 182 | + |
| 183 | + var topFrame = activeStack.getTopFrame(); |
| 184 | + if (!(topFrame instanceof DAPStackFrame dapFrame)) { |
| 185 | + return; |
| 186 | + } |
| 187 | + |
| 188 | + DAPClient client = dapFrame.getClient(); |
| 189 | + |
| 190 | + // Get stepping granularity (null for normal stepping, INSTRUCTION for disassembly) |
| 191 | + var granularity = getSteppingGranularity(client); |
| 192 | + |
| 193 | + // Execute stepIn with targetId |
| 194 | + Integer targetId = variant.getTargetId(); |
| 195 | + client.stepIn(threadId, targetId, granularity); |
| 196 | + } |
| 197 | + |
| 198 | + @Override |
| 199 | + public void stepIntoEmpty(XDebugSession session) { |
| 200 | + // Fallback to regular step into when no variants available |
| 201 | + session.stepInto(); |
| 202 | + } |
| 203 | + |
| 204 | + @Override |
| 205 | + public @Nullable String getPopupTitle(@NotNull XSourcePosition position) { |
| 206 | + return "Choose Method to Step Into"; |
| 207 | + } |
| 208 | + |
| 209 | + /** |
| 210 | + * Gets the stepping granularity for the current session. |
| 211 | + * Returns INSTRUCTION granularity when disassembly mode is active, null otherwise. |
| 212 | + * |
| 213 | + * @param client the DAP client |
| 214 | + * @return the stepping granularity, or null for default (source line) granularity |
| 215 | + */ |
| 216 | + private @Nullable org.eclipse.lsp4j.debug.SteppingGranularity getSteppingGranularity(@NotNull DAPClient client) { |
| 217 | + // Note: For Smart Step Into, we typically use source-level granularity |
| 218 | + // Disassembly-level stepping with target selection is rarely needed |
| 219 | + // If needed in the future, this could access DAPDebugProcess.getAlternativeSourceHandler() |
| 220 | + return null; |
| 221 | + } |
| 222 | +} |
0 commit comments