@@ -146,27 +146,30 @@ export const CodeActionsProvider: vscode.CodeActionProvider = {
146146 }
147147 else {
148148 const regexTree = regexTrees . get ( id ) ;
149- const rootNode = regexTree . rootNode ;
149+ const rootNode = regexTree ? .rootNode ;
150150 rootNodes . push ( rootNode ) ;
151151 }
152152
153153 const edit = new vscode . WorkspaceEdit ;
154154
155+ const minifyQuery = `;scm
156+ (backslash) @backslash
157+ (comment_group) @comment
158+ (comment_extended) @comment
159+ (non_capture_group) @non_capture_group
160+ (non_capture_group_extended) @non_capture_group
161+ ` ;
155162 for ( const rootNode of rootNodes ) {
156- const minifyQuery = `;scm
157- (backslash) @backslash
158- (comment_group) @comment
159- (comment_extended) @comment
160- (non_capture_group) @non_capture_group
161- (non_capture_group_extended) @non_capture_group
162- ` ;
163163 const minifyCaptures = queryNode ( rootNode , minifyQuery ) ;
164164 for ( const minifyCapture of minifyCaptures ) {
165165 const minifyName = minifyCapture . name ;
166166 const minifyNode = minifyCapture . node ;
167167 const minifyRange = toRange ( minifyNode ) ;
168168
169169 switch ( minifyName ) {
170+ case 'comment' :
171+ edit . delete ( uri , minifyRange ) ;
172+ break ;
170173 case 'backslash' :
171174 const text = minifyNode . text ;
172175 switch ( text . lastIndexOf ( '\\' ) ) {
@@ -211,18 +214,17 @@ export const CodeActionsProvider: vscode.CodeActionProvider = {
211214 break ;
212215 }
213216 break ;
214- case 'comment' :
215- edit . delete ( uri , minifyRange ) ;
216- break ;
217217 case 'non_capture_group' :
218218 const childCount = minifyNode . namedChildCount ;
219219
220+ let siblingCommentCount = 0 ; // Only accounts for comments directly after the group
220221 let nextSibling = minifyNode . nextNamedSibling ;
221222 while ( nextSibling ?. type ?. startsWith ( 'comment' ) ) {
222223 nextSibling = nextSibling . nextNamedSibling ;
224+ siblingCommentCount ++ ;
223225 }
224226
225- if ( nextSibling ?. type == 'quantifier' ) {
227+ if ( nextSibling ?. type == 'quantifier' ) { // Only minify group if theres only one valid child inside
226228 if ( childCount > 1 ) { // (?:..)?
227229 break ;
228230 }
@@ -252,7 +254,7 @@ export const CodeActionsProvider: vscode.CodeActionProvider = {
252254 case 'look_around' :
253255 case 'callout' :
254256 case 'alteration' :
255- continue ; // (?:(?=))?
257+ continue ; // Quantifier's can't be placed on certain expressions. (?:(?=))?
256258 default :
257259 break ;
258260 }
@@ -269,7 +271,14 @@ export const CodeActionsProvider: vscode.CodeActionProvider = {
269271 break ;
270272 }
271273 }
272- else if ( minifyNode . parent . namedChildCount > 1 ) { // TODO: ignore comment nodes
274+ else if ( minifyNode . parent . namedChildCount - siblingCommentCount > 1 ) {
275+ if ( childCount == 1 && minifyNode . firstNamedChild . type == 'alteration' ) {
276+ edit . delete ( // `(?:|)`
277+ uri ,
278+ minifyRange ,
279+ ) ;
280+ break ;
281+ }
273282 let stop = false ; // why
274283 for ( const child of minifyNode . namedChildren ) {
275284 const type = child . type ;
@@ -283,7 +292,7 @@ export const CodeActionsProvider: vscode.CodeActionProvider = {
283292 }
284293 }
285294
286- if ( childCount == 1 && minifyNode . firstNamedChild . type == 'non_capture_group' ) { // prevents issues with nested groups / a(?:(?:b|c))/
295+ if ( childCount == 1 && minifyNode . firstNamedChild . type == 'non_capture_group' && minifyNode . firstNamedChild . namedChildCount > 1 ) { // prevents issues with nested groups. a(?:(?:b|c))
287296 break ;
288297 }
289298
0 commit comments