-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1015 from getappmap/pin-files_20240905
Add the ability to pin files to the Navie Context
- Loading branch information
Showing
9 changed files
with
936 additions
and
670 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,51 @@ | ||
import type { PinFileRequest } from '@appland/components'; | ||
import * as vscode from 'vscode'; | ||
import ChatSearchWebview from '../webviews/chatSearchWebview'; | ||
|
||
export default function addToContext( | ||
context: vscode.ExtensionContext, | ||
chatSearchWebview: Promise<ChatSearchWebview> | ||
) { | ||
const currentWebview = async (): Promise<vscode.Webview | undefined> => | ||
(await chatSearchWebview).currentWebview; | ||
|
||
context.subscriptions.push( | ||
vscode.commands.registerCommand( | ||
'appmap.explorer.addToContext', | ||
(_item, selected: vscode.Uri[]) => { | ||
fetchFiles(selected); | ||
} | ||
) | ||
); | ||
|
||
context.subscriptions.push( | ||
vscode.commands.registerCommand('appmap.editor.title.addToContext', (item) => { | ||
fetchFiles([item]); | ||
}) | ||
); | ||
context.subscriptions.push( | ||
vscode.commands.registerCommand('appmap.addToContext', async () => { | ||
const webview = await currentWebview(); | ||
if (!webview) return; | ||
|
||
webview.postMessage({ type: 'choose-files-to-pin' }); | ||
}) | ||
); | ||
|
||
const fetchFiles = async (uris: vscode.Uri[]) => { | ||
const webview = await currentWebview(); | ||
if (!webview) return; | ||
|
||
const requests: PinFileRequest[] = uris.map((u) => { | ||
const name = u.path.split('/').slice(-1)[0]; | ||
return { | ||
name, | ||
uri: u.toString(), | ||
}; | ||
}); | ||
webview.postMessage({ | ||
type: 'fetch-pinned-files', | ||
requests, | ||
}); | ||
}; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import assert from 'assert'; | ||
import { promises as fs } from 'fs'; | ||
import sinon from 'sinon'; | ||
|
||
import path from 'path'; | ||
import * as vscode from 'vscode'; | ||
import { initializeWorkspaceServices } from '../../../src/services/workspaceServices'; | ||
import ChatSearchWebview from '../../../src/webviews/chatSearchWebview'; | ||
import { initializeWorkspace, waitForExtension, withAuthenticatedUser, withTmpDir } from '../util'; | ||
|
||
type PinFileEvent = { | ||
type: string; | ||
location: string; | ||
content: string; | ||
}; | ||
|
||
describe('chat', () => { | ||
let sandbox: sinon.SinonSandbox; | ||
let chatSearchWebview: ChatSearchWebview; | ||
|
||
withAuthenticatedUser(); | ||
|
||
before(async () => { | ||
await initializeWorkspace(); | ||
const extension = await waitForExtension(); | ||
chatSearchWebview = await extension.chatSearchWebview; | ||
initializeWorkspaceServices(); | ||
}); | ||
|
||
beforeEach(() => (sandbox = sinon.createSandbox())); | ||
afterEach(async () => { | ||
vscode.commands.executeCommand('workbench.action.closeActiveEditor'); | ||
sandbox.restore(); | ||
}); | ||
|
||
describe('appmap.explain', () => { | ||
it('opens a Chat + Search view', async () => { | ||
await vscode.commands.executeCommand('appmap.explain'); | ||
assert(chatSearchWebview.currentWebview); | ||
}); | ||
}); | ||
|
||
describe('once the Chat is opened', () => { | ||
let chatView: vscode.Webview; | ||
|
||
beforeEach(async () => { | ||
await vscode.commands.executeCommand('appmap.explain'); | ||
const v = chatSearchWebview.currentWebview; | ||
assert(v); | ||
|
||
await new Promise<void>((resolve) => { | ||
v.onDidReceiveMessage(async (msg) => { | ||
if (msg.command === 'chat-search-ready') { | ||
resolve(); | ||
} | ||
}); | ||
}); | ||
chatView = v; | ||
}); | ||
|
||
const waitForPin = () => | ||
new Promise<PinFileEvent>((resolve) => { | ||
chatView.onDidReceiveMessage(async (msg) => { | ||
if (msg.command === 'pin') { | ||
resolve(msg.event); | ||
} | ||
}); | ||
}); | ||
|
||
const expectedContent = 'Hello World!'; | ||
const newTmpFiles = async (tmpDir: string) => { | ||
const fullPath = path.join(tmpDir, 'hello-world.txt'); | ||
await fs.writeFile(fullPath, expectedContent, 'utf-8'); | ||
return [vscode.Uri.file(fullPath)]; | ||
}; | ||
|
||
const verifyPinEvent = (event: PinFileEvent, files: vscode.Uri[]) => { | ||
assert.strictEqual(event.type, 'file'); | ||
assert.strictEqual(event.location, files[0].path); | ||
assert.strictEqual(event.content, expectedContent); | ||
}; | ||
|
||
describe('appmap.addToContext', () => { | ||
it('pins a file', async () => { | ||
return withTmpDir(async (tmpDir) => { | ||
const files = await newTmpFiles(tmpDir); | ||
sandbox.stub(chatSearchWebview, 'chooseFilesToPin').resolves(files); | ||
const p = waitForPin(); | ||
await vscode.commands.executeCommand('appmap.addToContext'); | ||
const event = await p; | ||
verifyPinEvent(event, files); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('appmap.explorer.addToContext', () => { | ||
it('pins a file', async () => { | ||
return withTmpDir(async (tmpDir) => { | ||
const files = await newTmpFiles(tmpDir); | ||
const p = waitForPin(); | ||
await vscode.commands.executeCommand('appmap.explorer.addToContext', null, files); | ||
const event = await p; | ||
verifyPinEvent(event, files); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('appmap.editor.title.addToContext', () => { | ||
it('pins a file', async () => { | ||
return withTmpDir(async (tmpDir) => { | ||
const files = await newTmpFiles(tmpDir); | ||
const p = waitForPin(); | ||
await vscode.commands.executeCommand('appmap.editor.title.addToContext', files[0]); | ||
const event = await p; | ||
verifyPinEvent(event, files); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.