Skip to content

Commit

Permalink
Merge pull request #141822 from slidoooor/main
Browse files Browse the repository at this point in the history
Implement multiLinePasteWarning in VSCode terminal,fix the #122683
  • Loading branch information
Tyriar committed Feb 1, 2022
2 parents e4329bf + 05bd2e7 commit f6895ac
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
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 @@ -342,6 +343,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 @@ -1054,6 +1056,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;
}

const displayItemsCount = 3;
const maxPreviewLineLength = 30;

let detail = 'Preview:';
for (let i = 0; i < Math.min(textForLines.length, displayItemsCount); i++) {
const line = textForLines[i];
const cleanedLine = line.length > maxPreviewLineLength ? `${line.slice(0, maxPreviewLineLength)}…` : line;
detail += `\n${cleanedLine}`;
}

if (textForLines.length > displayItemsCount) {
detail += `\n…`;
}

confirmation = await this._dialogService.confirm({
type: 'question',
message: nls.localize('confirmMoveTrashMessageFilesAndDirectories', "Are you sure you want to paste {0} lines of text into the terminal?", textForLines.length),
detail,
primaryButton: nls.localize({ key: 'multiLinePasteButton', comment: ['&& denotes a mnemonic'] }, "&&Paste"),
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 @@ -1132,8 +1175,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

0 comments on commit f6895ac

Please sign in to comment.