Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
thenamankumar committed Jan 8, 2025
1 parent 04541ae commit c07fb6b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 35 deletions.
4 changes: 2 additions & 2 deletions vscode/src/chat/chat-view/ChatController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
70 changes: 38 additions & 32 deletions vscode/src/chat/chat-view/sourcegraphRemoteFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>({ max: 128 })
private cache = new LRUCache<string, Promise<Uint8Array>>({ max: 128 })
private disposables: vscode.Disposable[] = []

constructor() {
Expand All @@ -12,14 +12,20 @@ export class SourcegraphRemoteFileProvider implements vscode.FileSystemProvider,
)
}

async readFile(uri: vscode.Uri): Promise<Uint8Array<ArrayBufferLike>> {
const content =
this.cache.get(uri.toString()) ||
(await SourcegraphRemoteFileProvider.getFileContentsFromURL(uri))
async readFile(uri: vscode.Uri): Promise<Uint8Array> {
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 {
Expand All @@ -30,31 +36,6 @@ export class SourcegraphRemoteFileProvider implements vscode.FileSystemProvider,
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
}

// Below methods are unused

onDidChangeFile: vscode.Event<vscode.FileChangeEvent[]> = new vscode.EventEmitter<
Expand Down Expand Up @@ -94,3 +75,28 @@ export class SourcegraphRemoteFileProvider implements vscode.FileSystemProvider,
throw new Error('Method not implemented.')
}
}

async function 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
}
1 change: 0 additions & 1 deletion vscode/webviews/utils/useFeatureFlags.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c07fb6b

Please sign in to comment.