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 1 commit
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',
MultiLinePasteWarning = 'terminal.integrated.multiLinePasteWarning',
weartist marked this conversation as resolved.
Show resolved Hide resolved
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> {
let textForLines = text.split(/(\r\n|\n|\r)/);
weartist marked this conversation as resolved.
Show resolved Hide resolved
let confirmation: IConfirmationResult;

// If the clipboard has only one line, no prompt will be triggered
if (textForLines.length === 1 || this._configurationService.getValue<boolean>(TerminalSettingId.MultiLinePasteWarning) === false) {
confirmation = { confirmed: true };
} else {
weartist marked this conversation as resolved.
Show resolved Hide resolved
let 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);

let displayItemsCount = 3;
weartist marked this conversation as resolved.
Show resolved Hide resolved
let detail = textForLines.slice(0, displayItemsCount).join('');
if (textForLines.length > displayItemsCount) {
detail += '...';
weartist marked this conversation as resolved.
Show resolved Hide resolved
}
weartist marked this conversation as resolved.
Show resolved Hide resolved
let 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")
}
});
}

// Check for confirmation
if (!confirmation.confirmed) {
return false;
}

// Check for confirmation checkbox
if (confirmation.confirmed && confirmation.checkboxChecked === true) {
await this._configurationService.updateValue(TerminalSettingId.MultiLinePasteWarning, false);
}
return true;
}
weartist marked this conversation as resolved.
Show resolved Hide resolved


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) === false) {
weartist marked this conversation as resolved.
Show resolved Hide resolved
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.MultiLinePasteWarning]: {
description: localize('terminal.integrated.multiLinePasteWarning', "show the warning indicator when the terminal does a multi-line paste."),
weartist marked this conversation as resolved.
Show resolved Hide resolved
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