@bhsd/lezer-json 2.2.0 → 2.3.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 CHANGED
@@ -19,6 +19,9 @@ npm install @bhsd/lezer-json
19
19
 
20
20
  ## Language Support
21
21
 
22
+ You can directly import the language support that provides both the [language](#language)
23
+ and the basic [completion source](#completion-source) for JSON or JSONC.
24
+
22
25
  ```ts
23
26
  import {json} from '@bhsd/lezer-json';
24
27
  import type {LanguageSupport} from '@codemirror/language';
@@ -36,10 +39,23 @@ for JSON or JSONC alone.
36
39
  import {jsonLanguage, jsoncLanguage} from '@bhsd/lezer-json';
37
40
  ```
38
41
 
42
+ ## Completion Source
43
+
44
+ If you are importing the [language](#language) alone, you can choose to import
45
+ the completion source which provides basic completion for both JSON and JSONC.
46
+
47
+ ```ts
48
+ import {jsonCompletionSource, jsonLanguage, jsoncLanguage} from '@bhsd/lezer-json';
49
+ import type {Extension} from '@codemirror/state';
50
+
51
+ const jsonCompletion: Extension = jsonLanguage.data.of({autocomplete: jsonCompletionSource}),
52
+ jsoncCompletion: Extension = jsoncLanguage.data.of({autocomplete: jsonCompletionSource});
53
+ ```
54
+
39
55
  ## Lint Source
40
56
 
41
- This package also provides [lint sources](https://codemirror.net/docs/ref/#lint.LintSource)
42
- for JSON or JSONC syntax checking.
57
+ This package also provides lint sources for JSON or JSONC syntax checking. This
58
+ is not included in the language support by default.
43
59
 
44
60
  ```ts
45
61
  import {linter} from '@codemirror/lint';
package/dist/index.d.ts CHANGED
@@ -1,9 +1,11 @@
1
1
  import { LRLanguage, LanguageSupport } from '@codemirror/language';
2
2
  import type { LintSource } from '@codemirror/lint';
3
+ import type { CompletionSource } from '@codemirror/autocomplete';
3
4
  /** LR language for JSON. */
4
5
  export declare const jsonLanguage: LRLanguage;
5
6
  /** LR language for JSONC. */
6
7
  export declare const jsoncLanguage: LRLanguage;
8
+ export declare const jsonCompletionSource: CompletionSource;
7
9
  /**
8
10
  * Get language support for JSON or JSONC.
9
11
  * @param dialect The dialect to use, either omitted or `'jsonc'` for JSON with comments.
package/dist/index.js CHANGED
@@ -12,12 +12,6 @@ const props = [
12
12
  ], options = ['true', 'false', 'null'].map((label) => ({ label, type: 'keyword' })), languageData = {
13
13
  closeBrackets: { brackts: ['[', '{', '"'] },
14
14
  indentOnInput: /^\s*[}\]]$/u,
15
- autocomplete({ state, pos }) {
16
- const { name: n, parent: p, from } = syntaxTree(state).resolveInner(pos, -1);
17
- return n !== 'PropertyName' && n !== ':' && (p === null || p === void 0 ? void 0 : p.name) === 'Property'
18
- ? { from, options, validFor: /^[a-z]*$/iu }
19
- : null;
20
- },
21
15
  };
22
16
  /** LR language for JSON. */
23
17
  export const jsonLanguage = /* #__PURE__ */ (() => LRLanguage.define({
@@ -37,11 +31,29 @@ export const jsoncLanguage = /* #__PURE__ */ (() => LRLanguage.define({
37
31
  },
38
32
  },
39
33
  }))();
34
+ export const jsonCompletionSource = context => {
35
+ const mt = context.matchBefore(/(?:^|[[,:])\s*[a-z]+$/u);
36
+ if (!mt) {
37
+ return null;
38
+ }
39
+ const { state, pos } = context, { name: n, parent: p } = syntaxTree(state).resolveInner(pos, -1), { from, text } = mt;
40
+ return (p === null || p === void 0 ? void 0 : p.name) === 'Array' && !text.startsWith(':')
41
+ || n !== 'PropertyName' && (p === null || p === void 0 ? void 0 : p.name) === 'Property' && !/^[[,]/u.test(text)
42
+ ? {
43
+ from: from + text.search(/[a-z]/iu),
44
+ options,
45
+ validFor: /^[a-z]*$/iu,
46
+ }
47
+ : null;
48
+ };
40
49
  /**
41
50
  * Get language support for JSON or JSONC.
42
51
  * @param dialect The dialect to use, either omitted or `'jsonc'` for JSON with comments.
43
52
  */
44
- export const json = (dialect) => new LanguageSupport(dialect === 'jsonc' ? jsoncLanguage : jsonLanguage);
53
+ export const json = (dialect) => {
54
+ const lang = dialect === 'jsonc' ? jsoncLanguage : jsonLanguage;
55
+ return new LanguageSupport(lang, lang.data.of({ autocomplete: jsonCompletionSource }));
56
+ };
45
57
  const getLintSource = (lint) => ({ state: { doc } }) => lint(doc.toString())
46
58
  .map(({ message, from, to = from, severity }) => ({ message, severity, from, to }));
47
59
  /** Lint source for JSON */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bhsd/lezer-json",
3
- "version": "2.2.0",
3
+ "version": "2.3.0",
4
4
  "description": "Lezer-based JSON/JSONC grammar",
5
5
  "keywords": [
6
6
  "codemirror",
@@ -53,7 +53,7 @@
53
53
  "@bhsd/common": "^4.1.0"
54
54
  },
55
55
  "devDependencies": {
56
- "@bhsd/code-standard": "^4.2.0",
56
+ "@bhsd/code-standard": "^4.3.0",
57
57
  "@codemirror/autocomplete": "^6.20.3",
58
58
  "@codemirror/language": "^6.12.4",
59
59
  "@codemirror/lint": "^6.9.7",