Skip to content

Commit 73f93e4

Browse files
committed
editor: add code completion snippets
1 parent a6ddf16 commit 73f93e4

File tree

3 files changed

+134
-3
lines changed

3 files changed

+134
-3
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ Use `-help` to get information about command params
4444
### Third-party credits
4545

4646
* Default playground run server provided by [play.golang.org](https://play.golang.org)
47-
* Snippets for templates and tutorials provided by [gobyexample.com](https://gobyexample.com/)
47+
* Code for templates and tutorials provided by [gobyexample.com](https://gobyexample.com/)
48+
* Code completion snippets were inspired by [tj/vscode-snippets](https://github.com/tj/vscode-snippets/blob/master/go.json)
4849

4950

5051
## Contributors

web/src/editor/provider.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as monaco from 'monaco-editor';
22
import {IAPIClient} from '../services/api';
3+
import snippets from './snippets'
34

45
// Import aliases
56
type CompletionList = monaco.languages.CompletionList;
@@ -53,10 +54,11 @@ class GoCompletionItemProvider implements monaco.languages.CompletionItemProvide
5354

5455
try {
5556
const resp = await this.client.getSuggestions(query);
56-
return Promise.resolve(resp);
57+
resp.suggestions = resp.suggestions ? snippets.concat(resp.suggestions) : snippets;
58+
return resp;
5759
} catch (err) {
5860
console.error(`Failed to get code completion from server: ${err.message}`);
59-
return Promise.resolve({suggestions: []});
61+
return {suggestions: snippets};
6062
}
6163
}
6264
}

web/src/editor/snippets.ts

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import * as monaco from 'monaco-editor';
2+
3+
/* eslint-disable no-template-curly-in-string */
4+
5+
// Import aliases
6+
type CompletionList = monaco.languages.CompletionList;
7+
type CompletionContext = monaco.languages.CompletionContext;
8+
type ITextModel = monaco.editor.ITextModel;
9+
type Position = monaco.Position;
10+
11+
/**
12+
* List of snippets for editor
13+
*/
14+
const snippets = [
15+
{
16+
label: 'iferr',
17+
insertText: [
18+
'if err != nil {',
19+
'\treturn ${1:nil, err}',
20+
'}'
21+
].join('\n'),
22+
documentation: 'Return error if err != nil'
23+
},
24+
{
25+
label: 'switch',
26+
insertText: [
27+
"switch ${1:expression} {",
28+
"\tcase ${2:match}:",
29+
"\t\t$0",
30+
"\tdefault:",
31+
"\t\t// TODO: implement",
32+
"}",
33+
].join('\n'),
34+
documentation: 'Insert switch statement'
35+
},
36+
{
37+
label: 'go',
38+
insertText: [
39+
"go func(){",
40+
"\t$0",
41+
"}()"
42+
].join('\n'),
43+
documentation: 'Call goroutine'
44+
},
45+
{
46+
label: 'append',
47+
insertText: "$1 = append($1, $0)",
48+
documentation: 'Append to slice'
49+
},
50+
{
51+
label: 'typestruct',
52+
insertText: [
53+
"type ${1:name} struct {",
54+
"\t$0",
55+
"}"
56+
].join('\n'),
57+
documentation: 'Declare a struct'
58+
},
59+
{
60+
label: 'typeinterface',
61+
insertText: [
62+
"type ${1:name} interface {",
63+
"\t$0",
64+
"}"
65+
].join('\n'),
66+
documentation: 'Declare a struct'
67+
},
68+
{
69+
label: 'fmtprintf',
70+
insertText: 'fmt.Printf("${1:format}\n", $0)',
71+
documentation: 'fmt.Printf() shorthand'
72+
},
73+
{
74+
label: 'fmtprintln',
75+
insertText: 'fmt.Println(${0:message})',
76+
documentation: 'fmt.Println() shorthand'
77+
},
78+
{
79+
label: 'returnnil',
80+
insertText: 'return nil',
81+
documentation: 'return nil'
82+
},
83+
{
84+
label: 'timenow',
85+
insertText: '${0:t} := time.Now()',
86+
documentation: 'time.Now() shorthand'
87+
},
88+
{
89+
label: 'makeslice',
90+
insertText: '${0:items} := make([]${1:string}, ${2:0}, ${3:0})',
91+
documentation: 'Make a new slice'
92+
},
93+
{
94+
label: 'slice',
95+
insertText: '${1:items} := []${2:string}{${0}}',
96+
documentation: 'Declare slice'
97+
},
98+
{
99+
label: 'map',
100+
insertText: 'map[${1:string}]${0:value}',
101+
documentation: 'Insert map type'
102+
},
103+
{
104+
label: 'func',
105+
insertText: [
106+
"func ${1:functionName}(${3:param}) $4 {",
107+
" $0",
108+
"}"
109+
].join('\n'),
110+
documentation: 'Insert a function'
111+
},
112+
{
113+
label: 'for',
114+
insertText: [
115+
"for _, ${1:v} := range ${2:value} {",
116+
" $0",
117+
"}"
118+
].join('\n'),
119+
documentation: 'Insert a for range statement'
120+
},
121+
122+
].map(s => ({
123+
kind: monaco.languages.CompletionItemKind.Snippet,
124+
insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
125+
...s,
126+
}));
127+
128+
export default snippets as monaco.languages.CompletionItem[];

0 commit comments

Comments
 (0)