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