@@ -8,7 +8,7 @@ import * as nls from 'vs/nls';
88import { $ , append , runAtThisOrScheduleAtNextAnimationFrame } from 'vs/base/browser/dom' ;
99import { format } from 'vs/base/common/strings' ;
1010import { extname , basename } from 'vs/base/common/resources' ;
11- import { areFunctions , withNullAsUndefined } from 'vs/base/common/types' ;
11+ import { areFunctions , withNullAsUndefined , withUndefinedAsNull } from 'vs/base/common/types' ;
1212import { URI } from 'vs/base/common/uri' ;
1313import { IStatusbarItem } from 'vs/workbench/browser/parts/statusbar/statusbar' ;
1414import { Action } from 'vs/base/common/actions' ;
@@ -893,7 +893,7 @@ export class ChangeModeAction extends Action {
893893
894894 // Compute mode
895895 let currentModeId : string | undefined ;
896- let modeId : string ;
896+ let modeId : string | undefined ;
897897 if ( textModel ) {
898898 modeId = textModel . getLanguageIdentifier ( ) . language ;
899899 currentModeId = this . modeService . getLanguageName ( modeId ) || undefined ;
@@ -933,9 +933,9 @@ export class ChangeModeAction extends Action {
933933 }
934934
935935 // Offer action to configure via settings
936- let configureModeAssociations : IQuickPickItem ;
937- let configureModeSettings : IQuickPickItem ;
938- let galleryAction : Action ;
936+ let configureModeAssociations : IQuickPickItem | undefined ;
937+ let configureModeSettings : IQuickPickItem | undefined ;
938+ let galleryAction : Action | undefined ;
939939 if ( hasLanguageSupport && resource ) {
940940 const ext = extname ( resource ) || basename ( resource ) ;
941941
@@ -959,56 +959,55 @@ export class ChangeModeAction extends Action {
959959 picks . unshift ( autoDetectMode ) ;
960960 }
961961
962- return this . quickInputService . pick ( picks , { placeHolder : nls . localize ( 'pickLanguage' , "Select Language Mode" ) , matchOnDescription : true } ) . then ( pick => {
963- if ( ! pick ) {
964- return ;
965- }
962+ const pick = await this . quickInputService . pick ( picks , { placeHolder : nls . localize ( 'pickLanguage' , "Select Language Mode" ) , matchOnDescription : true } ) ;
963+ if ( ! pick ) {
964+ return ;
965+ }
966966
967- if ( pick === galleryAction ) {
968- galleryAction . run ( ) ;
969- return ;
970- }
967+ if ( pick === galleryAction ) {
968+ galleryAction . run ( ) ;
969+ return ;
970+ }
971971
972- // User decided to permanently configure associations, return right after
973- if ( pick === configureModeAssociations ) {
974- if ( resource ) {
975- this . configureFileAssociation ( resource ) ;
976- }
977- return ;
972+ // User decided to permanently configure associations, return right after
973+ if ( pick === configureModeAssociations ) {
974+ if ( resource ) {
975+ this . configureFileAssociation ( resource ) ;
978976 }
977+ return ;
978+ }
979979
980- // User decided to configure settings for current language
981- if ( pick === configureModeSettings ) {
982- this . preferencesService . configureSettingsForLanguage ( modeId ) ;
983- return ;
984- }
980+ // User decided to configure settings for current language
981+ if ( pick === configureModeSettings ) {
982+ this . preferencesService . configureSettingsForLanguage ( withUndefinedAsNull ( modeId ) ) ;
983+ return ;
984+ }
985985
986- // Change mode for active editor
987- const activeEditor = this . editorService . activeEditor ;
988- if ( activeEditor ) {
989- const modeSupport = toEditorWithModeSupport ( activeEditor ) ;
990- if ( modeSupport ) {
991-
992- // Find mode
993- let languageSelection : ILanguageSelection | undefined ;
994- if ( pick === autoDetectMode ) {
995- if ( textModel ) {
996- const resource = toResource ( activeEditor , { supportSideBySide : SideBySideEditor . MASTER } ) ;
997- if ( resource ) {
998- languageSelection = this . modeService . createByFilepathOrFirstLine ( resource . fsPath , textModel . getLineContent ( 1 ) ) ;
999- }
986+ // Change mode for active editor
987+ const activeEditor = this . editorService . activeEditor ;
988+ if ( activeEditor ) {
989+ const modeSupport = toEditorWithModeSupport ( activeEditor ) ;
990+ if ( modeSupport ) {
991+
992+ // Find mode
993+ let languageSelection : ILanguageSelection | undefined ;
994+ if ( pick === autoDetectMode ) {
995+ if ( textModel ) {
996+ const resource = toResource ( activeEditor , { supportSideBySide : SideBySideEditor . MASTER } ) ;
997+ if ( resource ) {
998+ languageSelection = this . modeService . createByFilepathOrFirstLine ( resource . fsPath , textModel . getLineContent ( 1 ) ) ;
1000999 }
1001- } else {
1002- languageSelection = this . modeService . createByLanguageName ( pick . label ) ;
10031000 }
1001+ } else {
1002+ languageSelection = this . modeService . createByLanguageName ( pick . label ) ;
1003+ }
10041004
1005- // Change mode
1006- if ( typeof languageSelection !== 'undefined' ) {
1007- modeSupport . setMode ( languageSelection . languageIdentifier . language ) ;
1008- }
1005+ // Change mode
1006+ if ( typeof languageSelection !== 'undefined' ) {
1007+ modeSupport . setMode ( languageSelection . languageIdentifier . language ) ;
10091008 }
10101009 }
1011- } ) ;
1010+ }
10121011 }
10131012
10141013 private configureFileAssociation ( resource : URI ) : void {
@@ -1171,7 +1170,7 @@ export class ChangeEncodingAction extends Action {
11711170 super ( actionId , actionLabel ) ;
11721171 }
11731172
1174- run ( ) : Promise < any > {
1173+ async run ( ) : Promise < any > {
11751174 if ( ! getCodeEditor ( this . editorService . activeTextEditorWidget ) ) {
11761175 return this . quickInputService . pick ( [ { label : nls . localize ( 'noEditor' , "No text editor active at this time" ) } ] ) ;
11771176 }
@@ -1196,93 +1195,88 @@ export class ChangeEncodingAction extends Action {
11961195 reopenWithEncodingPick = { label : nls . localize ( 'reopenWithEncoding' , "Reopen with Encoding" ) , detail : 'Reopen with Encoding' } ;
11971196 }
11981197
1199- let pickActionPromise : Promise < IQuickPickItem > ;
1198+ let action : IQuickPickItem ;
12001199 if ( encodingSupport instanceof UntitledEditorInput ) {
1201- pickActionPromise = Promise . resolve ( saveWithEncodingPick ) ;
1200+ action = saveWithEncodingPick ;
12021201 } else if ( ! isWritableBaseEditor ( activeControl ) ) {
1203- pickActionPromise = Promise . resolve ( reopenWithEncodingPick ) ;
1202+ action = reopenWithEncodingPick ;
12041203 } else {
1205- pickActionPromise = this . quickInputService . pick ( [ reopenWithEncodingPick , saveWithEncodingPick ] , { placeHolder : nls . localize ( 'pickAction' , "Select Action" ) , matchOnDetail : true } ) ;
1204+ action = await this . quickInputService . pick ( [ reopenWithEncodingPick , saveWithEncodingPick ] , { placeHolder : nls . localize ( 'pickAction' , "Select Action" ) , matchOnDetail : true } ) ;
12061205 }
12071206
1208- return pickActionPromise . then ( action => {
1209- if ( ! action ) {
1210- return undefined ;
1211- }
1207+ if ( ! action ) {
1208+ return ;
1209+ }
12121210
1213- const resource = toResource ( activeControl ! . input , { supportSideBySide : SideBySideEditor . MASTER } ) ;
1211+ await timeout ( 50 ) ; // quick open is sensitive to being opened so soon after another
12141212
1215- return timeout ( 50 /* quick open is sensitive to being opened so soon after another */ )
1216- . then ( ( ) => {
1217- if ( ! resource || ! this . fileService . canHandleResource ( resource ) ) {
1218- return Promise . resolve ( null ) ; // encoding detection only possible for resources the file service can handle
1219- }
1213+ const resource = toResource ( activeControl ! . input , { supportSideBySide : SideBySideEditor . MASTER } ) ;
1214+ if ( ! resource || ! this . fileService . canHandleResource ( resource ) ) {
1215+ return null ; // encoding detection only possible for resources the file service can handle
1216+ }
12201217
1221- return this . textFileService . read ( resource , { autoGuessEncoding : true , acceptTextOnly : true } ) . then ( content => content . encoding , err => null ) ;
1222- } )
1223- . then ( ( guessedEncoding : string ) => {
1224- const isReopenWithEncoding = ( action === reopenWithEncodingPick ) ;
1225-
1226- const configuredEncoding = this . textResourceConfigurationService . getValue ( withNullAsUndefined ( resource ) , 'files.encoding' ) ;
1227-
1228- let directMatchIndex : number | undefined ;
1229- let aliasMatchIndex : number | undefined ;
1230-
1231- // All encodings are valid picks
1232- const picks : QuickPickInput [ ] = Object . keys ( SUPPORTED_ENCODINGS )
1233- . sort ( ( k1 , k2 ) => {
1234- if ( k1 === configuredEncoding ) {
1235- return - 1 ;
1236- } else if ( k2 === configuredEncoding ) {
1237- return 1 ;
1238- }
1239-
1240- return SUPPORTED_ENCODINGS [ k1 ] . order - SUPPORTED_ENCODINGS [ k2 ] . order ;
1241- } )
1242- . filter ( k => {
1243- if ( k === guessedEncoding && guessedEncoding !== configuredEncoding ) {
1244- return false ; // do not show encoding if it is the guessed encoding that does not match the configured
1245- }
1246-
1247- return ! isReopenWithEncoding || ! SUPPORTED_ENCODINGS [ k ] . encodeOnly ; // hide those that can only be used for encoding if we are about to decode
1248- } )
1249- . map ( ( key , index ) => {
1250- if ( key === encodingSupport . getEncoding ( ) ) {
1251- directMatchIndex = index ;
1252- } else if ( SUPPORTED_ENCODINGS [ key ] . alias === encodingSupport . getEncoding ( ) ) {
1253- aliasMatchIndex = index ;
1254- }
1255-
1256- return { id : key , label : SUPPORTED_ENCODINGS [ key ] . labelLong , description : key } ;
1257- } ) ;
1258-
1259- const items = picks . slice ( ) as IQuickPickItem [ ] ;
1260-
1261- // If we have a guessed encoding, show it first unless it matches the configured encoding
1262- if ( guessedEncoding && configuredEncoding !== guessedEncoding && SUPPORTED_ENCODINGS [ guessedEncoding ] ) {
1263- picks . unshift ( { type : 'separator' } ) ;
1264- picks . unshift ( { id : guessedEncoding , label : SUPPORTED_ENCODINGS [ guessedEncoding ] . labelLong , description : nls . localize ( 'guessedEncoding' , "Guessed from content" ) } ) ;
1265- }
1218+ const content = await this . textFileService . read ( resource , { autoGuessEncoding : true , acceptTextOnly : true } ) ;
1219+ const guessedEncoding = content . encoding ;
12661220
1267- return this . quickInputService . pick ( picks , {
1268- placeHolder : isReopenWithEncoding ? nls . localize ( 'pickEncodingForReopen' , "Select File Encoding to Reopen File" ) : nls . localize ( 'pickEncodingForSave' , "Select File Encoding to Save with" ) ,
1269- activeItem : items [ typeof directMatchIndex === 'number' ? directMatchIndex : typeof aliasMatchIndex === 'number' ? aliasMatchIndex : - 1 ]
1270- } ) . then ( encoding => {
1271- if ( ! encoding ) {
1272- return ;
1273- }
1221+ const isReopenWithEncoding = ( action === reopenWithEncodingPick ) ;
12741222
1275- const activeControl = this . editorService . activeControl ;
1276- if ( ! activeControl ) {
1277- return ;
1278- }
1223+ const configuredEncoding = this . textResourceConfigurationService . getValue ( withNullAsUndefined ( resource ) , 'files.encoding' ) ;
12791224
1280- const encodingSupport = toEditorWithEncodingSupport ( activeControl . input ) ;
1281- if ( typeof encoding . id !== 'undefined' && encodingSupport && encodingSupport . getEncoding ( ) !== encoding . id ) {
1282- encodingSupport . setEncoding ( encoding . id , isReopenWithEncoding ? EncodingMode . Decode : EncodingMode . Encode ) ; // Set new encoding
1283- }
1284- } ) ;
1285- } ) ;
1225+ let directMatchIndex : number | undefined ;
1226+ let aliasMatchIndex : number | undefined ;
1227+
1228+ // All encodings are valid picks
1229+ const picks : QuickPickInput [ ] = Object . keys ( SUPPORTED_ENCODINGS )
1230+ . sort ( ( k1 , k2 ) => {
1231+ if ( k1 === configuredEncoding ) {
1232+ return - 1 ;
1233+ } else if ( k2 === configuredEncoding ) {
1234+ return 1 ;
1235+ }
1236+
1237+ return SUPPORTED_ENCODINGS [ k1 ] . order - SUPPORTED_ENCODINGS [ k2 ] . order ;
1238+ } )
1239+ . filter ( k => {
1240+ if ( k === guessedEncoding && guessedEncoding !== configuredEncoding ) {
1241+ return false ; // do not show encoding if it is the guessed encoding that does not match the configured
1242+ }
1243+
1244+ return ! isReopenWithEncoding || ! SUPPORTED_ENCODINGS [ k ] . encodeOnly ; // hide those that can only be used for encoding if we are about to decode
1245+ } )
1246+ . map ( ( key , index ) => {
1247+ if ( key === encodingSupport . getEncoding ( ) ) {
1248+ directMatchIndex = index ;
1249+ } else if ( SUPPORTED_ENCODINGS [ key ] . alias === encodingSupport . getEncoding ( ) ) {
1250+ aliasMatchIndex = index ;
1251+ }
1252+
1253+ return { id : key , label : SUPPORTED_ENCODINGS [ key ] . labelLong , description : key } ;
1254+ } ) ;
1255+
1256+ const items = picks . slice ( ) as IQuickPickItem [ ] ;
1257+
1258+ // If we have a guessed encoding, show it first unless it matches the configured encoding
1259+ if ( guessedEncoding && configuredEncoding !== guessedEncoding && SUPPORTED_ENCODINGS [ guessedEncoding ] ) {
1260+ picks . unshift ( { type : 'separator' } ) ;
1261+ picks . unshift ( { id : guessedEncoding , label : SUPPORTED_ENCODINGS [ guessedEncoding ] . labelLong , description : nls . localize ( 'guessedEncoding' , "Guessed from content" ) } ) ;
1262+ }
1263+
1264+ const encoding = await this . quickInputService . pick ( picks , {
1265+ placeHolder : isReopenWithEncoding ? nls . localize ( 'pickEncodingForReopen' , "Select File Encoding to Reopen File" ) : nls . localize ( 'pickEncodingForSave' , "Select File Encoding to Save with" ) ,
1266+ activeItem : items [ typeof directMatchIndex === 'number' ? directMatchIndex : typeof aliasMatchIndex === 'number' ? aliasMatchIndex : - 1 ]
12861267 } ) ;
1268+
1269+ if ( ! encoding ) {
1270+ return ;
1271+ }
1272+
1273+ if ( ! this . editorService . activeControl ) {
1274+ return ;
1275+ }
1276+
1277+ const activeEncodingSupport = toEditorWithEncodingSupport ( this . editorService . activeControl . input ) ;
1278+ if ( typeof encoding . id !== 'undefined' && activeEncodingSupport && activeEncodingSupport . getEncoding ( ) !== encoding . id ) {
1279+ activeEncodingSupport . setEncoding ( encoding . id , isReopenWithEncoding ? EncodingMode . Decode : EncodingMode . Encode ) ; // Set new encoding
1280+ }
12871281 }
12881282}
0 commit comments