sourcegraph/client/vscode/src/code-intel/SourcegraphReferenceProvider.ts
TJ Kandala 17918fd041
vsce: implement search UI (#30084)
Co-authored-by: Beatrix <68532117+abeatrix@users.noreply.github.com>
Co-authored-by: Alex Isken <alex.isken@sourcegraph.com>
Co-authored-by: Sara Lee <87138876+jjinnii@users.noreply.github.com>
Co-authored-by: Philipp Spiess <hello@philippspiess.com>
Co-authored-by: Giselle Northy <northyg@oregonstate.edu>
Co-authored-by: Beatrix <beatrix@sourcegraph.com>
2022-04-13 12:41:07 -04:00

79 lines
2.8 KiB
TypeScript

import * as Comlink from 'comlink'
import { EMPTY, of } from 'rxjs'
import { debounceTime, first, switchMap } from 'rxjs/operators'
import * as vscode from 'vscode'
import { finallyReleaseProxy, wrapRemoteObservable } from '@sourcegraph/shared/src/api/client/api/common'
import { makeRepoURI, parseRepoURI } from '@sourcegraph/shared/src/util/url'
import { SearchSidebarAPI } from '../contract'
import { SourcegraphFileSystemProvider } from '../file-system/SourcegraphFileSystemProvider'
export class SourcegraphReferenceProvider implements vscode.ReferenceProvider {
constructor(
private readonly fs: SourcegraphFileSystemProvider,
private readonly sourcegraphExtensionHostAPI: Comlink.Remote<SearchSidebarAPI>
) {}
public async provideReferences(
document: vscode.TextDocument,
position: vscode.Position,
referenceContext: vscode.ReferenceContext,
token: vscode.CancellationToken
): Promise<vscode.Location[] | undefined> {
const uri = this.fs.sourcegraphUri(document.uri)
const extensionHostUri = makeRepoURI({
repoName: uri.repositoryName,
revision: uri.revision,
filePath: uri.path,
})
const definitions = wrapRemoteObservable(
this.sourcegraphExtensionHostAPI.getReferences(
{
textDocument: {
uri: extensionHostUri,
},
position: {
line: position.line,
character: position.character,
},
},
referenceContext
)
)
.pipe(
finallyReleaseProxy(),
switchMap(({ isLoading, result }) => {
if (isLoading) {
return EMPTY
}
const locations = result.map(location => {
// Create a sourcegraph URI from this git URI (so we need both fromGitURI and toGitURI.)`
const uri = parseRepoURI(location.uri)
return this.fs.toVscodeLocation({
resource: {
path: uri.filePath ?? '',
repositoryName: uri.repoName,
revision: uri.commitID ?? uri.revision ?? '',
},
range: location.range,
})
})
return of(locations)
}),
debounceTime(1000),
first()
)
.toPromise()
token.onCancellationRequested(() => {
// Debt: manually create promise so we can cancel request.
})
return definitions
}
}