vscode#FileType TypeScript Examples
The following examples show how to use
vscode#FileType.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: extension.ts From vscode-file-browser with GNU Lesser General Public License v3.0 | 6 votes |
async stepIn() {
this.activeItem().ifSome(async (item) => {
if (item.action !== undefined) {
this.runAction(item);
} else if (item.fileType !== undefined) {
if ((item.fileType & FileType.Directory) === FileType.Directory) {
await this.stepIntoFolder(this.path.append(item.name));
} else if ((item.fileType & FileType.File) === FileType.File) {
this.path.push(item.name);
this.file = None;
this.inActions = true;
await this.update();
}
}
});
}
Example #2
Source File: extension.ts From vscode-file-browser with GNU Lesser General Public License v3.0 | 6 votes |
tabCompletion(tabNext: boolean) {
if (this.inActions) {
return;
}
if (this.autoCompletion) {
const length = this.autoCompletion.items.length;
const step = tabNext ? 1 : -1;
this.autoCompletion.index = (this.autoCompletion.index + length + step) % length;
} else {
const items = this.items.filter((i) =>
i.name.toLowerCase().startsWith(this.current.value.toLowerCase())
);
this.autoCompletion = {
index: tabNext ? 0 : items.length - 1,
items,
};
}
const newIndex = this.autoCompletion.index;
const length = this.autoCompletion.items.length;
if (newIndex < length) {
// This also checks out when items is empty
const item = this.autoCompletion.items[newIndex];
this.current.value = item.name;
if (length === 1 && item.fileType === FileType.Directory) {
this.current.value += "/";
}
this.onDidChangeValue(this.current.value, true);
}
}
Example #3
Source File: extension.ts From vscode-file-browser with GNU Lesser General Public License v3.0 | 6 votes |
onDidAccept() {
this.autoCompletion = undefined;
this.activeItem().ifSome((item) => {
if (item.action !== undefined) {
this.runAction(item);
} else if (
item.fileType !== undefined &&
(item.fileType & FileType.Directory) === FileType.Directory
) {
this.stepIn();
} else {
this.openFile(this.path.append(item.name).uri);
}
});
}
Example #4
Source File: fileitem.ts From vscode-file-browser with GNU Lesser General Public License v3.0 | 6 votes |
constructor(record: [string, FileType]) {
const [name, fileType] = record;
this.name = name;
this.fileType = fileType;
this.alwaysShow = config(ConfigItem.HideDotfiles) ? !name.startsWith(".") : true;
switch (this.fileType) {
case FileType.Directory:
this.label = `$(folder) ${name}`;
break;
case FileType.Directory | FileType.SymbolicLink:
this.label = `$(file-symlink-directory) ${name}`;
break;
case FileType.File | FileType.SymbolicLink:
this.label = `$(file-symlink-file) ${name}`;
default:
this.label = `$(file) ${name}`;
break;
}
}
Example #5
Source File: fileitem.ts From vscode-file-browser with GNU Lesser General Public License v3.0 | 6 votes |
export function fileRecordCompare(left: [string, FileType], right: [string, FileType]): -1 | 0 | 1 {
const [leftName, leftDir] = [
left[0].toLowerCase(),
(left[1] & FileType.Directory) === FileType.Directory,
];
const [rightName, rightDir] = [
right[0].toLowerCase(),
(right[1] & FileType.Directory) === FileType.Directory,
];
if (leftDir && !rightDir) {
return -1;
}
if (rightDir && !leftDir) {
return 1;
}
return leftName > rightName ? 1 : leftName === rightName ? 0 : -1;
}
Example #6
Source File: utopia-fs.ts From utopia with MIT License | 6 votes |
async stat(uri: Uri): Promise<FileStat> {
const path = fromUtopiaURI(uri)
const stats = await stat(path)
const fileType = isDirectory(stats) ? FileType.Directory : FileType.File
return {
type: fileType,
ctime: stats.ctime.valueOf(),
mtime: stats.lastSavedTime.valueOf(), // VS Code is only interested in changes to the saved content
size: stats.size,
}
}
Example #7
Source File: utopia-fs.ts From utopia with MIT License | 6 votes |
async readDirectory(uri: Uri): Promise<[string, FileType][]> {
const path = fromUtopiaURI(uri)
const children = await readDirectory(path)
const result: Promise<[string, FileType]>[] = children.map((childName) =>
pathIsDirectory(appendToPath(path, childName)).then((resultIsDirectory) => [
childName,
resultIsDirectory ? FileType.Directory : FileType.File,
]),
)
return Promise.all(result)
}
Example #8
Source File: VirtualFileSystemProvider.ts From vscode-drawio with GNU General Public License v3.0 | 6 votes |
stat(uri: Uri): FileStat {
const f = this.getOrCreateFile(uri).file;
return {
type: FileType.File,
ctime: 0,
mtime: 0,
size: f.data.length,
};
}
Example #9
Source File: extension.ts From vscode-file-browser with GNU Lesser General Public License v3.0 | 5 votes |
async update() {
this.current.enabled = false;
this.current.show();
this.current.busy = true;
this.current.title = this.path.fsPath;
this.current.value = "";
const stat = (await Result.try(vscode.workspace.fs.stat(this.path.uri))).unwrap();
if (stat && this.inActions && (stat.type & FileType.File) === FileType.File) {
this.items = [
action("$(file) Open this file", Action.OpenFile),
action("$(split-horizontal) Open this file to the side", Action.OpenFileBeside),
action("$(edit) Rename this file", Action.RenameFile),
action("$(trash) Delete this file", Action.DeleteFile),
];
this.current.items = this.items;
} else if (
stat &&
this.inActions &&
(stat.type & FileType.Directory) === FileType.Directory
) {
this.items = [
action("$(folder-opened) Open this folder", Action.OpenFolder),
action(
"$(folder-opened) Open this folder in a new window",
Action.OpenFolderInNewWindow
),
action("$(edit) Rename this folder", Action.RenameFile),
action("$(trash) Delete this folder", Action.DeleteFile),
];
this.current.items = this.items;
} else if (stat && (stat.type & FileType.Directory) === FileType.Directory) {
const records = await vscode.workspace.fs.readDirectory(this.path.uri);
records.sort(fileRecordCompare);
let items = records.map((entry) => new FileItem(entry));
if (config(ConfigItem.HideIgnoreFiles)) {
const rules = await Rules.forPath(this.path);
items = rules.filter(this.path, items);
}
if (config(ConfigItem.RemoveIgnoredFiles)) {
items = items.filter((item) => item.alwaysShow);
}
this.items = items;
this.current.items = items;
this.current.activeItems = items.filter((item) => this.file.contains(item.name));
} else {
this.items = [action("$(new-folder) Create this folder", Action.NewFolder)];
this.current.items = this.items;
}
this.current.enabled = true;
}
Example #10
Source File: fileitem.ts From vscode-file-browser with GNU Lesser General Public License v3.0 | 5 votes |
fileType?: FileType;
Example #11
Source File: fileitem.ts From vscode-file-browser with GNU Lesser General Public License v3.0 | 5 votes |
export function itemIsDir(item: FileItem): boolean {
if (item.fileType === undefined) {
return false;
}
return !!(item.fileType | FileType.Directory);
}
Example #12
Source File: path.ts From vscode-file-browser with GNU Lesser General Public License v3.0 | 5 votes |
async isDir(): Promise<boolean> {
const stat = await this.stat();
return stat.match(
(stat) => !!(stat.type | FileType.Directory),
() => false
);
}
Example #13
Source File: path.ts From vscode-file-browser with GNU Lesser General Public License v3.0 | 5 votes |
async isFile(): Promise<boolean> {
const stat = await this.stat();
return stat.match(
(stat) => !!(stat.type | FileType.File),
() => false
);
}
Example #14
Source File: VirtualFileSystemProvider.ts From vscode-drawio with GNU General Public License v3.0 | 5 votes |
readDirectory(
uri: Uri
):
| [string, import("vscode").FileType][]
| Thenable<[string, import("vscode").FileType][]> {
throw new Error("Method not implemented.");
}
Example #15
Source File: extension.ts From vscode-file-browser with GNU Lesser General Public License v3.0 | 4 votes |
async runAction(item: FileItem) {
switch (item.action) {
case Action.NewFolder: {
await vscode.workspace.fs.createDirectory(this.path.uri);
await this.update();
break;
}
case Action.NewFile: {
const uri = this.path.append(item.name).uri;
this.openFile(uri.with({ scheme: "untitled" }));
break;
}
case Action.OpenFile: {
const path = this.path.clone();
if (item.name && item.name.length > 0) {
path.push(item.name);
}
this.openFile(path.uri);
break;
}
case Action.OpenFileBeside: {
const path = this.path.clone();
if (item.name && item.name.length > 0) {
path.push(item.name);
}
this.openFile(path.uri, ViewColumn.Beside);
break;
}
case Action.RenameFile: {
this.keepAlive = true;
this.hide();
const uri = this.path.uri;
const stat = await vscode.workspace.fs.stat(uri);
const isDir = (stat.type & FileType.Directory) === FileType.Directory;
const fileName = this.path.pop().unwrapOrElse(() => {
throw new Error("Can't rename an empty file name!");
});
const fileType = isDir ? "folder" : "file";
const workspaceFolder = this.path.getWorkspaceFolder().map((wsf) => wsf.uri);
const relPath = workspaceFolder
.andThen((workspaceFolder) => new Path(uri).relativeTo(workspaceFolder))
.unwrapOr(fileName);
const extension = OSPath.extname(relPath);
const startSelection = relPath.length - fileName.length;
const endSelection = startSelection + (fileName.length - extension.length);
const result = await vscode.window.showInputBox({
prompt: `Enter the new ${fileType} name`,
value: relPath,
valueSelection: [startSelection, endSelection],
});
this.file = Some(fileName);
if (result !== undefined) {
const newUri = workspaceFolder.match(
(workspaceFolder) => Uri.joinPath(workspaceFolder, result),
() => Uri.joinPath(this.path.uri, result)
);
if ((await Result.try(vscode.workspace.fs.rename(uri, newUri))).isOk()) {
this.file = Some(OSPath.basename(result));
} else {
vscode.window.showErrorMessage(
`Failed to rename ${fileType} "${fileName}"`
);
}
}
this.show();
this.keepAlive = false;
this.inActions = false;
this.update();
break;
}
case Action.DeleteFile: {
this.keepAlive = true;
this.hide();
const uri = this.path.uri;
const stat = await vscode.workspace.fs.stat(uri);
const isDir = (stat.type & FileType.Directory) === FileType.Directory;
const fileName = this.path.pop().unwrapOrElse(() => {
throw new Error("Can't delete an empty file name!");
});
const fileType = isDir ? "folder" : "file";
const goAhead = `$(trash) Delete the ${fileType} "${fileName}"`;
const result = await vscode.window.showQuickPick(["$(close) Cancel", goAhead], {});
if (result === goAhead) {
const delOp = await Result.try(
vscode.workspace.fs.delete(uri, { recursive: isDir })
);
if (delOp.isErr()) {
vscode.window.showErrorMessage(
`Failed to delete ${fileType} "${fileName}"`
);
}
}
this.show();
this.keepAlive = false;
this.inActions = false;
this.update();
break;
}
case Action.OpenFolder: {
vscode.commands.executeCommand("vscode.openFolder", this.path.uri);
break;
}
case Action.OpenFolderInNewWindow: {
vscode.commands.executeCommand("vscode.openFolder", this.path.uri, true);
break;
}
default:
throw new Error(`Unhandled action ${item.action}`);
}
}