Skip to content

Commit 184bdd9

Browse files
angelozerrfbricon
authored andcommitted
XML Files support with settings
See eclipse-lemminx/lemminx#1464 Signed-off-by: azerr <[email protected]>
1 parent e08f378 commit 184bdd9

File tree

5 files changed

+174
-1
lines changed

5 files changed

+174
-1
lines changed

docs/Features/XMLFilePathSupport.md

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# XML File Paths Features
2+
3+
XML file path support provides the capability to mark a DOM node (attribute or text) as file path with the `xml.filePathSupport.mappings` setting, by using XPath expression :
4+
5+
* `path/text()` defines the text node of the `path` element.
6+
* `item/@path` defines the `path` attribute node of the `item` element.
7+
8+
Once the DOM node is designated as a file path, you will enjoy the benefits of file completion.
9+
10+
* Files completion.
11+
12+
## Define File path in Text Content with `path/text()`
13+
14+
Given this XML file `items.xml` sample:
15+
16+
```xml
17+
<?xml version="1.0" encoding="utf-8"?>
18+
<items>
19+
<path>path/to/file.xml</path>
20+
</items>
21+
```
22+
23+
In this example:
24+
25+
The text within the `path` tag element `<path>path/to/file.xml</path>` represents a file path. The [vscode-xml](https://github.com/redhat-developer/vscode-xml) extension offers file path support through the `xml.filePathSupport.mappings` settings. You can configure this setting as follows:
26+
27+
```json
28+
"xml.filePathSupport.mappings": [
29+
{
30+
"pattern": "**/items.xml",
31+
"expressions": [
32+
{
33+
"xpath": "items/path/text()"
34+
}
35+
]
36+
}
37+
]
38+
```
39+
40+
After saving this setting, you will get file path completion support for the text node of `path` tag element:
41+
42+
![XML File Paths in Text](../images/Features/XMLFilePathsInTextFeatures.png)
43+
44+
## Define File path in Attribute with `item/@path`
45+
46+
Attribute values may also be marked as file path by using the proper XPath.
47+
48+
Given this `items.xml` XML file:
49+
50+
```xml
51+
<?xml version="1.0" encoding="utf-8"?>
52+
<items>
53+
<item path="path/to/file.xml" ></item>
54+
</items>
55+
56+
```
57+
58+
You can declare this settings:
59+
60+
```json
61+
"xml.filePathSupport.mappings": [
62+
{
63+
"pattern": "**/items.xml",
64+
"expressions": [
65+
{
66+
"xpath": "item/@path"
67+
}
68+
]
69+
}
70+
]
71+
```
72+
73+
After saving this setting, you will get file path completion support for the text node of `path` attribute:
74+
75+
![XML File Paths in Attr](../images/Features/XMLFilePathsInAttrFeatures.png)
76+
77+
## Filter the file path completion result
78+
79+
Given this `images.xml` file:
80+
81+
```xml
82+
<items>
83+
<image src="" />
84+
</items>
85+
```
86+
87+
If you need to restrict file path completion on `image/@src` files with the `.png`, `.gif` or `.jpg` extensions, you can use the `filter` property, with this settings:
88+
89+
```json
90+
"xml.filePathSupport.mappings": [
91+
{
92+
"pattern": "**/images.xml",
93+
"expressions": [
94+
{
95+
"xpath": "image/@src",
96+
"filter": [".png", ".gif", ".jpg"]
97+
}
98+
]
99+
}
100+
]
101+
```
102+
103+
## Separator to declare multiple file paths.
104+
105+
Given this `paths.xml` file:
106+
107+
```xml
108+
<items>
109+
<item paths="path/to/file1.xml;path/to/file2.xml" />
110+
</items>
111+
```
112+
113+
If you want to handle file path completion for `item/@paths` by declaring several file paths separated by `;`, you can use the `separator` property with these settings:
114+
115+
```json
116+
"xml.filePathSupport.mappings": [
117+
{
118+
"pattern": "**/paths.xml",
119+
"expressions": [
120+
{
121+
"xpath": "item/@paths",
122+
"separator": ";"
123+
}
124+
]
125+
}
126+
]
127+
```
22.3 KB
Loading
21.9 KB
Loading

package.json

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,52 @@
728728
"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.",
729729
"scope": "window"
730730
},
731+
"xml.filePathSupport.mappings": {
732+
"type": "array",
733+
"default": [],
734+
"items": {
735+
"type": "object",
736+
"properties": {
737+
"pattern": {
738+
"type": "string",
739+
"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"
740+
},
741+
"expressions": {
742+
"type": "array",
743+
"default": [],
744+
"items": {
745+
"type": "object",
746+
"properties": {
747+
"xpath": {
748+
"type": "string",
749+
"description": "The file path DOM node (attribute, text) declared with XPath (ex: foo/@path, foo/text())"
750+
},
751+
"filter": {
752+
"type": "array",
753+
"items": {
754+
"type": "string"
755+
},
756+
"description": "String array which contains allowed file extensions (ex: [\".png\", \".gif\", \".jpg\"])"
757+
},
758+
"separator": {
759+
"type": "string",
760+
"description": "Separator character to use if multiple file paths are allowed (ex: \";\")"
761+
}
762+
}
763+
},
764+
"required": [
765+
"xpath"
766+
]
767+
}
768+
},
769+
"required": [
770+
"pattern",
771+
"expressions"
772+
]
773+
},
774+
"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.",
775+
"scope": "window"
776+
},
731777
"xml.extension.jars": {
732778
"type": "array",
733779
"default": [],

src/extension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212

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

0 commit comments

Comments
 (0)