@@ -32,6 +32,7 @@ export async function registerClientServerCommands(context: ExtensionContext, la
3232 registerCodeLensReferencesCommands ( context , languageClient ) ;
3333 registerValidationCommands ( context ) ;
3434 registerRefactorCommands ( context , languageClient ) ;
35+ registerMinifyCommand ( context , languageClient ) ;
3536 registerAssociationCommands ( context , languageClient ) ;
3637 registerRestartLanguageServerCommand ( context , languageClient ) ;
3738 registerConfigurationUpdateCommand ( ) ;
@@ -467,3 +468,43 @@ async function surroundWith(surroundWithType: SurroundWithKind, languageClient:
467468 }
468469
469470}
471+
472+ /**
473+ * Register command to minify XML document
474+ *
475+ * @param context the extension context
476+ * @param languageClient the language client
477+ */
478+ function registerMinifyCommand ( context : ExtensionContext , languageClient : LanguageClient ) {
479+ context . subscriptions . push ( commands . registerCommand ( ClientCommandConstants . MINIFY_DOCUMENT , async ( ) => {
480+ const activeEditor = window . activeTextEditor ;
481+ if ( ! activeEditor || activeEditor . document . languageId !== 'xml' ) {
482+ return ;
483+ }
484+
485+ const uri = activeEditor . document . uri ;
486+ const identifier = TextDocumentIdentifier . create ( uri . toString ( ) ) ;
487+
488+ try {
489+ // Call the server command to get the minified text edits
490+ const edits : TextEdit [ ] = await commands . executeCommand (
491+ ClientCommandConstants . EXECUTE_WORKSPACE_COMMAND ,
492+ ServerCommandConstants . MINIFY_DOCUMENT ,
493+ identifier
494+ ) ;
495+
496+ if ( ! edits || edits . length === 0 ) {
497+ return ;
498+ }
499+
500+ // Apply the text edits
501+ const workEdits = new WorkspaceEdit ( ) ;
502+ for ( const edit of edits ) {
503+ workEdits . replace ( uri , languageClient . protocol2CodeConverter . asRange ( edit . range ) , edit . newText ) ;
504+ }
505+ await workspace . applyEdit ( workEdits ) ;
506+ } catch ( error ) {
507+ window . showErrorMessage ( 'Error during XML minification: ' + error . message ) ;
508+ }
509+ } ) ) ;
510+ }
0 commit comments