1+ import nullthrows from "nullthrows" ;
12import { format } from "prettier/standalone" ;
23import estreePlugin from "prettier/plugins/estree" ;
34import tsPlugin from "prettier/plugins/typescript" ;
45
6+ import { assignFunctionCosmeticProperties } from "@code-chronicles/util/object-properties/assignFunctionCosmeticProperties" ;
7+
58import { isMonaco } from "./isMonaco.ts" ;
69
710function 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 } ) ;
0 commit comments