Skip to content

Commit aaa8add

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

File tree

4 files changed

+64
-12
lines changed

4 files changed

+64
-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: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,60 @@ 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+
if (typeof ed?.getAction !== "function") {
26+
// TODO: console.error something interesting
27+
return;
28+
}
29+
1730
const { getAction } = ed;
31+
1832
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) =>
33+
ed.getModel()?.updateOptions({ tabSize: 2, indentSize: "tabSize" });
34+
35+
const action = getAction.apply(
36+
this,
37+
// Slight lie but `.apply` will work with the `arguments` object.
38+
arguments as unknown as Parameters<typeof getAction>,
39+
);
40+
if (!action) {
41+
// TODO: console.error something interesting
42+
return action;
43+
}
44+
45+
action.run = async function () {
46+
try {
47+
const formattedText = await format(ed.getValue(), {
48+
parser: "typescript",
49+
plugins: [estreePlugin, tsPlugin],
50+
});
51+
2552
// 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-
);
53+
ed.setValue(formattedText);
54+
} catch (err) {
55+
console.error(err);
56+
}
2857
};
58+
2959
return action;
3060
};
3161
});

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)