Bundle language tools with CodeMirror package
This commit is contained in:
+17
@@ -0,0 +1,17 @@
|
||||
import type { EditorView } from '@codemirror/view';
|
||||
|
||||
export interface FormatSuccess {
|
||||
ok: true;
|
||||
source: string;
|
||||
}
|
||||
|
||||
export interface FormatFailure {
|
||||
ok: false;
|
||||
error: string;
|
||||
}
|
||||
|
||||
export type FormatResult = FormatSuccess | FormatFailure;
|
||||
|
||||
export declare function initDecodalFormatter(moduleOrPath?: RequestInfo | URL | Response | BufferSource | WebAssembly.Module): Promise<void>;
|
||||
export declare function formatDecodal(source: string): FormatResult;
|
||||
export declare function formatDecodalCommand(view: EditorView): boolean;
|
||||
@@ -0,0 +1,28 @@
|
||||
import initLanguageTools, { formatSource } from '../wasm/decodal_language_tools.js';
|
||||
|
||||
let initialized = false;
|
||||
|
||||
export async function initDecodalFormatter(moduleOrPath) {
|
||||
await initLanguageTools(moduleOrPath);
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
export function formatDecodal(source) {
|
||||
if (!initialized) {
|
||||
return { ok: false, error: 'Decodal formatter is not initialized' };
|
||||
}
|
||||
return JSON.parse(formatSource(source));
|
||||
}
|
||||
|
||||
export function formatDecodalCommand(view) {
|
||||
const result = formatDecodal(view.state.doc.toString());
|
||||
if (!result.ok) return false;
|
||||
view.dispatch({
|
||||
changes: {
|
||||
from: 0,
|
||||
to: view.state.doc.length,
|
||||
insert: result.source,
|
||||
},
|
||||
});
|
||||
return true;
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
import type { EditorView } from '@codemirror/view';
|
||||
|
||||
export interface FormatSuccess {
|
||||
ok: true;
|
||||
source: string;
|
||||
}
|
||||
|
||||
export interface FormatFailure {
|
||||
ok: false;
|
||||
error: string;
|
||||
}
|
||||
|
||||
export type FormatResult = FormatSuccess | FormatFailure;
|
||||
|
||||
export declare function initDecodalFormatter(moduleOrPath?: RequestInfo | URL | Response | BufferSource | WebAssembly.Module): Promise<void>;
|
||||
export declare function formatDecodal(source: string): FormatResult;
|
||||
export declare function formatDecodalCommand(view: EditorView): boolean;
|
||||
Reference in New Issue
Block a user