volar-service-prettier 0.0.30 → 0.0.31-patch.2

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/index.d.ts CHANGED
@@ -1,13 +1,10 @@
1
- import type { ServicePlugin, ServiceEnvironment } from '@volar/language-service';
2
- import type { Options, ResolveConfigOptions } from 'prettier';
3
- export declare function create(options?: {
4
- /**
5
- * Languages to be formatted by prettier.
6
- *
7
- * @default
8
- * ['html', 'css', 'scss', 'typescript', 'javascript']
9
- */
10
- languages?: string[];
1
+ import type { DocumentSelector, FormattingOptions, Result, ServiceContext, ServicePlugin, TextDocument } from '@volar/language-service';
2
+ import type { Options } from 'prettier';
3
+ export declare function create(
4
+ /**
5
+ * Prettier instance or getter to use.
6
+ */
7
+ prettierInstanceOrGetter: typeof import('prettier') | ((context: ServiceContext) => typeof import('prettier') | undefined), { html, documentSelector, isFormattingEnabled, getFormattingOptions, }?: {
11
8
  html?: {
12
9
  /**
13
10
  * Preprocessing to break "contents" from "HTML tags".
@@ -17,29 +14,13 @@ export declare function create(options?: {
17
14
  breakContentsFromTags?: boolean;
18
15
  };
19
16
  /**
20
- * Do not use settings from VSCode's `editor.tabSize` and temporary tabSize on status bar
17
+ * Languages to be formatted by prettier.
21
18
  *
22
- * @see https://github.com/volarjs/services/issues/5
23
- */
24
- ignoreIdeOptions?: boolean;
25
- /**
26
- * Determine if IDE options should be used as a fallback if there's no Prettier specific settings in the workspace
27
- */
28
- useIdeOptionsFallback?: boolean;
29
- /**
30
- * Additional options to pass to Prettier
31
- * This is useful, for instance, to add specific plugins you need.
32
- */
33
- additionalOptions?: (resolvedConfig: Options) => Options | Promise<Options>;
34
- /**
35
- * Options to use when resolving the Prettier config
36
- */
37
- resolveConfigOptions?: ResolveConfigOptions;
38
- /**
39
- * Prettier instance to use. If undefined, Prettier will be imported through a normal `import('prettier')`.
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)
19
+ * @default
20
+ * ['html', 'css', 'scss', 'typescript', 'javascript']
41
21
  */
42
- prettier?: typeof import('prettier') | undefined;
43
- getPrettier?: (serviceEnv: ServiceEnvironment) => typeof import('prettier') | undefined;
44
- }, getPrettierConfig?: (filePath: string, prettier: typeof import('prettier'), config?: ResolveConfigOptions) => Promise<Options>): ServicePlugin;
22
+ documentSelector?: DocumentSelector;
23
+ isFormattingEnabled?(prettier: typeof import('prettier'), document: TextDocument, context: ServiceContext): Result<boolean>;
24
+ getFormattingOptions?(prettier: typeof import('prettier'), document: TextDocument, formatOptions: FormattingOptions, context: ServiceContext): Result<Options>;
25
+ }): ServicePlugin;
45
26
  //# sourceMappingURL=index.d.ts.map
package/index.js CHANGED
@@ -2,65 +2,65 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.create = void 0;
4
4
  const vscode_uri_1 = require("vscode-uri");
5
- function create(options = {}, getPrettierConfig = async (filePath, prettier, config) => {
6
- return await prettier.resolveConfig(filePath, config) ?? {};
7
- }) {
5
+ function create(
6
+ /**
7
+ * Prettier instance or getter to use.
8
+ */
9
+ prettierInstanceOrGetter, { html, documentSelector = ['html', 'css', 'scss', 'typescript', 'javascript'], isFormattingEnabled = async (prettier, document) => {
10
+ const uri = vscode_uri_1.URI.parse(document.uri);
11
+ if (uri.scheme === 'file') {
12
+ try {
13
+ const fileInfo = await prettier.getFileInfo(uri.fsPath, { ignorePath: '.prettierignore', resolveConfig: false });
14
+ if (fileInfo.ignored) {
15
+ return false;
16
+ }
17
+ }
18
+ catch (err) {
19
+ console.warn(err);
20
+ }
21
+ }
22
+ return true;
23
+ }, getFormattingOptions = async (prettier, document, formatOptions, context) => {
24
+ const filepath = vscode_uri_1.URI.parse(document.uri).fsPath;
25
+ const configOptions = await prettier.resolveConfig(filepath);
26
+ const editorOptions = await context.env.getConfiguration?.('prettier', document.uri);
27
+ return {
28
+ filepath,
29
+ tabWidth: formatOptions.tabSize,
30
+ useTabs: !formatOptions.insertSpaces,
31
+ ...editorOptions,
32
+ ...configOptions,
33
+ };
34
+ }, } = {}) {
8
35
  return {
9
36
  name: 'prettier',
10
37
  create(context) {
11
- let prettier;
12
- try {
13
- prettier = options.prettier
14
- ?? options.getPrettier?.(context.env)
15
- ?? require('prettier');
16
- }
17
- catch (e) {
18
- throw new Error("Could not load Prettier: " + e);
38
+ const prettier = typeof prettierInstanceOrGetter === 'function'
39
+ ? prettierInstanceOrGetter(context)
40
+ : prettierInstanceOrGetter;
41
+ if (!prettier) {
42
+ return {};
19
43
  }
20
- const languages = options.languages ?? ['html', 'css', 'scss', 'typescript', 'javascript'];
21
44
  return {
22
45
  async provideDocumentFormattingEdits(document, _, formatOptions) {
23
- if (!languages.includes(document.languageId)) {
46
+ if (!matchDocument(documentSelector, document)) {
24
47
  return;
25
48
  }
26
- const filePath = vscode_uri_1.URI.parse(document.uri).fsPath;
27
- const fileInfo = await prettier.getFileInfo(filePath, { ignorePath: '.prettierignore', resolveConfig: false });
28
- if (fileInfo.ignored) {
49
+ if (!isFormattingEnabled(prettier, document, context)) {
29
50
  return;
30
51
  }
31
- const filePrettierOptions = await getPrettierConfig(filePath, prettier, options.resolveConfigOptions);
32
- const editorPrettierOptions = await context.env.getConfiguration?.('prettier', document.uri);
33
- const ideFormattingOptions = formatOptions !== undefined && options.useIdeOptionsFallback // We need to check for options existing here because some editors might not have it
34
- ? {
35
- tabWidth: formatOptions.tabSize,
36
- useTabs: !formatOptions.insertSpaces,
37
- }
38
- : {};
39
- // Return a config with the following cascade:
40
- // - Prettier config file should always win if it exists, if it doesn't:
41
- // - Prettier config from the VS Code extension is used, if it doesn't exist:
42
- // - Use the editor's basic configuration settings
43
- const prettierOptions = returnObjectIfHasKeys(filePrettierOptions) || returnObjectIfHasKeys(editorPrettierOptions) || ideFormattingOptions;
44
- const currentPrettierConfig = {
45
- ...(options.additionalOptions
46
- ? await options.additionalOptions(prettierOptions)
47
- : prettierOptions),
48
- filepath: filePath,
49
- };
50
- if (!options.ignoreIdeOptions) {
51
- currentPrettierConfig.useTabs = !formatOptions.insertSpaces;
52
- currentPrettierConfig.tabWidth = formatOptions.tabSize;
53
- }
54
52
  const fullText = document.getText();
55
53
  let oldText = fullText;
56
- const isHTML = document.languageId === "html";
57
- if (isHTML && options.html?.breakContentsFromTags) {
54
+ const isHTML = document.languageId === 'html';
55
+ if (isHTML && html?.breakContentsFromTags) {
58
56
  oldText = oldText
59
- .replace(/(<[a-z][^>]*>)([^ \n])/gi, "$1 $2")
60
- .replace(/([^ \n])(<\/[a-z][a-z0-9\t\n\r -]*>)/gi, "$1 $2");
57
+ .replace(/(<[a-z][^>]*>)([^ \n])/gi, '$1 $2')
58
+ .replace(/([^ \n])(<\/[a-z][a-z0-9\t\n\r -]*>)/gi, '$1 $2');
61
59
  }
60
+ const prettierOptions = await getFormattingOptions(prettier, document, formatOptions, context);
61
+ const newText = await prettier.format(oldText, prettierOptions);
62
62
  return [{
63
- newText: await prettier.format(oldText, currentPrettierConfig),
63
+ newText,
64
64
  range: {
65
65
  start: document.positionAt(0),
66
66
  end: document.positionAt(fullText.length),
@@ -72,9 +72,12 @@ function create(options = {}, getPrettierConfig = async (filePath, prettier, con
72
72
  };
73
73
  }
74
74
  exports.create = create;
75
- function returnObjectIfHasKeys(obj) {
76
- if (Object.keys(obj || {}).length > 0) {
77
- return obj;
75
+ function matchDocument(selector, document) {
76
+ for (const sel of selector) {
77
+ if (sel === document.languageId || (typeof sel === 'object' && sel.language === document.languageId)) {
78
+ return true;
79
+ }
78
80
  }
81
+ return false;
79
82
  }
80
83
  //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "volar-service-prettier",
3
- "version": "0.0.30",
3
+ "version": "0.0.31-patch.2",
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",
@@ -32,7 +32,7 @@
32
32
  "prettier": "^3.0.3"
33
33
  },
34
34
  "peerDependencies": {
35
- "@volar/language-service": "~2.0.1",
35
+ "@volar/language-service": "~2.1.0",
36
36
  "prettier": "^2.2 || ^3.0"
37
37
  },
38
38
  "peerDependenciesMeta": {
@@ -42,6 +42,5 @@
42
42
  "@volar/language-service": {
43
43
  "optional": true
44
44
  }
45
- },
46
- "gitHead": "30c3cc3c76e90f75f14fe0c2fa4fd33b7ff06507"
45
+ }
47
46
  }
@@ -0,0 +1 @@
1
+ Copy from `*.vue.html` to `*.vue` and `Format Document With Volar` (or simply save in Visual Studio Code) to see the differences.