@@ -6,36 +6,36 @@ import { Language } from "./codeEditorTypes";
6
6
7
7
export async function cssFormatter ( text : string ) {
8
8
const prettier = await import ( "prettier/standalone" ) ;
9
- const parserPlugin = await import ( "prettier/parser-postcss" ) ;
10
- return prettier . format ( text , { parser : "css" , plugins : [ parserPlugin ] , semi : false } ) . trim ( ) ;
9
+ const parserPlugin = await require ( "prettier/parser-postcss" ) ;
10
+ return ( await prettier . format ( text , { parser : "css" , plugins : [ parserPlugin ] , semi : false } ) ) . trim ( ) ;
11
11
}
12
12
13
13
export async function htmlFormatter ( text : string ) {
14
14
const prettier = await import ( "prettier/standalone" ) ;
15
- const parserPlugin = await import ( "prettier/parser-html" ) ;
16
- return prettier . format ( text , { parser : "html" , plugins : [ parserPlugin ] , semi : false } ) . trim ( ) ;
15
+ const parserPlugin = await require ( "prettier/parser-html" ) ;
16
+ return ( await prettier . format ( text , { parser : "html" , plugins : [ parserPlugin ] , semi : false } ) ) . trim ( ) ;
17
17
}
18
18
19
19
async function getJavascriptFormatter ( ) {
20
20
const prettier = await import ( "prettier/standalone" ) ;
21
- const parserBabel = await import ( "prettier/parser-babel" ) ;
22
- return ( text : string ) =>
23
- prettier . format ( text , { parser : "babel" , plugins : [ parserBabel ] , semi : false } ) . trim ( ) ;
21
+ const parserBabel = await require ( "prettier/parser-babel" ) ;
22
+ return async ( text : string ) =>
23
+ ( await prettier . format ( text , { parser : "babel" , plugins : [ parserBabel ] , semi : false } ) ) . trim ( ) ;
24
24
}
25
25
26
26
export async function getJsonFormatter ( ) {
27
27
const prettier = await import ( "prettier/standalone" ) ;
28
- const parserBabel = await import ( "prettier/parser-babel" ) ;
29
- return ( text : string ) => prettier . format ( text , { parser : "json" , plugins : [ parserBabel ] } ) . trim ( ) ;
28
+ const parserBabel = await require ( "prettier/parser-babel" ) ;
29
+ return async ( text : string ) => ( await prettier . format ( text , { parser : "json" , plugins : [ parserBabel ] } ) ) . trim ( ) ;
30
30
}
31
31
32
- function formatJsSegment ( formatter : ( text : string ) => string , script : string ) {
32
+ async function formatJsSegment ( formatter : ( text : string ) => Promise < string > , script : string ) : Promise < string > {
33
33
try {
34
- const s = formatter ( script ) ;
34
+ const s = await formatter ( script ) ;
35
35
return s . startsWith ( ";" ) ? s . slice ( 1 ) : s ;
36
36
} catch ( e1 ) {
37
37
try {
38
- const s = formatter ( `return (${ script } \n);` ) ; // same as evalScript()
38
+ const s = await formatter ( `return (${ script } \n);` ) ; // same as evalScript()
39
39
return s . startsWith ( "return " ) ? s . slice ( 7 ) : s ;
40
40
} catch ( e2 ) {
41
41
throw e1 ;
@@ -45,7 +45,9 @@ function formatJsSegment(formatter: (text: string) => string, script: string) {
45
45
46
46
async function getJsSegmentFormatter ( ) {
47
47
const formatter = await getJavascriptFormatter ( ) ;
48
- return ( segment : string ) => "{{" + formatJsSegment ( formatter , segment . slice ( 2 , - 2 ) ) + "}}" ;
48
+ return async ( segment : string ) => {
49
+ return "{{" + formatJsSegment ( formatter , segment . slice ( 2 , - 2 ) ) + "}}" ;
50
+ } ;
49
51
}
50
52
51
53
export async function formatStringWithJsSnippets ( text : string ) : Promise < string > {
@@ -75,15 +77,27 @@ export async function formatSqlWithJsSnippets(text: string) {
75
77
if ( jsSegments . length === 0 ) {
76
78
return newText ;
77
79
}
78
- return newText . replace ( / { { \d + } } / g, ( s ) => {
80
+ const replacements : Promise < string > [ ] = [ ] ;
81
+ const replacedText = newText . replace ( / { { \d + } } / g, ( s ) => {
79
82
const index = parseInt ( s . slice ( 4 , - 4 ) ) ;
80
83
if ( index >= 0 && index < jsSegments . length ) {
81
- return jsSegmentFormatter ( jsSegments [ index ] ) ;
84
+ const replacement = jsSegmentFormatter ( jsSegments [ index ] ) ;
85
+ replacements . push ( replacement ) ;
86
+ return s ; // Return the original placeholder for now
82
87
}
83
88
return s ;
84
89
} ) ;
90
+
91
+ const formattedSegments = await Promise . all ( replacements ) ;
92
+ let finalText = replacedText ;
93
+ formattedSegments . forEach ( ( formattedSegment , index ) => {
94
+ finalText = finalText . replace ( `{ { ${ index } } }` , formattedSegment ) ;
95
+ } ) ;
96
+
97
+ return finalText ;
85
98
}
86
99
100
+
87
101
async function formatJsonWithJsSnippetsImpl ( text : string ) {
88
102
if ( ! text || text . trim ( ) . length === 0 ) {
89
103
return "" ;
@@ -108,15 +122,20 @@ async function formatJsonWithJsSnippetsImpl(text: string) {
108
122
// here are 3 cases.
109
123
// - when the original "{{}}" is not in quotes as the single key or value, the whole "{{ index }}" should be replaced.
110
124
// - when the original "{{}}" is for concatenating strings, "{{ index }}" or "\\{\\{ index \\}\\}" should be replaced.
111
- return formattedJSON . replace ( / ( " { { \d + } } " ) | ( { { \d + } } ) | ( \\ \\ { \\ \\ { \d + \\ \\ } \\ \\ } ) / g, ( s ) => {
125
+ const formattedSegments = await Promise . all ( segments . map ( async ( segment ) => {
126
+ if ( isDynamicSegment ( segment ) ) {
127
+ const formattedSegment = await jsSegmentFormatter ( segment ) ;
128
+ return formattedSegment ;
129
+ }
130
+ return segment ;
131
+ } ) ) ;
132
+
133
+ return ( await formattedJSON ) . replace ( / ( " { { \d + } } " ) | ( { { \d + } } ) | ( \\ \\ { \\ \\ { \d + \\ \\ } \\ \\ } ) / g, ( s ) => {
112
134
const index = parseInt (
113
135
s . startsWith ( '"{{' ) ? s . slice ( 3 , - 3 ) : s . startsWith ( "{{" ) ? s . slice ( 2 , - 2 ) : s . slice ( 6 , - 6 )
114
136
) ;
115
- if ( index >= 0 && index < segments . length ) {
116
- const segment = segments [ index ] ;
117
- if ( isDynamicSegment ( segment ) ) {
118
- return jsSegmentFormatter ( segment ) ;
119
- }
137
+ if ( index >= 0 && index < formattedSegments . length ) {
138
+ return formattedSegments [ index ] ;
120
139
}
121
140
return s ;
122
141
} ) ;
0 commit comments