Skip to content

Commit 1e05d97

Browse files
authored
Merge pull request #12679 from CesiumGS/sandcastle-snippets
Sandcastle helper snippets
2 parents dbc3f72 + a33e968 commit 1e05d97

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

packages/sandcastle/src/SandcastleEditor.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as prettier from "prettier";
66
import * as babelPlugin from "prettier/plugins/babel";
77
import * as estreePlugin from "prettier/plugins/estree";
88
import * as htmlPlugin from "prettier/plugins/html";
9+
import { setupSandcastleSnippets } from "./setupSandcastleSnippets";
910

1011
const TYPES_URL = `${__PAGE_BASE_URL__}Source/Cesium.d.ts`;
1112
const SANDCASTLE_TYPES_URL = `templates/Sandcastle.d.ts`;
@@ -116,6 +117,7 @@ function SandcastleEditor({
116117
},
117118
});
118119

120+
setupSandcastleSnippets(monaco);
119121
setTypes(monaco);
120122
}
121123

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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

Comments
 (0)