Skip to content

Commit 6f69358

Browse files
committed
MinifyXML Minify support
Fixes redhat-developer#609 Signed-off-by: azerr <azerr@redhat.com>
1 parent ae4f12b commit 6f69358

File tree

5 files changed

+96
-56
lines changed

5 files changed

+96
-56
lines changed

package-lock.json

Lines changed: 32 additions & 54 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,11 @@
841841
"command": "xml.refactor.surround.with.cdata",
842842
"title": "Surround with CDATA",
843843
"category": "XML"
844+
},
845+
{
846+
"command": "xml.minify",
847+
"title": "Minify XML Document",
848+
"category": "XML"
844849
}
845850
],
846851
"menus": {
@@ -872,6 +877,10 @@
872877
{
873878
"command": "xml.refactor.surround.with.cdata",
874879
"when": "editorLangId in xml.supportedLanguageIds && XMLLSReady"
880+
},
881+
{
882+
"command": "xml.minify",
883+
"when": "editorLangId in xml.supportedLanguageIds && XMLLSReady"
875884
}
876885
],
877886
"editor/context": [

src/commands/clientCommandConstants.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,9 @@ export const EXECUTE_WORKSPACE_COMMAND = 'xml.workspace.executeCommand';
7474

7575
export const REFACTOR_SURROUND_WITH_COMMENTS = 'xml.refactor.surround.with.comments';
7676

77-
export const REFACTOR_SURROUND_WITH_CDATA = 'xml.refactor.surround.with.cdata';
77+
export const REFACTOR_SURROUND_WITH_CDATA = 'xml.refactor.surround.with.cdata';
78+
79+
/**
80+
* Command to minify XML document.
81+
*/
82+
export const MINIFY_DOCUMENT = 'xml.minify';

src/commands/registerCommands.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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,45 @@ 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+
507+
window.showInformationMessage('XML document minified successfully.');
508+
} catch (error) {
509+
window.showErrorMessage('Error during XML minification: ' + error.message);
510+
}
511+
}));
512+
}

src/commands/serverCommandConstants.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,9 @@ export const CHECK_FILE_PATTERN = "xml.check.file.pattern";
3434
/**
3535
* Command to surround with tags, comments, cdata
3636
*/
37-
export const REFACTOR_SURROUND_WITH = "xml.refactor.surround.with";
37+
export const REFACTOR_SURROUND_WITH = "xml.refactor.surround.with";
38+
39+
/**
40+
* Command to minify XML document
41+
*/
42+
export const MINIFY_DOCUMENT = "xml.minify.document";

0 commit comments

Comments
 (0)