Skip to content

Instantly share code, notes, and snippets.

@sailro
Last active April 22, 2020 17:27
Show Gist options
  • Save sailro/383d5fc1d365edb67fb41e9268e8acbb to your computer and use it in GitHub Desktop.
Save sailro/383d5fc1d365edb67fb41e9268e8acbb to your computer and use it in GitHub Desktop.
hookMonacoCompletionProvider(provider) {
const provideCompletionItems = provider.prototype.provideCompletionItems;
const owner = this;
provider.prototype.provideCompletionItems = async function (model, position, context, token) {
// reuse 'this' to preserve context through call (using apply)
const result = await provideCompletionItems.apply(this, [model, position, context, token]);
if (!result || !result.suggestions)
return result;
const suggestions = result.suggestions.filter(item => !item.label.startsWith("_"));
for (const suggestion of suggestions) {
if (owner.deprecatedCandidates.includes(suggestion.label)) {
// the following is time consuming on all suggestions, that's why we precompute deprecated candidate names in the definition worker to filter calls
const uri = suggestion.uri;
const worker = await this._worker(uri);
const model = monaco.editor.getModel(uri);
const details = await worker.getCompletionEntryDetails(uri.toString(), model.getOffsetAt(position), suggestion.label)
if (owner.isDeprecatedEntry(details)) {
suggestion.tags = [monaco.languages.CompletionItemTag.Deprecated];
}
}
}
// add our own templates when invoked without context
if (context.triggerKind == monaco.languages.CompletionTriggerKind.Invoke) {
var template = {
label: '@template',
kind: monaco.languages.CompletionItemKind.Snippet,
documentation: "BlaBlaBla\nBlobloblo",
insertText: '///start-template \n alert(\'yo\') \n///end-template'
}
suggestions.push(template);
}
// preserve incomplete flag or force it when the definition is not yet analyzed
const incomplete = (result.incomplete && result.incomplete == true) || owner.deprecatedCandidates.length == 0;
return {
suggestions: suggestions,
incomplete: incomplete
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment