-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add search provider for scheme vscode-test-web (#114)
* Add search provider for scheme vscode-test-web * test: add to sample and add test for search * fix: use extension context as base URI When using the URIs for requests, for instance for reading contents of the file, /static/mount had to be removed * polish * fix indentation --------- Co-authored-by: Martin Aeschlimann <martinae@microsoft.com>
- Loading branch information
Showing
8 changed files
with
1,953 additions
and
1,740 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,70 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
declare module 'vscode' { | ||
// https://github.com/microsoft/vscode/issues/73524 | ||
|
||
/** | ||
* The parameters of a query for file search. | ||
*/ | ||
export interface FileSearchQuery { | ||
/** | ||
* The search pattern to match against file paths. | ||
*/ | ||
pattern: string; | ||
} | ||
|
||
/** | ||
* Options that apply to file search. | ||
*/ | ||
export interface FileSearchOptions extends SearchOptions { | ||
/** | ||
* The maximum number of results to be returned. | ||
*/ | ||
maxResults?: number; | ||
|
||
/** | ||
* A CancellationToken that represents the session for this search query. If the provider chooses to, this object can be used as the key for a cache, | ||
* and searches with the same session object can search the same cache. When the token is cancelled, the session is complete and the cache can be cleared. | ||
*/ | ||
session?: CancellationToken; | ||
} | ||
|
||
/** | ||
* A FileSearchProvider provides search results for files in the given folder that match a query string. It can be invoked by quickopen or other extensions. | ||
* | ||
* A FileSearchProvider is the more powerful of two ways to implement file search in the editor. Use a FileSearchProvider if you wish to search within a folder for | ||
* all files that match the user's query. | ||
* | ||
* The FileSearchProvider will be invoked on every keypress in quickopen. When `workspace.findFiles` is called, it will be invoked with an empty query string, | ||
* and in that case, every file in the folder should be returned. | ||
*/ | ||
export interface FileSearchProvider { | ||
/** | ||
* Provide the set of files that match a certain file path pattern. | ||
* @param query The parameters for this query. | ||
* @param options A set of options to consider while searching files. | ||
* @param token A cancellation token. | ||
*/ | ||
provideFileSearchResults( | ||
query: FileSearchQuery, | ||
options: FileSearchOptions, | ||
token: CancellationToken | ||
): ProviderResult<Uri[]>; | ||
} | ||
|
||
export namespace workspace { | ||
/** | ||
* Register a search provider. | ||
* | ||
* Only one provider can be registered per scheme. | ||
* | ||
* @param scheme The provider will be invoked for workspace folders that have this file scheme. | ||
* @param provider The provider. | ||
* @return A {@link Disposable} that unregisters this provider when being disposed. | ||
*/ | ||
export function registerFileSearchProvider(scheme: string, provider: FileSearchProvider): Disposable; | ||
} | ||
} |
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,52 @@ | ||
import * as assert from 'assert'; | ||
import * as vscode from 'vscode'; | ||
|
||
suite('Workspace search', () => { | ||
// tests findFiles operation against the current workspace folder | ||
// when running with `@vscode/test-web`, this will be a virtual file system, powered | ||
// by the vscoe-web-test file system provider | ||
|
||
const workspaceFolder = vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders[0]; | ||
assert.ok(workspaceFolder, 'Expecting an open folder'); | ||
|
||
const workspaceFolderUri = workspaceFolder.uri; | ||
|
||
function getUri(path: string): vscode.Uri { | ||
return vscode.Uri.joinPath(workspaceFolderUri, path); | ||
} | ||
|
||
async function assertEntries(path: string, expectedFiles: string[], expectedFolders: string[]) { | ||
const entrySorter = (e1: [string, vscode.FileType], e2: [string, vscode.FileType]) => { | ||
const d = e1[1] - e2[1]; | ||
if (d === 0) { | ||
return e1[0].localeCompare(e2[0]); | ||
} | ||
return d; | ||
}; | ||
|
||
let entries = await vscode.workspace.fs.readDirectory(getUri(path)); | ||
entries = entries.sort(entrySorter); | ||
|
||
let expected = expectedFolders | ||
.map<[string, vscode.FileType]>((name) => [name, vscode.FileType.Directory]) | ||
.concat(expectedFiles.map((name) => [name, vscode.FileType.File])) | ||
.sort(entrySorter); | ||
|
||
assert.deepStrictEqual(entries, expected); | ||
} | ||
|
||
async function assertFindsFiles(pattern: string, expectedFiles: string[]) { | ||
let entries = await vscode.workspace.findFiles(pattern); | ||
let foundFiles = entries.map((uri) => uri.path.substring(uri.path.lastIndexOf('/') + 1)); | ||
|
||
assert.deepStrictEqual(foundFiles, expectedFiles); | ||
} | ||
|
||
test('Find files', async () => { | ||
await assertEntries('/folder', ['x.txt'], ['.bar']); | ||
await assertEntries('/folder/', ['x.txt'], ['.bar']); | ||
await assertEntries('/', ['hello.txt', 'world.txt'], ['folder', 'folder_with_utf_8_🧿']); | ||
|
||
await assertFindsFiles('**/*.txt', ['x.txt', 'hello.txt', 'world.txt']); | ||
}); | ||
}); |