Skip to content

Commit 095346f

Browse files
angelozerrdatho7561
authored andcommitted
MinifyXML Minify support
Fixes #609 Signed-off-by: azerr <azerr@redhat.com>
1 parent ae4f12b commit 095346f

7 files changed

Lines changed: 142 additions & 87 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ This VS Code extension provides support for creating and editing XML documents,
4646
* File associations
4747
* Code actions
4848
* Schema Caching
49+
* [Minify XML](https://github.com/redhat-developer/vscode-xml/blob/main/docs/Commands.md#minify-xml-document)
4950

5051
See the [changelog](CHANGELOG.md) for the latest release.
5152

docs/Commands.md

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,47 @@
1-
# Commands
2-
3-
[vscode-xml](https://github.com/redhat-developer/vscode-xml) provides several vscode commands which are available with `Ctrl+Shift+P`.
4-
5-
![XML Commands](images/Commands/XMLCommands.png)
6-
7-
## Bind to grammar/schema file
8-
9-
This command triggers the [XML Binding Wizard](BindingWithGrammar.md#the-xml-binding-wizard) for the current file.
10-
11-
Details on the command are described [here](BindingWithGrammar.md#command).
12-
13-
## Open XML Documentation
14-
15-
This command opens the `XML Documentation`.
16-
17-
## Revalidate current XML file
18-
19-
This command re-triggers the [XML Validation](Validation.md#xml-validation) for the current file.
20-
21-
When the [Server Cache Path](Preferences.md#server-cache-path) is activated, the command removes the referenced XSD, DTD grammar from the local cache.
22-
23-
## Revalidate all open XML files
24-
25-
This command re-triggers the [XML Validation](Validation.md#xml-validation) for the all opened XML files.
26-
27-
When the [Server Cache Path](Preferences.md#server-cache-path) is activated, the command clears the remote grammar cache and revalidates all opened files.
28-
29-
## Restart XML Language Server
30-
31-
This command restarts the XML language server.
1+
# Commands
2+
3+
[vscode-xml](https://github.com/redhat-developer/vscode-xml) provides several vscode commands which are available with `Ctrl+Shift+P`.
4+
5+
![XML Commands](images/Commands/XMLCommands.png)
6+
7+
## Bind to grammar/schema file
8+
9+
This command triggers the [XML Binding Wizard](BindingWithGrammar.md#the-xml-binding-wizard) for the current file.
10+
11+
Details on the command are described [here](BindingWithGrammar.md#command).
12+
13+
## Open XML Documentation
14+
15+
This command opens the `XML Documentation`.
16+
17+
## Revalidate current XML file
18+
19+
This command re-triggers the [XML Validation](Validation.md#xml-validation) for the current file.
20+
21+
When the [Server Cache Path](Preferences.md#server-cache-path) is activated, the command removes the referenced XSD, DTD grammar from the local cache.
22+
23+
## Revalidate all open XML files
24+
25+
This command re-triggers the [XML Validation](Validation.md#xml-validation) for the all opened XML files.
26+
27+
When the [Server Cache Path](Preferences.md#server-cache-path) is activated, the command clears the remote grammar cache and revalidates all opened files.
28+
29+
## Restart XML Language Server
30+
31+
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 can be triggered via the **Source > Minify XML** menu.
38+
39+
The minification process:
40+
- Removes all indentation and line breaks between elements
41+
- Removes whitespace between the XML declaration and the root element
42+
- Normalizes whitespace sequences inside text content to a single space
43+
- Preserves whitespace in elements with `xml:space="preserve"` attribute
44+
- Preserves content in CDATA sections
45+
- Reduces multiple spaces between attributes to a single space
46+
47+
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)