MCPcopy
hub / github.com/microsoft/vscode-js-debug / PrettyPrintUI

Class PrettyPrintUI

src/ui/prettyPrint.ts:15–92  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

13
14@injectable()
15export class PrettyPrintUI implements IExtensionContribution {
16 private readonly canPrettyPrintKey = new ManagedContextKey(ContextKey.CanPrettyPrint);
17
18 constructor(@inject(DebugSessionTracker) private readonly tracker: DebugSessionTracker) {}
19
20 /** @inheritdoc */
21 public register(context: vscode.ExtensionContext): void {
22 context.subscriptions.push(
23 registerCommand(vscode.commands, Commands.PrettyPrint, () => this.prettifyActive()),
24 vscode.window.onDidChangeActiveTextEditor(editor => this.updateEditorState(editor)),
25 this.tracker.onSessionAdded(() => this.updateEditorState(vscode.window.activeTextEditor)),
26 this.tracker.onSessionEnded(() => this.updateEditorState(vscode.window.activeTextEditor)),
27 );
28 }
29
30 /**
31 * Prettifies the active file in the editor.
32 */
33 public async prettifyActive() {
34 const editor = vscode.window.activeTextEditor;
35 if (!editor || !this.canPrettyPrint(editor)) {
36 return;
37 }
38
39 const { sessionId, source } = sourceForUri(editor.document.uri);
40 const session = sessionId && this.tracker.getById(sessionId);
41
42 // For ephemeral files, they're attached to a single session, so go ahead
43 // and send it to the owning session. For files on disk, send it to all
44 // sessions--they will no-op if they don't know about the source.
45 let prettied: { session: vscode.DebugSession; result: Dap.PrettyPrintSourceResult }[];
46 if (session) {
47 prettied = [{
48 session,
49 result: await sendPrintCommand(session, source, editor.selection.start),
50 }];
51 } else {
52 prettied = await Promise.all(
53 this.tracker.getConcreteSessions().map(async session => {
54 const result = await sendPrintCommand(session, source, editor.selection.start);
55 return { session, result };
56 }),
57 );
58 }
59
60 if (!prettied.some(p => p.result.didReveal)) {
61 const reveal = prettied.find(p => p.result.source);
62 if (reveal) {
63 const doc = await vscode.workspace.openTextDocument(
64 dapSourceToDebugUri(reveal.session, reveal.result.source!),
65 );
66 await vscode.window.showTextDocument(doc);
67 }
68 }
69 }
70
71 private canPrettyPrint(editor: vscode.TextEditor) {
72 return (

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected