Skip to content

Commit 54b4c35

Browse files
committed
Specify tab/indent size in LeetCode Prettier extension
1 parent 67966ca commit 54b4c35

File tree

5 files changed

+47
-13
lines changed

5 files changed

+47
-13
lines changed

.github/workflows/run-tests.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ jobs:
1515
# This is the version used by LeetCode.
1616
- 20.10.0
1717
- lts
18-
- latest
18+
# TODO: replace with latest, it was causing some Yarn checksum issues recently though
19+
- 22.6.0
1920

2021
runs-on: ubuntu-latest
2122

.github/workflows/set-up-everything/action.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ name: Set Up Everything
33
inputs:
44
node-version:
55
required: true
6-
default: latest
6+
# TODO: replace with latest, it was causing some Yarn checksum issues recently though
7+
default: 22.6.0
78

89
runs:
910
using: composite

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",

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

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,54 @@
1+
import type { editor as MonacoEditorNamespace } from "monaco-editor";
12
import { format } from "prettier/standalone";
23
import estreePlugin from "prettier/plugins/estree";
34
import tsPlugin from "prettier/plugins/typescript";
45

6+
type Monaco = { editor: typeof MonacoEditorNamespace };
7+
8+
type Obj = { [key: string]: Obj | undefined };
9+
10+
function isMonaco(obj: unknown): obj is Monaco {
11+
return typeof (obj as Obj)?.editor?.onDidCreateEditor === "function";
12+
}
13+
514
function main(): void {
6-
// TODO: improve types
7-
let monaco: any = undefined;
15+
let monaco: unknown;
816

917
Object.defineProperty(globalThis, "monaco", {
1018
get() {
1119
return monaco;
1220
},
1321

14-
set(newMonaco) {
22+
set(newMonaco: unknown) {
1523
monaco = newMonaco;
16-
monaco.editor.onDidCreateEditor((ed: any) => {
24+
if (!isMonaco(monaco)) {
25+
console.error(
26+
"The `monaco` property doesn't follow the expected interface!",
27+
);
28+
return;
29+
}
30+
31+
monaco.editor.onDidCreateEditor((ed) => {
1732
const { getAction } = ed;
1833
ed.getAction = function (this: unknown) {
19-
const action = getAction.apply(this, arguments);
20-
action.run = function () {
21-
format(ed.getValue(), {
34+
ed.getModel()?.updateOptions({ tabSize: 2, indentSize: "tabSize" });
35+
36+
// TODO: remove any
37+
const action = getAction.apply(this, arguments as any);
38+
if (!action) {
39+
return action;
40+
}
41+
42+
action.run = async function () {
43+
const text = await format(ed.getValue(), {
2244
parser: "typescript",
2345
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-
);
46+
});
47+
48+
// TODO: switch to https://microsoft.github.io/monaco-editor/typedoc/interfaces/editor.ITextModel.html#pushEditOperations.pushEditOperations-1 in the future
49+
ed.setValue(text);
2850
};
51+
2952
return action;
3053
};
3154
});

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)