Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions workspaces/leetcode-prettier-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
"typecheck": "code-chronicles-typecheck"
},
"dependencies": {
"@code-chronicles/util": "workspace:*",
"nullthrows": "patch:nullthrows@npm%3A1.1.1#~/.yarn/patches/nullthrows-npm-1.1.1-3d1f817134.patch",
"prettier": "3.3.3"
},
"devDependencies": {
Expand Down
40 changes: 27 additions & 13 deletions workspaces/leetcode-prettier-extension/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import nullthrows from "nullthrows";
import { format } from "prettier/standalone";
import estreePlugin from "prettier/plugins/estree";
import tsPlugin from "prettier/plugins/typescript";

import { assignFunctionCosmeticProperties } from "@code-chronicles/util/object-properties/assignFunctionCosmeticProperties";

import { isMonaco } from "./isMonaco.ts";

function main(): void {
Expand All @@ -22,44 +25,55 @@ function main(): void {
}

monaco.editor.onDidCreateEditor((ed) => {
ed
?.getModel?.()
?.updateOptions?.({ tabSize: 2, indentSize: "tabSize" });
try {
nullthrows(ed.getModel()).updateOptions({
tabSize: 2,
indentSize: "tabSize",
});
} catch (err) {
console.error(err);
}

if (typeof ed?.getAction !== "function") {
// TODO: console.error something interesting
console.error("Monaco editor doesn't have a `getAction` method!");
return;
}

const { getAction } = ed;

ed.getAction = function (this: unknown) {
ed.getAction = assignFunctionCosmeticProperties(function (
this: unknown,
) {
const action = getAction.apply(
this,
// Slight lie but `.apply` will work with the `arguments` object.
arguments as unknown as Parameters<typeof getAction>,
);
if (!action) {
// TODO: console.error something interesting

if (typeof action?.run !== "function") {
console.error("Monaco action object doesn't have a `run` method!");
return action;
}

action.run = async function () {
action.run = assignFunctionCosmeticProperties(async function () {
try {
const formattedText = await format(ed.getValue(), {
parser: "typescript",
plugins: [estreePlugin, tsPlugin],
});

// TODO: switch to https://microsoft.github.io/monaco-editor/typedoc/interfaces/editor.ITextModel.html#pushEditOperations.pushEditOperations-1 in the future
ed.setValue(formattedText);
ed.executeEdits("Prettier", [
{
range: nullthrows(ed.getModel()).getFullModelRange(),
text: formattedText,
},
]);
} catch (err) {
console.error(err);
}
};
}, action.run);

return action;
};
}, getAction);
});
},
});
Expand Down
2 changes: 2 additions & 0 deletions yarn.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading