1
- import { Range , Selection , TextDocument } from " vscode" ;
2
- import ConfigurationProvider from " ./ConfigurationProvider" ;
3
- import StringProcessingProvider , { Folding } from " ./StringProcessingProvider" ;
1
+ import { Range , Selection , TextDocument } from ' vscode' ;
2
+ import ConfigurationProvider from ' ./ConfigurationProvider' ;
3
+ import StringProcessingProvider , { Folding } from ' ./StringProcessingProvider' ;
4
4
5
- type SortingStrategy = " asc" | " desc" | " ascNatural" | " descNatural" ;
5
+ type SortingStrategy = ' asc' | ' desc' | ' ascNatural' | ' descNatural' ;
6
6
7
7
export default class BlockSortProvider {
8
8
public static sort : Record < SortingStrategy , ( a : string , b : string ) => number > = {
@@ -15,12 +15,13 @@ export default class BlockSortProvider {
15
15
protected static padNumbers ( line : string ) {
16
16
const { omitUuids, padding, sortNegativeValues } = ConfigurationProvider . getNaturalSortOptions ( ) ;
17
17
let result = line ;
18
- if ( omitUuids ) result = result . replace ( / \d + (? = [ ^ a - z A - z ] | $ ) | (?< = [ ^ a - z A - z ] | ^ ) \d + / g, ( match ) => match . padStart ( padding , "0" ) ) ;
19
- else result = result . replace ( / \d + / g, ( match ) => match . padStart ( padding , "0" ) ) ;
18
+ if ( omitUuids )
19
+ result = result . replace ( / \d + (? = [ ^ a - z A - z ] | $ ) | (?< = [ ^ a - z A - z ] | ^ ) \d + / g, ( match ) => match . padStart ( padding , '0' ) ) ;
20
+ else result = result . replace ( / \d + / g, ( match ) => match . padStart ( padding , '0' ) ) ;
20
21
21
22
if ( sortNegativeValues ) {
22
23
result = result . replace (
23
- new RegExp ( `-\\d{${ padding } }` , "g" ) ,
24
+ new RegExp ( `-\\d{${ padding } }` , 'g' ) ,
24
25
( match ) => `-${ ( Math . pow ( 10 , padding ) + parseInt ( match ) ) . toString ( ) } `
25
26
) ;
26
27
}
@@ -46,20 +47,20 @@ export default class BlockSortProvider {
46
47
textBlocks = textBlocks . map ( ( block ) => this . sortBlockHeaders ( block , sort ) ) ;
47
48
48
49
if ( this . stringProcessor . isList ( blocks ) && textBlocks . length && ! / , $ / . test ( textBlocks [ textBlocks . length - 1 ] ) ) {
49
- textBlocks [ textBlocks . length - 1 ] += "," ;
50
+ textBlocks [ textBlocks . length - 1 ] += ',' ;
50
51
this . applySort ( textBlocks , sort ) ;
51
- textBlocks [ textBlocks . length - 1 ] = textBlocks [ textBlocks . length - 1 ] . replace ( / , \s * $ / , "" ) ;
52
+ textBlocks [ textBlocks . length - 1 ] = textBlocks [ textBlocks . length - 1 ] . replace ( / , \s * $ / , '' ) ;
52
53
} else {
53
54
this . applySort ( textBlocks , sort ) ;
54
55
}
55
56
56
57
if ( textBlocks . length && ! textBlocks [ 0 ] . trim ( ) ) {
57
- textBlocks . push ( textBlocks . shift ( ) || "" ) ;
58
+ textBlocks . push ( textBlocks . shift ( ) || '' ) ;
58
59
} else if ( textBlocks . length && / ^ \s * \r ? \n / . test ( textBlocks [ 0 ] ) ) {
59
60
// For some reason a newline for the second block gets left behind sometimes
60
61
const front = ! / \r ? \n $ / . test ( textBlocks [ 0 ] ) && textBlocks [ 1 ] && ! / ^ \r ? \n / . test ( textBlocks [ 1 ] ) ;
61
- textBlocks [ 0 ] = textBlocks [ 0 ] . replace ( / ^ \s * \r ? \n / , "" ) ;
62
- textBlocks [ front ? 0 : textBlocks . length - 1 ] += "\n" ;
62
+ textBlocks [ 0 ] = textBlocks [ 0 ] . replace ( / ^ \s * \r ? \n / , '' ) ;
63
+ textBlocks [ front ? 0 : textBlocks . length - 1 ] += '\n' ;
63
64
}
64
65
65
66
return textBlocks ;
@@ -69,7 +70,7 @@ export default class BlockSortProvider {
69
70
const startLine = range . start . line ;
70
71
const text = this . document . getText ( range ) ;
71
72
const lines = text . split ( / \r ? \n / ) ;
72
- const firstLine = lines . shift ( ) || "" ;
73
+ const firstLine = lines . shift ( ) || '' ;
73
74
const initialIndent = this . stringProcessor . getIndent ( firstLine ) ;
74
75
const blocks : Range [ ] = [ ] ;
75
76
@@ -82,15 +83,15 @@ export default class BlockSortProvider {
82
83
if (
83
84
validBlock &&
84
85
this . stringProcessor . stripComments ( currentBlock ) . trim ( ) &&
85
- ! this . stringProcessor . isInCompleteBlock ( currentBlock ) &&
86
+ ! this . stringProcessor . isIncompleteBlock ( currentBlock ) &&
86
87
( ! this . stringProcessor . isIndentIgnoreLine ( line ) || this . stringProcessor . isCompleteBlock ( currentBlock ) ) &&
87
88
this . stringProcessor . getIndent ( line ) === initialIndent &&
88
89
! this . stringProcessor . hasFolding ( folding )
89
90
) {
90
91
blocks . push ( this . document . validateRange ( new Range ( startLine + lastStart , 0 , startLine + currentEnd , Infinity ) ) ) ;
91
92
lastStart = currentEnd + 1 ;
92
93
currentEnd = lastStart ;
93
- currentBlock = "" ;
94
+ currentBlock = '' ;
94
95
validBlock = false ;
95
96
} else {
96
97
currentEnd ++ ;
@@ -185,14 +186,16 @@ export default class BlockSortProvider {
185
186
if ( sortChildren === 0 ) return this . document . getText ( block ) ;
186
187
187
188
let blocks = this . getInnerBlocks ( block ) ;
188
- if ( ! blocks . length ) return this . document . getText ( block ) ;
189
+ // if (!blocks.length || (blocks.length === 1 && blocks[0].isSingleLine) ) return this.document.getText(block);
189
190
190
191
const head : Range = new Range ( block . start , blocks [ 0 ] ?. start || block . start ) ;
191
192
const tail : Range = new Range ( blocks [ blocks . length - 1 ] ?. end || block . end , block . end ) ;
192
193
194
+ if ( head . isEmpty && tail . isEmpty ) return this . document . getText ( block ) ;
195
+
193
196
return (
194
197
this . document . getText ( head ) +
195
- this . sortBlocks ( blocks , sort , sortChildren - 1 ) . join ( "\n" ) +
198
+ this . sortBlocks ( blocks , sort , sortChildren - 1 ) . join ( '\n' ) +
196
199
this . document . getText ( tail )
197
200
) ;
198
201
}
@@ -211,19 +214,18 @@ export default class BlockSortProvider {
211
214
this . applySort ( headers , sort ) ;
212
215
lines = [ ...headers , ...lines ] ;
213
216
214
- return lines . join ( "\n" ) ;
217
+ return lines . join ( '\n' ) ;
215
218
}
216
219
217
220
private applySort ( blocks : string [ ] , sort : ( a : string , b : string ) => number = BlockSortProvider . sort . asc ) {
218
- blocks . sort ( ( a , b ) =>
219
- this . stringProcessor . isForceFirstBlock ( a ) || this . stringProcessor . isForceLastBlock ( b )
221
+ blocks . sort ( ( a , b ) => {
222
+ const sanitizedA = this . stringProcessor . stripDecorators ( this . stringProcessor . stripComments ( a ) ) . trim ( ) || a . trim ( ) ;
223
+ const sanitizedB = this . stringProcessor . stripDecorators ( this . stringProcessor . stripComments ( b ) ) . trim ( ) || b . trim ( ) ;
224
+ return this . stringProcessor . isForceFirstBlock ( sanitizedA ) || this . stringProcessor . isForceLastBlock ( sanitizedB )
220
225
? - 1
221
- : this . stringProcessor . isForceLastBlock ( a )
226
+ : this . stringProcessor . isForceLastBlock ( sanitizedA ) || this . stringProcessor . isForceFirstBlock ( sanitizedB )
222
227
? 1
223
- : sort (
224
- this . stringProcessor . stripDecorators ( this . stringProcessor . stripComments ( a ) ) . trim ( ) || a . trim ( ) ,
225
- this . stringProcessor . stripDecorators ( this . stringProcessor . stripComments ( b ) ) . trim ( ) || b . trim ( )
226
- )
227
- ) ;
228
+ : sort ( sanitizedA , sanitizedB ) ;
229
+ } ) ;
228
230
}
229
231
}
0 commit comments