1
+ import nullthrows from "nullthrows" ;
1
2
import { format } from "prettier/standalone" ;
2
3
import estreePlugin from "prettier/plugins/estree" ;
3
4
import tsPlugin from "prettier/plugins/typescript" ;
4
5
6
+ import { assignFunctionCosmeticProperties } from "@code-chronicles/util/object-properties/assignFunctionCosmeticProperties" ;
7
+
5
8
import { isMonaco } from "./isMonaco.ts" ;
6
9
7
10
function main ( ) : void {
@@ -22,44 +25,55 @@ function main(): void {
22
25
}
23
26
24
27
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
+ }
28
36
29
37
if ( typeof ed ?. getAction !== "function" ) {
30
- // TODO: console.error something interesting
38
+ console . error ( "Monaco editor doesn't have a `getAction` method!" ) ;
31
39
return ;
32
40
}
33
41
34
42
const { getAction } = ed ;
35
-
36
- ed . getAction = function ( this : unknown ) {
43
+ ed . getAction = assignFunctionCosmeticProperties ( function (
44
+ this : unknown ,
45
+ ) {
37
46
const action = getAction . apply (
38
47
this ,
39
48
// Slight lie but `.apply` will work with the `arguments` object.
40
49
arguments as unknown as Parameters < typeof getAction > ,
41
50
) ;
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!" ) ;
44
54
return action ;
45
55
}
46
56
47
- action . run = async function ( ) {
57
+ action . run = assignFunctionCosmeticProperties ( async function ( ) {
48
58
try {
49
59
const formattedText = await format ( ed . getValue ( ) , {
50
60
parser : "typescript" ,
51
61
plugins : [ estreePlugin , tsPlugin ] ,
52
62
} ) ;
53
63
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
+ ] ) ;
56
70
} catch ( err ) {
57
71
console . error ( err ) ;
58
72
}
59
- } ;
73
+ } , action . run ) ;
60
74
61
75
return action ;
62
- } ;
76
+ } , getAction ) ;
63
77
} ) ;
64
78
} ,
65
79
} ) ;
0 commit comments