decodal-codemirror 0.1.4 → 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/package.json +1 -1
- package/src/format.ts +84 -0
- package/src/mod.ts +21 -0
package/package.json
CHANGED
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(
|