extending-language-services-completion-provider-example.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <!DOCTYPE html>
  2. <!-- THIS IS A GENERATED FILE VIA gulp generate-test-samples -->
  3. <html>
  4. <head>
  5. <base href="..">
  6. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  7. <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  8. </head>
  9. <body>
  10. <style>
  11. /*----------------------------------------SAMPLE CSS START*/
  12. /*----------------------------------------SAMPLE CSS END*/
  13. </style>
  14. <a class="loading-opts" href="playground.generated/index.html">[&lt;&lt; BACK]</a> <br/>
  15. THIS IS A GENERATED FILE VIA gulp generate-test-samples
  16. <div id="bar" style="margin-bottom: 6px;"></div>
  17. <div style="clear:both"></div>
  18. <div id="outer-container" style="width:800px;height:450px;border: 1px solid grey">
  19. <!-- ----------------------------------------SAMPLE HTML START-->
  20. <div id="container" style="height:100%;"></div>
  21. <!-- ----------------------------------------SAMPLE HTML END-->
  22. </div>
  23. <div style="clear:both"></div>
  24. <script src="../metadata.js"></script>
  25. <script src="dev-setup.js"></script>
  26. <script>
  27. loadEditor(function() {
  28. /*----------------------------------------SAMPLE JS START*/
  29. function createDependencyProposals(range) {
  30. // returning a static list of proposals, not even looking at the prefix (filtering is done by the Monaco editor),
  31. // here you could do a server side lookup
  32. return [
  33. {
  34. label: '"lodash"',
  35. kind: monaco.languages.CompletionItemKind.Function,
  36. documentation: "The Lodash library exported as Node.js modules.",
  37. insertText: '"lodash": "*"',
  38. range: range
  39. },
  40. {
  41. label: '"express"',
  42. kind: monaco.languages.CompletionItemKind.Function,
  43. documentation: "Fast, unopinionated, minimalist web framework",
  44. insertText: '"express": "*"',
  45. range: range
  46. },
  47. {
  48. label: '"mkdirp"',
  49. kind: monaco.languages.CompletionItemKind.Function,
  50. documentation: "Recursively mkdir, like <code>mkdir -p</code>",
  51. insertText: '"mkdirp": "*"',
  52. range: range
  53. },
  54. {
  55. label: '"my-third-party-library"',
  56. kind: monaco.languages.CompletionItemKind.Function,
  57. documentation: "Describe your library here",
  58. insertText: '"${1:my-third-party-library}": "${2:1.2.3}"',
  59. insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
  60. range: range
  61. }
  62. ];
  63. }
  64. monaco.languages.registerCompletionItemProvider('json', {
  65. provideCompletionItems: function(model, position) {
  66. // find out if we are completing a property in the 'dependencies' object.
  67. var textUntilPosition = model.getValueInRange({startLineNumber: 1, startColumn: 1, endLineNumber: position.lineNumber, endColumn: position.column});
  68. var match = textUntilPosition.match(/"dependencies"\s*:\s*\{\s*("[^"]*"\s*:\s*"[^"]*"\s*,\s*)*([^"]*)?$/);
  69. if (!match) {
  70. return { suggestions: [] };
  71. }
  72. var word = model.getWordUntilPosition(position);
  73. var range = {
  74. startLineNumber: position.lineNumber,
  75. endLineNumber: position.lineNumber,
  76. startColumn: word.startColumn,
  77. endColumn: word.endColumn
  78. };
  79. return {
  80. suggestions: createDependencyProposals(range)
  81. };
  82. }
  83. });
  84. monaco.editor.create(document.getElementById("container"), {
  85. value: "{\n\t\"dependencies\": {\n\t\t\n\t}\n}\n",
  86. language: "json"
  87. });
  88. /*----------------------------------------SAMPLE CSS END*/
  89. });
  90. </script>
  91. </body>
  92. </html>