|
| 1 | +/* -------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) 2024 TypeFox and others. |
| 3 | + * Licensed under the MIT License. See LICENSE in the package root for license information. |
| 4 | + * ------------------------------------------------------------------------------------------ */ |
| 5 | + |
| 6 | +import * as vscode from 'vscode'; |
| 7 | +import type { ExtensionConfig } from 'monaco-editor-wrapper'; |
| 8 | +import type { ConfigParams } from '../../python/client/config.js'; |
| 9 | + |
| 10 | +// This is derived from: |
| 11 | +// https://github.com/CodinGame/monaco-vscode-api/blob/main/demo/src/features/debugger.ts |
| 12 | + |
| 13 | +export const provideDebuggerExtensionConfig = (config: ConfigParams): ExtensionConfig => { |
| 14 | + const filesOrContents = new Map<string, string | URL>(); |
| 15 | + filesOrContents.set('./extension.js', '// nothing'); |
| 16 | + |
| 17 | + return { |
| 18 | + config: { |
| 19 | + name: 'debugger-py-client', |
| 20 | + publisher: 'TypeFox', |
| 21 | + version: '1.0.0', |
| 22 | + engines: { |
| 23 | + vscode: '*' |
| 24 | + }, |
| 25 | + // A browser field is mandatory for the extension to be flagged as `web` |
| 26 | + browser: 'extension.js', |
| 27 | + contributes: { |
| 28 | + debuggers: [ |
| 29 | + { |
| 30 | + type: config.languageId, |
| 31 | + label: 'Test', |
| 32 | + languages: [config.languageId] |
| 33 | + } |
| 34 | + ], |
| 35 | + breakpoints: [ |
| 36 | + { |
| 37 | + language: config.languageId |
| 38 | + } |
| 39 | + ] |
| 40 | + } |
| 41 | + }, |
| 42 | + filesOrContents |
| 43 | + }; |
| 44 | +}; |
| 45 | + |
| 46 | +export const confiugureDebugging = async (api: typeof vscode, config: ConfigParams) => { |
| 47 | + class WebsocketDebugAdapter implements vscode.DebugAdapter { |
| 48 | + private websocket: WebSocket; |
| 49 | + |
| 50 | + constructor(websocket: WebSocket) { |
| 51 | + this.websocket = websocket; |
| 52 | + this.websocket.onmessage = (message) => { |
| 53 | + this._onDidSendMessage.fire(JSON.parse(message.data)); |
| 54 | + }; |
| 55 | + } |
| 56 | + |
| 57 | + _onDidSendMessage = new api.EventEmitter<vscode.DebugProtocolMessage>(); |
| 58 | + onDidSendMessage = this._onDidSendMessage.event; |
| 59 | + |
| 60 | + handleMessage(message: vscode.DebugProtocolMessage): void { |
| 61 | + this.websocket.send(JSON.stringify(message)); |
| 62 | + } |
| 63 | + |
| 64 | + dispose() { |
| 65 | + this.websocket.close(); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + api.debug.registerDebugConfigurationProvider(config.languageId, { |
| 70 | + resolveDebugConfiguration() { |
| 71 | + return { |
| 72 | + name: 'Test debugger', |
| 73 | + type: config.languageId, |
| 74 | + request: 'launch' |
| 75 | + }; |
| 76 | + } |
| 77 | + }); |
| 78 | + |
| 79 | + api.debug.registerDebugAdapterDescriptorFactory(config.languageId, { |
| 80 | + async createDebugAdapterDescriptor() { |
| 81 | + const websocket = new WebSocket(config.debuggerUrl); |
| 82 | + |
| 83 | + await new Promise((resolve, reject) => { |
| 84 | + websocket.onopen = resolve; |
| 85 | + websocket.onerror = () => |
| 86 | + reject(new Error('Unable to connect to debugger server. Run `npm run start:debugServer`')); |
| 87 | + }); |
| 88 | + |
| 89 | + const file = config.files.get('hello.py'); |
| 90 | + if (file === undefined) { |
| 91 | + throw new Error('No file found'); |
| 92 | + } else { |
| 93 | + console.log(`Sending file: ${file.uri.path}`); |
| 94 | + websocket.send( |
| 95 | + JSON.stringify({ |
| 96 | + main: file.uri.path, |
| 97 | + files: { |
| 98 | + file: new TextDecoder().decode( |
| 99 | + await api.workspace.fs.readFile( |
| 100 | + file.uri |
| 101 | + ) |
| 102 | + ) |
| 103 | + } |
| 104 | + }) |
| 105 | + ); |
| 106 | + } |
| 107 | + const adapter = new WebsocketDebugAdapter(websocket); |
| 108 | + |
| 109 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 110 | + adapter.onDidSendMessage((message: any) => { |
| 111 | + if (message.type === 'event' && message.event === 'output') { |
| 112 | + console.log('OUTPUT', message.body.output); |
| 113 | + } |
| 114 | + }); |
| 115 | + return new api.DebugAdapterInlineImplementation(adapter); |
| 116 | + } |
| 117 | + }); |
| 118 | +}; |
0 commit comments