-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added support for completions in monaco editor.
- Monaco input type now accepts a vector of json values to be used for completions. - Added JS-FFI(via wasm-bindgen) to create monaco completions object. This is required as we have to call another JS-FFI to register this object, and that FFI itself accepts a foreign JS type, so needed to write the constructor in JS. - Completions get registered on render & cleaned up on de-render.
- Loading branch information
Showing
8 changed files
with
132 additions
and
12 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
function toStr(v) { | ||
switch (typeof v) { | ||
case "string": | ||
return v; | ||
case "object": | ||
return JSON.stringify(v); | ||
default: | ||
return v.toString(); | ||
} | ||
} | ||
|
||
/// Returns a `CompletionItemProvider` https://microsoft.github.io/monaco-editor/docs.html#interfaces/languages.CompletionItemProvider.html | ||
export function newSuggestionsProvider(suggestions) { | ||
if (typeof suggestions !== "object" || !Array.isArray(suggestions)) { | ||
throw new Error(`${suggestions} is not an array!`); | ||
} | ||
|
||
let triggers = []; | ||
for (const s of suggestions) { | ||
switch (typeof s) { | ||
case "string": | ||
triggers.push('"'); | ||
triggers.push("'"); | ||
break; | ||
// Not sure if we should provide completions in such cases... | ||
case "object": | ||
if (s !== null) { | ||
if (Array.isArray(s)) { | ||
triggers.push("["); | ||
} else { | ||
triggers.push("{"); | ||
} | ||
} else if (s == null) { | ||
triggers.push("n"); | ||
} | ||
break; | ||
case "number": | ||
// While technically not part of JsonSchema, | ||
// if we can, then why not. | ||
case "bigint": | ||
const d = s.toString()[0]; | ||
triggers.push(d) | ||
break; | ||
// Screw these. | ||
case "undefined": | ||
case "function": | ||
case "symbol": | ||
default: | ||
console.debug(`Un-supported type in suggestions: ${typeof s}, skipping.`); | ||
} | ||
} | ||
// Just in case duplicates lead to some bug. | ||
triggers = [...new Set(triggers)]; | ||
console.debug(`Trigger characters for suggestions: ${triggers}`); | ||
|
||
return { | ||
provideCompletionItems: function (model, position) { | ||
return { | ||
suggestions: suggestions.map((s) => ({ | ||
// This is the text that shows up in the completion hover. | ||
label: toStr(s), | ||
// FIXME Coupling w/ private Enum, this can break w/ a newer version of | ||
// monaco. | ||
kind: 15, // Maps to the `Enum` varaint of `Kind`. | ||
insertText: toStr(s), | ||
detail: "Enum variant", | ||
documentation: "Json Enum", | ||
})), | ||
}; | ||
}, | ||
triggerCharacters: triggers, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters