Skip to content

Commit 1ac26ce

Browse files
committed
feat: more snippets
1 parent bec5ff2 commit 1ac26ce

File tree

8 files changed

+229
-11
lines changed

8 files changed

+229
-11
lines changed

src/completion/XmlSnippetProvider.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ interface Snippet {
66
prefix: string;
77
body: string[];
88
description: string;
9-
parent?: string;
9+
parent?: string | string[] | null;
1010
}
1111

1212
interface SnippetProvider {
@@ -18,6 +18,18 @@ export class XmlSnippetProvider implements vscode.CompletionItemProvider {
1818
public static readonly TRIGGER_CHARACTERS = ['<'];
1919

2020
private readonly snippetProviders: SnippetProvider[] = [
21+
{
22+
pattern: '**/acl.xml',
23+
snippets: require('./xml/snippet/acl.json'),
24+
},
25+
{
26+
pattern: '**/extension-attributes.xml',
27+
snippets: require('./xml/snippet/extension-attributes.json'),
28+
},
29+
{
30+
pattern: '**/fieldset.xml',
31+
snippets: require('./xml/snippet/fieldset.json'),
32+
},
2133
{
2234
pattern: '**/di.xml',
2335
snippets: require('./xml/snippet/di.json'),
@@ -55,10 +67,20 @@ export class XmlSnippetProvider implements vscode.CompletionItemProvider {
5567
for (const name in snippets) {
5668
const snippet = snippets[name];
5769

58-
if (snippet.parent && snippet.parent !== directParentName) {
70+
if (snippet.parent === null && directParentName) {
5971
continue;
6072
}
6173

74+
if (snippet.parent) {
75+
if (Array.isArray(snippet.parent)) {
76+
if (!snippet.parent.includes(directParentName || '')) {
77+
continue;
78+
}
79+
} else if (snippet.parent !== directParentName) {
80+
continue;
81+
}
82+
}
83+
6284
const completionItem = new vscode.CompletionItem(
6385
{
6486
label: name,

src/completion/xml/snippet/acl.json

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"config": {
3+
"parent": null,
4+
"prefix": "config",
5+
"body": [
6+
"<?xml version=\"1.0\"?>",
7+
"<config xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"",
8+
" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Acl/etc/acl.xsd\">",
9+
" ${1}",
10+
"</config>"
11+
],
12+
"description": "Insert a config element"
13+
},
14+
"acl": {
15+
"parent": "config",
16+
"prefix": "acl",
17+
"body": [
18+
"<acl>",
19+
" <resources>",
20+
" ${1}",
21+
" </resources>",
22+
"</acl>"
23+
],
24+
"description": "Insert an acl element"
25+
},
26+
"resource (only ID)": {
27+
"parent": ["resources", "resource"],
28+
"prefix": "resource",
29+
"body": [
30+
"<resource id=\"${1}\">",
31+
" ${2}",
32+
"</resource>"
33+
],
34+
"description": "Insert a resource element"
35+
},
36+
"resource (with ID and title)": {
37+
"parent": ["resources", "resource"],
38+
"prefix": "resource",
39+
"body": [
40+
"<resource id=\"${1}\" title=\"${2}\" translate=\"title\">",
41+
" ${3}",
42+
"</resource>"
43+
],
44+
"description": "Insert a resource element"
45+
}
46+
}

src/completion/xml/snippet/crontab.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,17 @@
1818
"</job>"
1919
],
2020
"description": "Insert a cron job element"
21+
},
22+
"config": {
23+
"parent": null,
24+
"prefix": "config",
25+
"body": [
26+
"<?xml version=\"1.0\"?>",
27+
"<config xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"",
28+
" xsi:noNamespaceSchemaLocation=\"urn:magento:module:Magento_Cron:etc/crontab.xsd\">",
29+
" ${1}",
30+
"</config>"
31+
],
32+
"description": "Insert a sample config element"
2133
}
2234
}

src/completion/xml/snippet/di.json

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"body": [
66
"<preference for=\"${1}\" type=\"${2}\" />"
77
],
8-
"description": "Insert a preference element to the current file"
8+
"description": "Insert a preference element"
99
},
1010
"type": {
1111
"parent": "config",
@@ -15,7 +15,7 @@
1515
" ${2}",
1616
"</type>"
1717
],
18-
"description": "Insert a type element to the current file"
18+
"description": "Insert a type element"
1919
},
2020
"arguments": {
2121
"parent": "type",
@@ -25,23 +25,33 @@
2525
" ${1}",
2626
"</arguments>"
2727
],
28-
"description": "Insert an arguments element to the current file"
28+
"description": "Insert an arguments element"
2929
},
3030
"argument": {
3131
"parent": "arguments",
3232
"prefix": "argument",
3333
"body": [
3434
"<argument name=\"${1}\" xsi:type=\"${2}\">${3}</argument>"
3535
],
36-
"description": "Insert an argument element to the current file"
36+
"description": "Insert an argument element"
3737
},
3838
"plugin": {
3939
"parent": "type",
4040
"prefix": "plugin",
4141
"body": [
4242
"<plugin name=\"${1}\" type=\"${2}\" />"
4343
],
44-
"description": "Insert a plugin element to the current file"
44+
"description": "Insert a plugin element"
45+
},
46+
"type with plugin": {
47+
"parent": "config",
48+
"prefix": "type",
49+
"body": [
50+
"<type name=\"${1}\">",
51+
" <plugin name=\"${2}\" type=\"${3}\" />",
52+
"</type>"
53+
],
54+
"description": "Insert a type element with a plugin element"
4555
},
4656
"virtualType": {
4757
"parent": "config",
@@ -51,6 +61,18 @@
5161
" ${3}",
5262
"</virtualType>"
5363
],
54-
"description": "Insert a virtualType element to the current file"
64+
"description": "Insert a virtualType element"
65+
},
66+
"config": {
67+
"parent": null,
68+
"prefix": "config",
69+
"body": [
70+
"<?xml version=\"1.0\"?>",
71+
"<config xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"",
72+
" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:ObjectManager/etc/config.xsd\">",
73+
" ${1}",
74+
"</config>"
75+
],
76+
"description": "Insert a config element"
5577
}
5678
}

src/completion/xml/snippet/events.json

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,26 @@
55
"body": [
66
"<event name=\"${1}\">\n ${2}\n</event>"
77
],
8-
"description": "Insert a event element to the current file"
8+
"description": "Insert a event element"
99
},
1010
"observer": {
1111
"parent": "event",
1212
"prefix": "observer",
1313
"body": [
1414
"<observer name=\"${1}\" instance=\"${2}\" />"
1515
],
16-
"description": "Insert a observer element to the current file"
16+
"description": "Insert a observer element"
17+
},
18+
"config": {
19+
"parent": null,
20+
"prefix": "config",
21+
"body": [
22+
"<?xml version=\"1.0\"?>",
23+
"<config xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"",
24+
" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Event/etc/events.xsd\">",
25+
" ${1}",
26+
"</config>"
27+
],
28+
"description": "Insert a config element"
1729
}
1830
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"config": {
3+
"parent": null,
4+
"prefix": "config",
5+
"body": [
6+
"<?xml version=\"1.0\"?>",
7+
"<config xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"",
8+
" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Api/etc/extension_attributes.xsd\">",
9+
" ${1}",
10+
"</config>"
11+
],
12+
"description": "Insert a config element"
13+
},
14+
"extension_attributes": {
15+
"parent": "config",
16+
"prefix": "extension_attributes",
17+
"body": [
18+
"<extension_attributes for=\"${1}\">",
19+
" ${2}",
20+
"</extension_attributes>"
21+
],
22+
"description": "Insert an extension_attributes element"
23+
},
24+
"attribute": {
25+
"parent": "extension_attributes",
26+
"prefix": "attribute",
27+
"body": [
28+
"<attribute name=\"${1}\" type=\"${2}\" />"
29+
],
30+
"description": "Insert an attribute element"
31+
}
32+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"config": {
3+
"parent": null,
4+
"prefix": "config",
5+
"body": [
6+
"<?xml version=\"1.0\"?>",
7+
"<config xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"",
8+
" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:DataObject/etc/fieldset.xsd\">",
9+
" ${1}",
10+
"</config>"
11+
],
12+
"description": "Insert a config element"
13+
},
14+
"scope": {
15+
"parent": "config",
16+
"prefix": "scope",
17+
"body": [
18+
"<scope id=\"${1:global}\">",
19+
" ${2}",
20+
"</scope>"
21+
],
22+
"description": "Insert a scope element"
23+
},
24+
"fieldset": {
25+
"parent": "scope",
26+
"prefix": "fieldset",
27+
"body": [
28+
"<fieldset name=\"${1}\">",
29+
" ${2}",
30+
"</fieldset>"
31+
],
32+
"description": "Insert a fieldset element"
33+
},
34+
"field": {
35+
"parent": "fieldset",
36+
"prefix": "field",
37+
"body": [
38+
"<field name=\"${1}\">",
39+
" <aspect name=\"${2:to_order_item}\" />",
40+
"</field>"
41+
],
42+
"description": "Insert a field element"
43+
}
44+
}

src/completion/xml/snippet/webapi.json

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,34 @@
1010
" </resources>",
1111
"</route>"
1212
],
13-
"description": "Insert a route element to the current file"
13+
"description": "Insert a route element"
14+
},
15+
"resource": {
16+
"parent": "resources",
17+
"prefix": "resource",
18+
"body": [
19+
"<resource ref=\"${1}\" />"
20+
],
21+
"description": "Insert a resource element"
22+
},
23+
"service": {
24+
"parent": "route",
25+
"prefix": "service",
26+
"body": [
27+
"<service class=\"${1}\" method=\"${2}\" />"
28+
],
29+
"description": "Insert a service element"
30+
},
31+
"routes": {
32+
"parent": null,
33+
"prefix": "routes",
34+
"body": [
35+
"<?xml version=\"1.0\"?>",
36+
"<routes xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"",
37+
" xsi:noNamespaceSchemaLocation=\"urn:magento:module:Magento_Webapi:etc/webapi.xsd\">",
38+
" ${1}",
39+
"</routes>"
40+
],
41+
"description": "Insert a routes element"
1442
}
1543
}

0 commit comments

Comments
 (0)