Interface ExtensionContext

An extension context is a collection of utilities private to an extension.

An instance of an ExtensionContext is provided as the first parameter to the activate-call of an extension.

interface ExtensionContext {
    environmentVariableCollection: GlobalEnvironmentVariableCollection;
    extension: Extension<any>;
    extensionMode: ExtensionMode;
    extensionPath: string;
    extensionUri: Uri;
    globalState: Memento & {
        setKeysForSync(keys): void;
    };
    globalStoragePath: string;
    globalStorageUri: Uri;
    languageModelAccessInformation: LanguageModelAccessInformation;
    logPath: string;
    logUri: Uri;
    secrets: SecretStorage;
    storagePath: undefined | string;
    storageUri: undefined | Uri;
    subscriptions: {
        dispose(): any;
    }[];
    workspaceState: Memento;
    asAbsolutePath(relativePath): string;
}

Properties

environmentVariableCollection: GlobalEnvironmentVariableCollection

Gets the extension's environment variable collection for this workspace, enabling changes to be applied to terminal environment variables.

extension: Extension<any>

The current Extension instance.

extensionMode: ExtensionMode

The mode the extension is running in. This is specific to the current extension. One extension may be in ExtensionMode.Development while other extensions in the host run in ExtensionMode.Release.

extensionPath: string

The absolute file path of the directory containing the extension. Shorthand notation for ExtensionContext.extensionUri.fsPath (independent of the uri scheme).

extensionUri: Uri

The uri of the directory containing the extension.

globalState: Memento & {
    setKeysForSync(keys): void;
}

A memento object that stores state independent of the current opened workspace.workspaceFolders workspace.

Type declaration

  • setKeysForSync:function
    • Set the keys whose values should be synchronized across devices when synchronizing user-data like configuration, extensions, and mementos.

      Note that this function defines the whole set of keys whose values are synchronized:

      • calling it with an empty array stops synchronization for this memento
      • calling it with a non-empty array replaces all keys whose values are synchronized

      For any given set of keys this function needs to be called only once but there is no harm in repeatedly calling it.

      Parameters

      • keys: readonly string[]

        The set of keys whose values are synced.

      Returns void

globalStoragePath: string

An absolute file path in which the extension can store global state. The directory might not exist on disk and creation is up to the extension. However, the parent directory is guaranteed to be existent.

Use globalState to store key value data.

Deprecated

Use globalStorageUri instead.

globalStorageUri: Uri

The uri of a directory in which the extension can store global state. The directory might not exist on disk and creation is up to the extension. However, the parent directory is guaranteed to be existent.

Use globalState to store key value data.

See

workspace.fs for how to read and write files and folders from an uri.

languageModelAccessInformation: LanguageModelAccessInformation

An object that keeps information about how this extension can use language models.

logPath: string

An absolute file path of a directory in which the extension can create log files. The directory might not exist on disk and creation is up to the extension. However, the parent directory is guaranteed to be existent.

Deprecated

Use logUri instead.

logUri: Uri

The uri of a directory in which the extension can create log files. The directory might not exist on disk and creation is up to the extension. However, the parent directory is guaranteed to be existent.

See

workspace.fs for how to read and write files and folders from an uri.

secrets: SecretStorage

A storage utility for secrets. Secrets are persisted across reloads and are independent of the current opened workspace.workspaceFolders workspace.

storagePath: undefined | string

An absolute file path of a workspace specific directory in which the extension can store private state. The directory might not exist on disk and creation is up to the extension. However, the parent directory is guaranteed to be existent.

Use workspaceState or globalState to store key value data.

Deprecated

Use storageUri instead.

storageUri: undefined | Uri

The uri of a workspace specific directory in which the extension can store private state. The directory might not exist and creation is up to the extension. However, the parent directory is guaranteed to be existent. The value is undefined when no workspace nor folder has been opened.

Use workspaceState or globalState to store key value data.

See

workspace.fs for how to read and write files and folders from an uri.

subscriptions: {
    dispose(): any;
}[]

An array to which disposables can be added. When this extension is deactivated the disposables will be disposed.

Note that asynchronous dispose-functions aren't awaited.

Type declaration

workspaceState: Memento

A memento object that stores state in the context of the currently opened workspace.workspaceFolders workspace.

Methods

  • Get the absolute path of a resource contained in the extension.

    Note that an absolute uri can be constructed via Uri.joinPath and extensionUri, e.g. vscode.Uri.joinPath(context.extensionUri, relativePath);

    Parameters

    • relativePath: string

      A relative path to a resource contained in the extension.

    Returns string

    The absolute path of the resource.