volar-service-prettier 0.0.11 → 0.0.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/out/index.d.ts CHANGED
@@ -1,44 +1,44 @@
1
1
  import type { Service } from '@volar/language-service';
2
2
  import { type Options, type ResolveConfigOptions } from 'prettier';
3
- declare const _default: (options?: {
3
+ export declare function create(options?: {
4
4
  /**
5
5
  * Languages to be formatted by prettier.
6
6
  *
7
7
  * @default
8
8
  * ['html', 'css', 'scss', 'typescript', 'javascript']
9
9
  */
10
- languages?: string[] | undefined;
10
+ languages?: string[];
11
11
  html?: {
12
12
  /**
13
13
  * Preprocessing to break "contents" from "HTML tags".
14
14
  * This will prevent HTML closing tags, and opening tags without attributes
15
15
  * from breaking into a blank `>` or `<` on a new line.
16
16
  */
17
- breakContentsFromTags?: boolean | undefined;
18
- } | undefined;
17
+ breakContentsFromTags?: boolean;
18
+ };
19
19
  /**
20
20
  * Do not use settings from VSCode's `editor.tabSize` and temporary tabSize on status bar
21
21
  *
22
22
  * @see https://github.com/volarjs/services/issues/5
23
23
  */
24
- ignoreIdeOptions?: boolean | undefined;
24
+ ignoreIdeOptions?: boolean;
25
25
  /**
26
26
  * Determine if IDE options should be used as a fallback if there's no Prettier specific settings in the workspace
27
27
  */
28
- useIdeOptionsFallback?: boolean | undefined;
28
+ useIdeOptionsFallback?: boolean;
29
29
  /**
30
30
  * Additional options to pass to Prettier
31
31
  * This is useful, for instance, to add specific plugins you need.
32
32
  */
33
- additionalOptions?: ((resolvedConfig: Options) => Options | Promise<Options>) | undefined;
33
+ additionalOptions?: (resolvedConfig: Options) => Options | Promise<Options>;
34
34
  /**
35
35
  * Options to use when resolving the Prettier config
36
36
  */
37
- resolveConfigOptions?: ResolveConfigOptions | undefined;
37
+ resolveConfigOptions?: ResolveConfigOptions;
38
38
  /**
39
39
  * Prettier instance to use. If undefined, Prettier will be imported through a normal `import('prettier')`.
40
40
  * This property is useful whenever you want to load a specific instance of Prettier (for instance, loading the Prettier version installed in the user's project)
41
41
  */
42
42
  prettier?: typeof import('prettier') | undefined;
43
- }, getPrettierConfig?: (prettier: typeof import('prettier'), config?: ResolveConfigOptions) => Promise<Options>) => Service;
44
- export default _default;
43
+ }, getPrettierConfig?: (filePath: string, prettier: typeof import('prettier'), config?: ResolveConfigOptions) => Promise<Options>): Service;
44
+ export default create;
package/out/index.js CHANGED
@@ -1,71 +1,75 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.default = (options = {}, getPrettierConfig = async (prettier, config) => {
4
- const configFile = await prettier.resolveConfigFile();
5
- if (configFile) {
6
- return await prettier.resolveConfig(configFile, config) ?? {};
7
- }
8
- return {};
9
- }) => (context) => {
10
- if (!context) {
11
- return {};
12
- }
13
- let prettier;
14
- try {
15
- prettier = options.prettier ?? require('prettier');
16
- }
17
- catch (e) {
18
- throw new Error("Could not load Prettier: " + e);
19
- }
20
- const languages = options.languages ?? ['html', 'css', 'scss', 'typescript', 'javascript'];
21
- return {
22
- async provideDocumentFormattingEdits(document, _, formatOptions) {
23
- if (!languages.includes(document.languageId)) {
24
- return;
25
- }
26
- const filePrettierOptions = await getPrettierConfig(prettier, options.resolveConfigOptions);
27
- const fileInfo = await prettier.getFileInfo(context.env.uriToFileName(document.uri), { ignorePath: '.prettierignore' });
28
- if (fileInfo.ignored) {
29
- return;
30
- }
31
- const editorPrettierOptions = await context.env.getConfiguration?.('prettier', document.uri);
32
- const ideFormattingOptions = formatOptions !== undefined && options.useIdeOptionsFallback // We need to check for options existing here because some editors might not have it
33
- ? {
34
- tabWidth: formatOptions.tabSize,
35
- useTabs: !formatOptions.insertSpaces,
3
+ exports.create = void 0;
4
+ function create(options = {}, getPrettierConfig = async (filePath, prettier, config) => {
5
+ return await prettier.resolveConfig(filePath, config) ?? {};
6
+ }) {
7
+ return (context) => {
8
+ if (!context) {
9
+ return {};
10
+ }
11
+ let prettier;
12
+ try {
13
+ prettier = options.prettier ?? require('prettier');
14
+ }
15
+ catch (e) {
16
+ throw new Error("Could not load Prettier: " + e);
17
+ }
18
+ const languages = options.languages ?? ['html', 'css', 'scss', 'typescript', 'javascript'];
19
+ return {
20
+ async provideDocumentFormattingEdits(document, _, formatOptions) {
21
+ if (!languages.includes(document.languageId)) {
22
+ return;
36
23
  }
37
- : {};
38
- const fullText = document.getText();
39
- let oldText = fullText;
40
- const isHTML = document.languageId === 'html';
41
- if (isHTML && options.html?.breakContentsFromTags) {
42
- oldText = oldText
43
- .replace(/(<[a-z][^>]*>)([^ \n])/gi, '$1 $2')
44
- .replace(/([^ \n])(<\/[a-z][a-z0-9\t\n\r -]*>)/gi, '$1 $2');
45
- }
46
- // Return a config with the following cascade:
47
- // - Prettier config file should always win if it exists, if it doesn't:
48
- // - Prettier config from the VS Code extension is used, if it doesn't exist:
49
- // - Use the editor's basic configuration settings
50
- const prettierOptions = returnObjectIfHasKeys(filePrettierOptions) || returnObjectIfHasKeys(editorPrettierOptions) || ideFormattingOptions;
51
- const currentPrettierConfig = {
52
- ...options.additionalOptions ? await options.additionalOptions(prettierOptions) : prettierOptions,
53
- filepath: context.env.uriToFileName(document.uri),
54
- };
55
- if (!options.ignoreIdeOptions) {
56
- currentPrettierConfig.useTabs = !formatOptions.insertSpaces;
57
- currentPrettierConfig.tabWidth = formatOptions.tabSize;
58
- }
59
- return [{
60
- newText: await prettier.format(oldText, currentPrettierConfig),
61
- range: {
62
- start: document.positionAt(0),
63
- end: document.positionAt(fullText.length),
64
- },
65
- }];
66
- },
24
+ const filePath = context.env.uriToFileName(document.uri);
25
+ const fileInfo = await prettier.getFileInfo(filePath, { ignorePath: '.prettierignore', resolveConfig: false });
26
+ if (fileInfo.ignored) {
27
+ return;
28
+ }
29
+ const filePrettierOptions = await getPrettierConfig(filePath, prettier, options.resolveConfigOptions);
30
+ const editorPrettierOptions = await context.env.getConfiguration?.('prettier', document.uri);
31
+ const ideFormattingOptions = formatOptions !== undefined && options.useIdeOptionsFallback // We need to check for options existing here because some editors might not have it
32
+ ? {
33
+ tabWidth: formatOptions.tabSize,
34
+ useTabs: !formatOptions.insertSpaces,
35
+ }
36
+ : {};
37
+ // Return a config with the following cascade:
38
+ // - Prettier config file should always win if it exists, if it doesn't:
39
+ // - Prettier config from the VS Code extension is used, if it doesn't exist:
40
+ // - Use the editor's basic configuration settings
41
+ const prettierOptions = returnObjectIfHasKeys(filePrettierOptions) || returnObjectIfHasKeys(editorPrettierOptions) || ideFormattingOptions;
42
+ const currentPrettierConfig = {
43
+ ...(options.additionalOptions
44
+ ? await options.additionalOptions(prettierOptions)
45
+ : prettierOptions),
46
+ filepath: filePath,
47
+ };
48
+ if (!options.ignoreIdeOptions) {
49
+ currentPrettierConfig.useTabs = !formatOptions.insertSpaces;
50
+ currentPrettierConfig.tabWidth = formatOptions.tabSize;
51
+ }
52
+ const fullText = document.getText();
53
+ let oldText = fullText;
54
+ const isHTML = document.languageId === "html";
55
+ if (isHTML && options.html?.breakContentsFromTags) {
56
+ oldText = oldText
57
+ .replace(/(<[a-z][^>]*>)([^ \n])/gi, "$1 $2")
58
+ .replace(/([^ \n])(<\/[a-z][a-z0-9\t\n\r -]*>)/gi, "$1 $2");
59
+ }
60
+ return [{
61
+ newText: await prettier.format(oldText, currentPrettierConfig),
62
+ range: {
63
+ start: document.positionAt(0),
64
+ end: document.positionAt(fullText.length),
65
+ },
66
+ }];
67
+ },
68
+ };
67
69
  };
68
- };
70
+ }
71
+ exports.create = create;
72
+ exports.default = create;
69
73
  function returnObjectIfHasKeys(obj) {
70
74
  if (Object.keys(obj || {}).length > 0) {
71
75
  return obj;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "volar-service-prettier",
3
- "version": "0.0.11",
3
+ "version": "0.0.13",
4
4
  "main": "out/index.js",
5
5
  "types": "out/index.d.ts",
6
6
  "license": "MIT",
@@ -33,5 +33,5 @@
33
33
  "optional": true
34
34
  }
35
35
  },
36
- "gitHead": "5be7c0c4fc41f4ebe3d8f3d272d3a0d8377973ad"
36
+ "gitHead": "eeaa0ff3504c0ec251c8ad9cb2847f89a2c58865"
37
37
  }