-
Notifications
You must be signed in to change notification settings - Fork 326
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f515283
commit b4eb6ed
Showing
7 changed files
with
125 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { graphqlClient, isError } from '@sourcegraph/cody-shared' | ||
import { LRUCache } from 'lru-cache' | ||
import * as vscode from 'vscode' | ||
|
||
export class SourcegraphRemoteFileProvider | ||
implements vscode.TextDocumentContentProvider, vscode.Disposable | ||
{ | ||
private cache = new LRUCache<string, string>({ max: 128 }) | ||
private disposables: vscode.Disposable[] = [] | ||
|
||
constructor() { | ||
this.disposables.push(vscode.workspace.registerTextDocumentContentProvider('sourcegraph', this)) | ||
} | ||
|
||
async provideTextDocumentContent(uri: vscode.Uri): Promise<string> { | ||
const content = | ||
this.cache.get(uri.toString()) || | ||
(await SourcegraphRemoteFileProvider.getFileContentsFromURL(uri)) | ||
|
||
this.cache.set(uri.toString(), content) | ||
|
||
return content | ||
} | ||
|
||
public dispose(): void { | ||
this.cache.clear() | ||
for (const disposable of this.disposables) { | ||
disposable.dispose() | ||
} | ||
this.disposables = [] | ||
} | ||
|
||
private static async getFileContentsFromURL(URL: vscode.Uri): Promise<string> { | ||
const path = URL.path | ||
const [repoRev = '', filePath] = path.split('/-/blob/') | ||
let [repoName, rev = 'HEAD'] = repoRev.split('@') | ||
repoName = repoName.replace(/^\/+/, '') | ||
|
||
if (!repoName || !filePath) { | ||
throw new Error('Invalid URI') | ||
} | ||
|
||
const dataOrError = await graphqlClient.getFileContents(repoName, filePath, rev) | ||
|
||
if (isError(dataOrError)) { | ||
throw new Error(dataOrError.message) | ||
} | ||
|
||
const content = dataOrError.repository?.commit?.file?.content | ||
|
||
if (!content) { | ||
throw new Error('File not found') | ||
} | ||
|
||
return content | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters