format.ts 3.5 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

8
import {IFormattingSupport, IFormattingOptions} from 'vs/editor/common/modes';
E
Erich Gamma 已提交
9
import LanguageFeatureRegistry from 'vs/editor/common/modes/languageFeatureRegistry';
10 11 12 13 14 15 16 17
import {onUnexpectedError, illegalArgument} from 'vs/base/common/errors';
import URI from 'vs/base/common/uri';
import {IAction, Action} from 'vs/base/common/actions';
import {IModelService} from 'vs/editor/common/services/modelService';
import {TPromise} from 'vs/base/common/winjs.base';
import {IModel, IRange, IPosition, ISingleEditOperation} from 'vs/editor/common/editorCommon';
import {Range} from 'vs/editor/common/core/range';
import {CommonEditorRegistry} from 'vs/editor/common/editorCommonExtensions';
E
Erich Gamma 已提交
18 19 20 21

export const FormatRegistry = new LanguageFeatureRegistry<IFormattingSupport>('formattingSupport');
export const FormatOnTypeRegistry = new LanguageFeatureRegistry<IFormattingSupport>('formattingSupport');

22 23 24
export {IFormattingSupport};

export function formatRange(model: IModel, range: IRange, options: IFormattingOptions): TPromise<ISingleEditOperation[]> {
25
	const [support] = FormatRegistry.ordered(model);
26
	if (!support) {
27
		return TPromise.as(undefined);
28 29 30 31 32
	}
	return support.formatRange(model.getAssociatedResource(), range, options);
}

export function formatDocument(model: IModel, options: IFormattingOptions): TPromise<ISingleEditOperation[]> {
33
	const [support] = FormatRegistry.ordered(model);
34
	if (!support) {
35
		return TPromise.as(undefined);
36 37 38 39 40
	}
	if (typeof support.formatDocument !== 'function') {
		if (typeof support.formatRange === 'function') {
			return formatRange(model, model.getFullModelRange(), options);
		} else {
41
			return TPromise.as(undefined);
42 43 44 45 46 47
		}
	}

	return support.formatDocument(model.getAssociatedResource(), options);
}

48 49 50 51 52 53 54 55 56 57 58
export function formatAfterKeystroke(model: IModel, position: IPosition, ch: string, options: IFormattingOptions): TPromise<ISingleEditOperation[]> {
	const [support] = FormatOnTypeRegistry.ordered(model);
	if (!support) {
		return TPromise.as(undefined);
	}
	if (support.autoFormatTriggerCharacters.indexOf(ch) < 0) {
		return TPromise.as(undefined);
	}
	return support.formatAfterKeystroke(model.getAssociatedResource(), position, ch, options);
}

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
CommonEditorRegistry.registerLanguageCommand('_executeFormatRangeProvider', function(accessor, args) {
	const {resource, range, options} = args;
	if (!URI.isURI(resource) || !Range.isIRange(range)) {
		throw illegalArgument();
	}
	const model = accessor.get(IModelService).getModel(resource);
	if (!model) {
		throw illegalArgument('resource');
	}
	return formatRange(model, range, options);
});

CommonEditorRegistry.registerLanguageCommand('_executeFormatDocumentProvider', function(accessor, args) {
	const {resource, options} = args;
	if (!URI.isURI(resource)) {
		throw illegalArgument('resource');
	}
	const model = accessor.get(IModelService).getModel(resource);
	if (!model) {
		throw illegalArgument('resource');
	}

	return formatDocument(model, options)
82 83 84 85 86 87 88 89 90
});

CommonEditorRegistry.registerDefaultLanguageCommand('_executeFormatOnTypeProvider', function(model, position, args) {
	const {ch, options } = args;
	if (typeof ch !== 'string') {
		throw illegalArgument('ch');
	}
	return formatAfterKeystroke(model, position, ch, options);
});