Skip to content

Commit 3ad9749

Browse files
authored
Provide more alias for code snippets (#2314)
- sysout -> sout - syserr -> serr - main -> psvm - foreach -> iter Signed-off-by: Sheng Chen <[email protected]>
1 parent 8aadf21 commit 3ad9749

File tree

3 files changed

+28
-18
lines changed

3 files changed

+28
-18
lines changed

snippets/java.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"main": {
3-
"prefix": "main",
3+
"prefix": ["main", "psvm"],
44
"body": [
55
"public static void main(String[] args) {",
66
"\t$0",

snippets/server.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
{
22
"sysout": {
3-
"prefix": "sysout",
3+
"prefix": ["sysout", "sout"],
44
"body": [
55
"System.out.println($0);"
66
],
77
"description": "Print to standard out"
88
},
99
"syserr": {
10-
"prefix": "syserr",
10+
"prefix": ["syserr", "serr"],
1111
"body": [
1212
"System.err.println($0);"
1313
],
1414
"description": "Print to standard err"
1515
},
1616
"fori": {
17-
"prefix": "fori",
17+
"prefix": ["fori"],
1818
"body": [
1919
"for (${1:int} ${2:i} = ${3:0}; ${2:i} < ${4:max}; ${2:i}++) {",
2020
"\t$0",
@@ -23,7 +23,7 @@
2323
"description": "Indexed for loop"
2424
},
2525
"foreach": {
26-
"prefix": "foreach",
26+
"prefix": ["foreach", "iter"],
2727
"body": [
2828
"for (${1:type} ${2:var} : ${3:iterable}) {",
2929
"\t$0",
@@ -32,7 +32,7 @@
3232
"description": "Enhanced for loop"
3333
},
3434
"if": {
35-
"prefix": "if",
35+
"prefix": ["if"],
3636
"body": [
3737
"if (${1:condition}) {",
3838
"\t$0",
@@ -41,7 +41,7 @@
4141
"description": "if statement"
4242
},
4343
"ifelse": {
44-
"prefix": "ifelse",
44+
"prefix": ["ifelse"],
4545
"body": [
4646
"if (${1:condition}) {",
4747
"\t$2",
@@ -52,7 +52,7 @@
5252
"description": "if/else statement"
5353
},
5454
"ifnull": {
55-
"prefix": "ifnull",
55+
"prefix": ["ifnull"],
5656
"body": [
5757
"if (${1:condition} == null) {",
5858
"\t$0",
@@ -61,7 +61,7 @@
6161
"description": "if statement checking for null"
6262
},
6363
"ifnotnull": {
64-
"prefix": "ifnotnull",
64+
"prefix": ["ifnotnull"],
6565
"body": [
6666
"if (${1:condition} != null) {",
6767
"\t$0",
@@ -70,7 +70,7 @@
7070
"description": "if statement checking for not null"
7171
},
7272
"While Statement": {
73-
"prefix": "while",
73+
"prefix": ["while"],
7474
"body": [
7575
"while (${1:condition}) {",
7676
"\t$0",
@@ -79,7 +79,7 @@
7979
"description": "While Statement"
8080
},
8181
"Do-While Statement": {
82-
"prefix": "dowhile",
82+
"prefix": ["dowhile"],
8383
"body": [
8484
"do {",
8585
"\t$0",

src/snippetCompletionProvider.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,26 @@ export class SnippetCompletionProvider implements CompletionItemProvider {
2222
const snippetItems: CompletionItem[] = [];
2323
for (const label of Object.keys(this.snippets)) {
2424
const snippetContent = this.snippets[label];
25-
const snippetItem: CompletionItem = new CompletionItem(snippetContent.prefix);
26-
snippetItem.kind = CompletionItemKind.Snippet;
27-
snippetItem.detail = snippetContent.description;
28-
const insertText: string = (snippetContent.body as String[]).join('\n');
29-
snippetItem.insertText = new SnippetString(insertText);
30-
snippetItem.documentation = beautifyDocument(insertText);
31-
snippetItems.push(snippetItem);
25+
if (Array.isArray(snippetContent.prefix)) {
26+
for (const prefix of snippetContent.prefix) {
27+
snippetItems.push(this.getCompletionItem(prefix, snippetContent));
28+
}
29+
} else {
30+
snippetItems.push(this.getCompletionItem(snippetContent.prefix, snippetContent));
31+
}
3232
}
3333
return snippetItems;
3434
}
35+
36+
private getCompletionItem(prefix: string, snippetContent: any) {
37+
const snippetItem: CompletionItem = new CompletionItem(prefix);
38+
snippetItem.kind = CompletionItemKind.Snippet;
39+
snippetItem.detail = snippetContent.description;
40+
const insertText: string = (snippetContent.body as String[]).join('\n');
41+
snippetItem.insertText = new SnippetString(insertText);
42+
snippetItem.documentation = beautifyDocument(insertText);
43+
return snippetItem;
44+
}
3545
}
3646

3747
export function beautifyDocument(raw: string): MarkdownString {

0 commit comments

Comments
 (0)