Copy clipboard command in ADS (html/plain text supported) (#13527)

* draft commit

* few changes

* Changes to copy query with results in plain and html formatting

* undo changes

* undo unintended change

* remove comments

* Addressed comments

* Some clean up

Co-authored-by: Monica Gupta <mogupt@microsoft.com>
This commit is contained in:
Monica Gupta
2020-11-25 21:08:29 -08:00
committed by GitHub
parent 2a7b90fd70
commit 397354ebc3
9 changed files with 117 additions and 4 deletions

View File

@@ -9,6 +9,7 @@ import { Action } from 'vs/base/common/actions';
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
import * as azdata from 'azdata';
import { escape } from 'sql/base/common/strings';
import { IQueryManagementService } from 'sql/workbench/services/query/common/queryManagement';
import { IConnectionManagementService } from 'sql/platform/connection/common/connectionManagement';
@@ -21,6 +22,7 @@ import { EditDataEditor } from 'sql/workbench/contrib/editData/browser/editDataE
import { INotificationService, Severity } from 'vs/platform/notification/common/notification';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { QueryEditorInput } from 'sql/workbench/common/editor/query/queryEditorInput';
import { ClipboardData, IClipboardService } from 'vs/platform/clipboard/common/clipboardService';
const singleQuote = '\'';
@@ -131,6 +133,74 @@ export class RunCurrentQueryKeyboardAction extends Action {
}
}
export class CopyQueryWithResultsKeyboardAction extends Action {
public static ID = 'copyQueryWithResultsKeyboardAction';
public static LABEL = nls.localize('copyQueryWithResultsKeyboardAction', "Copy Query With Results");
constructor(
id: string,
label: string,
@IEditorService private _editorService: IEditorService,
@IClipboardService private _clipboardService: IClipboardService,
@IQueryModelService protected readonly queryModelService: IQueryModelService,
) {
super(id, label);
}
public async getFormattedResults(editor): Promise<ClipboardData> {
let queryRunner = this.queryModelService.getQueryRunner(editor.input.uri);
let allResults = '';
let allHtmlResults = '';
if (queryRunner && queryRunner.batchSets.length > 0) {
for (let i = 0; i < queryRunner.batchSets[0].resultSetSummaries.length; i++) {
let resultSummary = queryRunner.batchSets[0].resultSetSummaries[i];
let result = await queryRunner.getQueryRows(0, resultSummary.rowCount, resultSummary.batchId, resultSummary.id);
let tableHeaders = resultSummary.columnInfo.map((col, i) => (col.columnName));
let htmlTableHeaders = resultSummary.columnInfo.map((col, i) => (`<th style="border:solid black 1.0pt; whiteSpace:nowrap">${escape(col.columnName)}</th>`));
let copyString = '\n';
let htmlCopyString = '<tr>';
for (let rowEntry of result.rows) {
for (let colIdx = 0; colIdx < rowEntry.length; colIdx++) {
let value = rowEntry[colIdx].displayValue;
if (value) {
copyString = `${copyString}${value}\t`;
htmlCopyString = `${htmlCopyString}<td style="border:solid black 1.0pt;white-space:nowrap">${escape(value)}</td>`;
}
}
// Removes the tab seperator from the end of a row
copyString = copyString.slice(0, -1 * '\t'.length) + '\n';
htmlCopyString = htmlCopyString + '</tr>';
}
allResults = `${allResults}${tableHeaders.join('\t')}${copyString}\n`;
allHtmlResults = `${allHtmlResults}<div><br/><br/>
<table cellPadding="5" cellSpacing="1" style="border:1;border-color:Black;font-family:Segoe UI;font-size:12px;border-collapse:collapse">
<tr style="background-color:DarkGray">${htmlTableHeaders.join('')}</tr>${htmlCopyString}
</table></div>`;
}
}
return { text: allResults, html: allHtmlResults };
}
public async run(): Promise<void> {
const editor = this._editorService.activeEditorPane;
if (editor instanceof QueryEditor) {
let allResults = await this.getFormattedResults(editor);
let queryText = editor.getAllText();
let data = {
text: `${queryText}\n\n${allResults.text}`,
html: `${escape(queryText).replace(/\r\n|\n|\r/gm, '<br />')}${allResults.html}`
};
await this._clipboardService.write(data);
}
}
}
export class RunCurrentQueryWithActualPlanKeyboardAction extends Action {
public static ID = 'runCurrentQueryWithActualPlanKeyboardAction';
public static LABEL = nls.localize('runCurrentQueryWithActualPlanKeyboardAction', "Run Current Query with Actual Plan");