66 *--------------------------------------------------------------------------------------------*/
77'use strict' ;
88
9- import { workspace , ExtensionContext , extensions , window , commands , Uri } from 'vscode' ;
9+ import { workspace , ExtensionContext , extensions , window , commands , Uri , ConfigurationTarget } from 'vscode' ;
1010import {
1111 CommonLanguageClient ,
1212 LanguageClientOptions ,
@@ -157,7 +157,7 @@ export function startClient(
157157 findConflicts ( ) ;
158158 client
159159 . onReady ( )
160- . then ( ( ) => {
160+ . then ( async ( ) => {
161161 // Send a notification to the server with any YAML schema associations in all extensions
162162 client . sendNotification ( SchemaAssociationNotification . type , getSchemaAssociations ( ) ) ;
163163
@@ -214,6 +214,8 @@ export function startClient(
214214 } ) ;
215215
216216 initializeRecommendation ( context ) ;
217+
218+ await setDefaultFormatter ( [ 'dockercompose' , 'github-actions-workflow' ] ) ;
217219 } )
218220 . catch ( ( err ) => {
219221 sendStartupTelemetryEvent ( runtime . telemetry , false , err ) ;
@@ -222,6 +224,23 @@ export function startClient(
222224 return schemaExtensionAPI ;
223225}
224226
227+ /**
228+ * set redhat.vscode-yaml as default formatter
229+ * @param extensions [dockercompose, github-actions-workflow]
230+ */
231+ async function setDefaultFormatter ( extensions : string [ ] ) : Promise < void > {
232+ const config = workspace . getConfiguration ( ) ;
233+ extensions . forEach ( async ( extension ) => {
234+ const extensionConf = config . get < Record < string , string > > ( `[${ extension } ]` ) || { } ;
235+ if ( extensionConf ) {
236+ if ( extensionConf [ 'editor.defaultFormatter' ] === undefined ) {
237+ extensionConf [ 'editor.defaultFormatter' ] = 'redhat.vscode-yaml' ;
238+ await config . update ( `[${ extension } ]` , extensionConf , ConfigurationTarget . Global ) ;
239+ }
240+ }
241+ } ) ;
242+ }
243+
225244/**
226245 * Finds extensions that conflict with VSCode-YAML.
227246 * If one or more conflicts are found then show an uninstall notification
0 commit comments