Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can an extension require other extensions and call functions from them?

I'm developing an extension for multiple languages. I'd like to have just a single core extension, and then make the code for each language a separate extension. Is it possible to do this? The core extension would essentially need to be able to recognize that the others are installed and call some of their code.

like image 446
maxedison Avatar asked Mar 06 '23 11:03

maxedison


1 Answers

Yes, this should be possible via the extensions API - extensions can return an API from their activate() method:

export function activate(context: vscode.ExtensionContext) {
    let api = {
        sum(a, b) {
            return a + b;
        },
        mul(a, b) {
            return a * b;
        }
    };
    // 'export' public api-surface
    return api;
}

And another extension can then retrieve and use that API via a getExtension() call:

let mathExt = extensions.getExtension('genius.math');
let importedApi = mathExt.exports;

console.log(importedApi.mul(42, 1));

A list of all extensions known to VSCode is also available via extensions.all.

like image 58
Gama11 Avatar answered Apr 29 '23 00:04

Gama11