Skip to content

Commit

Permalink
Quick proof-of-concept on formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
OAGr committed Apr 18, 2024
1 parent fb7c2c1 commit 67006f0
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,37 @@ import * as prettier from "prettier/standalone";
import { useMemo } from "react";

import * as prettierSquigglePlugin from "@quri/prettier-plugin-squiggle/standalone";
import { astNodeIsEqual, parse } from "@quri/squiggle-lang";

export async function formatSquiggle(view: EditorView | undefined) {
if (!view) return;
const code = view.state.doc.toString();
const before = parse(code);
const { formatted, cursorOffset } = await prettier.formatWithCursor(code, {
parser: "squiggle",
plugins: [prettierSquigglePlugin],
cursorOffset: view.state.selection.main.to,
});
view.dispatch({
changes: {
from: 0,
to: view.state.doc.length,
insert: formatted,
},
selection: {
anchor: cursorOffset,
},
});
const after = parse(formatted);
if (before.ok !== after.ok) {
console.error("ERROR!");

Check warning on line 19 in packages/components/src/components/CodeEditor/useFormatSquiggleExtension.ts

View workflow job for this annotation

GitHub Actions / Build, test, lint

Unexpected console statement
} else if (before.ok && after.ok) {
const isEqual = astNodeIsEqual(before.value, after.value, true);
if (!isEqual) {
console.error("ERROR!");

Check warning on line 23 in packages/components/src/components/CodeEditor/useFormatSquiggleExtension.ts

View workflow job for this annotation

GitHub Actions / Build, test, lint

Unexpected console statement
} else {
view.dispatch({
changes: {
from: 0,
to: view.state.doc.length,
insert: formatted,
},
selection: {
anchor: cursorOffset,
},
});
}
}
}

export function useFormatSquiggleExtension() {
Expand Down
22 changes: 22 additions & 0 deletions packages/squiggle-lang/src/ast/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,25 @@ export function nodeResultToString(
}
return nodeToString(r.value, printOptions);
}

export function astNodeIsEqual(
node1: ASTNode,
node2: ASTNode,
compareLocation = false
): boolean {
if (compareLocation) {
return (
nodeToString(node1) === nodeToString(node2) &&
isLocationEqual(node1.location, node2.location)
);
} else {
return nodeToString(node1) === nodeToString(node2);
}
}

function isLocationEqual(loc1: LocationRange, loc2: LocationRange): boolean {
return (
loc1.start.offset === loc2.start.offset &&
loc1.end.offset === loc2.end.offset
);
}
2 changes: 1 addition & 1 deletion packages/squiggle-lang/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export {
type SqValue,
};

export { type AST, type ASTNode } from "./ast/parse.js";
export { type AST, type ASTNode, astNodeIsEqual } from "./ast/parse.js";
export { type ASTCommentNode } from "./ast/peggyHelpers.js";
export { type SqLinker } from "./public/SqLinker.js";
export { type SqOutput, type SqOutputResult } from "./public/types.js";
Expand Down

0 comments on commit 67006f0

Please sign in to comment.