decodal-codemirror 0.1.2 → 0.1.5

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
@@ -2,8 +2,8 @@
2
2
 
3
3
  CodeMirror 6 language support for Decodal.
4
4
 
5
- This package provides the Lezer parser metadata, highlighting tags, indentation rules, and block folding used by the Decodal Web playground.
6
- It does not include the Decodal runtime or WebAssembly evaluator.
5
+ This package provides the Lezer parser metadata, highlighting tags, indentation rules, block folding, and formatter command used by the Decodal Web playground.
6
+ It does not include the Decodal runtime evaluator.
7
7
  Use `decodal-wasm` when you want to evaluate Decodal in a browser.
8
8
 
9
9
  ## Install
@@ -52,3 +52,23 @@ decodal({ highlight: false })
52
52
  ```
53
53
 
54
54
  Use this when you want the language support without the bundled highlight style.
55
+
56
+ ## Formatting
57
+
58
+ The formatter is exposed as a separate subpath so plain syntax highlighting does not force callers to initialize WebAssembly.
59
+
60
+ ```js
61
+ import {
62
+ formatDecodal,
63
+ formatDecodalCommand,
64
+ initDecodalFormatter,
65
+ } from 'decodal-codemirror/format';
66
+ import formatterWasm from 'decodal-codemirror/wasm/decodal_language_tools_bg.wasm?url';
67
+
68
+ await initDecodalFormatter(formatterWasm);
69
+
70
+ const result = formatDecodal('Server={port=Int default 8080;};');
71
+ if (result.ok) console.log(result.source);
72
+
73
+ formatDecodalCommand(view);
74
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "decodal-codemirror",
3
- "version": "0.1.2",
3
+ "version": "0.1.5",
4
4
  "description": "CodeMirror 6 language support for Decodal.",
5
5
  "type": "module",
6
6
  "license": "MIT OR Apache-2.0",
@@ -14,21 +14,37 @@
14
14
  "types": "./src/decodal.d.ts",
15
15
  "import": "./src/decodal.js"
16
16
  },
17
+ "./format": {
18
+ "types": "./src/format.d.ts",
19
+ "import": "./src/format.js"
20
+ },
17
21
  "./parser": "./src/decodal-parser.js",
18
- "./terms": "./src/decodal-parser.terms.js"
22
+ "./terms": "./src/decodal-parser.terms.js",
23
+ "./wasm/decodal_language_tools_bg.wasm": "./wasm/decodal_language_tools_bg.wasm"
19
24
  },
20
25
  "types": "./src/decodal.d.ts",
21
26
  "files": [
22
27
  "README.md",
23
- "src/"
28
+ "src/",
29
+ "wasm/decodal_language_tools.js",
30
+ "wasm/decodal_language_tools.d.ts",
31
+ "wasm/decodal_language_tools_bg.wasm",
32
+ "wasm/decodal_language_tools_bg.wasm.d.ts"
24
33
  ],
25
34
  "keywords": [
26
35
  "decodal",
27
36
  "codemirror",
28
37
  "lezer"
29
38
  ],
30
- "dependencies": {
39
+ "peerDependencies": {
40
+ "@codemirror/language": "^6.12.4",
41
+ "@codemirror/view": "^6.43.6",
42
+ "@lezer/highlight": "^1.2.3",
43
+ "@lezer/lr": "^1.4.10"
44
+ },
45
+ "devDependencies": {
31
46
  "@codemirror/language": "^6.12.4",
47
+ "@codemirror/view": "^6.43.6",
32
48
  "@lezer/highlight": "^1.2.3",
33
49
  "@lezer/lr": "^1.4.10"
34
50
  }
@@ -0,0 +1,17 @@
1
+ import type { EditorView } from '@codemirror/view';
2
+
3
+ export interface FormatSuccess {
4
+ ok: true;
5
+ source: string;
6
+ }
7
+
8
+ export interface FormatFailure {
9
+ ok: false;
10
+ error: string;
11
+ }
12
+
13
+ export type FormatResult = FormatSuccess | FormatFailure;
14
+
15
+ export declare function initDecodalFormatter(moduleOrPath?: RequestInfo | URL | Response | BufferSource | WebAssembly.Module): Promise<void>;
16
+ export declare function formatDecodal(source: string): FormatResult;
17
+ export declare function formatDecodalCommand(view: EditorView): boolean;
package/src/format.js ADDED
@@ -0,0 +1,28 @@
1
+ import initLanguageTools, { formatSource } from '../wasm/decodal_language_tools.js';
2
+
3
+ let initialized = false;
4
+
5
+ export async function initDecodalFormatter(moduleOrPath) {
6
+ await initLanguageTools(moduleOrPath);
7
+ initialized = true;
8
+ }
9
+
10
+ export function formatDecodal(source) {
11
+ if (!initialized) {
12
+ return { ok: false, error: 'Decodal formatter is not initialized' };
13
+ }
14
+ return JSON.parse(formatSource(source));
15
+ }
16
+
17
+ export function formatDecodalCommand(view) {
18
+ const result = formatDecodal(view.state.doc.toString());
19
+ if (!result.ok) return false;
20
+ view.dispatch({
21
+ changes: {
22
+ from: 0,
23
+ to: view.state.doc.length,
24
+ insert: result.source,
25
+ },
26
+ });
27
+ return true;
28
+ }
@@ -0,0 +1,17 @@
1
+ import type { EditorView } from '@codemirror/view';
2
+
3
+ export interface FormatSuccess {
4
+ ok: true;
5
+ source: string;
6
+ }
7
+
8
+ export interface FormatFailure {
9
+ ok: false;
10
+ error: string;
11
+ }
12
+
13
+ export type FormatResult = FormatSuccess | FormatFailure;
14
+
15
+ export declare function initDecodalFormatter(moduleOrPath?: RequestInfo | URL | Response | BufferSource | WebAssembly.Module): Promise<void>;
16
+ export declare function formatDecodal(source: string): FormatResult;
17
+ export declare function formatDecodalCommand(view: EditorView): boolean;
package/src/format.ts ADDED
@@ -0,0 +1,84 @@
1
+ /**
2
+ * Decodal source formatting helpers for CodeMirror integrations.
3
+ *
4
+ * The formatter is backed by WebAssembly generated from the internal Rust
5
+ * `decodal-language-tools` crate. Call {@link initDecodalFormatter} once before
6
+ * using {@link formatDecodal} or {@link formatDecodalCommand}.
7
+ *
8
+ * @module
9
+ */
10
+ import type { EditorView } from 'npm:@codemirror/view@^6.43.6';
11
+ import initLanguageTools, { formatSource } from '../wasm/decodal_language_tools.js';
12
+
13
+ let initialized = false;
14
+
15
+ /** Successful formatter result. */
16
+ export interface FormatSuccess {
17
+ /** Indicates that formatting succeeded. */
18
+ ok: true;
19
+ /** The formatted Decodal source text. */
20
+ source: string;
21
+ }
22
+
23
+ /** Failed formatter result. */
24
+ export interface FormatFailure {
25
+ /** Indicates that formatting failed. */
26
+ ok: false;
27
+ /** Human-readable formatter error. */
28
+ error: string;
29
+ }
30
+
31
+ /** Result returned by {@link formatDecodal}. */
32
+ export type FormatResult = FormatSuccess | FormatFailure;
33
+
34
+ /**
35
+ * Initialize the bundled Decodal formatter WebAssembly module.
36
+ *
37
+ * Bundlers such as Vite should pass the package wasm asset URL explicitly:
38
+ *
39
+ * ```ts
40
+ * import wasmUrl from 'decodal-codemirror/wasm/decodal_language_tools_bg.wasm?url';
41
+ * await initDecodalFormatter(wasmUrl);
42
+ * ```
43
+ *
44
+ * @param moduleOrPath - Optional WebAssembly module, bytes, response, URL, or
45
+ * request accepted by wasm-bindgen's generated initializer.
46
+ */
47
+ export async function initDecodalFormatter(
48
+ moduleOrPath?: RequestInfo | URL | Response | BufferSource | WebAssembly.Module,
49
+ ): Promise<void> {
50
+ await initLanguageTools(moduleOrPath);
51
+ initialized = true;
52
+ }
53
+
54
+ /**
55
+ * Format a complete Decodal source document.
56
+ *
57
+ * @param source - Source text to format.
58
+ * @returns Either formatted source text or an error message.
59
+ */
60
+ export function formatDecodal(source: string): FormatResult {
61
+ if (!initialized) {
62
+ return { ok: false, error: 'Decodal formatter is not initialized' };
63
+ }
64
+ return JSON.parse(formatSource(source)) as FormatResult;
65
+ }
66
+
67
+ /**
68
+ * CodeMirror command that formats the whole document in-place.
69
+ *
70
+ * @param view - The editor view whose document should be formatted.
71
+ * @returns `true` when the document was formatted, otherwise `false`.
72
+ */
73
+ export function formatDecodalCommand(view: EditorView): boolean {
74
+ const result = formatDecodal(view.state.doc.toString());
75
+ if (!result.ok) return false;
76
+ view.dispatch({
77
+ changes: {
78
+ from: 0,
79
+ to: view.state.doc.length,
80
+ insert: result.source,
81
+ },
82
+ });
83
+ return true;
84
+ }
package/src/mod.ts CHANGED
@@ -1,3 +1,13 @@
1
+ /**
2
+ * CodeMirror 6 language support for Decodal.
3
+ *
4
+ * This module exports the Decodal language extension, parser metadata, and the
5
+ * bundled highlight style used by the official Decodal playground. Add
6
+ * {@link decodal} to a CodeMirror extension set to enable highlighting,
7
+ * indentation, and folding for Decodal documents.
8
+ *
9
+ * @module
10
+ */
1
11
  import { HighlightStyle, LRLanguage, LanguageSupport, foldNodeProp, indentNodeProp, syntaxHighlighting } from 'npm:@codemirror/language@^6.12.4';
2
12
  import { styleTags, tags as t } from 'npm:@lezer/highlight@^1.2.3';
3
13
  import { parser } from './decodal-parser-jsr.js';
@@ -44,6 +54,7 @@ function foldDelimited(open: string, close: string) {
44
54
  };
45
55
  }
46
56
 
57
+ /** Decodal LR language definition for CodeMirror 6. */
47
58
  export const decodalLanguage: LRLanguage = LRLanguage.define({
48
59
  parser: parserWithMetadata,
49
60
  languageData: {
@@ -52,6 +63,7 @@ export const decodalLanguage: LRLanguage = LRLanguage.define({
52
63
  },
53
64
  });
54
65
 
66
+ /** Default Decodal highlight style used by the playground. */
55
67
  export const decodalHighlightStyle: HighlightStyle = HighlightStyle.define([
56
68
  { tag: t.keyword, color: '#93c5fd', fontWeight: '700' },
57
69
  { tag: t.variableName, color: 'inherit' },
@@ -64,10 +76,19 @@ export const decodalHighlightStyle: HighlightStyle = HighlightStyle.define([
64
76
  { tag: [t.brace, t.squareBracket, t.paren, t.punctuation], color: '#f9a8d4' },
65
77
  ]);
66
78
 
79
+ /** Options for {@link decodal}. */
67
80
  export interface DecodalOptions {
81
+ /** Include the bundled {@link decodalHighlightStyle}. Defaults to `true`. */
68
82
  highlight?: boolean;
69
83
  }
70
84
 
85
+ /**
86
+ * Create CodeMirror language support for Decodal.
87
+ *
88
+ * @param options - Optional language support options.
89
+ * @returns A CodeMirror extension bundle containing the Decodal language and,
90
+ * unless disabled, the default highlight style.
91
+ */
71
92
  export function decodal(options: DecodalOptions = {}): LanguageSupport {
72
93
  const { highlight = true } = options;
73
94
  return new LanguageSupport(
@@ -0,0 +1,38 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+
4
+ export function formatSource(source: string): string;
5
+
6
+ export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
7
+
8
+ export interface InitOutput {
9
+ readonly memory: WebAssembly.Memory;
10
+ readonly formatSource: (a: number, b: number) => [number, number];
11
+ readonly __wbindgen_externrefs: WebAssembly.Table;
12
+ readonly __wbindgen_malloc: (a: number, b: number) => number;
13
+ readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
14
+ readonly __wbindgen_free: (a: number, b: number, c: number) => void;
15
+ readonly __wbindgen_start: () => void;
16
+ }
17
+
18
+ export type SyncInitInput = BufferSource | WebAssembly.Module;
19
+
20
+ /**
21
+ * Instantiates the given `module`, which can either be bytes or
22
+ * a precompiled `WebAssembly.Module`.
23
+ *
24
+ * @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
25
+ *
26
+ * @returns {InitOutput}
27
+ */
28
+ export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
29
+
30
+ /**
31
+ * If `module_or_path` is {RequestInfo} or {URL}, makes a request and
32
+ * for everything else, calls `WebAssembly.instantiate` directly.
33
+ *
34
+ * @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
35
+ *
36
+ * @returns {Promise<InitOutput>}
37
+ */
38
+ export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;
@@ -0,0 +1,209 @@
1
+ /* @ts-self-types="./decodal_language_tools.d.ts" */
2
+
3
+ /**
4
+ * @param {string} source
5
+ * @returns {string}
6
+ */
7
+ export function formatSource(source) {
8
+ let deferred2_0;
9
+ let deferred2_1;
10
+ try {
11
+ const ptr0 = passStringToWasm0(source, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
12
+ const len0 = WASM_VECTOR_LEN;
13
+ const ret = wasm.formatSource(ptr0, len0);
14
+ deferred2_0 = ret[0];
15
+ deferred2_1 = ret[1];
16
+ return getStringFromWasm0(ret[0], ret[1]);
17
+ } finally {
18
+ wasm.__wbindgen_free(deferred2_0, deferred2_1, 1);
19
+ }
20
+ }
21
+ function __wbg_get_imports() {
22
+ const import0 = {
23
+ __proto__: null,
24
+ __wbindgen_init_externref_table: function() {
25
+ const table = wasm.__wbindgen_externrefs;
26
+ const offset = table.grow(4);
27
+ table.set(0, undefined);
28
+ table.set(offset + 0, undefined);
29
+ table.set(offset + 1, null);
30
+ table.set(offset + 2, true);
31
+ table.set(offset + 3, false);
32
+ },
33
+ };
34
+ return {
35
+ __proto__: null,
36
+ "./decodal_language_tools_bg.js": import0,
37
+ };
38
+ }
39
+
40
+ function getStringFromWasm0(ptr, len) {
41
+ return decodeText(ptr >>> 0, len);
42
+ }
43
+
44
+ let cachedUint8ArrayMemory0 = null;
45
+ function getUint8ArrayMemory0() {
46
+ if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
47
+ cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
48
+ }
49
+ return cachedUint8ArrayMemory0;
50
+ }
51
+
52
+ function passStringToWasm0(arg, malloc, realloc) {
53
+ if (realloc === undefined) {
54
+ const buf = cachedTextEncoder.encode(arg);
55
+ const ptr = malloc(buf.length, 1) >>> 0;
56
+ getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
57
+ WASM_VECTOR_LEN = buf.length;
58
+ return ptr;
59
+ }
60
+
61
+ let len = arg.length;
62
+ let ptr = malloc(len, 1) >>> 0;
63
+
64
+ const mem = getUint8ArrayMemory0();
65
+
66
+ let offset = 0;
67
+
68
+ for (; offset < len; offset++) {
69
+ const code = arg.charCodeAt(offset);
70
+ if (code > 0x7F) break;
71
+ mem[ptr + offset] = code;
72
+ }
73
+ if (offset !== len) {
74
+ if (offset !== 0) {
75
+ arg = arg.slice(offset);
76
+ }
77
+ ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
78
+ const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
79
+ const ret = cachedTextEncoder.encodeInto(arg, view);
80
+
81
+ offset += ret.written;
82
+ ptr = realloc(ptr, len, offset, 1) >>> 0;
83
+ }
84
+
85
+ WASM_VECTOR_LEN = offset;
86
+ return ptr;
87
+ }
88
+
89
+ let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
90
+ cachedTextDecoder.decode();
91
+ const MAX_SAFARI_DECODE_BYTES = 2146435072;
92
+ let numBytesDecoded = 0;
93
+ function decodeText(ptr, len) {
94
+ numBytesDecoded += len;
95
+ if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
96
+ cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
97
+ cachedTextDecoder.decode();
98
+ numBytesDecoded = len;
99
+ }
100
+ return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
101
+ }
102
+
103
+ const cachedTextEncoder = new TextEncoder();
104
+
105
+ if (!('encodeInto' in cachedTextEncoder)) {
106
+ cachedTextEncoder.encodeInto = function (arg, view) {
107
+ const buf = cachedTextEncoder.encode(arg);
108
+ view.set(buf);
109
+ return {
110
+ read: arg.length,
111
+ written: buf.length
112
+ };
113
+ };
114
+ }
115
+
116
+ let WASM_VECTOR_LEN = 0;
117
+
118
+ let wasmModule, wasmInstance, wasm;
119
+ function __wbg_finalize_init(instance, module) {
120
+ wasmInstance = instance;
121
+ wasm = instance.exports;
122
+ wasmModule = module;
123
+ cachedUint8ArrayMemory0 = null;
124
+ wasm.__wbindgen_start();
125
+ return wasm;
126
+ }
127
+
128
+ async function __wbg_load(module, imports) {
129
+ if (typeof Response === 'function' && module instanceof Response) {
130
+ if (typeof WebAssembly.instantiateStreaming === 'function') {
131
+ try {
132
+ return await WebAssembly.instantiateStreaming(module, imports);
133
+ } catch (e) {
134
+ const validResponse = module.ok && expectedResponseType(module.type);
135
+
136
+ if (validResponse && module.headers.get('Content-Type') !== 'application/wasm') {
137
+ console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
138
+
139
+ } else { throw e; }
140
+ }
141
+ }
142
+
143
+ const bytes = await module.arrayBuffer();
144
+ return await WebAssembly.instantiate(bytes, imports);
145
+ } else {
146
+ const instance = await WebAssembly.instantiate(module, imports);
147
+
148
+ if (instance instanceof WebAssembly.Instance) {
149
+ return { instance, module };
150
+ } else {
151
+ return instance;
152
+ }
153
+ }
154
+
155
+ function expectedResponseType(type) {
156
+ switch (type) {
157
+ case 'basic': case 'cors': case 'default': return true;
158
+ }
159
+ return false;
160
+ }
161
+ }
162
+
163
+ function initSync(module) {
164
+ if (wasm !== undefined) return wasm;
165
+
166
+
167
+ if (module !== undefined) {
168
+ if (Object.getPrototypeOf(module) === Object.prototype) {
169
+ ({module} = module)
170
+ } else {
171
+ console.warn('using deprecated parameters for `initSync()`; pass a single object instead')
172
+ }
173
+ }
174
+
175
+ const imports = __wbg_get_imports();
176
+ if (!(module instanceof WebAssembly.Module)) {
177
+ module = new WebAssembly.Module(module);
178
+ }
179
+ const instance = new WebAssembly.Instance(module, imports);
180
+ return __wbg_finalize_init(instance, module);
181
+ }
182
+
183
+ async function __wbg_init(module_or_path) {
184
+ if (wasm !== undefined) return wasm;
185
+
186
+
187
+ if (module_or_path !== undefined) {
188
+ if (Object.getPrototypeOf(module_or_path) === Object.prototype) {
189
+ ({module_or_path} = module_or_path)
190
+ } else {
191
+ console.warn('using deprecated parameters for the initialization function; pass a single object instead')
192
+ }
193
+ }
194
+
195
+ if (module_or_path === undefined) {
196
+ module_or_path = new URL('decodal_language_tools_bg.wasm', import.meta.url);
197
+ }
198
+ const imports = __wbg_get_imports();
199
+
200
+ if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) {
201
+ module_or_path = fetch(module_or_path);
202
+ }
203
+
204
+ const { instance, module } = await __wbg_load(await module_or_path, imports);
205
+
206
+ return __wbg_finalize_init(instance, module);
207
+ }
208
+
209
+ export { initSync, __wbg_init as default };
@@ -0,0 +1,9 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+ export const memory: WebAssembly.Memory;
4
+ export const formatSource: (a: number, b: number) => [number, number];
5
+ export const __wbindgen_externrefs: WebAssembly.Table;
6
+ export const __wbindgen_malloc: (a: number, b: number) => number;
7
+ export const __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
8
+ export const __wbindgen_free: (a: number, b: number, c: number) => void;
9
+ export const __wbindgen_start: () => void;