-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial SearchProvider (very basic FileSearchProvider)
- Loading branch information
1 parent
b63d248
commit 119f2bd
Showing
2 changed files
with
59 additions
and
0 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,57 @@ | ||
|
||
import { posix as path } from 'path'; | ||
import { createInterface } from 'readline'; | ||
import { ClientChannel } from 'ssh2'; | ||
import * as vscode from 'vscode'; | ||
import { Manager } from './manager'; | ||
import { toPromise } from './toPromise'; | ||
|
||
export class SearchProvider implements vscode.FileSearchProvider { | ||
protected cache: [vscode.CancellationToken, Promise<string[]>][] = []; | ||
constructor(protected manager: Manager) { } | ||
public async provideFileSearchResults(query: vscode.FileSearchQuery, options: vscode.FileSearchOptions, token: vscode.CancellationToken): Promise<vscode.Uri[]> { | ||
const { folder, session = token } = options; | ||
let cached = this.cache.find(([t]) => session === t); | ||
if (!cached) { | ||
cached = [session, this.buildTree(options, session)] as SearchProvider['cache'][0]; | ||
this.cache.push(cached); | ||
session.onCancellationRequested(() => { | ||
this.cache.splice(this.cache.indexOf(cached!)); | ||
}); | ||
} | ||
const paths = await cached[1]; | ||
console.log('Found', paths.length); | ||
if (token.isCancellationRequested) return []; | ||
const pattern = query.pattern.toLowerCase(); | ||
return paths.map<vscode.Uri | null>((relative) => { | ||
if (!relative.toLowerCase().includes(pattern)) return null; | ||
return folder.with({ path: path.join(folder.path, relative) }); | ||
}).filter(s => !!s) as vscode.Uri[]; | ||
} | ||
protected async buildTree(options: vscode.FileSearchOptions, token: vscode.CancellationToken): Promise<string[]> { | ||
const { folder } = options; | ||
const fs = await this.manager.getFs(folder); | ||
if (!fs || token.isCancellationRequested) return []; | ||
const cmd = `ls -AQ1R "${fs.absoluteFromRelative(folder.path)}"`; | ||
console.log('Creating tree with command:', cmd); | ||
const exec = await toPromise<ClientChannel>(cb => fs.client.exec(cmd, cb)).catch(() => null); | ||
if (!exec || token.isCancellationRequested) return []; | ||
const res: string[] = []; | ||
const rl = createInterface(exec); | ||
let root = ''; | ||
rl.on('line', (line: string) => { | ||
if (!line) return; | ||
if (line.endsWith(':')) { | ||
root = JSON.parse(line.substr(0, line.length - 1)); | ||
} else { | ||
let relative = JSON.parse(line); | ||
relative = path.join(root, relative); | ||
relative = fs.relativeFromAbsolute(relative); | ||
res.push(relative); | ||
} | ||
}); | ||
token.onCancellationRequested(rl.close, rl); | ||
await toPromise(cb => rl.on('close', cb)); | ||
return res; | ||
} | ||
} |