浏览代码

Add language-service examples to playground

Martin Aeschlimann 9 年之前
父节点
当前提交
ac3194b4b8

+ 94 - 0
website/playground/playground.mdoc

@@ -1008,6 +1008,55 @@ function getCode() {
 =======================END
 
 
+== Completion provider example
+=======================JS
+function createDependencyProposals() {
+    // returning a static list of proposals, not even looking at the prefix (filtering is done by the Monaco editor),
+    // here you could do a server side lookup
+    return [
+        {
+            label: '"lodash"',
+            kind: monaco.languages.CompletionItemKind.Function,
+            documentation: "The Lodash library exported as Node.js modules.",
+            insertText: '"lodash": "*"'
+        },
+        {
+            label: '"express"',
+            kind: monaco.languages.CompletionItemKind.Function,
+            documentation: "Fast, unopinionated, minimalist web framework",
+            insertText: '"express": "*"'
+        },
+        {
+            label: '"mkdirp"',
+            kind: monaco.languages.CompletionItemKind.Function,
+            documentation: "Recursively mkdir, like <code>mkdir -p</code>",
+            insertText: '"mkdirp": "*"'
+        }
+    ];
+}
+
+
+monaco.languages.registerCompletionItemProvider('json', {
+    provideCompletionItems: function(model, position) {
+        // find out if we are completing a property in the 'dependencies' object.
+        var textUntilPosition = model.getValueInRange({startLineNumber: 1, startColumn: 1, endLineNumber: position.lineNumber, endColumn: position.column});
+        var match = textUntilPosition.match(/"dependencies"\s*:\s*{\s*("[^"]*"\s*:\s*"[^"]*"\s*,\s*)*("[^"]"]*)?$/);
+        if (match) {
+            return createDependencyProposals();
+        }
+        return [];
+    }
+});
+
+monaco.editor.create(document.getElementById("container"), {
+    value: "{\n\t\"dependencies\": {\n\t\t\n\t}\n}\n",
+    language: "json"
+});
+=======================HTML
+<div id="container" style="height:100%;"></div>
+
+=======================END
+
 
 == Hover provider example
 =======================JS
@@ -1120,4 +1169,49 @@ monaco.editor.create(document.getElementById("container"), {
 
 =======================END
 
+== Configure JSON defaults
+=======================JS
+// Configures two JSON schemas, with references.
+
+monaco.languages.json.jsonDefaults.setDiagnosticsOptions({
+	schemas: [{
+        uri: "http://myserver/foo-schema.json",
+        schema: {
+            type: "object",
+            properties: {
+                p1: {
+                    enum: [ "v1", "v2"]
+                },
+                p2: {
+                    $ref: "http://myserver/bar-schema.json"
+                }
+            }
+        }
+    },{
+        uri: "http://myserver/bar-schema.json",
+        schema: {
+            type: "object",
+            properties: {
+                q1: {
+                    enum: [ "x1", "x2"]
+                }
+            }
+        }
+    }]
+});
+
+
+var jsonCode = [
+	'{',
+	'    "$schema": "http://myserver/foo-schema.json"',
+	"}"
+].join('\n');
 
+monaco.editor.create(document.getElementById("container"), {
+	value: jsonCode,
+	language: "json"
+});
+=======================HTML
+<div id="container" style="height:100%;"></div>
+
+=======================END

文件差异内容过多而无法显示
+ 0 - 0
website/playground/samples/all.js


文件差异内容过多而无法显示
+ 2 - 0
website/playground/samples/extending-language-services-completion-provider-example.js


+ 3 - 0
website/playground/samples/extending-language-services-configure-json-defaults.js

@@ -0,0 +1,3 @@
+// This is a generated file. Please do not edit directly.
+var SAMPLES = this.SAMPLES || [];
+SAMPLES.push({"id":"extending-language-services-configure-json-defaults","js":"//---------------------------------------------------\n// Extending Language Services > Configure JSON defaults\n//---------------------------------------------------\n\n// Configures two JSON schemas, with references.\n\nmonaco.languages.json.jsonDefaults.setDiagnosticsOptions({\n\tschemas: [{\n        uri: \"http://myserver/foo-schema.json\",\n        schema: {\n            type: \"object\",\n            properties: {\n                p1: {\n                    enum: [ \"v1\", \"v2\"]\n                },\n                p2: {\n                    $ref: \"http://myserver/bar-schema.json\"\n                }\n            }\n        }\n    },{\n        uri: \"http://myserver/bar-schema.json\",\n        schema: {\n            type: \"object\",\n            properties: {\n                q1: {\n                    enum: [ \"x1\", \"x2\"]\n                }\n            }\n        }\n    }]\n});\n\n\nvar jsonCode = [\n\t'{',\n\t'    \"$schema\": \"http://myserver/foo-schema.json\"',\n\t\"}\"\n].join('\\n');\n\nmonaco.editor.create(document.getElementById(\"container\"), {\n\tvalue: jsonCode,\n\tlanguage: \"json\"\n});","html":"<div id=\"container\" style=\"height:100%;\"></div>\n","css":""});

部分文件因为文件数量过多而无法显示