volar-service-prettier 0.0.16 → 0.0.18

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,5 +1,5 @@
1
- import type { Service } from '@volar/language-service';
2
- import { type Options, type ResolveConfigOptions } from 'prettier';
1
+ import type { ServicePlugin } from '@volar/language-service';
2
+ import type { Options, ResolveConfigOptions } from 'prettier';
3
3
  export declare function create(options?: {
4
4
  /**
5
5
  * Languages to be formatted by prettier.
@@ -40,6 +40,5 @@ export declare function create(options?: {
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?: (filePath: string, prettier: typeof import('prettier'), config?: ResolveConfigOptions) => Promise<Options>): Service;
44
- export default create;
43
+ }, getPrettierConfig?: (filePath: string, prettier: typeof import('prettier'), config?: ResolveConfigOptions) => Promise<Options>): ServicePlugin;
45
44
  //# sourceMappingURL=index.d.ts.map
package/out/index.js CHANGED
@@ -4,72 +4,70 @@ exports.create = void 0;
4
4
  function create(options = {}, getPrettierConfig = async (filePath, prettier, config) => {
5
5
  return await prettier.resolveConfig(filePath, config) ?? {};
6
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;
23
- }
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,
7
+ return {
8
+ create(context) {
9
+ let prettier;
10
+ try {
11
+ prettier = options.prettier ?? require('prettier');
12
+ }
13
+ catch (e) {
14
+ throw new Error("Could not load Prettier: " + e);
15
+ }
16
+ const languages = options.languages ?? ['html', 'css', 'scss', 'typescript', 'javascript'];
17
+ return {
18
+ async provideDocumentFormattingEdits(document, _, formatOptions) {
19
+ if (!languages.includes(document.languageId)) {
20
+ return;
35
21
  }
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
- };
22
+ const filePath = context.env.uriToFileName(document.uri);
23
+ const fileInfo = await prettier.getFileInfo(filePath, { ignorePath: '.prettierignore', resolveConfig: false });
24
+ if (fileInfo.ignored) {
25
+ return;
26
+ }
27
+ const filePrettierOptions = await getPrettierConfig(filePath, prettier, options.resolveConfigOptions);
28
+ const editorPrettierOptions = await context.env.getConfiguration?.('prettier', document.uri);
29
+ const ideFormattingOptions = formatOptions !== undefined && options.useIdeOptionsFallback // We need to check for options existing here because some editors might not have it
30
+ ? {
31
+ tabWidth: formatOptions.tabSize,
32
+ useTabs: !formatOptions.insertSpaces,
33
+ }
34
+ : {};
35
+ // Return a config with the following cascade:
36
+ // - Prettier config file should always win if it exists, if it doesn't:
37
+ // - Prettier config from the VS Code extension is used, if it doesn't exist:
38
+ // - Use the editor's basic configuration settings
39
+ const prettierOptions = returnObjectIfHasKeys(filePrettierOptions) || returnObjectIfHasKeys(editorPrettierOptions) || ideFormattingOptions;
40
+ const currentPrettierConfig = {
41
+ ...(options.additionalOptions
42
+ ? await options.additionalOptions(prettierOptions)
43
+ : prettierOptions),
44
+ filepath: filePath,
45
+ };
46
+ if (!options.ignoreIdeOptions) {
47
+ currentPrettierConfig.useTabs = !formatOptions.insertSpaces;
48
+ currentPrettierConfig.tabWidth = formatOptions.tabSize;
49
+ }
50
+ const fullText = document.getText();
51
+ let oldText = fullText;
52
+ const isHTML = document.languageId === "html";
53
+ if (isHTML && options.html?.breakContentsFromTags) {
54
+ oldText = oldText
55
+ .replace(/(<[a-z][^>]*>)([^ \n])/gi, "$1 $2")
56
+ .replace(/([^ \n])(<\/[a-z][a-z0-9\t\n\r -]*>)/gi, "$1 $2");
57
+ }
58
+ return [{
59
+ newText: await prettier.format(oldText, currentPrettierConfig),
60
+ range: {
61
+ start: document.positionAt(0),
62
+ end: document.positionAt(fullText.length),
63
+ },
64
+ }];
65
+ },
66
+ };
67
+ },
69
68
  };
70
69
  }
71
70
  exports.create = create;
72
- exports.default = create;
73
71
  function returnObjectIfHasKeys(obj) {
74
72
  if (Object.keys(obj || {}).length > 0) {
75
73
  return obj;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "volar-service-prettier",
3
- "version": "0.0.16",
3
+ "version": "0.0.18",
4
4
  "description": "Integrate Prettier into Volar",
5
5
  "homepage": "https://github.com/volarjs/services/tree/master/packages/prettier",
6
6
  "bugs": "https://github.com/volarjs/services/issues",
@@ -30,7 +30,7 @@
30
30
  "prettier": "^3.0.3"
31
31
  },
32
32
  "peerDependencies": {
33
- "@volar/language-service": "~1.10.0",
33
+ "@volar/language-service": "2.0.0-alpha.0",
34
34
  "prettier": "^2.2 || ^3.0"
35
35
  },
36
36
  "peerDependenciesMeta": {
@@ -41,5 +41,5 @@
41
41
  "optional": true
42
42
  }
43
43
  },
44
- "gitHead": "fdcd47eb5dc865999ac7ac610f8e0dfb458aca04"
44
+ "gitHead": "8dedd3c6e73740fc7c1fe06df32b727ca504adc0"
45
45
  }