refactor🎨: (阅读代码):fileSystemRouter.ts增加注释

master
yetao 3 weeks ago
parent fde6f84bfd
commit 4406c12590

@ -1,30 +1,75 @@
// 导入 VSCode 模块,用于访问 VSCode 的 API
import * as vscode from 'vscode'; import * as vscode from 'vscode';
// 从 './flags' 模块中导入 getFlag 和 subscribeToGlobalFlags 函数
import { getFlag, subscribeToGlobalFlags } from './flags'; import { getFlag, subscribeToGlobalFlags } from './flags';
// 从 './logging' 模块中导入 Logging 类
import { Logging } from './logging'; import { Logging } from './logging';
// 从 './manager' 模块中导入 Manager 类型
import type { Manager } from './manager'; import type { Manager } from './manager';
// 定义一个包含所有可能的调试标志的数组
const ALL_DEBUG_FLAGS = [ const ALL_DEBUG_FLAGS = [
'stat', 'readDirectory', 'createDirectory', // 文件状态检查操作的调试标志
'readFile', 'writeFile', 'delete', 'rename', 'stat',
].map(v => v.toLowerCase()); // 读取目录操作的调试标志
'readDirectory',
// 创建目录操作的调试标志
'createDirectory',
// 读取文件操作的调试标志
'readFile',
// 写入文件操作的调试标志
'writeFile',
// 删除文件或目录操作的调试标志
'delete',
// 重命名文件或目录操作的调试标志
'rename',
].map(v => v.toLowerCase()); // 将所有标志转换为小写形式,以便于比较和匹配
/**
* FileSystemRouter vscode.FileSystemProvider
*/
export class FileSystemRouter implements vscode.FileSystemProvider { export class FileSystemRouter implements vscode.FileSystemProvider {
// 定义一个事件,用于通知文件系统的变化
public onDidChangeFile: vscode.Event<vscode.FileChangeEvent[]>; public onDidChangeFile: vscode.Event<vscode.FileChangeEvent[]>;
// 定义一个事件发射器,用于发射文件系统变化的事件
protected onDidChangeFileEmitter = new vscode.EventEmitter<vscode.FileChangeEvent[]>(); protected onDidChangeFileEmitter = new vscode.EventEmitter<vscode.FileChangeEvent[]>();
// 定义一个数组,用于存储调试标志
protected debugFlags: string[]; protected debugFlags: string[];
/**
* FileSystemRouter
* @param manager - Manager
*/
constructor(protected readonly manager: Manager) { constructor(protected readonly manager: Manager) {
// 将 onDidChangeFileEmitter 的事件绑定到 onDidChangeFile
this.onDidChangeFile = this.onDidChangeFileEmitter.event; this.onDidChangeFile = this.onDidChangeFileEmitter.event;
// 订阅全局标志,当全局标志发生变化时,更新 debugFlags
subscribeToGlobalFlags(() => { subscribeToGlobalFlags(() => {
// 获取 DEBUG_FSR 标志的值,并将其转换为小写,然后分割成数组
this.debugFlags = `${getFlag('DEBUG_FSR')?.[0] || ''}`.toLowerCase().split(/,\s*|\s+/g); this.debugFlags = `${getFlag('DEBUG_FSR')?.[0] || ''}`.toLowerCase().split(/,\s*|\s+/g);
// 如果 debugFlags 中包含 'all',则将 ALL_DEBUG_FLAGS 中的所有标志添加到 debugFlags 中
if (this.debugFlags.includes('all')) this.debugFlags.push(...ALL_DEBUG_FLAGS); if (this.debugFlags.includes('all')) this.debugFlags.push(...ALL_DEBUG_FLAGS);
}); });
} }
/**
*
* @param uri - URI
* @returns
*/
public async assertFs(uri: vscode.Uri): Promise<vscode.FileSystemProvider> { public async assertFs(uri: vscode.Uri): Promise<vscode.FileSystemProvider> {
// 从 manager 中获取文件系统实例
const fs = this.manager.getFs(uri); const fs = this.manager.getFs(uri);
// 如果文件系统实例存在,则返回该实例
if (fs) return fs; if (fs) return fs;
// 如果文件系统实例不存在,则创建一个新的文件系统实例并返回
return this.manager.createFileSystem(uri.authority); return this.manager.createFileSystem(uri.authority);
} }
/* FileSystemProvider */ /* FileSystemProvider */
/**
*
* @param uri - URI
* @param options -
* @returns
*/
public watch(uri: vscode.Uri, options: { recursive: boolean; excludes: string[]; }): vscode.Disposable { public watch(uri: vscode.Uri, options: { recursive: boolean; excludes: string[]; }): vscode.Disposable {
// TODO: Store watched files/directories in an array and periodically check if they're modified // TODO: Store watched files/directories in an array and periodically check if they're modified
/*let disp = () => {}; /*let disp = () => {};
@ -34,42 +79,98 @@ export class FileSystemRouter implements vscode.FileSystemProvider {
return new vscode.Disposable(() => disp());*/ return new vscode.Disposable(() => disp());*/
return new vscode.Disposable(() => { }); return new vscode.Disposable(() => { });
} }
/**
*
* @param uri - URI
* @returns
*/
public async stat(uri: vscode.Uri): Promise<vscode.FileStat> { public async stat(uri: vscode.Uri): Promise<vscode.FileStat> {
// 如果 debugFlags 中包含 'stat',则输出调试信息
if (this.debugFlags.includes('stat')) if (this.debugFlags.includes('stat'))
Logging.debug`Performing stat for ${uri}`; Logging.debug`Performing stat for ${uri}`;
// 获取文件系统实例,并调用其 stat 方法
return (await this.assertFs(uri)).stat(uri); return (await this.assertFs(uri)).stat(uri);
} }
/**
*
* @param uri - URI
* @returns
*/
public async readDirectory(uri: vscode.Uri): Promise<[string, vscode.FileType][]> { public async readDirectory(uri: vscode.Uri): Promise<[string, vscode.FileType][]> {
// 如果 debugFlags 中包含 'readdirectory',则输出调试信息
if (this.debugFlags.includes('readdirectory')) if (this.debugFlags.includes('readdirectory'))
Logging.debug`Reading directory ${uri}`; Logging.debug`Reading directory ${uri}`;
// 获取文件系统实例,并调用其 readDirectory 方法
return (await this.assertFs(uri)).readDirectory(uri); return (await this.assertFs(uri)).readDirectory(uri);
} }
/**
*
* @param uri - URI
* @returns Promise
*/
public async createDirectory(uri: vscode.Uri): Promise<void> { public async createDirectory(uri: vscode.Uri): Promise<void> {
// 如果 debugFlags 中包含 'createdirectory',则输出调试信息
if (this.debugFlags.includes('createdirectory')) if (this.debugFlags.includes('createdirectory'))
Logging.debug`Creating directory ${uri}`; Logging.debug`Creating directory ${uri}`;
// 获取文件系统实例,并调用其 createDirectory 方法
return (await this.assertFs(uri)).createDirectory(uri); return (await this.assertFs(uri)).createDirectory(uri);
} }
/**
*
* @param uri - URI
* @returns Uint8Array
*/
public async readFile(uri: vscode.Uri): Promise<Uint8Array> { public async readFile(uri: vscode.Uri): Promise<Uint8Array> {
// 如果 debugFlags 中包含 'readfile',则输出调试信息
if (this.debugFlags.includes('readfile')) if (this.debugFlags.includes('readfile'))
Logging.debug`Reading ${uri}`; Logging.debug`Reading ${uri}`;
// 获取文件系统实例,并调用其 readFile 方法
return (await this.assertFs(uri)).readFile(uri); return (await this.assertFs(uri)).readFile(uri);
} }
/**
*
* @param uri - URI
* @param content -
* @param options -
* @returns Promise
*/
public async writeFile(uri: vscode.Uri, content: Uint8Array, options: { create: boolean; overwrite: boolean; }): Promise<void> { public async writeFile(uri: vscode.Uri, content: Uint8Array, options: { create: boolean; overwrite: boolean; }): Promise<void> {
// 如果 debugFlags 中包含 'writefile',则输出调试信息
if (this.debugFlags.includes('writefile')) if (this.debugFlags.includes('writefile'))
Logging.debug`Writing ${content.length} bytes to ${uri} (options: ${options})`; Logging.debug`Writing ${content.length} bytes to ${uri} (options: ${options})`;
// 获取文件系统实例,并调用其 writeFile 方法
return (await this.assertFs(uri)).writeFile(uri, content, options); return (await this.assertFs(uri)).writeFile(uri, content, options);
} }
/**
*
* @param uri - URI
* @param options -
* @returns Promise
*/
public async delete(uri: vscode.Uri, options: { recursive: boolean; }): Promise<void> { public async delete(uri: vscode.Uri, options: { recursive: boolean; }): Promise<void> {
// 如果 debugFlags 中包含 'delete',则输出调试信息
if (this.debugFlags.includes('delete')) if (this.debugFlags.includes('delete'))
Logging.debug`Deleting ${uri} (options: ${options})`; Logging.debug`Deleting ${uri} (options: ${options})`;
// 获取文件系统实例,并调用其 delete 方法
return (await this.assertFs(uri)).delete(uri, options); return (await this.assertFs(uri)).delete(uri, options);
} }
/**
*
* @param oldUri - URI
* @param newUri - URI
* @param options -
* @returns Promise
*/
public async rename(oldUri: vscode.Uri, newUri: vscode.Uri, options: { overwrite: boolean; }): Promise<void> { public async rename(oldUri: vscode.Uri, newUri: vscode.Uri, options: { overwrite: boolean; }): Promise<void> {
// 如果 debugFlags 中包含 'rename',则输出调试信息
if (this.debugFlags.includes('rename')) if (this.debugFlags.includes('rename'))
Logging.debug`Renaming ${oldUri} to ${newUri}`; Logging.debug`Renaming ${oldUri} to ${newUri}`;
// 获取旧的文件系统实例
const fs = await this.assertFs(oldUri); const fs = await this.assertFs(oldUri);
// 如果新的文件系统实例与旧的不同,则抛出错误
if (fs !== (await this.assertFs(newUri))) if (fs !== (await this.assertFs(newUri)))
throw new Error(`Can't rename between different SSH filesystems`); throw new Error(`Can't rename between different SSH filesystems`);
// 调用文件系统实例的 rename 方法
return fs.rename(oldUri, newUri, options); return fs.rename(oldUri, newUri, options);
} }
} }
Loading…
Cancel
Save