@@ -448,13 +448,7 @@ class Resolver {
448
448
return parsedNamespaces ;
449
449
}
450
450
451
- sortImports ( ) {
452
- let [ useStatements , ] = this . getDeclarations ( ) ;
453
-
454
- if ( useStatements . length <= 1 ) {
455
- throw new Error ( '$(issue-opened) Nothing to sort.' ) ;
456
- }
457
-
451
+ getSortFunction ( ) {
458
452
let sortFunction = ( a , b ) => {
459
453
if ( this . config ( 'sortAlphabetically' ) ) {
460
454
if ( a . text . toLowerCase ( ) < b . text . toLowerCase ( ) ) return - 1 ;
@@ -481,7 +475,27 @@ class Resolver {
481
475
} ;
482
476
}
483
477
484
- let sorted = useStatements . slice ( ) . sort ( sortFunction ) ;
478
+ return sortFunction ;
479
+ }
480
+
481
+ sortImports ( ) {
482
+ const sortFunction = this . getSortFunction ( ) ;
483
+
484
+ const [ useStatements , sorted ] = ( ( ) => {
485
+ if ( this . config ( 'sortBlockWise' ) ) {
486
+ const useStatements = this . getUseStatementsArray ( true ) ;
487
+ if ( useStatements . length === 0 || useStatements [ 0 ] . length <= 1 ) {
488
+ throw new Error ( '$(issue-opened) Nothing to sort.' ) ;
489
+ }
490
+ return [ useStatements . flat ( ) , useStatements . map ( block => block . slice ( ) . sort ( sortFunction ) ) . flat ( ) ] ;
491
+ }
492
+
493
+ const useStatements = this . getDeclarations ( ) ;
494
+ if ( useStatements . length <= 1 ) {
495
+ throw new Error ( '$(issue-opened) Nothing to sort.' ) ;
496
+ }
497
+ return [ useStatements , useStatements . slice ( ) . sort ( sortFunction ) ] ;
498
+ } ) ( ) ;
485
499
486
500
this . activeEditor ( ) . edit ( textEdit => {
487
501
for ( let i = 0 ; i < sorted . length ; i ++ ) {
@@ -507,21 +521,28 @@ class Resolver {
507
521
return false ;
508
522
}
509
523
510
- getUseStatementsArray ( ) {
511
- let useStatements = [ ] ;
524
+ getUseStatementsArray ( blocked = false ) {
525
+ const useStatements = [ ] ;
526
+ const matchRegex = new RegExp ( / ^ ( u s e \s [ \w \\ ] + ; ) / , 'g' ) ;
512
527
513
528
for ( let line = 0 ; line < this . activeEditor ( ) . document . lineCount ; line ++ ) {
514
- let text = this . activeEditor ( ) . document . lineAt ( line ) . text ;
515
-
516
- if ( text . startsWith ( 'use ' ) ) {
517
- useStatements . push (
518
- text . match ( / ( \w + ?) ; / ) [ 1 ]
519
- ) ;
529
+ const text = this . activeEditor ( ) . document . lineAt ( line ) . text ;
530
+ const matchRes = text . match ( matchRegex ) ;
531
+
532
+ if ( matchRes != null ) {
533
+ const prevLine = this . activeEditor ( ) . document . lineAt ( Math . max ( 0 , line - 1 ) ) . text ;
534
+ if ( prevLine === '' || ! prevLine . match ( matchRegex ) || useStatements . length === 0 )
535
+ useStatements . push ( [ { text : matchRes [ 0 ] , line } ] ) ;
536
+ else
537
+ useStatements [ useStatements . length - 1 ] . push ( { text : matchRes [ 0 ] , line } ) ;
520
538
} else if ( / ( c l a s s | t r a i t | i n t e r f a c e ) \s + \w + / . test ( text ) ) {
521
539
break ;
522
540
}
523
541
}
524
542
543
+ if ( ! blocked )
544
+ return useStatements . flat ( ) ;
545
+
525
546
return useStatements ;
526
547
}
527
548
0 commit comments