Skip to content

Commit 2782ade

Browse files
authored
Respect undo/redo stack in LeetCode Prettier extension (#496)
The previous method of editing the editor content would destroy the history of the user's edits. This doesn't!
1 parent c7949ae commit 2782ade

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

workspaces/leetcode-prettier-extension/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
"typecheck": "code-chronicles-typecheck"
2323
},
2424
"dependencies": {
25+
"@code-chronicles/util": "workspace:*",
26+
"nullthrows": "patch:nullthrows@npm%3A1.1.1#~/.yarn/patches/nullthrows-npm-1.1.1-3d1f817134.patch",
2527
"prettier": "3.3.3"
2628
},
2729
"devDependencies": {

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

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import nullthrows from "nullthrows";
12
import { format } from "prettier/standalone";
23
import estreePlugin from "prettier/plugins/estree";
34
import tsPlugin from "prettier/plugins/typescript";
45

6+
import { assignFunctionCosmeticProperties } from "@code-chronicles/util/object-properties/assignFunctionCosmeticProperties";
7+
58
import { isMonaco } from "./isMonaco.ts";
69

710
function main(): void {
@@ -22,44 +25,55 @@ function main(): void {
2225
}
2326

2427
monaco.editor.onDidCreateEditor((ed) => {
25-
ed
26-
?.getModel?.()
27-
?.updateOptions?.({ tabSize: 2, indentSize: "tabSize" });
28+
try {
29+
nullthrows(ed.getModel()).updateOptions({
30+
tabSize: 2,
31+
indentSize: "tabSize",
32+
});
33+
} catch (err) {
34+
console.error(err);
35+
}
2836

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

3442
const { getAction } = ed;
35-
36-
ed.getAction = function (this: unknown) {
43+
ed.getAction = assignFunctionCosmeticProperties(function (
44+
this: unknown,
45+
) {
3746
const action = getAction.apply(
3847
this,
3948
// Slight lie but `.apply` will work with the `arguments` object.
4049
arguments as unknown as Parameters<typeof getAction>,
4150
);
42-
if (!action) {
43-
// TODO: console.error something interesting
51+
52+
if (typeof action?.run !== "function") {
53+
console.error("Monaco action object doesn't have a `run` method!");
4454
return action;
4555
}
4656

47-
action.run = async function () {
57+
action.run = assignFunctionCosmeticProperties(async function () {
4858
try {
4959
const formattedText = await format(ed.getValue(), {
5060
parser: "typescript",
5161
plugins: [estreePlugin, tsPlugin],
5262
});
5363

54-
// TODO: switch to https://microsoft.github.io/monaco-editor/typedoc/interfaces/editor.ITextModel.html#pushEditOperations.pushEditOperations-1 in the future
55-
ed.setValue(formattedText);
64+
ed.executeEdits("Prettier", [
65+
{
66+
range: nullthrows(ed.getModel()).getFullModelRange(),
67+
text: formattedText,
68+
},
69+
]);
5670
} catch (err) {
5771
console.error(err);
5872
}
59-
};
73+
}, action.run);
6074

6175
return action;
62-
};
76+
}, getAction);
6377
});
6478
},
6579
});

yarn.lock

Lines changed: 2 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)