Skip to content

Commit 02e241e

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

File tree

4 files changed

+49
-11
lines changed

4 files changed

+49
-11
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 Obj = { [key: string]: Obj | undefined };
6+
7+
export function isMonaco(obj: unknown): obj is Monaco {
8+
return (
9+
// The optional chaining should make this safe.
10+
typeof (obj as Obj | null | undefined)?.editor?.onDidCreateEditor ===
11+
"function"
12+
);
13+
}

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

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,46 @@ 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) => {
1725
const { getAction } = ed;
1826
ed.getAction = function (this: unknown) {
19-
const action = getAction.apply(this, arguments);
20-
action.run = function () {
21-
format(ed.getValue(), {
27+
ed.getModel()?.updateOptions({ tabSize: 2, indentSize: "tabSize" });
28+
29+
// TODO: remove any
30+
const action = getAction.apply(this, arguments as any);
31+
if (!action) {
32+
return action;
33+
}
34+
35+
action.run = async function () {
36+
const text = await format(ed.getValue(), {
2237
parser: "typescript",
2338
plugins: [estreePlugin, tsPlugin],
24-
}).then((text) =>
25-
// 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-
);
39+
});
40+
41+
// TODO: switch to https://microsoft.github.io/monaco-editor/typedoc/interfaces/editor.ITextModel.html#pushEditOperations.pushEditOperations-1 in the future
42+
ed.setValue(text);
2843
};
44+
2945
return action;
3046
};
3147
});

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)