Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ This VS Code extension provides support for creating and editing XML documents,
* File associations
* Code actions
* Schema Caching
* [Minify XML](https://github.com/redhat-developer/vscode-xml/blob/main/docs/Commands.md#minify-xml-document)

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

Expand Down
78 changes: 47 additions & 31 deletions docs/Commands.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,47 @@
# Commands

[vscode-xml](https://github.com/redhat-developer/vscode-xml) provides several vscode commands which are available with `Ctrl+Shift+P`.

![XML Commands](images/Commands/XMLCommands.png)

## Bind to grammar/schema file

This command triggers the [XML Binding Wizard](BindingWithGrammar.md#the-xml-binding-wizard) for the current file.

Details on the command are described [here](BindingWithGrammar.md#command).

## Open XML Documentation

This command opens the `XML Documentation`.

## Revalidate current XML file

This command re-triggers the [XML Validation](Validation.md#xml-validation) for the current file.

When the [Server Cache Path](Preferences.md#server-cache-path) is activated, the command removes the referenced XSD, DTD grammar from the local cache.

## Revalidate all open XML files

This command re-triggers the [XML Validation](Validation.md#xml-validation) for the all opened XML files.

When the [Server Cache Path](Preferences.md#server-cache-path) is activated, the command clears the remote grammar cache and revalidates all opened files.

## Restart XML Language Server

This command restarts the XML language server.
# Commands

[vscode-xml](https://github.com/redhat-developer/vscode-xml) provides several vscode commands which are available with `Ctrl+Shift+P`.

![XML Commands](images/Commands/XMLCommands.png)

## Bind to grammar/schema file

This command triggers the [XML Binding Wizard](BindingWithGrammar.md#the-xml-binding-wizard) for the current file.

Details on the command are described [here](BindingWithGrammar.md#command).

## Open XML Documentation

This command opens the `XML Documentation`.

## Revalidate current XML file

This command re-triggers the [XML Validation](Validation.md#xml-validation) for the current file.

When the [Server Cache Path](Preferences.md#server-cache-path) is activated, the command removes the referenced XSD, DTD grammar from the local cache.

## Revalidate all open XML files

This command re-triggers the [XML Validation](Validation.md#xml-validation) for the all opened XML files.

When the [Server Cache Path](Preferences.md#server-cache-path) is activated, the command clears the remote grammar cache and revalidates all opened files.

## Restart XML Language Server

This command restarts the XML language server.

## Minify XML Document

This command minifies the current XML document by removing unnecessary whitespace while preserving the document's structure and content.

The minification can be triggered via the **Source > Minify XML** menu.

The minification process:
- Removes all indentation and line breaks between elements
- Removes whitespace between the XML declaration and the root element
- Normalizes whitespace sequences inside text content to a single space
- Preserves whitespace in elements with `xml:space="preserve"` attribute
- Preserves content in CDATA sections
- Reduces multiple spaces between attributes to a single space

This is useful for reducing file size before transmitting or storing XML documents.
86 changes: 32 additions & 54 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,11 @@
"command": "xml.refactor.surround.with.cdata",
"title": "Surround with CDATA",
"category": "XML"
},
{
"command": "xml.minify",
"title": "Minify XML Document",
"category": "XML"
}
],
"menus": {
Expand Down Expand Up @@ -872,6 +877,10 @@
{
"command": "xml.refactor.surround.with.cdata",
"when": "editorLangId in xml.supportedLanguageIds && XMLLSReady"
},
{
"command": "xml.minify",
"when": "editorLangId in xml.supportedLanguageIds && XMLLSReady"
}
],
"editor/context": [
Expand Down
7 changes: 6 additions & 1 deletion src/commands/clientCommandConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,9 @@ export const EXECUTE_WORKSPACE_COMMAND = 'xml.workspace.executeCommand';

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

export const REFACTOR_SURROUND_WITH_CDATA = 'xml.refactor.surround.with.cdata';
export const REFACTOR_SURROUND_WITH_CDATA = 'xml.refactor.surround.with.cdata';

/**
* Command to minify XML document.
*/
export const MINIFY_DOCUMENT = 'xml.minify';
41 changes: 41 additions & 0 deletions src/commands/registerCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
registerCodeLensReferencesCommands(context, languageClient);
registerValidationCommands(context);
registerRefactorCommands(context, languageClient);
registerMinifyCommand(context, languageClient);
registerAssociationCommands(context, languageClient);
registerRestartLanguageServerCommand(context, languageClient);
registerConfigurationUpdateCommand();
Expand All @@ -39,7 +40,7 @@
// Register client command to execute custom XML Language Server command
context.subscriptions.push(commands.registerCommand(ClientCommandConstants.EXECUTE_WORKSPACE_COMMAND, (command, ...rest) => {
let token: CancellationToken;
let commandArgs: any[] = rest;

Check warning on line 43 in src/commands/registerCommands.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

Check warning on line 43 in src/commands/registerCommands.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
if (rest && rest.length && CancellationToken.is(rest[rest.length - 1])) {
token = rest[rest.length - 1];
commandArgs = rest.slice(0, rest.length - 1);
Expand Down Expand Up @@ -467,3 +468,43 @@
}

}

/**
* Register command to minify XML document
*
* @param context the extension context
* @param languageClient the language client
*/
function registerMinifyCommand(context: ExtensionContext, languageClient: LanguageClient) {
context.subscriptions.push(commands.registerCommand(ClientCommandConstants.MINIFY_DOCUMENT, async () => {
const activeEditor = window.activeTextEditor;
if (!activeEditor || activeEditor.document.languageId !== 'xml') {
return;
}

const uri = activeEditor.document.uri;
const identifier = TextDocumentIdentifier.create(uri.toString());

try {
// Call the server command to get the minified text edits
const edits: TextEdit[] = await commands.executeCommand(
ClientCommandConstants.EXECUTE_WORKSPACE_COMMAND,
ServerCommandConstants.MINIFY_DOCUMENT,
identifier
);

if (!edits || edits.length === 0) {
return;
}

// Apply the text edits
const workEdits = new WorkspaceEdit();
for (const edit of edits) {
workEdits.replace(uri, languageClient.protocol2CodeConverter.asRange(edit.range), edit.newText);
}
await workspace.applyEdit(workEdits);
} catch (error) {
window.showErrorMessage('Error during XML minification: ' + error.message);
}
}));
}
7 changes: 6 additions & 1 deletion src/commands/serverCommandConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ export const CHECK_FILE_PATTERN = "xml.check.file.pattern";
/**
* Command to surround with tags, comments, cdata
*/
export const REFACTOR_SURROUND_WITH = "xml.refactor.surround.with";
export const REFACTOR_SURROUND_WITH = "xml.refactor.surround.with";

/**
* Command to minify XML document
*/
export const MINIFY_DOCUMENT = "xml.minify.document";
Loading