mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-19 08:08:39 +01:00
@@ -142,9 +142,7 @@ export default class FileConfigurationManager extends Disposable {
|
||||
document: vscode.TextDocument,
|
||||
options: FormattingOptions
|
||||
): Proto.FormatCodeSettings {
|
||||
const config = vscode.workspace.getConfiguration(
|
||||
isTypeScriptDocument(document) ? 'typescript.format' : 'javascript.format',
|
||||
document.uri);
|
||||
const fallbackSection = isTypeScriptDocument(document) ? 'typescript' : 'javascript';
|
||||
|
||||
return {
|
||||
tabSize: options.tabSize,
|
||||
@@ -152,24 +150,24 @@ export default class FileConfigurationManager extends Disposable {
|
||||
convertTabsToSpaces: options.insertSpaces,
|
||||
// We can use \n here since the editor normalizes later on to its line endings.
|
||||
newLineCharacter: '\n',
|
||||
insertSpaceAfterCommaDelimiter: config.get<boolean>('insertSpaceAfterCommaDelimiter'),
|
||||
insertSpaceAfterConstructor: config.get<boolean>('insertSpaceAfterConstructor'),
|
||||
insertSpaceAfterSemicolonInForStatements: config.get<boolean>('insertSpaceAfterSemicolonInForStatements'),
|
||||
insertSpaceBeforeAndAfterBinaryOperators: config.get<boolean>('insertSpaceBeforeAndAfterBinaryOperators'),
|
||||
insertSpaceAfterKeywordsInControlFlowStatements: config.get<boolean>('insertSpaceAfterKeywordsInControlFlowStatements'),
|
||||
insertSpaceAfterFunctionKeywordForAnonymousFunctions: config.get<boolean>('insertSpaceAfterFunctionKeywordForAnonymousFunctions'),
|
||||
insertSpaceBeforeFunctionParenthesis: config.get<boolean>('insertSpaceBeforeFunctionParenthesis'),
|
||||
insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: config.get<boolean>('insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis'),
|
||||
insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: config.get<boolean>('insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets'),
|
||||
insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces: config.get<boolean>('insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces'),
|
||||
insertSpaceAfterOpeningAndBeforeClosingEmptyBraces: config.get<boolean>('insertSpaceAfterOpeningAndBeforeClosingEmptyBraces'),
|
||||
insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: config.get<boolean>('insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces'),
|
||||
insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces: config.get<boolean>('insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces'),
|
||||
insertSpaceAfterTypeAssertion: config.get<boolean>('insertSpaceAfterTypeAssertion'),
|
||||
placeOpenBraceOnNewLineForFunctions: config.get<boolean>('placeOpenBraceOnNewLineForFunctions'),
|
||||
placeOpenBraceOnNewLineForControlBlocks: config.get<boolean>('placeOpenBraceOnNewLineForControlBlocks'),
|
||||
semicolons: config.get<Proto.SemicolonPreference>('semicolons'),
|
||||
indentSwitchCase: config.get<boolean>('indentSwitchCase'),
|
||||
insertSpaceAfterCommaDelimiter: readUnifiedConfig<boolean>('format.insertSpaceAfterCommaDelimiter', true, { scope: document.uri, fallbackSection }),
|
||||
insertSpaceAfterConstructor: readUnifiedConfig<boolean>('format.insertSpaceAfterConstructor', false, { scope: document.uri, fallbackSection }),
|
||||
insertSpaceAfterSemicolonInForStatements: readUnifiedConfig<boolean>('format.insertSpaceAfterSemicolonInForStatements', true, { scope: document.uri, fallbackSection }),
|
||||
insertSpaceBeforeAndAfterBinaryOperators: readUnifiedConfig<boolean>('format.insertSpaceBeforeAndAfterBinaryOperators', true, { scope: document.uri, fallbackSection }),
|
||||
insertSpaceAfterKeywordsInControlFlowStatements: readUnifiedConfig<boolean>('format.insertSpaceAfterKeywordsInControlFlowStatements', true, { scope: document.uri, fallbackSection }),
|
||||
insertSpaceAfterFunctionKeywordForAnonymousFunctions: readUnifiedConfig<boolean>('format.insertSpaceAfterFunctionKeywordForAnonymousFunctions', true, { scope: document.uri, fallbackSection }),
|
||||
insertSpaceBeforeFunctionParenthesis: readUnifiedConfig<boolean>('format.insertSpaceBeforeFunctionParenthesis', false, { scope: document.uri, fallbackSection }),
|
||||
insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: readUnifiedConfig<boolean>('format.insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis', false, { scope: document.uri, fallbackSection }),
|
||||
insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: readUnifiedConfig<boolean>('format.insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets', false, { scope: document.uri, fallbackSection }),
|
||||
insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces: readUnifiedConfig<boolean>('format.insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces', true, { scope: document.uri, fallbackSection }),
|
||||
insertSpaceAfterOpeningAndBeforeClosingEmptyBraces: readUnifiedConfig<boolean>('format.insertSpaceAfterOpeningAndBeforeClosingEmptyBraces', true, { scope: document.uri, fallbackSection }),
|
||||
insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: readUnifiedConfig<boolean>('format.insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces', false, { scope: document.uri, fallbackSection }),
|
||||
insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces: readUnifiedConfig<boolean>('format.insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces', false, { scope: document.uri, fallbackSection }),
|
||||
insertSpaceAfterTypeAssertion: readUnifiedConfig<boolean>('format.insertSpaceAfterTypeAssertion', false, { scope: document.uri, fallbackSection }),
|
||||
placeOpenBraceOnNewLineForFunctions: readUnifiedConfig<boolean>('format.placeOpenBraceOnNewLineForFunctions', false, { scope: document.uri, fallbackSection }),
|
||||
placeOpenBraceOnNewLineForControlBlocks: readUnifiedConfig<boolean>('format.placeOpenBraceOnNewLineForControlBlocks', false, { scope: document.uri, fallbackSection }),
|
||||
semicolons: readUnifiedConfig<Proto.SemicolonPreference>('format.semicolons', 'ignore' as Proto.SemicolonPreference, { scope: document.uri, fallbackSection }),
|
||||
indentSwitchCase: readUnifiedConfig<boolean>('format.indentSwitchCase', true, { scope: document.uri, fallbackSection }),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -9,12 +9,14 @@ import { LanguageDescription } from '../configuration/languageDescription';
|
||||
import type * as Proto from '../tsServer/protocol/protocol';
|
||||
import * as typeConverters from '../typeConverters';
|
||||
import { ITypeScriptServiceClient } from '../typescriptService';
|
||||
import { readUnifiedConfig } from '../utils/configuration';
|
||||
import FileConfigurationManager from './fileConfigurationManager';
|
||||
import { conditionalRegistration, requireGlobalConfiguration } from './util/dependentRegistration';
|
||||
import { conditionalRegistration, requireHasModifiedUnifiedConfig } from './util/dependentRegistration';
|
||||
|
||||
class TypeScriptFormattingProvider implements vscode.DocumentRangeFormattingEditProvider, vscode.OnTypeFormattingEditProvider {
|
||||
public constructor(
|
||||
private readonly client: ITypeScriptServiceClient,
|
||||
private readonly language: LanguageDescription,
|
||||
private readonly fileConfigurationManager: FileConfigurationManager
|
||||
) { }
|
||||
|
||||
@@ -24,6 +26,10 @@ class TypeScriptFormattingProvider implements vscode.DocumentRangeFormattingEdit
|
||||
options: vscode.FormattingOptions,
|
||||
token: vscode.CancellationToken
|
||||
): Promise<vscode.TextEdit[] | undefined> {
|
||||
if (!this.isEnabled(document)) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const file = this.client.toOpenTsFilePath(document);
|
||||
if (!file) {
|
||||
return undefined;
|
||||
@@ -46,10 +52,14 @@ class TypeScriptFormattingProvider implements vscode.DocumentRangeFormattingEdit
|
||||
ch: string,
|
||||
options: vscode.FormattingOptions,
|
||||
token: vscode.CancellationToken
|
||||
): Promise<vscode.TextEdit[]> {
|
||||
): Promise<vscode.TextEdit[] | undefined> {
|
||||
if (!this.isEnabled(document)) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const file = this.client.toOpenTsFilePath(document);
|
||||
if (!file) {
|
||||
return [];
|
||||
return undefined;
|
||||
}
|
||||
|
||||
await this.fileConfigurationManager.ensureConfigurationOptions(document, options, token);
|
||||
@@ -83,6 +93,10 @@ class TypeScriptFormattingProvider implements vscode.DocumentRangeFormattingEdit
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private isEnabled(document: vscode.TextDocument): boolean {
|
||||
return readUnifiedConfig<boolean>('format.enable', true, { scope: document, fallbackSection: this.language.id });
|
||||
}
|
||||
}
|
||||
|
||||
export function register(
|
||||
@@ -92,9 +106,9 @@ export function register(
|
||||
fileConfigurationManager: FileConfigurationManager
|
||||
) {
|
||||
return conditionalRegistration([
|
||||
requireGlobalConfiguration(language.id, 'format.enable'),
|
||||
requireHasModifiedUnifiedConfig('format.enable', language.id),
|
||||
], () => {
|
||||
const formattingProvider = new TypeScriptFormattingProvider(client, fileConfigurationManager);
|
||||
const formattingProvider = new TypeScriptFormattingProvider(client, language, fileConfigurationManager);
|
||||
return vscode.Disposable.from(
|
||||
vscode.languages.registerOnTypeFormattingEditProvider(selector.syntax, formattingProvider, ';', '}', '\n'),
|
||||
vscode.languages.registerDocumentRangeFormattingEditProvider(selector.syntax, formattingProvider),
|
||||
|
||||
Reference in New Issue
Block a user