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 +3 -4
- package/out/index.js +60 -62
- package/package.json +3 -3
package/out/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import {
|
|
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>):
|
|
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
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
?
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
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.
|
|
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": "
|
|
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": "
|
|
44
|
+
"gitHead": "8dedd3c6e73740fc7c1fe06df32b727ca504adc0"
|
|
45
45
|
}
|