@@ -1076,43 +1076,6 @@ export class LowerCaseAction extends AbstractCaseAction {
1076
1076
}
1077
1077
}
1078
1078
1079
- export class TitleCaseAction extends AbstractCaseAction {
1080
- constructor ( ) {
1081
- super ( {
1082
- id :'editor.action.transformToTitlecase' ,
1083
- label :nls . localize ( 'editor.transformToTitlecase' , "Transform to Title Case" ) ,
1084
- alias :'Transform to Title Case' ,
1085
- precondition :EditorContextKeys . writable
1086
- } ) ;
1087
- }
1088
-
1089
- protected _modifyText ( text :string , wordSeparators :string ) :string {
1090
- const separators = '\r\n\t ' + wordSeparators ;
1091
- const excludedChars = separators . split ( '' ) ;
1092
-
1093
- let title = '' ;
1094
- let startUpperCase = true ;
1095
-
1096
- for ( let i = 0 ; i < text . length ; i ++ ) {
1097
- let currentChar = text [ i ] ;
1098
-
1099
- if ( excludedChars . indexOf ( currentChar ) >= 0 ) {
1100
- startUpperCase = true ;
1101
-
1102
- title += currentChar ;
1103
- } else if ( startUpperCase ) {
1104
- startUpperCase = false ;
1105
-
1106
- title += currentChar . toLocaleUpperCase ( ) ;
1107
- } else {
1108
- title += currentChar . toLocaleLowerCase ( ) ;
1109
- }
1110
- }
1111
-
1112
- return title ;
1113
- }
1114
- }
1115
-
1116
1079
class BackwardsCompatibleRegExp {
1117
1080
1118
1081
private _actual :RegExp | null ;
@@ -1143,10 +1106,35 @@ class BackwardsCompatibleRegExp {
1143
1106
}
1144
1107
}
1145
1108
1109
+ export class TitleCaseAction extends AbstractCaseAction {
1110
+
1111
+ public static titleBoundary = new BackwardsCompatibleRegExp ( '(^|[^\\p{L}\\p{N}\']|((^|\\P{L})\'))\\p{L}' , 'gmu' ) ;
1112
+
1113
+ constructor ( ) {
1114
+ super ( {
1115
+ id :'editor.action.transformToTitlecase' ,
1116
+ label :nls . localize ( 'editor.transformToTitlecase' , "Transform to Title Case" ) ,
1117
+ alias :'Transform to Title Case' ,
1118
+ precondition :EditorContextKeys . writable
1119
+ } ) ;
1120
+ }
1121
+
1122
+ protected _modifyText ( text :string , wordSeparators :string ) :string {
1123
+ const titleBoundary = TitleCaseAction . titleBoundary . get ( ) ;
1124
+ if ( ! titleBoundary ) {
1125
+ // cannot support this
1126
+ return text ;
1127
+ }
1128
+ return text
1129
+ . toLocaleLowerCase ( )
1130
+ . replace ( titleBoundary , ( b ) => b . toLocaleUpperCase ( ) ) ;
1131
+ }
1132
+ }
1133
+
1146
1134
export class SnakeCaseAction extends AbstractCaseAction {
1147
1135
1148
- public static regExp1 = new BackwardsCompatibleRegExp ( '(\\p{Ll})(\\p{Lu})' , 'gmu' ) ;
1149
- public static regExp2 = new BackwardsCompatibleRegExp ( '(\\p{Lu}|\\p{N})(\\p{Lu})(\\p{Ll})' , 'gmu' ) ;
1136
+ public static caseBoundary = new BackwardsCompatibleRegExp ( '(\\p{Ll})(\\p{Lu})' , 'gmu' ) ;
1137
+ public static singleLetters = new BackwardsCompatibleRegExp ( '(\\p{Lu}|\\p{N})(\\p{Lu})(\\p{Ll})' , 'gmu' ) ;
1150
1138
1151
1139
constructor ( ) {
1152
1140
super ( {
@@ -1158,15 +1146,15 @@ export class SnakeCaseAction extends AbstractCaseAction {
1158
1146
}
1159
1147
1160
1148
protected _modifyText ( text :string , wordSeparators :string ) :string {
1161
- const regExp1 = SnakeCaseAction . regExp1 . get ( ) ;
1162
- const regExp2 = SnakeCaseAction . regExp2 . get ( ) ;
1163
- if ( ! regExp1 || ! regExp2 ) {
1149
+ const caseBoundary = SnakeCaseAction . caseBoundary . get ( ) ;
1150
+ const singleLetters = SnakeCaseAction . singleLetters . get ( ) ;
1151
+ if ( ! caseBoundary || ! singleLetters ) {
1164
1152
// cannot support this
1165
1153
return text ;
1166
1154
}
1167
1155
return ( text
1168
- . replace ( regExp1 , '$1_$2' )
1169
- . replace ( regExp2 , '$1_$2$3' )
1156
+ . replace ( caseBoundary , '$1_$2' )
1157
+ . replace ( singleLetters , '$1_$2$3' )
1170
1158
. toLocaleLowerCase ( )
1171
1159
) ;
1172
1160
}
@@ -1192,8 +1180,10 @@ registerEditorAction(JoinLinesAction);
1192
1180
registerEditorAction ( TransposeAction ) ;
1193
1181
registerEditorAction ( UpperCaseAction ) ;
1194
1182
registerEditorAction ( LowerCaseAction ) ;
1195
- registerEditorAction ( TitleCaseAction ) ;
1196
1183
1197
- if ( SnakeCaseAction . regExp1 . isSupported ( ) && SnakeCaseAction . regExp2 . isSupported ( ) ) {
1184
+ if ( SnakeCaseAction . caseBoundary . isSupported ( ) && SnakeCaseAction . singleLetters . isSupported ( ) ) {
1198
1185
registerEditorAction ( SnakeCaseAction ) ;
1199
1186
}
1187
+ if ( TitleCaseAction . titleBoundary . isSupported ( ) ) {
1188
+ registerEditorAction ( TitleCaseAction ) ;
1189
+ }