diff --git a/src/vs/editor/contrib/format/common/formatActions.ts b/src/vs/editor/contrib/format/common/formatActions.ts index 73737d382883f..646e91a35bac5 100644 --- a/src/vs/editor/contrib/format/common/formatActions.ts +++ b/src/vs/editor/contrib/format/common/formatActions.ts @@ -19,6 +19,7 @@ import { CommandsRegistry } from 'vs/platform/commands/common/commands'; import { ICodeEditorService } from 'vs/editor/common/services/codeEditorService'; import { IEditorWorkerService } from 'vs/editor/common/services/editorWorkerService'; import { CharacterSet } from 'vs/editor/common/core/characterClassifier'; +import { Range } from 'vs/editor/common/core/range'; import ModeContextKeys = editorCommon.ModeContextKeys; import EditorContextKeys = editorCommon.EditorContextKeys; @@ -235,6 +236,33 @@ export class FormatSelectionAction extends AbstractFormatAction { } } +@editorAction +export class FormatOnPasteAction extends AbstractFormatAction { + constructor() { + super({ + id: 'editor.action.formatOnPaste', + label: nls.localize('formatOnPaste.label', "Format on paste"), + alias: 'Format on paste', + precondition: EditorContextKeys.Writable + }); + } + + protected _getFormattingEdits(editor: editorCommon.ICommonCodeEditor): TPromise { + const originalSelectionStart = editor.getSelection().getStartPosition(); + editor.focus(); + document.execCommand('paste'); + + // paste doesn't persist selection + const currentCursorPosition = editor.getSelection().getStartPosition(); + const pastedContentRange = new Range(currentCursorPosition.lineNumber, currentCursorPosition.column, originalSelectionStart.lineNumber, originalSelectionStart.column); + + const model = editor.getModel(); + const { tabSize, insertSpaces} = model.getOptions(); + + return getDocumentRangeFormattingEdits(model, pastedContentRange, { tabSize, insertSpaces }); + } +} + // this is the old format action that does both (format document OR format selection) // and we keep it here such that existing keybinding configurations etc will still work CommandsRegistry.registerCommand('editor.action.format', accessor => {