66import { sep } from 'vs/base/common/path' ;
77import { IdleValue } from 'vs/base/common/async' ;
88
9- const intlFileNameCollator : IdleValue < { collator : Intl . Collator , collatorIsNumeric : boolean } > = new IdleValue ( ( ) => {
9+ // When comparing large numbers of strings, such as in sorting large arrays, is better for
10+ // performance to create an Intl.Collator object and use the function provided by its compare
11+ // property than it is to use String.prototype.localeCompare()
12+
13+ // A collator with numeric sorting enabled, and no sensitivity to case or to accents
14+ const intlFileNameCollatorBaseNumeric : IdleValue < { collator : Intl . Collator , collatorIsNumeric : boolean } > = new IdleValue ( ( ) => {
1015 const collator = new Intl . Collator ( undefined , { numeric : true , sensitivity : 'base' } ) ;
1116 return {
1217 collator : collator ,
1318 collatorIsNumeric : collator . resolvedOptions ( ) . numeric
1419 } ;
1520} ) ;
1621
22+ // A collator with numeric sorting enabled.
23+ const intlFileNameCollatorNumeric : IdleValue < { collator : Intl . Collator } > = new IdleValue ( ( ) => {
24+ const collator = new Intl . Collator ( undefined , { numeric : true } ) ;
25+ return {
26+ collator : collator
27+ } ;
28+ } ) ;
29+
30+ // A collator with numeric sorting enabled, and sensitivity to accents and diacritics but not case.
31+ const intlFileNameCollatorNumericCaseInsenstive : IdleValue < { collator : Intl . Collator } > = new IdleValue ( ( ) => {
32+ const collator = new Intl . Collator ( undefined , { numeric : true , sensitivity : 'accent' } ) ;
33+ return {
34+ collator : collator
35+ } ;
36+ } ) ;
37+
1738export function compareFileNames ( one : string | null , other : string | null , caseSensitive = false ) : number {
1839 const a = one || '' ;
1940 const b = other || '' ;
20- const result = intlFileNameCollator . getValue ( ) . collator . compare ( a , b ) ;
41+ const result = intlFileNameCollatorBaseNumeric . getValue ( ) . collator . compare ( a , b ) ;
2142
2243 // Using the numeric option in the collator will
2344 // make compare(`foo1`, `foo01`) === 0. We must disambiguate.
24- if ( intlFileNameCollator . getValue ( ) . collatorIsNumeric && result === 0 && a !== b ) {
45+ if ( intlFileNameCollatorBaseNumeric . getValue ( ) . collatorIsNumeric && result === 0 && a !== b ) {
2546 return a < b ? - 1 : 1 ;
2647 }
2748
2849 return result ;
2950}
3051
52+ /** Compares filenames by name then extension, sorting numbers numerically instead of alphabetically. */
53+ export function compareFileNamesNumeric ( one : string | null , other : string | null ) : number {
54+ const [ oneName , oneExtension ] = extractNameAndExtension ( one , true ) ;
55+ const [ otherName , otherExtension ] = extractNameAndExtension ( other , true ) ;
56+ const collatorNumeric = intlFileNameCollatorNumeric . getValue ( ) . collator ;
57+ const collatorNumericCaseInsensitive = intlFileNameCollatorNumericCaseInsenstive . getValue ( ) . collator ;
58+ let result ;
59+
60+ // Check for name differences, comparing numbers numerically instead of alphabetically.
61+ result = collatorNumeric . compare ( oneName , otherName ) ;
62+ if ( result !== 0 ) {
63+ return result ;
64+ }
65+
66+ // Using the numeric option in the collator will make compare(`foo1`, `foo01`) === 0. Sort the shorter name first.
67+ if ( oneName . length !== otherName . length ) {
68+ return oneName . length < otherName . length ? - 1 : 1 ;
69+ }
70+
71+ // Check for case insensitive extension differences, comparing numbers numerically instead of alphabetically.
72+ result = collatorNumericCaseInsensitive . compare ( oneExtension , otherExtension ) ;
73+ if ( result !== 0 ) {
74+ return result ;
75+ }
76+
77+ // If extensions are numerically equal but not equal in length, sort the shorter extension first.
78+ if ( oneExtension . length !== otherExtension . length ) {
79+ return oneExtension . length < otherExtension . length ? - 1 : 1 ;
80+ }
81+
82+ // Disambiguate the extension case if needed.
83+ if ( oneExtension !== otherExtension ) {
84+ return collatorNumeric . compare ( oneExtension , otherExtension ) ;
85+ }
86+
87+ return 0 ;
88+ }
89+
3190const FileNameMatch = / ^ ( .* ?) ( \. ( [ ^ . ] * ) ) ? $ / ;
3291
3392export function noIntlCompareFileNames ( one : string | null , other : string | null , caseSensitive = false ) : number {
@@ -54,30 +113,77 @@ export function compareFileExtensions(one: string | null, other: string | null):
54113 const [ oneName , oneExtension ] = extractNameAndExtension ( one ) ;
55114 const [ otherName , otherExtension ] = extractNameAndExtension ( other ) ;
56115
57- let result = intlFileNameCollator . getValue ( ) . collator . compare ( oneExtension , otherExtension ) ;
116+ let result = intlFileNameCollatorBaseNumeric . getValue ( ) . collator . compare ( oneExtension , otherExtension ) ;
58117
59118 if ( result === 0 ) {
60119 // Using the numeric option in the collator will
61120 // make compare(`foo1`, `foo01`) === 0. We must disambiguate.
62- if ( intlFileNameCollator . getValue ( ) . collatorIsNumeric && oneExtension !== otherExtension ) {
121+ if ( intlFileNameCollatorBaseNumeric . getValue ( ) . collatorIsNumeric && oneExtension !== otherExtension ) {
63122 return oneExtension < otherExtension ? - 1 : 1 ;
64123 }
65124
66125 // Extensions are equal, compare filenames
67- result = intlFileNameCollator . getValue ( ) . collator . compare ( oneName , otherName ) ;
126+ result = intlFileNameCollatorBaseNumeric . getValue ( ) . collator . compare ( oneName , otherName ) ;
68127
69- if ( intlFileNameCollator . getValue ( ) . collatorIsNumeric && result === 0 && oneName !== otherName ) {
128+ if ( intlFileNameCollatorBaseNumeric . getValue ( ) . collatorIsNumeric && result === 0 && oneName !== otherName ) {
70129 return oneName < otherName ? - 1 : 1 ;
71130 }
72131 }
73132
74133 return result ;
75134}
76135
77- function extractNameAndExtension ( str ?: string | null ) : [ string , string ] {
136+ /** Compares filenames by extenson, then by name. Sorts numbers numerically, not alphabetically. */
137+ export function compareFileExtensionsNumeric ( one : string | null , other : string | null ) : number {
138+ const [ oneName , oneExtension ] = extractNameAndExtension ( one , true ) ;
139+ const [ otherName , otherExtension ] = extractNameAndExtension ( other , true ) ;
140+ const collatorNumeric = intlFileNameCollatorNumeric . getValue ( ) . collator ;
141+ const collatorNumericCaseInsensitive = intlFileNameCollatorNumericCaseInsenstive . getValue ( ) . collator ;
142+ let result ;
143+
144+ // Check for extension differences, ignoring differences in case and comparing numbers numerically.
145+ result = collatorNumericCaseInsensitive . compare ( oneExtension , otherExtension ) ;
146+ if ( result !== 0 ) {
147+ return result ;
148+ }
149+
150+ // Disambiguate equivalent numbers in extensions.
151+ if ( oneExtension . length !== otherExtension . length ) {
152+ return oneExtension . length < otherExtension . length ? - 1 : 1 ;
153+ }
154+
155+ // Compare names.
156+ result = collatorNumeric . compare ( oneName , otherName ) ;
157+ if ( result !== 0 ) {
158+ return result ;
159+ }
160+
161+ // Disambiguate equivalent numbers in names.
162+ if ( oneName . length !== otherName . length ) {
163+ return oneName . length < otherName . length ? - 1 : 1 ;
164+ }
165+
166+ // Disambiguate extension case if needed.
167+ if ( oneExtension !== otherExtension ) {
168+ return collatorNumeric . compare ( oneExtension , otherExtension ) ;
169+ }
170+
171+ return 0 ;
172+ }
173+
174+ /** Extracts the name and extension from a full filename, with optional special handling for dotfiles */
175+ export function extractNameAndExtension ( str ?: string | null , dotfilesAsNames = false ) : [ string , string ] {
78176 const match = str ? FileNameMatch . exec ( str ) as Array < string > : ( [ ] as Array < string > ) ;
79177
80- return [ ( match && match [ 1 ] ) || '' , ( match && match [ 3 ] ) || '' ] ;
178+ let result : [ string , string ] = [ ( match && match [ 1 ] ) || '' , ( match && match [ 3 ] ) || '' ] ;
179+
180+ // if the dotfilesAsNames option is selected, treat an empty filename with an extension,
181+ // or a filename that starts with a dot, as a dotfile name
182+ if ( dotfilesAsNames && ( ! result [ 0 ] && result [ 1 ] || result [ 0 ] && result [ 0 ] . charAt ( 0 ) === '.' ) ) {
183+ result = [ result [ 0 ] + '.' + result [ 1 ] , '' ] ;
184+ }
185+
186+ return result ;
81187}
82188
83189function comparePathComponents ( one : string , other : string , caseSensitive = false ) : number {
0 commit comments