Skip to content

Commit 0c22e4e

Browse files
committed
Specify tab/indent size in LeetCode Prettier extension
1 parent 14eb68f commit 0c22e4e

File tree

4 files changed

+66
-12
lines changed

4 files changed

+66
-12
lines changed

workspaces/leetcode-prettier-extension/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"cross-env": "7.0.3",
3232
"eslint": "9.12.0",
3333
"fork-ts-checker-webpack-plugin": "9.0.2",
34+
"monaco-editor": "0.52.0",
3435
"ts-loader": "9.5.1",
3536
"tsx": "4.19.1",
3637
"type-fest": "4.26.1",
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import type { editor as MonacoEditorNamespace } from "monaco-editor";
2+
3+
type Monaco = { editor: typeof MonacoEditorNamespace };
4+
5+
type RecursiveObj = { [key: string]: RecursiveObj | undefined };
6+
7+
export function isMonaco(obj: unknown): obj is Monaco {
8+
return (
9+
// The optional chaining should make the cast safe enough.
10+
typeof (obj as RecursiveObj | null | undefined)?.editor
11+
?.onDidCreateEditor === "function"
12+
);
13+
}

workspaces/leetcode-prettier-extension/src/main.ts

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,62 @@ import { format } from "prettier/standalone";
22
import estreePlugin from "prettier/plugins/estree";
33
import tsPlugin from "prettier/plugins/typescript";
44

5+
import { isMonaco } from "./isMonaco.ts";
6+
57
function main(): void {
6-
// TODO: improve types
7-
let monaco: any = undefined;
8+
let monaco: unknown;
89

910
Object.defineProperty(globalThis, "monaco", {
1011
get() {
1112
return monaco;
1213
},
1314

14-
set(newMonaco) {
15+
set(newMonaco: unknown) {
1516
monaco = newMonaco;
16-
monaco.editor.onDidCreateEditor((ed: any) => {
17+
if (!isMonaco(monaco)) {
18+
console.error(
19+
"The `monaco` property doesn't follow the expected interface!",
20+
);
21+
return;
22+
}
23+
24+
monaco.editor.onDidCreateEditor((ed) => {
25+
ed
26+
?.getModel?.()
27+
?.updateOptions?.({ tabSize: 2, indentSize: "tabSize" });
28+
29+
if (typeof ed?.getAction !== "function") {
30+
// TODO: console.error something interesting
31+
return;
32+
}
33+
1734
const { getAction } = ed;
35+
1836
ed.getAction = function (this: unknown) {
19-
const action = getAction.apply(this, arguments);
20-
action.run = function () {
21-
format(ed.getValue(), {
22-
parser: "typescript",
23-
plugins: [estreePlugin, tsPlugin],
24-
}).then((text) =>
37+
const action = getAction.apply(
38+
this,
39+
// Slight lie but `.apply` will work with the `arguments` object.
40+
arguments as unknown as Parameters<typeof getAction>,
41+
);
42+
if (!action) {
43+
// TODO: console.error something interesting
44+
return action;
45+
}
46+
47+
action.run = async function () {
48+
try {
49+
const formattedText = await format(ed.getValue(), {
50+
parser: "typescript",
51+
plugins: [estreePlugin, tsPlugin],
52+
});
53+
2554
// TODO: switch to https://microsoft.github.io/monaco-editor/typedoc/interfaces/editor.ITextModel.html#pushEditOperations.pushEditOperations-1 in the future
26-
ed.setValue(text),
27-
);
55+
ed.setValue(formattedText);
56+
} catch (err) {
57+
console.error(err);
58+
}
2859
};
60+
2961
return action;
3062
};
3163
});

yarn.lock

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)