-
Notifications
You must be signed in to change notification settings - Fork 227
Add new command: 'Go to File in Selected Database' #4390
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hvitved
wants to merge
5
commits into
github:main
Choose a base branch
from
hvitved:go-to-file-selected-db
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
061e792
Add new command: 'Go to File in Selected Database'
hvitved a2d827d
Add unit tests for `handleGoToFile`
hvitved dad7f4a
Address review comments
hvitved 2e384c3
Add change note
hvitved 71e36d1
Cache source archive list inside `DataBaseItemImpl`
hvitved File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
63 changes: 63 additions & 0 deletions
63
extensions/ql-vscode/src/databases/source-archive-file-search.ts
This file contains hidden or 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,63 @@ | ||
| import type { QuickPickItem, Uri } from "vscode"; | ||
| import { window, workspace } from "vscode"; | ||
| import type { DatabaseItem } from "./local-databases"; | ||
|
|
||
| interface SourceArchiveFileQuickPickItem extends QuickPickItem { | ||
| uri: Uri; | ||
| } | ||
|
|
||
| /** | ||
| * Shows a Quick Pick to search for and open a file from the source archive | ||
| * of the given database. | ||
| */ | ||
| export async function searchSourceArchiveFiles( | ||
| databaseItem: DatabaseItem, | ||
| ): Promise<void> { | ||
| const filesPromise = databaseItem.getSourceArchiveFiles(); | ||
|
|
||
| const quickPick = window.createQuickPick<SourceArchiveFileQuickPickItem>(); | ||
| quickPick.placeholder = "Go to File in Selected Database..."; | ||
| quickPick.matchOnDescription = true; | ||
| quickPick.busy = true; | ||
| quickPick.show(); | ||
|
|
||
| try { | ||
| const files = await filesPromise; | ||
| quickPick.items = files.map((f) => ({ | ||
| label: f.name, | ||
| description: f.path, | ||
| uri: f.uri, | ||
| })); | ||
| quickPick.busy = false; | ||
| } catch (e) { | ||
| quickPick.dispose(); | ||
| void window.showErrorMessage( | ||
| `Failed to read source archive: ${e instanceof Error ? e.message : String(e)}`, | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| return new Promise<void>((resolve) => { | ||
| quickPick.onDidAccept(async () => { | ||
| const selected = quickPick.selectedItems[0]; | ||
| quickPick.dispose(); | ||
| try { | ||
| if (selected) { | ||
| const doc = await workspace.openTextDocument(selected.uri); | ||
| await window.showTextDocument(doc); | ||
| } | ||
| } catch (e) { | ||
| void window.showErrorMessage( | ||
| `Failed to open source archive file: ${e instanceof Error ? e.message : String(e)}`, | ||
| ); | ||
| } finally { | ||
| resolve(); | ||
| } | ||
| }); | ||
|
|
||
| quickPick.onDidHide(() => { | ||
| quickPick.dispose(); | ||
| resolve(); | ||
| }); | ||
| }); | ||
| } |
106 changes: 106 additions & 0 deletions
106
...ons/ql-vscode/test/vscode-tests/no-workspace/databases/source-archive-file-search.test.ts
This file contains hidden or 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,106 @@ | ||
| import { Uri } from "vscode"; | ||
| import { DatabaseUI } from "../../../../src/databases/local-databases-ui"; | ||
| import { testDisposeHandler } from "../../test-dispose-handler"; | ||
| import { createMockApp } from "../../../__mocks__/appMock"; | ||
| import { mockedObject } from "../../utils/mocking.helpers"; | ||
| import type { DatabaseFetcher } from "../../../../src/databases/database-fetcher"; | ||
| import type { DatabaseItem } from "../../../../src/databases/local-databases"; | ||
| import { searchSourceArchiveFiles } from "../../../../src/databases/source-archive-file-search"; | ||
|
|
||
| jest.mock("../../../../src/databases/source-archive-file-search"); | ||
| const mockedSearchSourceArchiveFiles = jest.mocked(searchSourceArchiveFiles); | ||
|
|
||
| describe("handleGoToFile", () => { | ||
| const app = createMockApp({}); | ||
| const storageDir = "/tmp/test-storage"; | ||
|
|
||
| afterEach(() => { | ||
| jest.restoreAllMocks(); | ||
| }); | ||
|
|
||
| describe("when there is no current database", () => { | ||
| const databaseUI = new DatabaseUI( | ||
| app, | ||
| { | ||
| databaseItems: [], | ||
| onDidChangeDatabaseItem: () => { | ||
| /**/ | ||
| }, | ||
| onDidChangeCurrentDatabaseItem: () => { | ||
| /**/ | ||
| }, | ||
| setCurrentDatabaseItem: () => {}, | ||
| currentDatabaseItem: undefined, | ||
| } as any, | ||
| mockedObject<DatabaseFetcher>({}), | ||
| { | ||
| onLanguageContextChanged: () => { | ||
| /**/ | ||
| }, | ||
| } as any, | ||
| {} as any, | ||
| storageDir, | ||
| storageDir, | ||
| ); | ||
|
|
||
| afterAll(() => { | ||
| databaseUI.dispose(testDisposeHandler); | ||
| }); | ||
|
|
||
| it("should show an error message", async () => { | ||
| const commands = databaseUI.getCommands(); | ||
| await commands["codeQL.goToFile"](); | ||
|
|
||
| expect(mockedSearchSourceArchiveFiles).not.toHaveBeenCalled(); | ||
| expect(app.logger.showErrorMessage).toHaveBeenCalledWith( | ||
| expect.stringContaining("No CodeQL database selected"), | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe("when there is a current database", () => { | ||
| const mockDbItem = mockedObject<DatabaseItem>({ | ||
| databaseUri: Uri.file("/test/db"), | ||
| name: "test-db", | ||
| language: "javascript", | ||
| sourceArchive: Uri.file("/test/db/src.zip"), | ||
| }); | ||
|
|
||
| const databaseUI = new DatabaseUI( | ||
| app, | ||
| { | ||
| databaseItems: [mockDbItem], | ||
| onDidChangeDatabaseItem: () => { | ||
| /**/ | ||
| }, | ||
| onDidChangeCurrentDatabaseItem: () => { | ||
| /**/ | ||
| }, | ||
| setCurrentDatabaseItem: () => {}, | ||
| currentDatabaseItem: mockDbItem, | ||
| } as any, | ||
| mockedObject<DatabaseFetcher>({}), | ||
| { | ||
| onLanguageContextChanged: () => { | ||
| /**/ | ||
| }, | ||
| } as any, | ||
| {} as any, | ||
| storageDir, | ||
| storageDir, | ||
| ); | ||
|
|
||
| afterAll(() => { | ||
| databaseUI.dispose(testDisposeHandler); | ||
| }); | ||
|
|
||
| it("should call searchSourceArchiveFiles with the current database", async () => { | ||
| mockedSearchSourceArchiveFiles.mockResolvedValue(undefined); | ||
|
|
||
| const commands = databaseUI.getCommands(); | ||
| await commands["codeQL.goToFile"](); | ||
|
|
||
| expect(mockedSearchSourceArchiveFiles).toHaveBeenCalledWith(mockDbItem); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.