1
1
import prettier , { Options } from 'prettier' ;
2
2
import { workspace } from 'vscode' ;
3
3
4
+ import { Snippet } from './generateSnippets' ;
4
5
import { SnippetPlaceholders } from './types' ;
5
6
6
7
export type ExtensionSettings = {
@@ -37,6 +38,11 @@ const getPrettierConfig = (): Options => {
37
38
} ;
38
39
} ;
39
40
41
+ const formatSnippet = ( string : string ) => {
42
+ const prettierConfig = getPrettierConfig ( ) ;
43
+ return prettier . format ( string , prettierConfig ) ;
44
+ } ;
45
+
40
46
export const replaceSnippetPlaceholders = ( snippetString : string ) =>
41
47
String ( snippetString )
42
48
. replace (
@@ -59,15 +65,75 @@ export const revertSnippetPlaceholders = (snippetString: string) =>
59
65
. replace ( new RegExp ( / \$ { 3:t h i r d } / , 'g' ) , SnippetPlaceholders . ThirdTab )
60
66
. replace ( new RegExp ( / \$ 0 / , 'g' ) , SnippetPlaceholders . LastTab ) ;
61
67
62
- export const formatSnippet = ( string : string ) => {
63
- const prettierConfig = getPrettierConfig ( ) ;
64
- return prettier . format ( string , prettierConfig ) ;
65
- } ;
66
-
67
68
export const parseSnippet = ( body : string | string [ ] ) => {
68
69
const snippetBody = typeof body === 'string' ? body : body . join ( '\n' ) ;
69
70
70
71
return replaceSnippetPlaceholders (
71
72
formatSnippet ( revertSnippetPlaceholders ( snippetBody ) ) ,
72
73
) ;
73
74
} ;
75
+
76
+ // This is array of prefixes which are currently skipped because of syntax format issues
77
+ const skippedSnippets = [
78
+ 'pge' ,
79
+ 'pse' ,
80
+ 'gdsfp' ,
81
+ 'gsbu' ,
82
+ 'scu' ,
83
+ 'cwun' ,
84
+ 'cdm' ,
85
+ 'cdup' ,
86
+ 'rconst' ,
87
+ ] ;
88
+
89
+ const withReactImport = [
90
+ 'rfce' ,
91
+ 'rfc' ,
92
+ 'rfcp' ,
93
+ 'rafce' ,
94
+ 'rafc' ,
95
+ 'rafcp' ,
96
+ 'rnfe' ,
97
+ 'rnfes' ,
98
+ 'rnf' ,
99
+ 'rnfs' ,
100
+ 'stest' ,
101
+ 'sntest' ,
102
+ 'srtest' ,
103
+ 'snrtest' ,
104
+ 'hocredux' ,
105
+ 'hoc' ,
106
+ ] ;
107
+
108
+ const replaceOrRemoveReactImport = ( snippetBody : string [ ] ) => {
109
+ const reactImportIndex = snippetBody . findIndex ( ( line ) =>
110
+ line . match ( new RegExp ( / i m p o r t R e a c t / , 'g' ) ) ,
111
+ ) ;
112
+
113
+ if ( reactImportIndex !== - 1 ) {
114
+ const line = snippetBody [ reactImportIndex ] ;
115
+ snippetBody [ reactImportIndex ] = line
116
+ . replace ( new RegExp ( / ^ i m p o r t R e a c t .* $ / , 'g' ) , '' )
117
+ . replace ( new RegExp ( / ^ i m p o r t R e a c t , / , 'g' ) , 'import ' ) ;
118
+ }
119
+
120
+ return snippetBody . join ( '\n' ) ;
121
+ } ;
122
+
123
+ export const parseSnippetToBody = ( snippet : Snippet ) => {
124
+ const { importReactOnTop } = extensionConfig ( ) ;
125
+ const body =
126
+ typeof snippet . body === 'string' ? snippet . body : snippet . body . join ( '\n' ) ;
127
+
128
+ const snippetBody = importReactOnTop
129
+ ? body
130
+ : withReactImport . includes ( snippet . prefix )
131
+ ? replaceOrRemoveReactImport ( snippet . body )
132
+ : body ;
133
+
134
+ const formattedSnippet = skippedSnippets . includes ( snippet . prefix )
135
+ ? snippetBody
136
+ : formatSnippet ( snippetBody ) . split ( '\n' ) ;
137
+
138
+ return snippet . body . length === 1 ? formattedSnippet [ 0 ] : formattedSnippet ;
139
+ } ;
0 commit comments