Skip to content

Commit

Permalink
Fix #2840
Browse files Browse the repository at this point in the history
  • Loading branch information
yoyo930021 committed Jun 4, 2021
1 parent 8518971 commit 3927d43
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 31 deletions.
3 changes: 1 addition & 2 deletions server/src/modes/pug/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
},
Expand Down
10 changes: 9 additions & 1 deletion server/src/modes/script/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
9 changes: 2 additions & 7 deletions server/src/modes/style/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
},
Expand Down
2 changes: 1 addition & 1 deletion server/src/modes/template/services/htmlFormat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
58 changes: 38 additions & 20 deletions server/src/utils/prettier/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);
Expand All @@ -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
});
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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];
Expand All @@ -138,28 +140,44 @@ 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;
} else {
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));
Expand Down

0 comments on commit 3927d43

Please sign in to comment.