Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save thautwarm/9fd6c47f0344cb24998346d9181c6bb2 to your computer and use it in GitHub Desktop.
Save thautwarm/9fd6c47f0344cb24998346d9181c6bb2 to your computer and use it in GitHub Desktop.
autocompletion in vscode
const commandCreateFile = vscode.commands.registerCommand("extension.dired.createFile", async () => {
// list files of provider.dirname
const quickPick = vscode.window.createQuickPick();
quickPick.canSelectMany = false;
quickPick.placeholder = "Filename:";
let dirname = provider.dirname ?? path.normalize(".");
quickPick.items = [{ label: dirname }];
let fileName = quickPick.value;
let disposables: vscode.Disposable[] = [];
try {
await new Promise<void>(resolve => {
quickPick.placeholder = "Create Or Open Files"
disposables.push(
quickPick.onDidChangeValue(directoryOrFile => {
if (!path.isAbsolute(directoryOrFile))
{
if (provider.dirname == undefined)
return
directoryOrFile = path.join(provider.dirname, directoryOrFile);
}
let files: vscode.QuickPickItem[] = []
let dirname: string
try {
let stat = fs.statSync(directoryOrFile);
files.push({label : directoryOrFile});
if (stat.isDirectory()) {
dirname = directoryOrFile;
}
else {
dirname = path.dirname(directoryOrFile);
}
}
catch
{
dirname = path.dirname(directoryOrFile);
}
fs.readdirSync(dirname).forEach(
fileName => files.push( {label : path.join(dirname, fileName)})
)
quickPick.items = files;
}),
quickPick.onDidAccept(() => {
if (quickPick.selectedItems.length == 0 || quickPick.selectedItems[0].label == quickPick.value) {
fileName = quickPick.value;
quickPick.hide();
resolve();
}
else {
quickPick.value = quickPick.selectedItems[0].label;
}
}),
quickPick.onDidHide(() => {
quickPick.dispose();
resolve();
})
);
quickPick.show();
});
}
finally {
disposables.forEach(d => d.dispose());
}
vscode.window.showInformationMessage(`${fileName} success`);
if (fileName == undefined) {
return;
}
let openFile: boolean = false;
try {
let stat = await fs.promises.stat(fileName);
if (stat.isDirectory()) {
provider.openDir(fileName);
}
else {
openFile = true;
}
}
catch {
openFile = true;
}
if (openFile) {
let document = await vscode.workspace.openTextDocument(fileName);
await vscode.window.showTextDocument(document);
provider.reload();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment