Skip to content

Commit

Permalink
过滤文档代码层级优化
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaohuohumax committed Apr 2, 2024
1 parent d2e4da1 commit 92872f0
Showing 1 changed file with 62 additions and 67 deletions.
129 changes: 62 additions & 67 deletions src/cmd/impl/formatFolderCmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,104 +60,99 @@ export default class FormatFolderCmd extends ICmd {
}

/**
* 通过 ignore 配置过滤文件夹
* 通过文件夹路径获取 ignore 规则
* @param pUri 文件夹路径
* @param lExt 扩展规则(在所有文件规则之前,优先级最低)
* @returns
*/
async filterFolder(): Promise<string[]> {

/**
* 通过文件夹路径获取 ignore 规则
* @param pUri 文件夹路径
* @param lExt 扩展规则(在所有文件规则之前,优先级最低)
* @returns
*/
async function initIgnore(pUri: Uri, lExt: string[]) {
const filter = ignore({ allowRelativePaths: true }).add(lExt);
async initIgnore(pUri: Uri, lExt: string[]) {
const filter = ignore({ allowRelativePaths: true }).add(lExt);

// 获取文件夹下的过滤规则
for (const ignoreFile of Config.get.ignoreFileNames.map(i => Uri.joinPath(pUri, i))) {
if (!await fsUtil.isExists(ignoreFile)) {
continue;
}
log.debug('Find ignore file:', ignoreFile);
filter.add(await fsUtil.readFile(ignoreFile, 'utf-8'));
// 获取文件夹下的过滤规则
for (const ignoreFile of Config.get.ignoreFileNames.map(i => Uri.joinPath(pUri, i))) {
if (!await fsUtil.isExists(ignoreFile)) {
continue;
}

return filter;
log.debug('Find ignore file:', ignoreFile);
filter.add(await fsUtil.readFile(ignoreFile, 'utf-8'));
}

// 进度条控制
let progress: TaskProgress = null!;
// 取消令牌
let token: CancellationToken = null!;
return filter;
}

/**
* 递归过滤文档
* @param p 文档路径
* @param lExt 扩展过滤规则
* @returns
*/
const loopFilter = async (p: string[], lExt: string[]): Promise<string[]> => {
if (token.isCancellationRequested) {
// 取消搜索
throw new ECancelAborted(l10n.t('Format cancelled'));
}
/**
* 递归过滤文档
* @param p 文档路径
* @param lExt 扩展过滤规则
* @param progress 进度条
* @param token 取消令牌
* @returns
*/
async loopFilter(p: string[], lExt: string[], progress: TaskProgress, token: CancellationToken): Promise<string[]> {
if (token.isCancellationRequested) {
// 取消搜索
throw new ECancelAborted(l10n.t('Format cancelled'));
}

const res: string[] = [];
const res: string[] = [];

const pUri = Uri.joinPath(this.folder, ...p);
const pUri = Uri.joinPath(this.folder, ...p);

if (!await fsUtil.isExists(pUri)) {
log.error('Folder not exists:', pUri);
return res;
}
if (!await fsUtil.isExists(pUri)) {
log.error('Folder not exists:', pUri);
return res;
}

const filter = await initIgnore(pUri, lExt);
const filter = await this.initIgnore(pUri, lExt);

const files = [];
const files = [];

for (const [name, fileType] of await fsUtil.readDirectory(pUri)) {
const file = p.concat(name).join('/');
for (const [name, fileType] of await fsUtil.readDirectory(pUri)) {
const file = p.concat(name).join('/');

progress.report({ message: file });
progress.report({ message: file });

// 判断文档是否存在
const fileUri = Uri.joinPath(this.folder, file);
if (!await fsUtil.isExists(fileUri)) {
log.error('Document not exists:', fileUri);
continue;
}
// 判断文档是否存在
const fileUri = Uri.joinPath(this.folder, file);
if (!await fsUtil.isExists(fileUri)) {
log.error('Document not exists:', fileUri);
continue;
}

if (fileType === FileType.Directory && !filter.ignores(name + '/')) {
// 子目录
const children = (await loopFilter(p.concat(name), []))
.map(f => name + '/' + f);
files.push(...children);
continue;
}
if (fileType === FileType.Directory && !filter.ignores(name + '/')) {
// 子目录
const children = (await this.loopFilter(p.concat(name), [], progress, token))
.map(f => name + '/' + f);
files.push(...children);
continue;
}

if (fileType === FileType.File && !filter.ignores(name)) {
files.push(name);
}
if (fileType === FileType.File && !filter.ignores(name)) {
files.push(name);
}
}

return res.concat(filter.filter(files));
};
return res.concat(filter.filter(files));
}

/**
* 通过 ignore 配置过滤文件夹
* @returns
*/
async filterFolder(): Promise<string[]> {
return await window.withProgress<string[]>(
{
cancellable: true,
location: ProgressLocation.Notification,
title: l10n.t('Filter files by ignores')
},
async (p: TaskProgress, t: CancellationToken) => {
progress = p, token = t;
async (progress: TaskProgress, token: CancellationToken) => {

const lExt = Config.get.useIgnoreExtension
? Config.get.ignoreExtension
: [];

return await loopFilter([], lExt);
return await this.loopFilter([], lExt, progress, token);
}
);
}
Expand Down

0 comments on commit 92872f0

Please sign in to comment.