@@ -34,7 +34,7 @@ import { IEditor as IBaseEditor, IEditorInput } from 'vs/platform/editor/common/
3434import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService' ;
3535import { IQuickOpenService , IPickOpenEntry , IFilePickOpenEntry } from 'vs/platform/quickOpen/common/quickOpen' ;
3636import { IWorkspaceConfigurationService } from 'vs/workbench/services/configuration/common/configuration' ;
37- import { IFilesConfiguration , SUPPORTED_ENCODINGS } from 'vs/platform/files/common/files' ;
37+ import { IFilesConfiguration , SUPPORTED_ENCODINGS , IFileService } from 'vs/platform/files/common/files' ;
3838import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation' ;
3939import { IModeService } from 'vs/editor/common/services/modeService' ;
4040import { IModelService } from 'vs/editor/common/services/modelService' ;
@@ -1031,7 +1031,8 @@ export class ChangeEncodingAction extends Action {
10311031 actionLabel : string ,
10321032 @IWorkbenchEditorService private editorService : IWorkbenchEditorService ,
10331033 @IQuickOpenService private quickOpenService : IQuickOpenService ,
1034- @IWorkspaceConfigurationService private configurationService : IWorkspaceConfigurationService
1034+ @IWorkspaceConfigurationService private configurationService : IWorkspaceConfigurationService ,
1035+ @IFileService private fileService : IFileService
10351036 ) {
10361037 super ( actionId , actionLabel ) ;
10371038 }
@@ -1072,51 +1073,69 @@ export class ChangeEncodingAction extends Action {
10721073 return undefined ;
10731074 }
10741075
1075- return TPromise . timeout ( 50 /* quick open is sensitive to being opened so soon after another */ ) . then ( ( ) => {
1076- const configuration = this . configurationService . getConfiguration < IFilesConfiguration > ( ) ;
1077-
1078- const isReopenWithEncoding = ( action === reopenWithEncodingPick ) ;
1079- const configuredEncoding = configuration && configuration . files && configuration . files . encoding ;
1080- let directMatchIndex : number ;
1081- let aliasMatchIndex : number ;
1082-
1083- // All encodings are valid picks
1084- const picks : IPickOpenEntry [ ] = Object . keys ( SUPPORTED_ENCODINGS )
1085- . sort ( ( k1 , k2 ) => {
1086- if ( k1 === configuredEncoding ) {
1087- return - 1 ;
1088- } else if ( k2 === configuredEncoding ) {
1089- return 1 ;
1090- }
1091-
1092- return SUPPORTED_ENCODINGS [ k1 ] . order - SUPPORTED_ENCODINGS [ k2 ] . order ;
1093- } )
1094- . filter ( k => {
1095- return ! isReopenWithEncoding || ! SUPPORTED_ENCODINGS [ k ] . encodeOnly ; // hide those that can only be used for encoding if we are about to decode
1096- } )
1097- . map ( ( key , index ) => {
1098- if ( key === encodingSupport . getEncoding ( ) ) {
1099- directMatchIndex = index ;
1100- } else if ( SUPPORTED_ENCODINGS [ key ] . alias === encodingSupport . getEncoding ( ) ) {
1101- aliasMatchIndex = index ;
1102- }
1076+ const guessEncoding = ( ) => {
1077+ const resource = toResource ( activeEditor . input ) ;
1078+ return this . fileService . resolveContent ( resource , { autoGuessEncoding : true , acceptTextOnly : true } )
1079+ . then ( content => content . encoding , err => null ) ;
1080+ } ;
11031081
1104- return { id : key , label : SUPPORTED_ENCODINGS [ key ] . labelLong } ;
1105- } ) ;
1082+ return TPromise . timeout ( 50 /* quick open is sensitive to being opened so soon after another */ )
1083+ . then ( guessEncoding )
1084+ . then ( guessedEncoding => {
1085+ const configuration = this . configurationService . getConfiguration < IFilesConfiguration > ( ) ;
1086+
1087+ const isReopenWithEncoding = ( action === reopenWithEncodingPick ) ;
1088+ const configuredEncoding = configuration && configuration . files && configuration . files . encoding ;
1089+ let directMatchIndex : number ;
1090+ let aliasMatchIndex : number ;
1091+
1092+ // All encodings are valid picks
1093+ const picks : IPickOpenEntry [ ] = Object . keys ( SUPPORTED_ENCODINGS )
1094+ . sort ( ( k1 , k2 ) => {
1095+ if ( k1 === configuredEncoding ) {
1096+ return - 1 ;
1097+ } else if ( k2 === configuredEncoding ) {
1098+ return 1 ;
1099+ }
1100+
1101+ return SUPPORTED_ENCODINGS [ k1 ] . order - SUPPORTED_ENCODINGS [ k2 ] . order ;
1102+ } )
1103+ . filter ( k => {
1104+ if ( k === guessedEncoding && guessedEncoding !== configuredEncoding ) {
1105+ return false ; // do not show encoding if it is the guessed encoding that does not match the configured
1106+ }
1107+
1108+ return ! isReopenWithEncoding || ! SUPPORTED_ENCODINGS [ k ] . encodeOnly ; // hide those that can only be used for encoding if we are about to decode
1109+ } )
1110+ . map ( ( key , index ) => {
1111+ if ( key === encodingSupport . getEncoding ( ) ) {
1112+ directMatchIndex = index ;
1113+ } else if ( SUPPORTED_ENCODINGS [ key ] . alias === encodingSupport . getEncoding ( ) ) {
1114+ aliasMatchIndex = index ;
1115+ }
1116+
1117+ return { id : key , label : SUPPORTED_ENCODINGS [ key ] . labelLong } ;
1118+ } ) ;
1119+
1120+ // If we have a guessed encoding, show it first unless it matches the configured encoding
1121+ if ( guessedEncoding && configuredEncoding !== guessedEncoding && SUPPORTED_ENCODINGS [ guessedEncoding ] ) {
1122+ picks [ 0 ] . separator = { border : true } ;
1123+ picks . unshift ( { id : guessedEncoding , label : SUPPORTED_ENCODINGS [ guessedEncoding ] . labelLong , description : nls . localize ( 'guessedEncoding' , "Guessed from content" ) } ) ;
1124+ }
11061125
1107- return this . quickOpenService . pick ( picks , {
1108- placeHolder : isReopenWithEncoding ? nls . localize ( 'pickEncodingForReopen' , "Select File Encoding to Reopen File" ) : nls . localize ( 'pickEncodingForSave' , "Select File Encoding to Save with" ) ,
1109- autoFocus : { autoFocusIndex : typeof directMatchIndex === 'number' ? directMatchIndex : typeof aliasMatchIndex === 'number' ? aliasMatchIndex : void 0 }
1110- } ) . then ( encoding => {
1111- if ( encoding ) {
1112- activeEditor = this . editorService . getActiveEditor ( ) ;
1113- encodingSupport = toEditorWithEncodingSupport ( activeEditor . input ) ;
1114- if ( encodingSupport && encodingSupport . getEncoding ( ) !== encoding . id ) {
1115- encodingSupport . setEncoding ( encoding . id , isReopenWithEncoding ? EncodingMode . Decode : EncodingMode . Encode ) ; // Set new encoding
1126+ return this . quickOpenService . pick ( picks , {
1127+ placeHolder : isReopenWithEncoding ? nls . localize ( 'pickEncodingForReopen' , "Select File Encoding to Reopen File" ) : nls . localize ( 'pickEncodingForSave' , "Select File Encoding to Save with" ) ,
1128+ autoFocus : { autoFocusIndex : typeof directMatchIndex === 'number' ? directMatchIndex : typeof aliasMatchIndex === 'number' ? aliasMatchIndex : void 0 }
1129+ } ) . then ( encoding => {
1130+ if ( encoding ) {
1131+ activeEditor = this . editorService . getActiveEditor ( ) ;
1132+ encodingSupport = toEditorWithEncodingSupport ( activeEditor . input ) ;
1133+ if ( encodingSupport && encodingSupport . getEncoding ( ) !== encoding . id ) {
1134+ encodingSupport . setEncoding ( encoding . id , isReopenWithEncoding ? EncodingMode . Decode : EncodingMode . Encode ) ; // Set new encoding
1135+ }
11161136 }
1117- }
1137+ } ) ;
11181138 } ) ;
1119- } ) ;
11201139 } ) ;
11211140 }
11221141}
0 commit comments