@@ -448,13 +448,7 @@ class Resolver {
448448 return parsedNamespaces ;
449449 }
450450
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 ( ) {
458452 let sortFunction = ( a , b ) => {
459453 if ( this . config ( 'sortAlphabetically' ) ) {
460454 if ( a . text . toLowerCase ( ) < b . text . toLowerCase ( ) ) return - 1 ;
@@ -481,7 +475,27 @@ class Resolver {
481475 } ;
482476 }
483477
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+ } ) ( ) ;
485499
486500 this . activeEditor ( ) . edit ( textEdit => {
487501 for ( let i = 0 ; i < sorted . length ; i ++ ) {
@@ -507,21 +521,28 @@ class Resolver {
507521 return false ;
508522 }
509523
510- getUseStatementsArray ( ) {
511- let useStatements = [ ] ;
524+ getUseStatementsArray ( blocked = false ) {
525+ const useStatements = [ ] ;
526+ const matchRegex = new RegExp ( / ^ ( u s e \s [ \w \\ ] + ; ) / , 'g' ) ;
512527
513528 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 } ) ;
520538 } 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 ) ) {
521539 break ;
522540 }
523541 }
524542
543+ if ( ! blocked )
544+ return useStatements . flat ( ) ;
545+
525546 return useStatements ;
526547 }
527548
0 commit comments