|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import * as os from "os"; |
| 7 | +import * as vscode from "vscode"; |
| 8 | +import * as Constants from "../constants/constants"; |
| 9 | +import * as LocalizedConstants from "../constants/locConstants"; |
| 10 | +import type { |
| 11 | + NotebookQueryResultBlock, |
| 12 | + NotebookQueryResultOutputData, |
| 13 | +} from "../sharedInterfaces/notebookQueryResult"; |
| 14 | + |
| 15 | +const MIME_TEXT_PLAIN = "text/plain"; |
| 16 | +const MIME_STDERR = "application/vnd.code.notebook.stderr"; |
| 17 | +const MIME_NOTEBOOK_QUERY_RESULT = "application/vnd.mssql.query-result"; |
| 18 | + |
| 19 | +/** |
| 20 | + * Registers a "Copy messages" status bar item for SQL notebook cells. |
| 21 | + * Reads raw output from the cell model to avoid virtualization issues (#21378). |
| 22 | + * For rich outputs (result grids), copies only text/error blocks. |
| 23 | + */ |
| 24 | +export function registerNotebookCopyOutput(context: vscode.ExtensionContext): void { |
| 25 | + context.subscriptions.push( |
| 26 | + vscode.commands.registerCommand( |
| 27 | + Constants.cmdNotebooksCopyCellMessages, |
| 28 | + async (cell: vscode.NotebookCell | undefined) => { |
| 29 | + if (!cell) { |
| 30 | + return; |
| 31 | + } |
| 32 | + const text = collectTextOutput(cell); |
| 33 | + if (!text) { |
| 34 | + return; |
| 35 | + } |
| 36 | + await vscode.env.clipboard.writeText(text); |
| 37 | + vscode.window.setStatusBarMessage( |
| 38 | + LocalizedConstants.Notebooks.copiedMessages, |
| 39 | + 2000, |
| 40 | + ); |
| 41 | + }, |
| 42 | + ), |
| 43 | + ); |
| 44 | + |
| 45 | + context.subscriptions.push( |
| 46 | + vscode.notebooks.registerNotebookCellStatusBarItemProvider("jupyter-notebook", { |
| 47 | + provideCellStatusBarItems(cell) { |
| 48 | + if (cell.document.languageId !== "sql") { |
| 49 | + return; |
| 50 | + } |
| 51 | + if (!cell.outputs.some(isCopyableTextOutput)) { |
| 52 | + return; |
| 53 | + } |
| 54 | + const item = new vscode.NotebookCellStatusBarItem( |
| 55 | + `$(copy) ${LocalizedConstants.Notebooks.copyMessages}`, |
| 56 | + vscode.NotebookCellStatusBarAlignment.Right, |
| 57 | + ); |
| 58 | + item.command = { |
| 59 | + command: Constants.cmdNotebooksCopyCellMessages, |
| 60 | + title: LocalizedConstants.Notebooks.copyMessages, |
| 61 | + arguments: [cell], |
| 62 | + }; |
| 63 | + item.tooltip = LocalizedConstants.Notebooks.copyMessagesTooltip; |
| 64 | + return item; |
| 65 | + }, |
| 66 | + }), |
| 67 | + ); |
| 68 | +} |
| 69 | + |
| 70 | +function isCopyableTextOutput(output: vscode.NotebookCellOutput): boolean { |
| 71 | + if (output.items.length === 0) { |
| 72 | + return false; |
| 73 | + } |
| 74 | + const rich = findRichItem(output); |
| 75 | + if (rich) { |
| 76 | + return hasNonResultSetBlock(rich); |
| 77 | + } |
| 78 | + return output.items.some((item) => item.mime === MIME_TEXT_PLAIN || item.mime === MIME_STDERR); |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Checks whether a rich output contains any non-resultSet blocks (text/error) |
| 83 | + * without deserializing the full JSON payload. This avoids the cost of |
| 84 | + * materializing large resultSet row arrays during frequent status-bar updates. |
| 85 | + */ |
| 86 | +function hasNonResultSetBlock(item: vscode.NotebookCellOutputItem): boolean { |
| 87 | + const raw = Buffer.from(item.data).toString("utf8"); |
| 88 | + return /\"type\"\s*:\s*\"(?:text|error)\"/.test(raw); |
| 89 | +} |
| 90 | + |
| 91 | +function collectTextOutput(cell: vscode.NotebookCell): string { |
| 92 | + const chunks: string[] = []; |
| 93 | + for (const output of cell.outputs) { |
| 94 | + const rich = findRichItem(output); |
| 95 | + if (rich) { |
| 96 | + chunks.push(...extractRichMessageText(rich)); |
| 97 | + continue; |
| 98 | + } |
| 99 | + for (const item of output.items) { |
| 100 | + if (item.mime === MIME_TEXT_PLAIN || item.mime === MIME_STDERR) { |
| 101 | + chunks.push(Buffer.from(item.data).toString("utf8")); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + return chunks.join(os.EOL); |
| 106 | +} |
| 107 | + |
| 108 | +function findRichItem( |
| 109 | + output: vscode.NotebookCellOutput, |
| 110 | +): vscode.NotebookCellOutputItem | undefined { |
| 111 | + return output.items.find((item) => item.mime === MIME_NOTEBOOK_QUERY_RESULT); |
| 112 | +} |
| 113 | + |
| 114 | +function extractRichMessageText(item: vscode.NotebookCellOutputItem): string[] { |
| 115 | + let data: NotebookQueryResultOutputData; |
| 116 | + try { |
| 117 | + data = JSON.parse(Buffer.from(item.data).toString("utf8")); |
| 118 | + } catch { |
| 119 | + return []; |
| 120 | + } |
| 121 | + if (!data || !Array.isArray(data.blocks)) { |
| 122 | + return []; |
| 123 | + } |
| 124 | + return data.blocks |
| 125 | + .filter( |
| 126 | + (block): block is Exclude<NotebookQueryResultBlock, { type: "resultSet" }> => |
| 127 | + block.type !== "resultSet", |
| 128 | + ) |
| 129 | + .map((block) => block.text); |
| 130 | +} |
0 commit comments