Skip to content

Instantly share code, notes, and snippets.

@andyl
Last active December 5, 2018 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andyl/8edfe2f86e45eed5a7d417b92cd2e251 to your computer and use it in GitHub Desktop.
Save andyl/8edfe2f86e45eed5a7d417b92cd2e251 to your computer and use it in GitHub Desktop.
const vscode = require('vscode');
const shelljs = require('shelljs');
// This extension is a hack to explore a possible VsCode way to search
// for a directory name.
//
// In IntelliJ you can search for a directory by ending the search string with '/'.
// In Vim, you can `:call fzf#run({'source': 'fd -t d', 'sink': 'NERDTreeFind'})`
//
// But VsCode doesn't seem to provide an API to open the current
// explorer on a given folder.
//
// Related issue:
// https://github.com/Microsoft/vscode/issues/10026#issuecomment-441833246
function activate(context) {
vscode.window.showInformationMessage("ACTIVATE");
let cwd = process.cwd();
let root = vscode.workspace.rootPath;
// create a picklist object
function picklist_item(path, dir) {
return {
'label': path,
'description': dir,
'detail': ''
}
}
// return a list of directories (picklist objects)
function dirLst() {
shelljs.cd(root);
// generate a list of non-hidden directories
let dlst = shelljs.find('.').filter((el) => {
return shelljs.test('-d', el) && el[0] != '.'
});
// map paths to picklist objects
dlst.forEach((path, idx) => {dlst[idx] = picklist_item(path)});
// we always put 'cwd' on top of the list
// this allows returning to the home directory
return [picklist_item('cwd', cwd)].concat(dlst);
}
// open the explorer window at a directory
// it changes the root-path of the explorer to `dir`
// `vscode.openFolder` is not exactly what I want
// but comes closest to the desired solution
function exploreOpen(dir) {
let path = dir.description || root + '/' + dir.label
let uri = vscode.Uri.parse('file:' + path);
vscode.commands.executeCommand('vscode.openFolder', uri, false);
}
// the extension command : starts a directory picklist
function cmd() {
let dlst = dirLst();
let opts = {
placeHolder: 'Enter directory name',
ignoreFocusOut: true,
matchOnDescription: true,
matchOnDetail: false
}
vscode.window.showQuickPick(dlst, opts).then((opt) => {exploreOpen(opt)});
}
let ext = vscode.commands.registerCommand("ext.fuzDir", cmd);
context.subscriptions.push(ext);
}
exports.activate = activate;
function deactivate() {}
exports.deactivate = deactivate;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment