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

Adding [Reindent Selected Lines] command. #47116

Merged
merged 4 commits into from
May 2, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/vs/editor/contrib/indentation/indentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,52 @@ export class ReindentLinesAction extends EditorAction {
}
}

export class ReindentSelectedLinesAction extends EditorAction {
constructor() {
super({
id: 'editor.action.reindentselectedlines',
label: nls.localize('editor.reindentselectedlines', "Reindent Selected Lines"),
alias: 'Reindent Selected Lines',
precondition: EditorContextKeys.writable
});
}

public run(accessor: ServicesAccessor, editor: ICodeEditor): void {
let model = editor.getModel();
if (!model) {
return;
}

let edits: IIdentifiedSingleEditOperation[] = [];

for (let selection of editor.getSelections()) {
let startLineNumber = selection.startLineNumber;
let endLineNumber = selection.endLineNumber;

if (startLineNumber !== endLineNumber && selection.endColumn === 1) {
endLineNumber--;
}

if (startLineNumber === 1) {
if (startLineNumber === endLineNumber) {
continue;
}
} else {
startLineNumber--;
}

let editOperations = getReindentEditOperations(model, startLineNumber, endLineNumber) || [];
edits.push(...editOperations);
}

if (edits.length > 0) {
editor.pushUndoStop();
editor.executeEdits(this.id, edits);
editor.pushUndoStop();
}
}
}

export class AutoIndentOnPasteCommand implements ICommand {

private _edits: TextEdit[];
Expand Down Expand Up @@ -649,3 +695,4 @@ registerEditorAction(IndentUsingTabs);
registerEditorAction(IndentUsingSpaces);
registerEditorAction(DetectIndentation);
registerEditorAction(ReindentLinesAction);
registerEditorAction(ReindentSelectedLinesAction);