Skip to content

Commit 9df0000

Browse files
DanTupCommit Queue
authored andcommitted
[analysis_server] Exclude blocks/switch cases from Inline Values unless execution is within them
This prevents inline values showing up alongside code that didn't execute in conditional blocks. It does also mean they don't know up in conditional blocks that _did_ execute one execution has fallen out of them, but I believe this is the best we can do with the current LSP APIs (unless we try to track execution across requests, but that would probably be complete and not entirely reliable). Fixes Dart-Code/Dart-Code#5454 Change-Id: Ia1cf2437b76844ff0a6aeeb958d769c9523dc5dd Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/433200 Commit-Queue: Keerti Parthasarathy <[email protected]> Reviewed-by: Keerti Parthasarathy <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 60d4180 commit 9df0000

File tree

2 files changed

+83
-2
lines changed

2 files changed

+83
-2
lines changed

pkg/analysis_server/lib/src/lsp/handlers/handler_inline_value.dart

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ class InlineValueHandler
9292
server.lspClientConfiguration,
9393
collector,
9494
function,
95+
stoppedOffset,
9596
);
9697
function.accept(visitor);
9798

@@ -139,7 +140,7 @@ class _InlineValueCollector {
139140
final Range rangeAlreadyExecuted;
140141

141142
/// A [LineInfo] used to convert offsets to lines/columns for comparing to
142-
/// [applicableRange].
143+
/// locations provided by the client.
143144
final LineInfo lineInfo;
144145

145146
_InlineValueCollector(
@@ -263,11 +264,32 @@ class _InlineValueVisitor extends GeneralizingAstVisitor<void> {
263264
final _InlineValueCollector collector;
264265
final AstNode rootNode;
265266

266-
_InlineValueVisitor(this.clientConfiguration, this.collector, this.rootNode);
267+
/// The offset where execution currently is.
268+
///
269+
/// This is used to determine which block of code we're inside, so we can
270+
/// avoid showing inline values in other branches.
271+
final int currentExecutionOffset;
272+
273+
_InlineValueVisitor(
274+
this.clientConfiguration,
275+
this.collector,
276+
this.rootNode,
277+
this.currentExecutionOffset,
278+
);
267279

268280
bool get experimentalInlineValuesProperties =>
269281
clientConfiguration.global.experimentalInlineValuesProperties;
270282

283+
@override
284+
void visitBlock(Block node) {
285+
if (currentExecutionOffset < node.offset ||
286+
currentExecutionOffset > node.end) {
287+
return;
288+
}
289+
290+
super.visitBlock(node);
291+
}
292+
271293
@override
272294
void visitFormalParameter(FormalParameter node) {
273295
var name = node.name;
@@ -359,6 +381,16 @@ class _InlineValueVisitor extends GeneralizingAstVisitor<void> {
359381
super.visitSimpleIdentifier(node);
360382
}
361383

384+
@override
385+
void visitSwitchPatternCase(SwitchPatternCase node) {
386+
if (currentExecutionOffset < node.offset ||
387+
currentExecutionOffset > (node.statements.endToken?.end ?? node.end)) {
388+
return;
389+
}
390+
391+
super.visitSwitchPatternCase(node);
392+
}
393+
362394
@override
363395
void visitVariableDeclaration(VariableDeclaration node) {
364396
var name = node.name;

pkg/analysis_server/test/lsp/inline_value_test.dart

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,55 @@ class InlineValueTest extends AbstractLspAnalysisServerTest {
2525
/// client configuration passed during initialization.
2626
bool experimentalInlineValuesProperties = false;
2727

28+
Future<void> test_block_ifStatement_inside() async {
29+
code = TestCode.parse(r'''
30+
void f(int a, int b, int c) {
31+
if (a == 1) {
32+
/*[0*/a/*0]*/;
33+
/*[1*/b/*1]*/;
34+
/*[2*/c/*2]*/;
35+
^
36+
}
37+
}
38+
''');
39+
40+
await verify_values(code, ofType: InlineValueVariableLookup);
41+
}
42+
43+
Future<void> test_block_ifStatement_notInside() async {
44+
code = TestCode.parse(r'''
45+
void f(int a, int /*[0*/b/*0]*/, int c) {
46+
if (/*[1*/a/*1]*/ == 1) {
47+
// Code inside blocks is excluded
48+
a;
49+
b;
50+
c;
51+
}
52+
^/*[2*/c/*2]*/;
53+
}
54+
''');
55+
56+
await verify_values(code, ofType: InlineValueVariableLookup);
57+
}
58+
59+
Future<void> test_block_switchStatement() async {
60+
code = TestCode.parse(r'''
61+
void f(int a, int /*[0*/b/*0]*/, int c) {
62+
switch (/*[1*/a/*1]*/) {
63+
case 0:
64+
// Ignored because not in this case
65+
a;
66+
b;
67+
c;
68+
case 1:
69+
^/*[2*/c/*2]*/;
70+
}
71+
}
72+
''');
73+
74+
await verify_values(code, ofType: InlineValueVariableLookup);
75+
}
76+
2877
Future<void> test_iterables() async {
2978
experimentalInlineValuesProperties = true;
3079

0 commit comments

Comments
 (0)