Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor copying query result to clipboard #25152

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions src/sql/workbench/services/query/common/gridDataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export async function copySelectionToClipboard(clipboardService: IClipboardServi
const getMessageText = (): string => {
return nls.localize('gridDataProvider.loadingRowsInProgress', "Loading the rows to be copied ({0}/{1})...", processedRows, rowCount);
};
let resultString = '';
let headerString = '';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Instead of making a whole separate var and copying that into the array you create below I'd suggest just creating the array here and push the header to it directly on line 145.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I guess I could do that but I wanted the logic of string-join to be separated from the logic of creating the data rows.

if (includeHeaders) {
const headers: string[] = [];
columnRanges.forEach(range => {
Expand All @@ -142,7 +142,7 @@ export async function copySelectionToClipboard(clipboardService: IClipboardServi
toCell: range.end
}));
});
resultString = Array.from(headers.values()).join(valueSeparator).concat(eol);
headerString = Array.from(headers.values()).join(valueSeparator).concat(eol);
}

const rowValues: string[] = [];
Expand Down Expand Up @@ -176,10 +176,22 @@ export async function copySelectionToClipboard(clipboardService: IClipboardServi
});
}
if (!cancellationTokenSource.token.isCancellationRequested) {
resultString += rowValues.reduce(
(prevVal, currVal, idx) => prevVal + (idx > 0 && (!prevVal?.endsWith(eol) || !shouldSkipNewLineAfterTrailingLineBreak) ? eol : '') + currVal,
);
await clipboardService.writeText(resultString);
const resultParts: string[] = [];
if (includeHeaders)
resultParts.push(headerString);

if (rowValues.length > 0) {
let prevVal = rowValues[0];
resultParts.push(prevVal);

for (let i = 1; i < rowValues.length; i++) {
const currVal = rowValues[i];
resultParts.push((!prevVal?.endsWith(eol) || !shouldSkipNewLineAfterTrailingLineBreak ? eol : '') + currVal);
prevVal = currVal;
}
}

await clipboardService.writeText(resultParts.join(""));
}
}, cancellationTokenSource);
}
Expand Down