Skip to content

Commit

Permalink
fix #13945. support format on paste
Browse files Browse the repository at this point in the history
  • Loading branch information
rebornix committed Jan 13, 2017
1 parent f400b95 commit a524360
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/vs/editor/contrib/format/common/formatActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<editorCommon.ISingleEditOperation[]> {
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 => {
Expand Down

0 comments on commit a524360

Please sign in to comment.