diff --git a/client/extension-api/package.json b/client/extension-api/package.json index c4c938b8a08..79ab9498a1c 100644 --- a/client/extension-api/package.json +++ b/client/extension-api/package.json @@ -1,6 +1,6 @@ { "name": "sourcegraph", - "version": "25.1.0", + "version": "25.2.0", "license": "Apache-2.0", "description": "Sourcegraph extension API: build extensions that enhance reading and reviewing code in your existing tools", "author": "Sourcegraph", diff --git a/client/extension-api/src/sourcegraph.d.ts b/client/extension-api/src/sourcegraph.d.ts index 2ead7840cc9..d1c7a79926c 100644 --- a/client/extension-api/src/sourcegraph.d.ts +++ b/client/extension-api/src/sourcegraph.d.ts @@ -1236,6 +1236,21 @@ declare module 'sourcegraph' { * An event that is fired when a workspace's version context changes. */ export const versionContextChanges: Subscribable + + /** + * The current search context of the workspace, if any. + * + * A search context is a set of repositories and revisions on a Sourcegraph instance. + * When set, extensions use it to scope search queries, code intelligence actions, etc. + * + * See more information at https://docs.sourcegraph.com/code_search/explanations/features#search-contexts-experimental. + */ + export const searchContext: string | undefined + + /** + * An event that is fired when a workspace's search context changes. + */ + export const searchContextChanges: Subscribable } /** diff --git a/client/shared/src/api/contract.ts b/client/shared/src/api/contract.ts index d41437dd147..de000f305fd 100644 --- a/client/shared/src/api/contract.ts +++ b/client/shared/src/api/contract.ts @@ -45,6 +45,7 @@ export interface FlatExtensionHostAPI { removeWorkspaceRoot: (uri: string) => void setVersionContext: (versionContext: string | undefined) => void + setSearchContext: (searchContext: string | undefined) => void // Search transformSearchQuery: (query: string) => ProxySubscribable diff --git a/client/shared/src/api/extension/extensionApi.ts b/client/shared/src/api/extension/extensionApi.ts index a0cfc24a9c0..ad06fc2ae46 100644 --- a/client/shared/src/api/extension/extensionApi.ts +++ b/client/shared/src/api/extension/extensionApi.ts @@ -56,11 +56,15 @@ export function createExtensionAPI(state: ExtensionHostState, mainAPI: Remote { + state.searchContext = context + state.searchContextChanges.next(context) + }, // Search transformSearchQuery: query => diff --git a/client/shared/src/api/extension/extensionHostState.ts b/client/shared/src/api/extension/extensionHostState.ts index 1b66b4894cf..7704098fe59 100644 --- a/client/shared/src/api/extension/extensionHostState.ts +++ b/client/shared/src/api/extension/extensionHostState.ts @@ -36,6 +36,8 @@ export function createExtensionHostState( rootChanges: new Subject(), versionContextChanges: new Subject(), versionContext: undefined, + searchContextChanges: new Subject(), + searchContext: undefined, // Most extensions never call `configuration.get()` synchronously in `activate()` to get // the initial settings data, and instead only subscribe to configuration changes. @@ -110,6 +112,8 @@ export interface ExtensionHostState { rootChanges: Subject versionContextChanges: Subject versionContext: string | undefined + searchContextChanges: Subject + searchContext: string | undefined // Search queryTransformers: BehaviorSubject diff --git a/client/web/src/SourcegraphWebApp.tsx b/client/web/src/SourcegraphWebApp.tsx index 48ecb5857ca..857b90a6f72 100644 --- a/client/web/src/SourcegraphWebApp.tsx +++ b/client/web/src/SourcegraphWebApp.tsx @@ -386,6 +386,10 @@ class ColdSourcegraphWebApp extends React.Component { + console.error('Error sending search context to extensions', error) + }) + this.userRepositoriesUpdates.next() } @@ -566,10 +570,19 @@ class ColdSourcegraphWebApp extends React.Component { this.setState({ selectedSearchContextSpec: availableSearchContextSpecOrDefault }) localStorage.setItem(LAST_SEARCH_CONTEXT_KEY, availableSearchContextSpecOrDefault) + + this.setWorkspaceSearchContext(availableSearchContextSpecOrDefault).catch(error => { + console.error('Error sending search context to extensions', error) + }) } ) ) } + + private async setWorkspaceSearchContext(spec: string | undefined): Promise { + const extensionHostAPI = await this.extensionsController.extHostAPI + await extensionHostAPI.setSearchContext(spec) + } } export const SourcegraphWebApp = hot(ColdSourcegraphWebApp)