This repository has been archived by the owner on Dec 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
067d9d7
commit e01dd35
Showing
2 changed files
with
102 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,97 @@ | ||
import * as vscode from "vscode"; | ||
import * as fs from "fs"; | ||
import * as path from "path"; | ||
import FileTreeItem from "../utils/file-tree-item"; | ||
export default class LayoutsProvider | ||
implements vscode.TreeDataProvider<FileTreeItem> | ||
{ | ||
constructor(private projectDirUri: vscode.Uri) {} | ||
private _onDidChangeTreeData: vscode.EventEmitter< | ||
void | FileTreeItem | FileTreeItem[] | null | undefined | ||
> = new vscode.EventEmitter< | ||
void | FileTreeItem | FileTreeItem[] | null | undefined | ||
>(); | ||
onDidChangeTreeData?: | ||
| vscode.Event<void | FileTreeItem | FileTreeItem[] | null | undefined> | ||
| undefined = this._onDidChangeTreeData.event; | ||
refresh(): void { | ||
this._onDidChangeTreeData?.fire(); | ||
} | ||
getTreeItem( | ||
element: FileTreeItem | ||
): vscode.TreeItem | Thenable<vscode.TreeItem> { | ||
return element; | ||
} | ||
|
||
getChildren( | ||
element?: FileTreeItem | undefined | ||
): vscode.ProviderResult<FileTreeItem[]> { | ||
if (!this.projectDirUri) { | ||
vscode.window.showInformationMessage("No file in empty workspace"); | ||
return []; | ||
} | ||
let rootDir: vscode.Uri = this.projectDirUri; | ||
if (element) { | ||
rootDir = element.resourceUri!; | ||
} | ||
if (!rootDir || !rootDir.fsPath) { | ||
vscode.window.showErrorMessage("Invalid directory"); | ||
return []; | ||
} | ||
|
||
try { | ||
const files = fs.readdirSync(rootDir.fsPath, { withFileTypes: true }); | ||
const fileTreeItems: FileTreeItem[] = []; | ||
files.forEach((file) => { | ||
let filePath = path.join(rootDir.fsPath, file.name); | ||
|
||
const fileTreeItem = new FileTreeItem( | ||
file.isFile() ? "/" : `/${file.name}`, | ||
|
||
file.isDirectory() | ||
? vscode.TreeItemCollapsibleState.Collapsed | ||
: vscode.TreeItemCollapsibleState.None, | ||
|
||
{ | ||
command: "vscode.open", | ||
title: "", | ||
arguments: [vscode.Uri.file(filePath)], | ||
} | ||
); | ||
fileTreeItem.resourceUri = vscode.Uri.file(filePath); | ||
if (!file.isDirectory() && !file.name.includes("layout")) { | ||
return; | ||
} | ||
fileTreeItems.push(fileTreeItem); | ||
}); | ||
return fileTreeItems; | ||
} catch (err) { | ||
vscode.window.showErrorMessage("Error reading directory"); | ||
return []; | ||
} | ||
} | ||
async deleteFile(element: FileTreeItem) { | ||
const fileToDelete = await this.getTreeItem(element); | ||
if (!fileToDelete) { | ||
vscode.window.showErrorMessage("Failed to delete"); | ||
} | ||
await vscode.workspace.fs.delete(fileToDelete.resourceUri!, { | ||
recursive: true, | ||
}); | ||
} | ||
async renameFile(appDirUri: vscode.Uri, element: FileTreeItem) { | ||
const fileToRename = await this.getTreeItem(element); | ||
if (!fileToRename) { | ||
vscode.window.showErrorMessage("Failed to rename"); | ||
} | ||
const newPath = await vscode.window.showInputBox(); | ||
if (!newPath) { | ||
return; | ||
} | ||
const newPathUri = vscode.Uri.parse(newPath); | ||
await vscode.workspace.fs.rename( | ||
fileToRename.resourceUri!, | ||
vscode.Uri.joinPath(appDirUri, newPathUri.fsPath) | ||
); | ||
} | ||
} |