volar-service-prettier 0.0.0

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/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # volar-service-prettier
2
+
3
+ Volar plugin for [prettier](https://prettier.io/).
4
+
5
+ ## Usage
6
+
7
+ `package.json`
8
+
9
+ ```json
10
+ {
11
+ "devDependencies": {
12
+ "volar-service-prettier": "latest"
13
+ }
14
+ }
15
+ ```
16
+
17
+ `volar.config.js`
18
+
19
+ ```js
20
+ module.exports = {
21
+ plugins: [
22
+ require('volar-service-prettier').default(
23
+ {
24
+ languages: ['html', 'css', 'scss', 'typescript', 'javascript'],
25
+ html: {
26
+ breakContentsFromTags: true,
27
+ },
28
+ ignoreIdeOptions: true,
29
+ },
30
+ // provide your prettier options, otherwise auto resolve config file by plugin
31
+ () => ({
32
+ // ...
33
+ })
34
+ ),
35
+ ],
36
+ };
37
+ ```
package/out/index.d.ts ADDED
@@ -0,0 +1,35 @@
1
+ import type { Service } from '@volar/language-service';
2
+ import { type Options, type ResolveConfigOptions } from 'prettier';
3
+ declare const _default: (options?: {
4
+ /**
5
+ * Languages to be formatted by prettier.
6
+ *
7
+ * @default
8
+ * ['html', 'css', 'scss', 'typescript', 'javascript']
9
+ */
10
+ languages?: string[] | undefined;
11
+ html?: {
12
+ /**
13
+ * Preprocessing to break "contents" from "HTML tags".
14
+ * This will prevent HTML closing tags, and opening tags without attributes
15
+ * from breaking into a blank `>` or `<` on a new line.
16
+ */
17
+ breakContentsFromTags?: boolean | undefined;
18
+ } | undefined;
19
+ /**
20
+ * Do not use settings from VSCode's `editor.tabSize` and temporary tabSize on status bar
21
+ *
22
+ * @see https://github.com/volarjs/services/issues/5
23
+ */
24
+ ignoreIdeOptions?: boolean | undefined;
25
+ /**
26
+ * Additional options to pass to Prettier
27
+ * This is useful, for instance, to add specific plugins you need.
28
+ */
29
+ additionalOptions?: ((resolvedConfig: Options) => Options) | undefined;
30
+ /**
31
+ * Options to use when resolving the Prettier config
32
+ */
33
+ resolveConfigOptions?: ResolveConfigOptions | undefined;
34
+ }, getPrettierConfig?: (config?: ResolveConfigOptions) => Options) => Service;
35
+ export default _default;
package/out/index.js ADDED
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const prettier_1 = require("prettier");
4
+ exports.default = (options = {}, getPrettierConfig = (config) => {
5
+ const configFile = prettier_1.resolveConfigFile.sync();
6
+ if (configFile) {
7
+ return prettier_1.resolveConfig.sync(configFile, config) ?? {};
8
+ }
9
+ return {};
10
+ }) => (context) => {
11
+ if (!context) {
12
+ return {};
13
+ }
14
+ const languages = options.languages ?? ['html', 'css', 'scss', 'typescript', 'javascript'];
15
+ const filePrettierOptions = getPrettierConfig(options.resolveConfigOptions);
16
+ return {
17
+ async provideDocumentFormattingEdits(document, _, formatOptions) {
18
+ if (!languages.includes(document.languageId)) {
19
+ return;
20
+ }
21
+ const fileInfo = await (0, prettier_1.getFileInfo)(context.env.uriToFileName(document.uri), { ignorePath: '.prettierignore' });
22
+ if (fileInfo.ignored) {
23
+ return;
24
+ }
25
+ const editorPrettierOptions = await context.env.getConfiguration?.('prettier', document.uri);
26
+ const ideFormattingOptions = formatOptions !== undefined && !options.ignoreIdeOptions // We need to check for options existing here because some editors might not have it
27
+ ? {
28
+ tabWidth: formatOptions.tabSize,
29
+ useTabs: !formatOptions.insertSpaces,
30
+ }
31
+ : {};
32
+ const fullText = document.getText();
33
+ let oldText = fullText;
34
+ const isHTML = document.languageId === 'html';
35
+ if (isHTML && options.html?.breakContentsFromTags) {
36
+ oldText = oldText
37
+ .replace(/(<[a-z][^>]*>)([^ \n])/gi, '$1 $2')
38
+ .replace(/([^ \n])(<\/[a-z][a-z0-9\t\n\r -]*>)/gi, '$1 $2');
39
+ }
40
+ // Return a config with the following cascade:
41
+ // - Prettier config file should always win if it exists, if it doesn't:
42
+ // - Prettier config from the VS Code extension is used, if it doesn't exist:
43
+ // - Use the editor's basic configuration settings
44
+ const prettierOptions = returnObjectIfHasKeys(filePrettierOptions) || returnObjectIfHasKeys(editorPrettierOptions) || ideFormattingOptions;
45
+ const currentPrettierConfig = {
46
+ ...options.additionalOptions ? options.additionalOptions(prettierOptions) : prettierOptions,
47
+ filepath: context.env.uriToFileName(document.uri),
48
+ };
49
+ if (!options.ignoreIdeOptions) {
50
+ currentPrettierConfig.useTabs = !formatOptions.insertSpaces;
51
+ currentPrettierConfig.tabWidth = formatOptions.tabSize;
52
+ }
53
+ return [{
54
+ newText: (0, prettier_1.format)(oldText, currentPrettierConfig),
55
+ range: {
56
+ start: document.positionAt(0),
57
+ end: document.positionAt(fullText.length),
58
+ },
59
+ }];
60
+ },
61
+ };
62
+ };
63
+ function returnObjectIfHasKeys(obj) {
64
+ if (Object.keys(obj || {}).length > 0) {
65
+ return obj;
66
+ }
67
+ }
68
+ //# sourceMappingURL=index.js.map
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "volar-service-prettier",
3
+ "version": "0.0.0",
4
+ "main": "out/index.js",
5
+ "types": "out/index.d.ts",
6
+ "license": "MIT",
7
+ "files": [
8
+ "out/**/*.js",
9
+ "out/**/*.d.ts"
10
+ ],
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "https://github.com/volarjs/services.git",
14
+ "directory": "packages/prettier"
15
+ },
16
+ "author": {
17
+ "name": "Pacharapol Withayasakpunt",
18
+ "email": "polv@polv.cc",
19
+ "url": "https://www.polv.cc"
20
+ },
21
+ "devDependencies": {
22
+ "@types/prettier": "^2.7.2"
23
+ },
24
+ "peerDependencies": {
25
+ "@volar/language-service": "*",
26
+ "prettier": "*"
27
+ },
28
+ "peerDependenciesMeta": {
29
+ "@volar/language-service": {
30
+ "optional": true
31
+ }
32
+ },
33
+ "gitHead": "1011561ac4bbf79c53c3b80c27692569bf861519"
34
+ }