indentation.ts 3.9 KB
Newer Older
I
isidor 已提交
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

A
Alex Dima 已提交
6
import * as nls from 'vs/nls';
I
isidor 已提交
7 8
import {TPromise} from 'vs/base/common/winjs.base';
import {INullService} from 'vs/platform/instantiation/common/instantiation';
A
Alex Dima 已提交
9 10 11 12
import {EditorAction} from 'vs/editor/common/editorAction';
import {ICommonCodeEditor, IEditorActionDescriptorData} from 'vs/editor/common/editorCommon';
import {CommonEditorRegistry, EditorActionDescriptor} from 'vs/editor/common/editorCommonExtensions';
import {IndentationToSpacesCommand, IndentationToTabsCommand} from 'vs/editor/contrib/indentation/common/indentationCommands';
13
import {IQuickOpenService} from 'vs/workbench/services/quickopen/common/quickOpenService';
I
isidor 已提交
14

I
isidor 已提交
15
export class IndentationToSpacesAction extends EditorAction {
I
isidor 已提交
16 17
	static ID = 'editor.action.indentationToSpaces';

A
Alex Dima 已提交
18
	constructor(descriptor: IEditorActionDescriptorData, editor: ICommonCodeEditor, @INullService ns) {
I
isidor 已提交
19 20 21 22 23 24 25
		super(descriptor, editor);
	}

	public run(): TPromise<boolean> {

		const command = new IndentationToSpacesCommand(this.editor.getSelection(), this.editor.getIndentationOptions().tabSize);
		this.editor.executeCommands(this.id, [command]);
26

I
isidor 已提交
27 28 29 30
		return TPromise.as(true);
	}
}

I
isidor 已提交
31
export class IndentationToTabsAction extends EditorAction {
I
isidor 已提交
32 33
	static ID = 'editor.action.indentationToTabs';

A
Alex Dima 已提交
34
	constructor(descriptor: IEditorActionDescriptorData, editor: ICommonCodeEditor, @INullService ns) {
I
isidor 已提交
35 36 37 38 39 40 41 42 43 44 45 46
		super(descriptor, editor);
	}

	public run(): TPromise<boolean> {

		const command = new IndentationToTabsCommand(this.editor.getSelection(), this.editor.getIndentationOptions().tabSize);
		this.editor.executeCommands(this.id, [command]);

		return TPromise.as(true);
	}
}

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
abstract class Indent extends EditorAction {

	constructor(descriptor: IEditorActionDescriptorData, editor: ICommonCodeEditor, private insertSpaces: boolean, private quickOpenService: IQuickOpenService) {
		super(descriptor, editor);
	}

	public run(): TPromise<boolean> {
		return TPromise.timeout(50 /* quick open is sensitive to being opened so soon after another */).then(() =>
			this.quickOpenService.pick(['1', '2', '3', '4', '5', '6', '7', '8'], { placeHolder: nls.localize('selectTabWidth', "Select Tab Width")}).then(pick => {
				this.editor.setIndentationOptions({
					insertSpaces: this.insertSpaces,
					tabSize: parseInt(pick)
				});
				return true;
			})
		);
	}
}

export class IndentUsingSpaces extends Indent {
	static ID = 'editor.action.indentUsingSpaces';

	constructor(descriptor: IEditorActionDescriptorData, editor: ICommonCodeEditor, @IQuickOpenService quickOpenService: IQuickOpenService) {
		super(descriptor, editor, true, quickOpenService);
	}
}

export class IndentUsingTabs extends Indent {
	static ID = 'editor.action.indentUsingTabs';

	constructor(descriptor: IEditorActionDescriptorData, editor: ICommonCodeEditor, @IQuickOpenService quickOpenService: IQuickOpenService) {
		super(descriptor, editor, false, quickOpenService);
	}
}

I
isidor 已提交
82 83 84
// register actions
CommonEditorRegistry.registerEditorAction(new EditorActionDescriptor(IndentationToSpacesAction, IndentationToSpacesAction.ID, nls.localize('indentationToSpaces', "Convert Indentation to Spaces")));
CommonEditorRegistry.registerEditorAction(new EditorActionDescriptor(IndentationToTabsAction, IndentationToTabsAction.ID, nls.localize('indentationToTabs', "Convert Indentation to Tabs")));
85 86
CommonEditorRegistry.registerEditorAction(new EditorActionDescriptor(IndentUsingSpaces, IndentUsingSpaces.ID, nls.localize('indentUsingSpaces', "Indent Using Spaces")));
CommonEditorRegistry.registerEditorAction(new EditorActionDescriptor(IndentUsingTabs, IndentUsingTabs.ID, nls.localize('indentUsingTabs', "Indent Using Tabs")));