英文:
Customizing VSCode file explorer through vscode API
问题
有没有一种通过API自定义VSCode文件资源管理器的方法?我想创建一个扩展,允许在文件资源管理器中突出显示文件,但是通过查看VSCode API参考,我找不到任何自定义文件资源管理器的方法。这是否有可能?
英文:
Is there a way to customize VSCode file explorer through the API?
I would like to create an extension that allows highlighting files in the file explorer, but I can't find any way to customize the file explorer by looking at the VSCode API reference. Is that possible?
答案1
得分: 1
A vscode.window.registerFileDecorationProvider(provider: FileDecorationProvider): Disposable
是操作资源管理器中文件颜色的唯一方式。
你可以查看这个扩展 Highlight Files,我写了它来为不在当前工作区的文件应用自定义颜色。以下是来自该 extension.js
的一些代码:
let decClass = new FileDecorationProvider(coloredEnabled, badgedEnabled, badge); // fileDecorator Class
class FileDecorationProvider {
constructor(colorEnabled, badgeEnabled, newBadge) {
this.disposables = [];
this.disposables.push(vscode.window.registerFileDecorationProvider(this));
this.colorEnabled = colorEnabled;
this.colorEnabled ? this.color = new vscode.ThemeColor("highlightFiles.nonWorkspaceFiles") : this.color = null;
this.badgeEnabled = badgeEnabled;
this.badgeEnabled ? this.newBadge = newBadge : this.newBadge = null;
if (this.badgeEnabled) this.newBadge = this.newBadge || '!';
}
/**
* 检查非工作区文件
*
* @param {vscode.Uri} uri
* @memberof FileDecorationProvider
**/
async provideFileDecoration(uri) {
let workspaceFiles = await vscode.workspace.findFiles('**');
if (uri.scheme === 'vscode-userdata') return; // 忽略 settings.json 和 keybindings.json
const isFile = await vscode.workspace.fs.stat(uri);
const result = workspaceFiles.findIndex(file => file.fsPath === uri.fsPath);
const workspaces = vscode.workspace.workspaceFolders;
const thisWorkspaceFolder = vscode.workspace.getWorkspaceFolder(uri).name;
// if ((isFile.type === 1) && (thisWorkspaceFolder === "TestMultiRoot")) // 是文件,不是目录,不在工作区
if (thisWorkspaceFolder === "TestMultiRoot") // 是文件,不是目录,不在工作区
return {
badge: this.newBadge,
// badge: "⛖", // ⛖
color: new vscode.ThemeColor("highlightFiles.workspaceFolder1"),
propagate: true,
tooltip: "Workspace TestMultiRoot"
};
else
return {
color: new vscode.ThemeColor("highlightFiles.workspaceFolder2"),
propagate: true,
tooltip: "Workspace is not TestMultiRoot"
};
}
// if ((isFile.type === 1) && (result < 0)) // 是文件,不是目录,不在工作区
// return {
// badge: this.newBadge,
// // badge: "⛖", // ⛖
// color: this.color,
// tooltip: "File not in workspace"
// };
// }
dispose() {
this.disposables.forEach((d) => d.dispose());
}
}
英文:
A vscode.window.registerFileDecorationProvider(provider: FileDecorationProvider): Disposable
is the only way to manipulate the color of files in the Explorer.
You can have a look at this extension Highlight Files which I wrote to apply a custom color to files not in the current workspace. Here is some code from that extension.js
:
let decClass = new FileDecorationProvider(coloredEnabled, badgedEnabled, badge); // fileDecorator Class
class FileDecorationProvider {
constructor(colorEnabled, badgeEnabled, newBadge) {
this.disposables = [];
this.disposables.push(vscode.window.registerFileDecorationProvider(this));
this.colorEnabled = colorEnabled;
this.colorEnabled ? this.color = new vscode.ThemeColor("highlightFiles.nonWorkspaceFiles") : this.color = null;
this.badgeEnabled = badgeEnabled;
this.badgeEnabled ? this.newBadge = newBadge : this.newBadge = null;
if (this.badgeEnabled) this.newBadge = this.newBadge || '!';
}
/**
* Checks for non-workspace files
*
* @param {vscode.Uri} uri
* @memberof FileDecorationProvider
**/
async provideFileDecoration(uri) {
let workspaceFiles = await vscode.workspace.findFiles('**');
if (uri.scheme === 'vscode-userdata') return; // ignore settings.json and keybindings.json
const isFile = await vscode.workspace.fs.stat(uri);
const result = workspaceFiles.findIndex(file => file.fsPath === uri.fsPath);
const workspaces = vscode.workspace.workspaceFolders;
const thisWorkspaceFolder = vscode.workspace.getWorkspaceFolder(uri).name;
// if ((isFile.type === 1) && (thisWorkspaceFolder === "TestMultiRoot")) // is a file, not a directory && not in workspace
if (thisWorkspaceFolder === "TestMultiRoot") // is a file, not a directory && not in workspace
return {
badge: this.newBadge,
// badge: "\u26D6", // ⛖
color: new vscode.ThemeColor("highlightFiles.workspaceFolder1"),
propagate: true,
tooltip: "Workspace TestMultiRoot"
};
else
return {
color: new vscode.ThemeColor("highlightFiles.workspaceFolder2"),
propagate: true,
tooltip: "Workspace is not TestMultiRoot"
};
}
// if ((isFile.type === 1) && (result < 0)) // is a file, not a directory && not in workspace
// return {
// badge: this.newBadge,
// // badge: "\u26D6", // ⛖
// color: this.color,
// tooltip: "File not in workspace"
// };
// }
dispose() {
this.disposables.forEach((d) => d.dispose());
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论