|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT license. |
| 3 | + |
| 4 | +import * as compareVersions from "compare-versions"; |
| 5 | +import { debug, InlineValue, InlineValueContext, InlineValueEvaluatableExpression, InlineValuesProvider, InlineValueText, InlineValueVariableLookup, |
| 6 | + Range, TextDocument, version } from "vscode"; |
| 7 | +import { instrumentOperation, instrumentOperationStep, sendInfo } from "vscode-extension-telemetry-wrapper"; |
| 8 | +import * as CodeConverter from "vscode-languageclient/lib/codeConverter"; |
| 9 | +import * as ProtocolConverter from "vscode-languageclient/lib/protocolConverter"; |
| 10 | +import { InlineKind, InlineVariable, resolveInlineVariables } from "./languageServerPlugin"; |
| 11 | + |
| 12 | +// In VS Code 1.55.0, viewport doesn't change while scrolling the editor and it's fixed in 1.56.0. |
| 13 | +// So dynamically enable viewport support based on the user's VS Code version. |
| 14 | +const isViewPortSupported = compareVersions(version.replace(/-insider$/i, ""), "1.56.0") >= 0; |
| 15 | + |
| 16 | +const protoConverter: ProtocolConverter.Converter = ProtocolConverter.createConverter(); |
| 17 | +const codeConverter: CodeConverter.Converter = CodeConverter.createConverter(); |
| 18 | + |
| 19 | +export class JavaInlineValuesProvider implements InlineValuesProvider { |
| 20 | + |
| 21 | + public async provideInlineValues(document: TextDocument, viewPort: Range, context: InlineValueContext): Promise<InlineValue[]> { |
| 22 | + const provideInlineValuesOperation = instrumentOperation("provideInlineValues", async (operationId) => { |
| 23 | + const resolveInlineVariablesStep = instrumentOperationStep(operationId, "resolveInlineVariables", async () => { |
| 24 | + return <InlineVariable[]> (await resolveInlineVariables({ |
| 25 | + uri: document.uri.toString(), |
| 26 | + viewPort: isViewPortSupported ? codeConverter.asRange(viewPort) : undefined, |
| 27 | + stoppedLocation: codeConverter.asRange(context.stoppedLocation), |
| 28 | + })); |
| 29 | + }); |
| 30 | + const variables: InlineVariable[] = await resolveInlineVariablesStep(); |
| 31 | + |
| 32 | + const resolveInlineValuesStep = instrumentOperationStep(operationId, "resolveInlineValues", async () => { |
| 33 | + if (!variables || !variables.length) { |
| 34 | + sendInfo(operationId, { |
| 35 | + inlineVariableCount: 0, |
| 36 | + }); |
| 37 | + return []; |
| 38 | + } |
| 39 | + |
| 40 | + const unresolvedVariables: any[] = variables.filter((variable) => variable.kind === InlineKind.Evaluation).map((variable) => { |
| 41 | + return { |
| 42 | + expression: variable.expression || variable.name, |
| 43 | + declaringClass: variable.declaringClass, |
| 44 | + }; |
| 45 | + }); |
| 46 | + sendInfo(operationId, { |
| 47 | + inlineVariableCount: variables.length, |
| 48 | + inlineVariableLookupCount: variables.length - unresolvedVariables.length, |
| 49 | + inlineVariableEvaluationCount: unresolvedVariables.length, |
| 50 | + }); |
| 51 | + |
| 52 | + let resolvedVariables: any; |
| 53 | + if (unresolvedVariables.length && debug.activeDebugSession) { |
| 54 | + const response = await debug.activeDebugSession.customRequest("inlineValues", { |
| 55 | + frameId: context.frameId, |
| 56 | + variables: unresolvedVariables, |
| 57 | + }); |
| 58 | + resolvedVariables = response?.variables; |
| 59 | + } |
| 60 | + |
| 61 | + const result: InlineValue[] = []; |
| 62 | + let next = 0; |
| 63 | + for (const variable of variables) { |
| 64 | + if (variable.kind === InlineKind.VariableLookup) { |
| 65 | + result.push(new InlineValueVariableLookup(protoConverter.asRange(variable.range), variable.name, true)); |
| 66 | + } else if (resolvedVariables && resolvedVariables.length > next) { |
| 67 | + const resolvedValue = resolvedVariables[next++]; |
| 68 | + if (resolvedValue) { |
| 69 | + result.push(new InlineValueText(protoConverter.asRange(variable.range), `${variable.name} = ${resolvedValue.value}`)); |
| 70 | + } else { |
| 71 | + result.push(new InlineValueEvaluatableExpression(protoConverter.asRange(variable.range), variable.name)); |
| 72 | + } |
| 73 | + } else { |
| 74 | + result.push(new InlineValueEvaluatableExpression(protoConverter.asRange(variable.range), variable.name)); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return result; |
| 79 | + }); |
| 80 | + return resolveInlineValuesStep(); |
| 81 | + }); |
| 82 | + |
| 83 | + return provideInlineValuesOperation(); |
| 84 | + } |
| 85 | + |
| 86 | +} |
0 commit comments