volar-service-prettier 0.0.31 → 0.0.32
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 +14 -33
- package/index.js +47 -47
- package/package.json +2 -2
package/index.d.ts
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
1
|
-
import type { ServicePlugin,
|
|
2
|
-
import type { Options
|
|
3
|
-
export declare function create(
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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) => Result<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
|
-
*
|
|
17
|
+
* Languages to be formatted by prettier.
|
|
21
18
|
*
|
|
22
|
-
* @
|
|
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
|
-
|
|
43
|
-
|
|
44
|
-
|
|
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,62 @@
|
|
|
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(
|
|
6
|
-
|
|
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
|
+
const fileInfo = await prettier.getFileInfo(uri.fsPath, { ignorePath: '.prettierignore', resolveConfig: false });
|
|
13
|
+
if (fileInfo.ignored) {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return true;
|
|
18
|
+
}, getFormattingOptions = async (prettier, document, formatOptions, context) => {
|
|
19
|
+
const filepath = vscode_uri_1.URI.parse(document.uri).fsPath;
|
|
20
|
+
const configOptions = await prettier.resolveConfig(filepath);
|
|
21
|
+
const editorOptions = await context.env.getConfiguration?.('prettier', document.uri);
|
|
22
|
+
return {
|
|
23
|
+
filepath,
|
|
24
|
+
tabWidth: formatOptions.tabSize,
|
|
25
|
+
useTabs: !formatOptions.insertSpaces,
|
|
26
|
+
...editorOptions,
|
|
27
|
+
...configOptions,
|
|
28
|
+
};
|
|
29
|
+
}, } = {}) {
|
|
8
30
|
return {
|
|
9
31
|
name: 'prettier',
|
|
10
32
|
create(context) {
|
|
11
|
-
let
|
|
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);
|
|
19
|
-
}
|
|
20
|
-
const languages = options.languages ?? ['html', 'css', 'scss', 'typescript', 'javascript'];
|
|
33
|
+
let prettierInstanceOrPromise;
|
|
21
34
|
return {
|
|
22
35
|
async provideDocumentFormattingEdits(document, _, formatOptions) {
|
|
23
|
-
if (!
|
|
36
|
+
if (!matchDocument(documentSelector, document)) {
|
|
24
37
|
return;
|
|
25
38
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
39
|
+
prettierInstanceOrPromise ??= typeof prettierInstanceOrGetter === 'function'
|
|
40
|
+
? prettierInstanceOrGetter(context)
|
|
41
|
+
: prettierInstanceOrGetter;
|
|
42
|
+
const prettier = await prettierInstanceOrPromise;
|
|
43
|
+
if (!prettier) {
|
|
29
44
|
return;
|
|
30
45
|
}
|
|
31
|
-
|
|
32
|
-
|
|
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;
|
|
46
|
+
if (!isFormattingEnabled(prettier, document, context)) {
|
|
47
|
+
return;
|
|
53
48
|
}
|
|
54
49
|
const fullText = document.getText();
|
|
55
50
|
let oldText = fullText;
|
|
56
|
-
const isHTML = document.languageId ===
|
|
57
|
-
if (isHTML &&
|
|
51
|
+
const isHTML = document.languageId === 'html';
|
|
52
|
+
if (isHTML && html?.breakContentsFromTags) {
|
|
58
53
|
oldText = oldText
|
|
59
|
-
.replace(/(<[a-z][^>]*>)([^ \n])/gi,
|
|
60
|
-
.replace(/([^ \n])(<\/[a-z][a-z0-9\t\n\r -]*>)/gi,
|
|
54
|
+
.replace(/(<[a-z][^>]*>)([^ \n])/gi, '$1 $2')
|
|
55
|
+
.replace(/([^ \n])(<\/[a-z][a-z0-9\t\n\r -]*>)/gi, '$1 $2');
|
|
61
56
|
}
|
|
57
|
+
const prettierOptions = await getFormattingOptions(prettier, document, formatOptions, context);
|
|
58
|
+
const newText = await prettier.format(oldText, prettierOptions);
|
|
62
59
|
return [{
|
|
63
|
-
newText
|
|
60
|
+
newText,
|
|
64
61
|
range: {
|
|
65
62
|
start: document.positionAt(0),
|
|
66
63
|
end: document.positionAt(fullText.length),
|
|
@@ -72,9 +69,12 @@ function create(options = {}, getPrettierConfig = async (filePath, prettier, con
|
|
|
72
69
|
};
|
|
73
70
|
}
|
|
74
71
|
exports.create = create;
|
|
75
|
-
function
|
|
76
|
-
|
|
77
|
-
|
|
72
|
+
function matchDocument(selector, document) {
|
|
73
|
+
for (const sel of selector) {
|
|
74
|
+
if (sel === document.languageId || (typeof sel === 'object' && sel.language === document.languageId)) {
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
78
77
|
}
|
|
78
|
+
return false;
|
|
79
79
|
}
|
|
80
80
|
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "volar-service-prettier",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.32",
|
|
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",
|
|
@@ -43,5 +43,5 @@
|
|
|
43
43
|
"optional": true
|
|
44
44
|
}
|
|
45
45
|
},
|
|
46
|
-
"gitHead": "
|
|
46
|
+
"gitHead": "717049e7dcd5c30f451f6db8eb71eaba43f74c83"
|
|
47
47
|
}
|