|
| 1 | +import { Monaco } from "@monaco-editor/react"; |
| 2 | +import { languages, Range } from "monaco-editor"; |
| 3 | + |
| 4 | +function createSandcastleSnippets(range: Range): languages.CompletionItem[] { |
| 5 | + // returning a static list of proposals, not even looking at the prefix (filtering is done by the Monaco editor) |
| 6 | + |
| 7 | + // the syntax for snippets with tab stops matches the implementation of VSCode, |
| 8 | + // read more here: https://code.visualstudio.com/docs/editing/userdefinedsnippets |
| 9 | + return [ |
| 10 | + { |
| 11 | + label: "scbutton", |
| 12 | + kind: languages.CompletionItemKind.Function, |
| 13 | + documentation: "Create a Sandcastle button", |
| 14 | + insertText: `Sandcastle.addToolbarButton(\${1:"New Button"}, function () { |
| 15 | + \${0:// your code here} |
| 16 | +});`, |
| 17 | + insertTextRules: languages.CompletionItemInsertTextRule.InsertAsSnippet, |
| 18 | + range: range, |
| 19 | + }, |
| 20 | + { |
| 21 | + label: "sctoggle", |
| 22 | + kind: languages.CompletionItemKind.Function, |
| 23 | + documentation: "Create a Sandcastle toggle button", |
| 24 | + insertText: `let \${2:toggleValue} = \${3:true}; |
| 25 | +Sandcastle.addToggleButton(\${1:"Toggle"}, \${2:toggleValue}, function (checked) { |
| 26 | + \${2:toggleValue} = checked;$0 |
| 27 | +});`, |
| 28 | + insertTextRules: languages.CompletionItemInsertTextRule.InsertAsSnippet, |
| 29 | + range: range, |
| 30 | + }, |
| 31 | + { |
| 32 | + label: "scmenu", |
| 33 | + kind: languages.CompletionItemKind.Function, |
| 34 | + documentation: "Create a Sandcastle select menu", |
| 35 | + insertText: `const \${1:options} = [ |
| 36 | + { |
| 37 | + text: \${2:"Option 1"}, |
| 38 | + onselect: function () { |
| 39 | + \${0:// your code here, the first option is always run at load} |
| 40 | + }, |
| 41 | + }, |
| 42 | +]; |
| 43 | +Sandcastle.addToolbarMenu(\${1:options});`, |
| 44 | + insertTextRules: languages.CompletionItemInsertTextRule.InsertAsSnippet, |
| 45 | + range: range, |
| 46 | + }, |
| 47 | + { |
| 48 | + label: "scmenuitem", |
| 49 | + kind: languages.CompletionItemKind.Function, |
| 50 | + documentation: "Create a Sandcastle select menu item", |
| 51 | + insertText: `{ |
| 52 | + text: \${1:"New Option"}, |
| 53 | + onselect: function () { |
| 54 | + \${0:// your code here, the first option is always run at load} |
| 55 | + }, |
| 56 | +},`, |
| 57 | + insertTextRules: languages.CompletionItemInsertTextRule.InsertAsSnippet, |
| 58 | + range: range, |
| 59 | + }, |
| 60 | + ]; |
| 61 | +} |
| 62 | + |
| 63 | +// Largely based on the completion provider example: |
| 64 | +// https://microsoft.github.io/monaco-editor/playground.html?source=v0.52.2#example-extending-language-services-completion-provider-example |
| 65 | +export function setupSandcastleSnippets(monaco: Monaco) { |
| 66 | + monaco.languages.registerCompletionItemProvider("javascript", { |
| 67 | + provideCompletionItems(model, position) { |
| 68 | + const word = model.getWordUntilPosition(position); |
| 69 | + const range = new Range( |
| 70 | + position.lineNumber, |
| 71 | + word.startColumn, |
| 72 | + position.lineNumber, |
| 73 | + word.endColumn, |
| 74 | + ); |
| 75 | + return { |
| 76 | + suggestions: createSandcastleSnippets(range), |
| 77 | + }; |
| 78 | + }, |
| 79 | + }); |
| 80 | +} |
0 commit comments