|
@@ -21,6 +21,7 @@ class MonacoCreator {
|
|
this.blockEditorChange = false;
|
|
this.blockEditorChange = false;
|
|
this.definitionWorker = null;
|
|
this.definitionWorker = null;
|
|
this.deprecatedCandidates = [];
|
|
this.deprecatedCandidates = [];
|
|
|
|
+ this.templates = [];
|
|
|
|
|
|
this.compilerTriggerTimeoutID = null;
|
|
this.compilerTriggerTimeoutID = null;
|
|
|
|
|
|
@@ -113,6 +114,12 @@ class MonacoCreator {
|
|
|
|
|
|
this.setupDefinitionWorker(libContent);
|
|
this.setupDefinitionWorker(libContent);
|
|
|
|
|
|
|
|
+ // Load code templates
|
|
|
|
+ response = await fetch("templates.json");
|
|
|
|
+ if (response.ok) {
|
|
|
|
+ this.templates = await response.json();
|
|
|
|
+ }
|
|
|
|
+
|
|
// WARNING !!! We need the 'dev' version of Monaco, as we use monkey-patching to hook into the suggestion adapter
|
|
// WARNING !!! We need the 'dev' version of Monaco, as we use monkey-patching to hook into the suggestion adapter
|
|
require.config({
|
|
require.config({
|
|
paths: {
|
|
paths: {
|
|
@@ -127,6 +134,12 @@ class MonacoCreator {
|
|
// This is used for a vscode-like color preview for ColorX types
|
|
// This is used for a vscode-like color preview for ColorX types
|
|
this.setupMonacoColorProvider();
|
|
this.setupMonacoColorProvider();
|
|
|
|
|
|
|
|
+ // enhance templates with extra properties
|
|
|
|
+ for (const template of this.templates) {
|
|
|
|
+ template.kind = monaco.languages.CompletionItemKind.Snippet,
|
|
|
|
+ template.sortText = "!" + template.label; // make sure templates are on top of the completion window
|
|
|
|
+ }
|
|
|
|
+
|
|
// As explained above, we need the 'dev' version of Monaco to access this adapter!
|
|
// As explained above, we need the 'dev' version of Monaco to access this adapter!
|
|
require(['vs/language/typescript/languageFeatures'], module => {
|
|
require(['vs/language/typescript/languageFeatures'], module => {
|
|
this.hookMonacoCompletionProvider(module.SuggestAdapter);
|
|
this.hookMonacoCompletionProvider(module.SuggestAdapter);
|
|
@@ -289,6 +302,17 @@ class MonacoCreator {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ // add our own templates when invoked without context
|
|
|
|
+ if (context.triggerKind == monaco.languages.CompletionTriggerKind.Invoke) {
|
|
|
|
+ for (const template of owner.templates) {
|
|
|
|
+ if (template.language && owner.monacoMode != template.language)
|
|
|
|
+ continue;
|
|
|
|
+
|
|
|
|
+ template.range = undefined;
|
|
|
|
+ suggestions.push(template);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
// preserve incomplete flag or force it when the definition is not yet analyzed
|
|
// preserve incomplete flag or force it when the definition is not yet analyzed
|
|
const incomplete = (result.incomplete && result.incomplete == true) || owner.deprecatedCandidates.length == 0;
|
|
const incomplete = (result.incomplete && result.incomplete == true) || owner.deprecatedCandidates.length == 0;
|
|
|
|
|