|
| 1 | +import { DataExtractionResult, DataResult } from "@hediet/debug-visualizer-data-extraction"; |
| 2 | +import { hotClass, registerUpdateReconciler } from "@hediet/node-reload"; |
| 3 | +import { Config } from "../Config"; |
| 4 | +import { DebuggerViewProxy } from "../proxies/DebuggerViewProxy"; |
| 5 | +import { DebugSessionProxy } from "../proxies/DebugSessionProxy"; |
| 6 | +import { FormattedMessage } from "../webviewContract"; |
| 7 | +import { DebugSessionVisualizationSupport, GetVisualizationDataArgs, VisualizationBackend, VisualizationBackendBase } from "./VisualizationBackend"; |
| 8 | + |
| 9 | +registerUpdateReconciler(module); |
| 10 | + |
| 11 | +@hotClass(module) |
| 12 | +export class RbEvaluationEngine implements DebugSessionVisualizationSupport { |
| 13 | + constructor( |
| 14 | + private readonly debuggerView: DebuggerViewProxy, |
| 15 | + private readonly config: Config |
| 16 | + ) { } |
| 17 | + |
| 18 | + createBackend( |
| 19 | + session: DebugSessionProxy |
| 20 | + ): VisualizationBackend | undefined { |
| 21 | + const supportedDebugAdapters = ['rdbg']; |
| 22 | + |
| 23 | + if (supportedDebugAdapters.indexOf(session.session.type) !== -1) { |
| 24 | + return new RbVisualizationBackend( |
| 25 | + session, |
| 26 | + this.debuggerView, |
| 27 | + this.config |
| 28 | + ); |
| 29 | + } |
| 30 | + return undefined; |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +class RbVisualizationBackend extends VisualizationBackendBase { |
| 35 | + public readonly expressionLanguageId = 'ruby'; |
| 36 | + constructor( |
| 37 | + debugSession: DebugSessionProxy, |
| 38 | + debuggerView: DebuggerViewProxy, |
| 39 | + private readonly config: Config |
| 40 | + ) { |
| 41 | + super(debugSession, debuggerView) |
| 42 | + } |
| 43 | + |
| 44 | + private readonly defaultContext = "repl"; |
| 45 | + |
| 46 | + public async getVisualizationData( |
| 47 | + args: GetVisualizationDataArgs |
| 48 | + ): Promise< |
| 49 | + | { kind: "data"; result: DataExtractionResult; } |
| 50 | + | { kind: "error"; message: FormattedMessage; } |
| 51 | + > { |
| 52 | + const result = await this._getVisualizationData(args); |
| 53 | + return result; |
| 54 | + } |
| 55 | + |
| 56 | + private async _getVisualizationData({ |
| 57 | + expression, |
| 58 | + preferredExtractorId |
| 59 | + }: GetVisualizationDataArgs): Promise< |
| 60 | + | { kind: "data"; result: DataExtractionResult } |
| 61 | + | { kind: "error"; message: FormattedMessage } |
| 62 | + > { |
| 63 | + try { |
| 64 | + if (expression.length === 0) throw new Error("No extractors"); |
| 65 | + |
| 66 | + const frameId = this.debuggerView.getActiveStackFrameId( |
| 67 | + this.debugSession |
| 68 | + ); |
| 69 | + const initialReply = await this.debugSession.evaluate({ |
| 70 | + expression: "require 'debugvisualizer'", |
| 71 | + frameId, |
| 72 | + context: this.defaultContext |
| 73 | + }); |
| 74 | + if (initialReply.result.includes('LoadError')) { |
| 75 | + return { |
| 76 | + kind: "error", |
| 77 | + message: { |
| 78 | + kind: "list", |
| 79 | + items: [ |
| 80 | + "LoadError: Failed to load debugvisualizer.", |
| 81 | + { |
| 82 | + kind: "inlineList", |
| 83 | + items: [ |
| 84 | + "Install the gem by executing:", |
| 85 | + { kind: "code", content: "$ bundle add debugvisualizer" }, |
| 86 | + ], |
| 87 | + }, |
| 88 | + { |
| 89 | + kind: "inlineList", |
| 90 | + items: [ |
| 91 | + "If bundler is not being used, install the gem by executing:", |
| 92 | + { kind: "code", content: "$ gem install debugvisualizer" }, |
| 93 | + ], |
| 94 | + } |
| 95 | + ], |
| 96 | + } |
| 97 | + }; |
| 98 | + } |
| 99 | + const preferredId = preferredExtractorId || ''; |
| 100 | + const wrappedExpr = ` |
| 101 | + DebugVisualizer.to_debug_visualizer_protocol_json("${preferredId}", ${expression}) |
| 102 | + `; |
| 103 | + const reply = await this.debugSession.evaluate({ |
| 104 | + expression: wrappedExpr, |
| 105 | + frameId, |
| 106 | + context: this.defaultContext |
| 107 | + }) |
| 108 | + |
| 109 | + let dataResult: DataResult; |
| 110 | + try { |
| 111 | + // Debuggee converts result to a JSON string twice. |
| 112 | + dataResult = JSON.parse(JSON.parse(reply.result)) as DataResult; |
| 113 | + } catch (error) { |
| 114 | + let message = error.message |
| 115 | + // The `reply.result` is as follows when error occurs in the evaluation of an expression and parsing will fail. |
| 116 | + // e.g. "#<ZeroDivisionError: divided by 0>" |
| 117 | + // In this case, it is more beneficial to display this error message. |
| 118 | + if (reply.result.includes('Error')) message = reply.result; |
| 119 | + throw new Error(message) |
| 120 | + } |
| 121 | + |
| 122 | + switch (dataResult.kind) { |
| 123 | + case 'NoExtractors': |
| 124 | + throw new Error("No extractors"); |
| 125 | + case 'Error': |
| 126 | + throw new Error(dataResult.message); |
| 127 | + case 'Data': |
| 128 | + return { |
| 129 | + kind: "data", |
| 130 | + result: dataResult.extractionResult, |
| 131 | + }; |
| 132 | + default: |
| 133 | + throw new Error("Invalid Data"); |
| 134 | + } |
| 135 | + } catch (error) { |
| 136 | + return { |
| 137 | + kind: "error", |
| 138 | + message: error.message |
| 139 | + }; |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments