volar-service-prettyhtml 0.0.29 → 0.0.31

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.
Files changed (3) hide show
  1. package/index.d.ts +7 -2
  2. package/index.js +15 -5
  3. package/package.json +3 -3
package/index.d.ts CHANGED
@@ -1,4 +1,9 @@
1
- import type { ServicePlugin } from '@volar/language-service';
2
1
  import * as prettyhtml from '@starptech/prettyhtml';
3
- export declare function create(configs: NonNullable<Parameters<typeof prettyhtml>[1]>): ServicePlugin;
2
+ import type { DocumentSelector, Result, ServiceContext, ServicePlugin, TextDocument } from '@volar/language-service';
3
+ export type FormattingOptions = Parameters<typeof prettyhtml>[1];
4
+ export declare function create({ documentSelector, isFormattingEnabled, getFormattingOptions, }?: {
5
+ documentSelector?: DocumentSelector;
6
+ isFormattingEnabled?(document: TextDocument, context: ServiceContext): Result<boolean>;
7
+ getFormattingOptions?(document: TextDocument, context: ServiceContext): Result<FormattingOptions>;
8
+ }): ServicePlugin;
4
9
  //# sourceMappingURL=index.d.ts.map
package/index.js CHANGED
@@ -2,19 +2,21 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.create = void 0;
4
4
  const prettyhtml = require("@starptech/prettyhtml");
5
- function create(configs) {
5
+ function create({ documentSelector = ['html'], isFormattingEnabled = () => true, getFormattingOptions = () => ({}), } = {}) {
6
6
  return {
7
7
  name: 'prettyhtml',
8
- create() {
8
+ create(context) {
9
9
  return {
10
- provideDocumentFormattingEdits(document, range, options) {
11
- if (document.languageId !== 'html')
10
+ async provideDocumentFormattingEdits(document, range, options) {
11
+ if (!matchDocument(documentSelector, document))
12
+ return;
13
+ if (!await isFormattingEnabled(document, context))
12
14
  return;
13
15
  const oldRangeText = document.getText(range);
14
16
  const newRangeText = prettyhtml(oldRangeText, {
15
- ...configs,
16
17
  tabWidth: options.tabSize,
17
18
  useTabs: !options.insertSpaces,
19
+ ...await getFormattingOptions(document, context),
18
20
  }).contents;
19
21
  if (newRangeText === oldRangeText)
20
22
  return [];
@@ -40,4 +42,12 @@ function create(configs) {
40
42
  };
41
43
  }
42
44
  exports.create = create;
45
+ function matchDocument(selector, document) {
46
+ for (const sel of selector) {
47
+ if (sel === document.languageId || (typeof sel === 'object' && sel.language === document.languageId)) {
48
+ return true;
49
+ }
50
+ }
51
+ return false;
52
+ }
43
53
  //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "volar-service-prettyhtml",
3
- "version": "0.0.29",
3
+ "version": "0.0.31",
4
4
  "description": "Integrate prettyhtml into Volar",
5
5
  "homepage": "https://github.com/volarjs/services/tree/master/packages/prettyhtml",
6
6
  "bugs": "https://github.com/volarjs/services/issues",
@@ -27,12 +27,12 @@
27
27
  "@starptech/prettyhtml": "^0.10.0"
28
28
  },
29
29
  "peerDependencies": {
30
- "@volar/language-service": "~2.0.1"
30
+ "@volar/language-service": "~2.1.0"
31
31
  },
32
32
  "peerDependenciesMeta": {
33
33
  "@volar/language-service": {
34
34
  "optional": true
35
35
  }
36
36
  },
37
- "gitHead": "6927645293abcc249e7a39c98e52b9f5a2c08469"
37
+ "gitHead": "f7005aef724767786ee9fe943fa976231cc79bf1"
38
38
  }