@@ -17,6 +17,7 @@ This project is based on the SQL language project of Monaco Editor, which was fo
1717- Code Highlighting
1818- Syntax Validation
1919- Code Completion
20+ - Built-in SQL Snippets
2021
2122> Powered By [ dt-sql-parser] ( https://github.com/DTStack/dt-sql-parser )
2223
@@ -91,7 +92,7 @@ npm install monaco-sql-languages
9192 });
9293 ` ` `
9394
94- By default , Monaco SQL Languages only provides keyword autocompletion , and you can customize your completionItem list via ` completionService ` .
95+ By default , Monaco SQL Languages only provides keyword autocompletion and built - in SQL snippets , and you can customize your completionItem list via ` completionService ` .
9596
9697 ` ` ` typescript
9798 import { languages } from 'monaco-editor/esm/vs/editor/editor.api';
@@ -108,7 +109,8 @@ npm install monaco-sql-languages
108109 position,
109110 completionContext,
110111 suggestions, // syntax context info at caretPosition
111- entities // tables, columns in the syntax context of the editor text
112+ entities, // tables, columns in the syntax context of the editor text
113+ snippets // SQL snippets
112114 ) {
113115 return new Promise((resolve, reject) => {
114116 if (!suggestions) {
@@ -160,6 +162,86 @@ npm install monaco-sql-languages
160162
161163< br / >
162164
165+ ## SQL Snippets
166+
167+ We provide some built - in SQL snippets for each SQL language , which helps us to write SQL quickly .
168+
169+ ** How to customize SQL snippets ? **
170+
171+ When setting language features , you can customize SQL snippets via ` snippets ` configuration . When ` snippets ` is passed in as an empty array , the built - in SQL snippets are disabled .
172+
173+ ` ` ` typescript
174+ import { snippets, CompletionSnippetOption } from 'monaco-sql-languages/esm/main.js';
175+
176+ const customSnippets: CompletionSnippetOption[] = [
177+ {
178+ label: 'INSERT',
179+ prefix: 'insert',
180+ // Will join the line with ` \n `
181+ body: [
182+ 'INSERT INTO ${1 :table_name }',
183+ 'SELECT ${3 :column1 }, ${4 :column2 }',
184+ 'FROM ${2 :source_table }',
185+ 'WHERE ${5 :conditions };\n $6'
186+ ],
187+ description: "This is an 'insert into select' snippet"
188+ }
189+ ];
190+ ` ` `
191+
192+ Snippets syntax can refer to [vscode - snippet ](https :// code.visualstudio.com/docs/editor/userdefinedsnippets#_snippet-syntax).
193+ But it is different from vscode code snippets , we only provide snippets completions ** at the beginning of the SQL statement ** .
194+
195+ Note : If you provide a custom ` completionService ` method , you need to manually return the ` snippets ` as completions , as shown in the following example :
196+
197+ ` ` ` typescript
198+ const completionService: CompletionService = async function (
199+ model,
200+ position,
201+ completionContext,
202+ suggestions,
203+ entities,
204+ snippets
205+ ) {
206+ const { keywords } = suggestions;
207+
208+ const keywordsCompletionItems: ICompletionItem[] = keywords.map((kw) => ({
209+ label: kw,
210+ kind: languages.CompletionItemKind.Keyword,
211+ detail: 'keyword',
212+ sortText: '2' + kw
213+ }));
214+
215+ const snippetCompletionItems: ICompletionItem[] =
216+ snippets?.map((item) => ({
217+ label: item.label || item.prefix,
218+ kind: languages.CompletionItemKind.Snippet,
219+ filterText: item.prefix,
220+ insertText: item.insertText,
221+ insertTextRules: languages.CompletionItemInsertTextRule.InsertAsSnippet,
222+ sortText: '3' + item.prefix,
223+ detail: item.description !== undefined ? item.description : 'SQL Snippet',
224+ documentation: item.insertText
225+ })) || [];
226+
227+ return [...keywordsCompletionItems, ...snippetCompletionItems];
228+ };
229+ ` ` `
230+
231+ Other Notes :
232+
233+ When in code snippet context , you can use ` Tab ` key to move to the next input position , but the keywords completions is also triggered by ` Tab ` key , which will cause a shortcut key conflict . So Monaco - Editor stipulates that when in code snippet context , it will not trigger completion .
234+ ! [snippet - prevent - completion ](./ documents / images / snippet - prevent - completion .gif )
235+ If you want to still support intelligent completion in code snippet context , you can set the Monaco - Editor configuration item ` suggest.snippetsPreventQuickSuggestions ` to ` false ` to achieve it .
236+ ` ` ` typescript
237+ editor.create(editorElement, {
238+ suggest: {
239+ snippetsPreventQuickSuggestions: false
240+ }
241+ })
242+ ` ` `
243+ ! [snippet - no - prevent - completion ](./ documents / images / snippet - no - prevent - completion .gif )
244+
163245## Monaco Theme
164246
165247> Monaco SQL Languages plan to support more themes in the future .
0 commit comments