Skip to content

Commit

Permalink
XML Files support with settings
Browse files Browse the repository at this point in the history
See eclipse/lemminx#1464

Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed Nov 28, 2023
1 parent e08f378 commit 67dfce3
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 1 deletion.
75 changes: 75 additions & 0 deletions docs/Features/XMLFilePathSupport.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# XML File Paths Features

XML file path support provides the capability to mark a DOM node (attribute or text) as file path with the `xml.filePathSupport` settings by using XPath expression :

* `path/text()` defines the text node of the `path` element.
* `item/@path` defines the `path` attribute node of the `item` element.

Once the DOM node is marked as file path, you will benefit with:

* Files completion.

## Define File path in Text Content with `path/text()`

Given this XML file `items.xml` sample:

```xml
<?xml version="1.0" encoding="utf-8"?>
<items>
<path>path/to/file.xml</path>
</items>
```

In this sample:

* text of `path` tag element `<path>/path/to/file.xml</path>` declare a file path. [vscode-xml](https:/redhat-developer/vscode-xml) provides a file path support with the `xml.filePathSupport` settings. You can declare this settings:

```json
"xml.filePathSupport": [
{
"pattern": "**/items.xml",
"expressions": [
{
"xpath": "items/path/text()"
}
]
}
]
```

After saving this setting, you will get file path completion support for the text node of `path` tag element:

![XML File Paths in Text](../images/Features/XMLFilePathsInTextFeatures.png)

## Define File path in Attribute with `item/@path`

Attribute values may also be marked as file path by using the proper XPath.

Given this `items.xml` XML file:

```xml
<?xml version="1.0" encoding="utf-8"?>
<items>
<item path="path/to/file.xml" ></item>
</items>

```

You can declare this settings:

```json
"xml.filePathSupport": [
{
"pattern": "**/items.xml",
"expressions": [
{
"xpath": "item/@path"
}
]
}
]
```

After saving this setting, you will get file path completion support for the text node of `path` attribute:

![XML File Paths in Attr](../images/Features/XMLFilePathsInAttrFeatures.png)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,41 @@
"markdownDescription": "Allows colors for the given file name patterns. See [here](command:xml.open.docs?%5B%7B%22page%22%3A%22Features/XMLColorsFeatures%22%2C%22section%22%3A%22xmlcolorsfeatures%22%7D%5D) for more information.",
"scope": "window"
},
"xml.filePathSupport.mappings": {
"type": "array",
"default": [],
"items": {
"type": "object",
"properties": {
"pattern": {
"type": "string",
"markdownDescription": "matches the files that file path declared with `expressions` applies to.\n\nMore information on the glob syntax: https://docs.oracle.com/javase/tutorial/essential/io/fileOps.html#glob"
},
"expressions": {
"type": "array",
"default": [],
"items": {
"type": "object",
"properties": {
"xpath": {
"type": "string",
"description": "The file path DOM node (attribute, text) declared with XPath (ex: foo/@path, foo/text())"
}
}
},
"required": [
"xpath"
]
}
},
"required": [
"pattern",
"expressions"
]
},
"markdownDescription": "Allows file path for the given file name patterns. See [here](command:xml.open.docs?%5B%7B%22page%22%3A%22Features/XMLFilePathSupport%22%2C%22section%22%3A%22xmlfilepathsfeatures%22%7D%5D) for more information.",
"scope": "window"
},
"xml.extension.jars": {
"type": "array",
"default": [],
Expand Down
2 changes: 1 addition & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/

import * as fs from 'fs-extra';
import { DocumentFilter, DocumentSelector, ExtensionContext, Uri, commands, extensions, languages } from "vscode";
import { ExtensionContext, Uri, commands, extensions, languages } from "vscode";
import { Executable, LanguageClient } from 'vscode-languageclient/node';
import { XMLExtensionApi } from './api/xmlExtensionApi';
import { getXmlExtensionApiImplementation } from './api/xmlExtensionApiImplementation';
Expand Down

0 comments on commit 67dfce3

Please sign in to comment.