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

Implement multiLinePasteWarning in VSCode terminal,fix the #122683 #141822

Merged
merged 5 commits into from
Feb 1, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/vs/platform/terminal/common/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export const enum TerminalSettingId {
MacOptionClickForcesSelection = 'terminal.integrated.macOptionClickForcesSelection',
AltClickMovesCursor = 'terminal.integrated.altClickMovesCursor',
CopyOnSelection = 'terminal.integrated.copyOnSelection',
EnableMultiLinePasteWarning = 'terminal.integrated.enableMultiLinePasteWarning',
DrawBoldTextInBrightColors = 'terminal.integrated.drawBoldTextInBrightColors',
FontFamily = 'terminal.integrated.fontFamily',
FontSize = 'terminal.integrated.fontSize',
Expand Down
51 changes: 50 additions & 1 deletion src/vs/workbench/contrib/terminal/browser/terminalInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import { TerminalCapability } from 'vs/workbench/contrib/terminal/common/capabil
import { ITextModel } from 'vs/editor/common/model';
import { IModelService } from 'vs/editor/common/services/model';
import { ITextModelContentProvider, ITextModelService } from 'vs/editor/common/services/resolverService';
import { IDialogService, IConfirmationResult } from 'vs/platform/dialogs/common/dialogs';

const enum Constants {
/**
Expand Down Expand Up @@ -343,6 +344,7 @@ export class TerminalInstance extends Disposable implements ITerminalInstance {
@IThemeService private readonly _themeService: IThemeService,
@IConfigurationService private readonly _configurationService: IConfigurationService,
@ILogService private readonly _logService: ILogService,
@IDialogService private readonly _dialogService: IDialogService,
@IStorageService private readonly _storageService: IStorageService,
@IAccessibilityService private readonly _accessibilityService: IAccessibilityService,
@IProductService private readonly _productService: IProductService,
Expand Down Expand Up @@ -1058,6 +1060,47 @@ export class TerminalInstance extends Disposable implements ITerminalInstance {
this._terminalAltBufferActiveContextKey.set(!!(this.xterm && this.xterm.raw.buffer.active === this.xterm.raw.buffer.alternate));
}

private async _shouldPasteText(text: string): Promise<boolean> {
const textForLines = text.split(/(\r?\n)/);
let confirmation: IConfirmationResult;

// If the clipboard has only one line, no prompt will be triggered
if (textForLines.length === 1 || !this._configurationService.getValue<boolean>(TerminalSettingId.EnableMultiLinePasteWarning)) {
return true;
} else {
const message = nls.localize('confirmMoveTrashMessageFilesAndDirectories', "Are you sure you want to paste the following {0} lines to the terminal?", text.split(/\r\n|\n|\r/).length);

const displayItemsCount = 3;
const ellipsis = '…';

let detail = textForLines
.slice(0, displayItemsCount)
.map(s => s.length >= 30 ? s.slice(0, 30) + ellipsis : s)
.join('');
Tyriar marked this conversation as resolved.
Show resolved Hide resolved
if (textForLines.length > displayItemsCount && !detail.endsWith(ellipsis)) {
detail += ellipsis;
}
const primaryButton = nls.localize({ key: 'multiLinePasteButton', comment: ['&& denotes a mnemonic'] }, "&&Paste");

confirmation = await this._dialogService.confirm({
type: 'question',
message: message,
detail: detail,
primaryButton: primaryButton,
checkbox: {
label: nls.localize('doNotAskAgain', "Do not ask me again")
}
});
}

if (confirmation.confirmed && confirmation.checkboxChecked) {
await this._configurationService.updateValue(TerminalSettingId.EnableMultiLinePasteWarning, false);
}

return confirmation.confirmed;
}


override dispose(immediate?: boolean): void {
this._logService.trace(`terminalInstance#dispose (instanceId: ${this.instanceId})`);
dispose(this._linkManager);
Expand Down Expand Up @@ -1136,8 +1179,14 @@ export class TerminalInstance extends Disposable implements ITerminalInstance {
if (!this.xterm) {
return;
}

let currentText: string = await this._clipboardService.readText();
if (!await this._shouldPasteText(currentText)) {
return;
}

this.focus();
this.xterm.raw.paste(await this._clipboardService.readText());
this.xterm.raw.paste(currentText);
}

async pasteSelection(): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ const terminalConfiguration: IConfigurationNode = {
type: 'boolean',
default: false
},
[TerminalSettingId.EnableMultiLinePasteWarning]: {
description: localize('terminal.integrated.enableMultiLinePasteWarning', "Show a warning dialog when pasting multiple lines into the terminal."),
type: 'boolean',
default: true
},
[TerminalSettingId.DrawBoldTextInBrightColors]: {
description: localize('terminal.integrated.drawBoldTextInBrightColors', "Controls whether bold text in the terminal will always use the \"bright\" ANSI color variant."),
type: 'boolean',
Expand Down