search contexts: add to extension API (#19803)

This commit is contained in:
Rok Novosel 2021-04-19 07:43:17 +02:00 committed by GitHub
parent e30a7c2a0d
commit 487adcb958
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 42 additions and 1 deletions

View File

@ -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",

View File

@ -1236,6 +1236,21 @@ declare module 'sourcegraph' {
* An event that is fired when a workspace's version context changes.
*/
export const versionContextChanges: Subscribable<string | undefined>
/**
* 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<string | undefined>
}
/**

View File

@ -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<string>

View File

@ -56,11 +56,15 @@ export function createExtensionAPI(state: ExtensionHostState, mainAPI: Remote<Ma
get versionContext() {
return state.versionContext
},
get searchContext() {
return state.searchContext
},
onDidOpenTextDocument: state.openedTextDocuments.asObservable(),
openedTextDocuments: state.openedTextDocuments.asObservable(),
onDidChangeRoots: state.roots.pipe(mapTo(undefined)),
rootChanges: state.rootChanges.asObservable(),
versionContextChanges: state.versionContextChanges.asObservable(),
searchContextChanges: state.searchContextChanges.asObservable(),
}
const createProgressReporter = async (

View File

@ -107,6 +107,10 @@ export function createExtensionHostAPI(state: ExtensionHostState): FlatExtension
state.versionContext = context
state.versionContextChanges.next(context)
},
setSearchContext: context => {
state.searchContext = context
state.searchContextChanges.next(context)
},
// Search
transformSearchQuery: query =>

View File

@ -36,6 +36,8 @@ export function createExtensionHostState(
rootChanges: new Subject<void>(),
versionContextChanges: new Subject<string | undefined>(),
versionContext: undefined,
searchContextChanges: new Subject<string | undefined>(),
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<void>
versionContextChanges: Subject<string | undefined>
versionContext: string | undefined
searchContextChanges: Subject<string | undefined>
searchContext: string | undefined
// Search
queryTransformers: BehaviorSubject<readonly sourcegraph.QueryTransformer[]>

View File

@ -386,6 +386,10 @@ class ColdSourcegraphWebApp extends React.Component<SourcegraphWebAppProps, Sour
console.error('Error sending initial version context to extensions', error)
})
this.setWorkspaceSearchContext(this.state.selectedSearchContextSpec).catch(error => {
console.error('Error sending search context to extensions', error)
})
this.userRepositoriesUpdates.next()
}
@ -566,10 +570,19 @@ class ColdSourcegraphWebApp extends React.Component<SourcegraphWebAppProps, Sour
availableSearchContextSpecOrDefault => {
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<void> {
const extensionHostAPI = await this.extensionsController.extHostAPI
await extensionHostAPI.setSearchContext(spec)
}
}
export const SourcegraphWebApp = hot(ColdSourcegraphWebApp)