Skip to content

Commit 00d9db6

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

File tree

6 files changed

+108
-56
lines changed

6 files changed

+108
-56
lines changed

docs/Commands.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,17 @@ When the [Server Cache Path](Preferences.md#server-cache-path) is activated, the
2929
## Restart XML Language Server
3030

3131
This command restarts the XML language server.
32+
33+
## Minify XML Document
34+
35+
This command minifies the current XML document by removing unnecessary whitespace while preserving the document's structure and content.
36+
37+
The minification process:
38+
- Removes all indentation and line breaks between elements
39+
- Removes whitespace between the XML declaration and the root element
40+
- Normalizes whitespace sequences inside text content to a single space
41+
- Preserves whitespace in elements with `xml:space="preserve"` attribute
42+
- Preserves content in CDATA sections
43+
- Reduces multiple spaces between attributes to a single space
44+
45+
This is useful for reducing file size before transmitting or storing XML documents.

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: 41 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,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+
}

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)