|
| 1 | +// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:analysis_server/lsp_protocol/protocol.dart' hide MessageType; |
| 6 | +import 'package:analysis_server/lsp_protocol/protocol.dart'; |
| 7 | +import 'package:analysis_server/src/lsp/error_or.dart'; |
| 8 | +import 'package:analysis_server/src/lsp/extensions/positions.dart'; |
| 9 | +import 'package:analysis_server/src/lsp/handlers/handlers.dart'; |
| 10 | +import 'package:analysis_server/src/lsp/mapping.dart'; |
| 11 | +import 'package:analysis_server/src/lsp/registration/feature_registration.dart'; |
| 12 | +import 'package:analyzer/dart/ast/ast.dart'; |
| 13 | +import 'package:analyzer/dart/ast/visitor.dart'; |
| 14 | +import 'package:analyzer/dart/element/element2.dart'; |
| 15 | +import 'package:analyzer/source/line_info.dart'; |
| 16 | +import 'package:analyzer/src/dart/element/extensions.dart'; |
| 17 | + |
| 18 | +typedef StaticOptions = |
| 19 | + Either3<bool, InlineValueOptions, InlineValueRegistrationOptions>; |
| 20 | + |
| 21 | +class InlineValueHandler |
| 22 | + extends |
| 23 | + SharedMessageHandler<InlineValueParams, TextDocumentInlineValueResult> { |
| 24 | + InlineValueHandler(super.server); |
| 25 | + |
| 26 | + @override |
| 27 | + Method get handlesMessage => Method.textDocument_inlineValue; |
| 28 | + |
| 29 | + @override |
| 30 | + LspJsonHandler<InlineValueParams> get jsonHandler => |
| 31 | + InlineValueParams.jsonHandler; |
| 32 | + |
| 33 | + @override |
| 34 | + bool get requiresTrustedCaller => false; |
| 35 | + |
| 36 | + @override |
| 37 | + Future<ErrorOr<TextDocumentInlineValueResult>> handle( |
| 38 | + InlineValueParams params, |
| 39 | + MessageInfo message, |
| 40 | + CancellationToken token, |
| 41 | + ) async { |
| 42 | + if (!isDartDocument(params.textDocument)) { |
| 43 | + return success(null); |
| 44 | + } |
| 45 | + |
| 46 | + var filePath = pathOfDoc(params.textDocument); |
| 47 | + return filePath.mapResult((filePath) async { |
| 48 | + var unitResult = await server.getResolvedUnit(filePath); |
| 49 | + if (unitResult == null) { |
| 50 | + return success(null); |
| 51 | + } |
| 52 | + var lineInfo = unitResult.lineInfo; |
| 53 | + |
| 54 | + // We will provide values from the start of the visible range up to |
| 55 | + // the end of the line the debugger is stopped on (which will do by just |
| 56 | + // jumping to position 0 of the next line). |
| 57 | + var visibleRange = params.range; |
| 58 | + var stoppedLocation = params.context.stoppedLocation; |
| 59 | + var applicableRange = Range( |
| 60 | + start: visibleRange.start, |
| 61 | + end: Position(line: stoppedLocation.end.line + 1, character: 0), |
| 62 | + ); |
| 63 | + |
| 64 | + var stoppedOffset = toOffset(lineInfo, stoppedLocation.end); |
| 65 | + return stoppedOffset.mapResult((stoppedOffset) async { |
| 66 | + // Find the function that is executing. We will only show values for |
| 67 | + // this single function expression. |
| 68 | + var node = await server.getNodeAtOffset(filePath, stoppedOffset); |
| 69 | + var function = node?.thisOrAncestorOfType<FunctionExpression>(); |
| 70 | + if (function == null) { |
| 71 | + return success(null); |
| 72 | + } |
| 73 | + |
| 74 | + var collector = _InlineValueCollector(lineInfo, applicableRange); |
| 75 | + var visitor = _InlineValueVisitor(collector, function); |
| 76 | + function.accept(visitor); |
| 77 | + |
| 78 | + return success(collector.values.values.toList()); |
| 79 | + }); |
| 80 | + }); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +class InlineValueRegistrations extends FeatureRegistration |
| 85 | + with SingleDynamicRegistration, StaticRegistration<StaticOptions> { |
| 86 | + InlineValueRegistrations(super.info); |
| 87 | + |
| 88 | + @override |
| 89 | + ToJsonable? get options => |
| 90 | + TextDocumentRegistrationOptions(documentSelector: dartFiles); |
| 91 | + |
| 92 | + @override |
| 93 | + Method get registrationMethod => Method.textDocument_inlineValue; |
| 94 | + |
| 95 | + @override |
| 96 | + StaticOptions get staticOptions => Either3.t1(true); |
| 97 | + |
| 98 | + @override |
| 99 | + bool get supportsDynamic => clientDynamic.inlineValue; |
| 100 | +} |
| 101 | + |
| 102 | +/// Collects inline values, keeping only the most relevant where an element |
| 103 | +/// is recorded multiple times. |
| 104 | +class _InlineValueCollector { |
| 105 | + /// A map of elements and their inline value. |
| 106 | + final Map<Element2, InlineValue> values = {}; |
| 107 | + |
| 108 | + /// The range for which inline values should be retained. |
| 109 | + /// |
| 110 | + /// This should be approximately the range of the visible code on screen up to |
| 111 | + /// the point of execution. |
| 112 | + final Range applicableRange; |
| 113 | + |
| 114 | + /// A [LineInfo] used to convert offsets to lines/columns for comparing to |
| 115 | + /// [applicableRange]. |
| 116 | + final LineInfo lineInfo; |
| 117 | + |
| 118 | + _InlineValueCollector(this.lineInfo, this.applicableRange); |
| 119 | + |
| 120 | + /// Records a variable inline value for [element] with [offset]/[length]. |
| 121 | + /// |
| 122 | + /// Variable inline values are sent to the client without expressions/names |
| 123 | + /// because the client can infer the name from the range and look it up from |
| 124 | + /// the debuggers Scopes/Variables. |
| 125 | + void recordVariableLookup(Element2? element, int offset, int length) { |
| 126 | + if (element == null || element.isWildcardVariable) return; |
| 127 | + |
| 128 | + assert(offset >= 0); |
| 129 | + assert(length > 0); |
| 130 | + |
| 131 | + var range = toRange(lineInfo, offset, length); |
| 132 | + |
| 133 | + // Never record anything outside of the visible range. |
| 134 | + if (!range.intersects(applicableRange)) { |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + // We only want to show each variable once, so keep only the one furthest |
| 139 | + // into the source (closest to the execution pointer). |
| 140 | + var existingPosition = values[element]?.map( |
| 141 | + (expression) => expression.range.start, |
| 142 | + (text) => text.range.start, |
| 143 | + (variable) => variable.range.start, |
| 144 | + ); |
| 145 | + if (existingPosition != null && |
| 146 | + existingPosition.isAfterOrEqual(range.start)) { |
| 147 | + return; |
| 148 | + } |
| 149 | + |
| 150 | + values[element] = InlineValue.t3( |
| 151 | + InlineValueVariableLookup( |
| 152 | + caseSensitiveLookup: true, |
| 153 | + range: range, |
| 154 | + // We don't provide name, because it always matches the source code |
| 155 | + // for a variable and can be inferred. |
| 156 | + ), |
| 157 | + ); |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +/// Visits a function expression and reports nodes that should have inline |
| 162 | +/// values to [collector]. |
| 163 | +class _InlineValueVisitor extends GeneralizingAstVisitor<void> { |
| 164 | + final _InlineValueCollector collector; |
| 165 | + final FunctionExpression rootFunction; |
| 166 | + |
| 167 | + _InlineValueVisitor(this.collector, this.rootFunction); |
| 168 | + |
| 169 | + @override |
| 170 | + void visitFormalParameter(FormalParameter node) { |
| 171 | + var name = node.name; |
| 172 | + if (name != null) { |
| 173 | + collector.recordVariableLookup( |
| 174 | + node.declaredFragment?.element, |
| 175 | + name.offset, |
| 176 | + name.length, |
| 177 | + ); |
| 178 | + } |
| 179 | + super.visitFormalParameter(node); |
| 180 | + } |
| 181 | + |
| 182 | + @override |
| 183 | + void visitFunctionExpression(FunctionExpression node) { |
| 184 | + // Don't descend into nested functions. |
| 185 | + if (node != rootFunction) { |
| 186 | + return; |
| 187 | + } |
| 188 | + |
| 189 | + super.visitFunctionExpression(node); |
| 190 | + } |
| 191 | + |
| 192 | + @override |
| 193 | + void visitSimpleIdentifier(SimpleIdentifier node) { |
| 194 | + switch (node.element) { |
| 195 | + case LocalVariableElement2(name3: _?): |
| 196 | + case FormalParameterElement(): |
| 197 | + collector.recordVariableLookup(node.element, node.offset, node.length); |
| 198 | + } |
| 199 | + super.visitSimpleIdentifier(node); |
| 200 | + } |
| 201 | + |
| 202 | + @override |
| 203 | + void visitVariableDeclaration(VariableDeclaration node) { |
| 204 | + var name = node.name; |
| 205 | + collector.recordVariableLookup( |
| 206 | + node.declaredElement2, |
| 207 | + name.offset, |
| 208 | + name.length, |
| 209 | + ); |
| 210 | + super.visitVariableDeclaration(node); |
| 211 | + } |
| 212 | +} |
0 commit comments