提交 9a9c8c7a 编写于 作者: J Johannes Rieken

show message when no formatter is installed, #36303

上级 10a0e425
...@@ -153,6 +153,7 @@ export abstract class EditorCommand extends Command { ...@@ -153,6 +153,7 @@ export abstract class EditorCommand extends Command {
export interface IEditorCommandMenuOptions { export interface IEditorCommandMenuOptions {
group?: string; group?: string;
order?: number; order?: number;
when?: ContextKeyExpr;
} }
export interface IActionOptions extends ICommandOptions { export interface IActionOptions extends ICommandOptions {
label: string; label: string;
...@@ -182,7 +183,7 @@ export abstract class EditorAction extends EditorCommand { ...@@ -182,7 +183,7 @@ export abstract class EditorAction extends EditorCommand {
id: this.id, id: this.id,
title: this.label title: this.label
}, },
when: this.precondition, when: ContextKeyExpr.and(this.precondition, this.menuOpts.when),
group: this.menuOpts.group, group: this.menuOpts.group,
order: this.menuOpts.order order: this.menuOpts.order
}; };
......
...@@ -13,7 +13,7 @@ import * as editorCommon from 'vs/editor/common/editorCommon'; ...@@ -13,7 +13,7 @@ import * as editorCommon from 'vs/editor/common/editorCommon';
import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey'; import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
import { editorAction, ServicesAccessor, EditorAction, commonEditorContribution } from 'vs/editor/common/editorCommonExtensions'; import { editorAction, ServicesAccessor, EditorAction, commonEditorContribution } from 'vs/editor/common/editorCommonExtensions';
import { OnTypeFormattingEditProviderRegistry, DocumentRangeFormattingEditProviderRegistry } from 'vs/editor/common/modes'; import { OnTypeFormattingEditProviderRegistry, DocumentRangeFormattingEditProviderRegistry } from 'vs/editor/common/modes';
import { getOnTypeFormattingEdits, getDocumentFormattingEdits, getDocumentRangeFormattingEdits } from '../common/format'; import { getOnTypeFormattingEdits, getDocumentFormattingEdits, getDocumentRangeFormattingEdits, NoProviderError } from '../common/format';
import { EditOperationsCommand } from '../common/formatCommand'; import { EditOperationsCommand } from '../common/formatCommand';
import { CommandsRegistry } from 'vs/platform/commands/common/commands'; import { CommandsRegistry } from 'vs/platform/commands/common/commands';
import { ICodeEditorService } from 'vs/editor/common/services/codeEditorService'; import { ICodeEditorService } from 'vs/editor/common/services/codeEditorService';
...@@ -23,6 +23,7 @@ import { Range } from 'vs/editor/common/core/range'; ...@@ -23,6 +23,7 @@ import { Range } from 'vs/editor/common/core/range';
import { alert } from 'vs/base/browser/ui/aria/aria'; import { alert } from 'vs/base/browser/ui/aria/aria';
import { EditorState, CodeEditorStateFlag } from 'vs/editor/common/core/editorState'; import { EditorState, CodeEditorStateFlag } from 'vs/editor/common/core/editorState';
import { EditorContextKeys } from 'vs/editor/common/editorContextKeys'; import { EditorContextKeys } from 'vs/editor/common/editorContextKeys';
import { IMessageService, Severity } from 'vs/platform/message/common/message';
function alertFormattingEdits(edits: editorCommon.ISingleEditOperation[]): void { function alertFormattingEdits(edits: editorCommon.ISingleEditOperation[]): void {
...@@ -263,6 +264,7 @@ export abstract class AbstractFormatAction extends EditorAction { ...@@ -263,6 +264,7 @@ export abstract class AbstractFormatAction extends EditorAction {
public run(accessor: ServicesAccessor, editor: editorCommon.ICommonCodeEditor): TPromise<void> { public run(accessor: ServicesAccessor, editor: editorCommon.ICommonCodeEditor): TPromise<void> {
const workerService = accessor.get(IEditorWorkerService); const workerService = accessor.get(IEditorWorkerService);
const messageService = accessor.get(IMessageService);
const formattingPromise = this._getFormattingEdits(editor); const formattingPromise = this._getFormattingEdits(editor);
if (!formattingPromise) { if (!formattingPromise) {
...@@ -281,6 +283,15 @@ export abstract class AbstractFormatAction extends EditorAction { ...@@ -281,6 +283,15 @@ export abstract class AbstractFormatAction extends EditorAction {
EditOperationsCommand.execute(editor, edits); EditOperationsCommand.execute(editor, edits);
alertFormattingEdits(edits); alertFormattingEdits(edits);
editor.focus(); editor.focus();
}, err => {
if (err instanceof Error && err.name === NoProviderError.Name) {
messageService.show(
Severity.Info,
nls.localize('no.provider', "Sorry, but there is no formatter for '{0}'-files installed.", editor.getModel().getLanguageIdentifier().language),
);
} else {
throw err;
}
}); });
} }
...@@ -296,7 +307,7 @@ export class FormatDocumentAction extends AbstractFormatAction { ...@@ -296,7 +307,7 @@ export class FormatDocumentAction extends AbstractFormatAction {
id: 'editor.action.formatDocument', id: 'editor.action.formatDocument',
label: nls.localize('formatDocument.label', "Format Document"), label: nls.localize('formatDocument.label', "Format Document"),
alias: 'Format Document', alias: 'Format Document',
precondition: ContextKeyExpr.and(EditorContextKeys.writable, EditorContextKeys.hasDocumentFormattingProvider), precondition: EditorContextKeys.writable,
kbOpts: { kbOpts: {
kbExpr: EditorContextKeys.textFocus, kbExpr: EditorContextKeys.textFocus,
primary: KeyMod.Shift | KeyMod.Alt | KeyCode.KEY_F, primary: KeyMod.Shift | KeyMod.Alt | KeyCode.KEY_F,
...@@ -304,6 +315,7 @@ export class FormatDocumentAction extends AbstractFormatAction { ...@@ -304,6 +315,7 @@ export class FormatDocumentAction extends AbstractFormatAction {
linux: { primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KEY_I } linux: { primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KEY_I }
}, },
menuOpts: { menuOpts: {
when: EditorContextKeys.hasDocumentFormattingProvider,
group: '1_modification', group: '1_modification',
order: 1.3 order: 1.3
} }
...@@ -325,12 +337,13 @@ export class FormatSelectionAction extends AbstractFormatAction { ...@@ -325,12 +337,13 @@ export class FormatSelectionAction extends AbstractFormatAction {
id: 'editor.action.formatSelection', id: 'editor.action.formatSelection',
label: nls.localize('formatSelection.label', "Format Selection"), label: nls.localize('formatSelection.label', "Format Selection"),
alias: 'Format Code', alias: 'Format Code',
precondition: ContextKeyExpr.and(EditorContextKeys.writable, EditorContextKeys.hasDocumentSelectionFormattingProvider, EditorContextKeys.hasNonEmptySelection), precondition: ContextKeyExpr.and(EditorContextKeys.writable, EditorContextKeys.hasNonEmptySelection),
kbOpts: { kbOpts: {
kbExpr: EditorContextKeys.textFocus, kbExpr: EditorContextKeys.textFocus,
primary: KeyChord(KeyMod.CtrlCmd | KeyCode.KEY_K, KeyMod.CtrlCmd | KeyCode.KEY_F) primary: KeyChord(KeyMod.CtrlCmd | KeyCode.KEY_K, KeyMod.CtrlCmd | KeyCode.KEY_F)
}, },
menuOpts: { menuOpts: {
when: ContextKeyExpr.and(EditorContextKeys.hasDocumentSelectionFormattingProvider, EditorContextKeys.hasNonEmptySelection),
group: '1_modification', group: '1_modification',
order: 1.31 order: 1.31
} }
......
...@@ -17,12 +17,23 @@ import { IModelService } from 'vs/editor/common/services/modelService'; ...@@ -17,12 +17,23 @@ import { IModelService } from 'vs/editor/common/services/modelService';
import { asWinJsPromise, sequence } from 'vs/base/common/async'; import { asWinJsPromise, sequence } from 'vs/base/common/async';
import { Position } from 'vs/editor/common/core/position'; import { Position } from 'vs/editor/common/core/position';
export function getDocumentRangeFormattingEdits(model: IReadOnlyModel, range: Range, options: FormattingOptions): TPromise<TextEdit[]> { export class NoProviderError extends Error {
static readonly Name = 'NOPRO';
constructor(message?: string) {
super();
this.name = NoProviderError.Name;
this.message = message;
}
}
export function getDocumentRangeFormattingEdits(model: IReadOnlyModel, range: Range, options: FormattingOptions): TPromise<TextEdit[], NoProviderError> {
const providers = DocumentRangeFormattingEditProviderRegistry.ordered(model); const providers = DocumentRangeFormattingEditProviderRegistry.ordered(model);
if (providers.length === 0) { if (providers.length === 0) {
return TPromise.as(undefined); return TPromise.wrapError(new NoProviderError());
} }
let result: TextEdit[]; let result: TextEdit[];
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册