From 3927d43c160718556cc25f1a88bccee93850c01c Mon Sep 17 00:00:00 2001 From: yoyo930021 Date: Fri, 4 Jun 2021 17:03:33 +0800 Subject: [PATCH] Fix #2840 --- server/src/modes/pug/index.ts | 3 +- server/src/modes/script/javascript.ts | 10 +++- server/src/modes/style/index.ts | 9 +-- .../src/modes/template/services/htmlFormat.ts | 2 +- server/src/utils/prettier/index.ts | 58 ++++++++++++------- 5 files changed, 51 insertions(+), 31 deletions(-) diff --git a/server/src/modes/pug/index.ts b/server/src/modes/pug/index.ts index 03761832bd..2464cf1d51 100644 --- a/server/src/modes/pug/index.ts +++ b/server/src/modes/pug/index.ts @@ -23,10 +23,9 @@ export function getPugMode(env: EnvironmentService, dependencyService: Dependenc dependencyService, value, getFileFsPath(document.uri), + 'pug', range, env.getConfig().vetur.format as VLSFormatConfig, - // @ts-expect-error - 'pug', false ); }, diff --git a/server/src/modes/script/javascript.ts b/server/src/modes/script/javascript.ts index 09edcc929b..9f46023a20 100644 --- a/server/src/modes/script/javascript.ts +++ b/server/src/modes/script/javascript.ts @@ -720,7 +720,15 @@ export async function getJavascriptMode( } else { doFormat = prettierify; } - return doFormat(dependencyService, code, filePath, range, vlsFormatConfig, parser, needInitialIndent); + return doFormat( + dependencyService, + code, + filePath, + scriptDoc.languageId, + range, + vlsFormatConfig, + needInitialIndent + ); } else { const initialIndentLevel = needInitialIndent ? 1 : 0; const formatSettings: ts.FormatCodeSettings = diff --git a/server/src/modes/style/index.ts b/server/src/modes/style/index.ts index 2ea4dce660..e0bbafebbf 100644 --- a/server/src/modes/style/index.ts +++ b/server/src/modes/style/index.ts @@ -173,19 +173,14 @@ function getStyleMode( const { value, range } = getValueAndRange(document, currRange); const needIndent = env.getConfig().vetur.format.styleInitialIndent; - const parserMap: { [k: string]: BuiltInParserName } = { - css: 'css', - postcss: 'css', - scss: 'scss', - less: 'less' - }; + return prettierify( dependencyService, value, getFileFsPath(document.uri), + languageId, range, env.getConfig().vetur.format as VLSFormatConfig, - parserMap[languageId], needIndent ); }, diff --git a/server/src/modes/template/services/htmlFormat.ts b/server/src/modes/template/services/htmlFormat.ts index c7e97844ee..5668d1e7e7 100644 --- a/server/src/modes/template/services/htmlFormat.ts +++ b/server/src/modes/template/services/htmlFormat.ts @@ -101,7 +101,7 @@ function formatWithPrettier( vlsFormatConfig: VLSFormatConfig, initialIndent: boolean ) { - return prettierify(dependencyService, code, fileFsPath, range, vlsFormatConfig, 'vue', initialIndent); + return prettierify(dependencyService, code, fileFsPath, 'vue', range, vlsFormatConfig, initialIndent); } function getPrettyHtmlOptions( diff --git a/server/src/utils/prettier/index.ts b/server/src/utils/prettier/index.ts index 849542630b..5c88723c98 100644 --- a/server/src/utils/prettier/index.ts +++ b/server/src/utils/prettier/index.ts @@ -1,6 +1,6 @@ import { TextEdit, Range } from 'vscode-languageserver-types'; -import type { BuiltInParserName, CustomParser } from 'prettier'; +import type { BuiltInParserName, CustomParser, Options as PrettierOptions } from 'prettier'; import { indentSection } from '../strings'; import { VLSFormatConfig } from '../../config'; @@ -16,14 +16,14 @@ export function prettierify( dependencyService: DependencyService, code: string, fileFsPath: string, + languageId: string, range: Range, vlsFormatConfig: VLSFormatConfig, - parser: PrettierParserOption, initialIndent: boolean ): TextEdit[] { try { const prettier = dependencyService.get('prettier', fileFsPath).module; - const prettierOptions = getPrettierOptions(dependencyService, prettier, fileFsPath, parser, vlsFormatConfig); + const prettierOptions = getPrettierOptions(dependencyService, prettier, fileFsPath, languageId, vlsFormatConfig); logger.logDebug(`Using prettier. Options\n${JSON.stringify(prettierOptions)}`); const prettierifiedCode = prettier.format(code, prettierOptions); @@ -43,21 +43,23 @@ export function prettierEslintify( dependencyService: DependencyService, code: string, fileFsPath: string, + languageId: string, range: Range, vlsFormatConfig: VLSFormatConfig, - parser: PrettierParserOption, initialIndent: boolean ): TextEdit[] { try { const prettier = dependencyService.get('prettier', fileFsPath).module; const prettierEslint = dependencyService.get('prettier-eslint', fileFsPath).module; - const prettierOptions = getPrettierOptions(dependencyService, prettier, fileFsPath, parser, vlsFormatConfig); + const prettierOptions = getPrettierOptions(dependencyService, prettier, fileFsPath, languageId, vlsFormatConfig); logger.logDebug(`Using prettier-eslint. Options\n${JSON.stringify(prettierOptions)}`); + const ext = languageId === 'javascript' ? '.js' : '.ts'; + const prettierifiedCode = prettierEslint({ - filePath: fileFsPath, - prettierOptions: { parser }, + filePath: fileFsPath + ext, + prettierOptions: { parser: prettierOptions.parser }, text: code, fallbackPrettierOptions: prettierOptions }); @@ -76,20 +78,20 @@ export function prettierTslintify( dependencyService: DependencyService, code: string, fileFsPath: string, + languageId: string, range: Range, vlsFormatConfig: VLSFormatConfig, - parser: PrettierParserOption, initialIndent: boolean ): TextEdit[] { try { const prettier = dependencyService.get('prettier', fileFsPath).module; const prettierTslint = dependencyService.get('prettier-tslint', fileFsPath).module.format; - const prettierOptions = getPrettierOptions(dependencyService, prettier, fileFsPath, parser, vlsFormatConfig); + const prettierOptions = getPrettierOptions(dependencyService, prettier, fileFsPath, languageId, vlsFormatConfig); logger.logDebug(`Using prettier-tslint. Options\n${JSON.stringify(prettierOptions)}`); const prettierifiedCode = prettierTslint({ - prettierOptions: { parser }, + prettierOptions: { parser: prettierOptions.parser }, text: code, filePath: fileFsPath, fallbackPrettierOptions: prettierOptions @@ -107,9 +109,9 @@ export function prettierPluginPugify( dependencyService: DependencyService, code: string, fileFsPath: string, + languageId: string, range: Range, vlsFormatConfig: VLSFormatConfig, - parser: PrettierParserOption, initialIndent: boolean ): TextEdit[] { try { @@ -118,8 +120,8 @@ export function prettierPluginPugify( prettier = dependencyService.getBundled('prettier').module; } const prettierPluginPug = dependencyService.get('@prettier/plugin-pug', fileFsPath).module; - const prettierOptions = getPrettierOptions(dependencyService, prettier, fileFsPath, parser, vlsFormatConfig); - prettierOptions.pluginSearchDirs = []; + const prettierOptions = getPrettierOptions(dependencyService, prettier, fileFsPath, languageId, vlsFormatConfig); + (prettierOptions as { pluginSearchDirs: string[] }).pluginSearchDirs = []; prettierOptions.plugins = Array.isArray(prettierOptions.plugins) ? [...prettierOptions.plugins, prettierPluginPug] : [prettierPluginPug]; @@ -138,20 +140,36 @@ function getPrettierOptions( dependencyService: DependencyService, prettierModule: RuntimeLibrary['prettier'], fileFsPath: string, - parser: PrettierParserOption, + languageId: string, vlsFormatConfig: VLSFormatConfig -) { +): PrettierOptions { const prettierrcOptions = prettierModule.resolveConfig.sync(fileFsPath, { useCache: false }); + const getParser = () => { + const table = { + javascript: 'babel', + typescript: 'pug', + pug: 'pug', + vue: 'vue', + css: 'css', + postcss: 'css', + scss: 'scss', + less: 'less' + }; + return table?.[languageId as 'javascript'] ?? 'babel'; + }; + if (prettierrcOptions) { prettierrcOptions.tabWidth = prettierrcOptions.tabWidth || vlsFormatConfig.options.tabSize; prettierrcOptions.useTabs = prettierrcOptions.useTabs || vlsFormatConfig.options.useTabs; - prettierrcOptions.parser = parser; + prettierrcOptions.parser = getParser(); if (dependencyService.useWorkspaceDependencies) { // For loading plugins such as @prettier/plugin-pug - (prettierrcOptions as { - pluginSearchDirs: string[]; - }).pluginSearchDirs = dependencyService.nodeModulesPaths.map(el => path.dirname(el)); + ( + prettierrcOptions as { + pluginSearchDirs: string[]; + } + ).pluginSearchDirs = dependencyService.nodeModulesPaths.map(el => path.dirname(el)); } return prettierrcOptions; @@ -159,7 +177,7 @@ function getPrettierOptions( const vscodePrettierOptions = vlsFormatConfig.defaultFormatterOptions.prettier || {}; vscodePrettierOptions.tabWidth = vscodePrettierOptions.tabWidth || vlsFormatConfig.options.tabSize; vscodePrettierOptions.useTabs = vscodePrettierOptions.useTabs || vlsFormatConfig.options.useTabs; - vscodePrettierOptions.parser = parser; + vscodePrettierOptions.parser = getParser(); if (dependencyService.useWorkspaceDependencies) { // For loading plugins such as @prettier/plugin-pug vscodePrettierOptions.pluginSearchDirs = dependencyService.nodeModulesPaths.map(el => path.dirname(el));