From c07fb6bcb73783b2d84a597509df141256d61d8f Mon Sep 17 00:00:00 2001 From: Naman Kumar Date: Wed, 8 Jan 2025 23:12:25 +0530 Subject: [PATCH] address comments --- vscode/src/chat/chat-view/ChatController.ts | 4 +- .../chat/chat-view/sourcegraphRemoteFile.ts | 70 ++++++++++--------- vscode/webviews/utils/useFeatureFlags.tsx | 1 - 3 files changed, 40 insertions(+), 35 deletions(-) diff --git a/vscode/src/chat/chat-view/ChatController.ts b/vscode/src/chat/chat-view/ChatController.ts index 9d9a29d16434..1b3744882419 100644 --- a/vscode/src/chat/chat-view/ChatController.ts +++ b/vscode/src/chat/chat-view/ChatController.ts @@ -923,10 +923,10 @@ export class ChatController implements vscode.Disposable, vscode.WebviewViewProv return } - private async openRemoteFile(uri: vscode.Uri) { + private openRemoteFile(uri: vscode.Uri) { const json = uri.toJSON() json.scheme = 'codysourcegraph' - const sourcegraphSchemaURI = vscode.Uri.from(json).with({ query: 'readonly' }) + const sourcegraphSchemaURI = vscode.Uri.from(json) vscode.workspace .openTextDocument(sourcegraphSchemaURI) diff --git a/vscode/src/chat/chat-view/sourcegraphRemoteFile.ts b/vscode/src/chat/chat-view/sourcegraphRemoteFile.ts index ab370535298e..5cfb73b41af1 100644 --- a/vscode/src/chat/chat-view/sourcegraphRemoteFile.ts +++ b/vscode/src/chat/chat-view/sourcegraphRemoteFile.ts @@ -3,7 +3,7 @@ import { LRUCache } from 'lru-cache' import * as vscode from 'vscode' export class SourcegraphRemoteFileProvider implements vscode.FileSystemProvider, vscode.Disposable { - private cache = new LRUCache({ max: 128 }) + private cache = new LRUCache>({ max: 128 }) private disposables: vscode.Disposable[] = [] constructor() { @@ -12,14 +12,20 @@ export class SourcegraphRemoteFileProvider implements vscode.FileSystemProvider, ) } - async readFile(uri: vscode.Uri): Promise> { - const content = - this.cache.get(uri.toString()) || - (await SourcegraphRemoteFileProvider.getFileContentsFromURL(uri)) + async readFile(uri: vscode.Uri): Promise { + const cachedResult = this.cache.get(uri.toString()) - this.cache.set(uri.toString(), content) + if (cachedResult) { + return cachedResult + } + + const contentPromise = getFileContentsFromURL(uri).then(content => + new TextEncoder().encode(content) + ) + + this.cache.set(uri.toString(), contentPromise) - return new TextEncoder().encode(content) + return contentPromise } public dispose(): void { @@ -30,31 +36,6 @@ export class SourcegraphRemoteFileProvider implements vscode.FileSystemProvider, this.disposables = [] } - private static async getFileContentsFromURL(URL: vscode.Uri): Promise { - 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 - } - // Below methods are unused onDidChangeFile: vscode.Event = new vscode.EventEmitter< @@ -94,3 +75,28 @@ export class SourcegraphRemoteFileProvider implements vscode.FileSystemProvider, throw new Error('Method not implemented.') } } + +async function getFileContentsFromURL(URL: vscode.Uri): Promise { + 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 +} diff --git a/vscode/webviews/utils/useFeatureFlags.tsx b/vscode/webviews/utils/useFeatureFlags.tsx index 669631a20016..7bee1ec9b4f5 100644 --- a/vscode/webviews/utils/useFeatureFlags.tsx +++ b/vscode/webviews/utils/useFeatureFlags.tsx @@ -3,7 +3,6 @@ import { useExtensionAPI, useObservable } from '@sourcegraph/prompt-editor' import { useMemo } from 'react' /** - * * React hook for getting a feature flag's value. * * @returns `true` or `false` if the flag is exposed by the server endpoint, has been fetched, and