@@ -1008,43 +1008,6 @@ export class LowerCaseAction extends AbstractCaseAction {
1008
1008
}
1009
1009
}
1010
1010
1011
- export class TitleCaseAction extends AbstractCaseAction {
1012
- constructor ( ) {
1013
- super ( {
1014
- id : 'editor.action.transformToTitlecase' ,
1015
- label : nls . localize ( 'editor.transformToTitlecase' , "Transform to Title Case" ) ,
1016
- alias : 'Transform to Title Case' ,
1017
- precondition : EditorContextKeys . writable
1018
- } ) ;
1019
- }
1020
-
1021
- protected _modifyText ( text : string , wordSeparators : string ) : string {
1022
- const separators = '\r\n\t ' + wordSeparators ;
1023
- const excludedChars = separators . split ( '' ) ;
1024
-
1025
- let title = '' ;
1026
- let startUpperCase = true ;
1027
-
1028
- for ( let i = 0 ; i < text . length ; i ++ ) {
1029
- let currentChar = text [ i ] ;
1030
-
1031
- if ( excludedChars . indexOf ( currentChar ) >= 0 ) {
1032
- startUpperCase = true ;
1033
-
1034
- title += currentChar ;
1035
- } else if ( startUpperCase ) {
1036
- startUpperCase = false ;
1037
-
1038
- title += currentChar . toLocaleUpperCase ( ) ;
1039
- } else {
1040
- title += currentChar . toLocaleLowerCase ( ) ;
1041
- }
1042
- }
1043
-
1044
- return title ;
1045
- }
1046
- }
1047
-
1048
1011
class BackwardsCompatibleRegExp {
1049
1012
1050
1013
private _actual : RegExp | null ;
@@ -1075,10 +1038,38 @@ class BackwardsCompatibleRegExp {
1075
1038
}
1076
1039
}
1077
1040
1041
+ export class TitleCaseAction extends AbstractCaseAction {
1042
+
1043
+ public static titleBoundary = new BackwardsCompatibleRegExp ( '(^|[^\\p{L}\\p{N}])[\\p{L}\\p{N}]' , 'gmu' ) ;
1044
+ public static apostrophe = new BackwardsCompatibleRegExp ( '[\\p{L}\\p{N}]\'[\\p{L}\\p{N}]' , 'gmu' ) ;
1045
+
1046
+ constructor ( ) {
1047
+ super ( {
1048
+ id : 'editor.action.transformToTitlecase' ,
1049
+ label : nls . localize ( 'editor.transformToTitlecase' , "Transform to Title Case" ) ,
1050
+ alias : 'Transform to Title Case' ,
1051
+ precondition : EditorContextKeys . writable
1052
+ } ) ;
1053
+ }
1054
+
1055
+ protected _modifyText ( text : string , wordSeparators : string ) : string {
1056
+ const titleBoundary = TitleCaseAction . titleBoundary . get ( ) ;
1057
+ const apostrophe = TitleCaseAction . apostrophe . get ( ) ;
1058
+ if ( ! titleBoundary || ! apostrophe ) {
1059
+ // cannot support this
1060
+ return text ;
1061
+ }
1062
+ return text
1063
+ . toLocaleLowerCase ( )
1064
+ . replace ( titleBoundary , ( b ) => b . toLocaleUpperCase ( ) )
1065
+ . replace ( apostrophe , ( a ) => a . toLocaleLowerCase ( ) ) ;
1066
+ }
1067
+ }
1068
+
1078
1069
export class SnakeCaseAction extends AbstractCaseAction {
1079
1070
1080
- public static regExp1 = new BackwardsCompatibleRegExp ( '(\\p{Ll})(\\p{Lu})' , 'gmu' ) ;
1081
- public static regExp2 = new BackwardsCompatibleRegExp ( '(\\p{Lu}|\\p{N})(\\p{Lu})(\\p{Ll})' , 'gmu' ) ;
1071
+ public static caseBoundary = new BackwardsCompatibleRegExp ( '(\\p{Ll})(\\p{Lu})' , 'gmu' ) ;
1072
+ public static singleLetters = new BackwardsCompatibleRegExp ( '(\\p{Lu}|\\p{N})(\\p{Lu})(\\p{Ll})' , 'gmu' ) ;
1082
1073
1083
1074
constructor ( ) {
1084
1075
super ( {
@@ -1090,15 +1081,15 @@ export class SnakeCaseAction extends AbstractCaseAction {
1090
1081
}
1091
1082
1092
1083
protected _modifyText ( text : string , wordSeparators : string ) : string {
1093
- const regExp1 = SnakeCaseAction . regExp1 . get ( ) ;
1094
- const regExp2 = SnakeCaseAction . regExp2 . get ( ) ;
1095
- if ( ! regExp1 || ! regExp2 ) {
1084
+ const caseBoundary = SnakeCaseAction . caseBoundary . get ( ) ;
1085
+ const singleLetters = SnakeCaseAction . singleLetters . get ( ) ;
1086
+ if ( ! caseBoundary || ! singleLetters ) {
1096
1087
// cannot support this
1097
1088
return text ;
1098
1089
}
1099
1090
return ( text
1100
- . replace ( regExp1 , '$1_$2' )
1101
- . replace ( regExp2 , '$1_$2$3' )
1091
+ . replace ( caseBoundary , '$1_$2' )
1092
+ . replace ( singleLetters , '$1_$2$3' )
1102
1093
. toLocaleLowerCase ( )
1103
1094
) ;
1104
1095
}
@@ -1125,6 +1116,9 @@ registerEditorAction(UpperCaseAction);
1125
1116
registerEditorAction ( LowerCaseAction ) ;
1126
1117
registerEditorAction ( TitleCaseAction ) ;
1127
1118
1128
- if ( SnakeCaseAction . regExp1 . isSupported ( ) && SnakeCaseAction . regExp2 . isSupported ( ) ) {
1119
+ if ( SnakeCaseAction . caseBoundary . isSupported ( ) && SnakeCaseAction . singleLetters . isSupported ( ) ) {
1129
1120
registerEditorAction ( SnakeCaseAction ) ;
1130
1121
}
1122
+ if ( TitleCaseAction . titleBoundary . isSupported ( ) && TitleCaseAction . apostrophe . isSupported ( ) ) {
1123
+ registerEditorAction ( TitleCaseAction ) ;
1124
+ }
0 commit comments