Skip to content

Commit f7d3e62

Browse files
committed
feat: add configuration of max depth limit while generating tree string for directory
1 parent bcf8b68 commit f7d3e62

File tree

7 files changed

+30
-15
lines changed

7 files changed

+30
-15
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ In addition to being able to format pre-formatted tree strings, you may also gen
5959

6060
The walking process through files is performed asynchronously. Therefore, selecting heavily-nested folders (e.g. `node_modules`) will directly affect performance speed.
6161

62-
By default, `node_modules` and `.git` are ignored while generating tree string for directories. However, this can be customized by setting `asciiTreeGenerator.directoryIgnore` in configurations.
62+
By default, `node_modules` and `.git` are ignored while generating tree string for directories. However, this can be customized by setting `asciiTreeGenerator.directoryIgnore` in configurations. Also, setting `asciiTreeGenerator.directoryMaxDepth` can limit the depth of directory walking-through.
6363

6464
## Configuration
6565

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,12 @@
7070
"type": "string"
7171
},
7272
"default": ["node_modules", ".git"],
73-
"description": "the glob patterns of ignored path while generate tree string for directory"
73+
"description": "The glob patterns of ignored path while generate tree string for directory"
74+
},
75+
"asciiTreeGenerator.directoryMaxDepth": {
76+
"type": "integer",
77+
"default": 0,
78+
"description": "The max walk-through depths while generate tree string for directory. Set to 0 means no limitation."
7479
}
7580
}
7681
}

src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { IVsCodeConfig } from './lib/interface';
44
export function getConfig(): IVsCodeConfig {
55
const config: IVsCodeConfig = {
66
directoryIgnore: vscode.workspace.getConfiguration().get<string[]>('asciiTreeGenerator.directoryIgnore'),
7+
directoryMaxDepth: vscode.workspace.getConfiguration().get<number>('asciiTreeGenerator.directoryMaxDepth'),
78
rootCharCode: vscode.workspace
89
.getConfiguration()
910
.get<number>('asciiTreeGenerator.rootElement'),

src/extension.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as path from 'path';
66

77
import { formatFileTreeItemsFromDirectory } from './lib/directory';
88
import { generate } from './lib/generator';
9-
import { getUserEOL, createWebview, revertTreeString, getCharCodesFromConfig, getDirectoryIgnoreFromConfig } from './utils';
9+
import { getUserEOL, createWebview, revertTreeString, getCharCodesFromConfig, getDirectoryIgnoreFromConfig, getDirectoryMaxDepthFromConfig } from './utils';
1010
import { formatFileTreeItemsFromText } from './lib/text';
1111

1212
export function activate(context: vscode.ExtensionContext) {
@@ -63,9 +63,9 @@ export function activate(context: vscode.ExtensionContext) {
6363
path.relative(rootWorkspace.uri.fsPath, target.fsPath) || '.';
6464

6565
const items = await formatFileTreeItemsFromDirectory(target!.fsPath, {
66-
maxDepth: Number.MAX_VALUE,
67-
sort: true,
6866
ignore: getDirectoryIgnoreFromConfig(),
67+
maxDepth: getDirectoryMaxDepthFromConfig(),
68+
sort: true,
6969
});
7070
const text = generate(items, {
7171
eol: getUserEOL(),

src/lib/interface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export interface ICharset {
3636

3737
// the VS code config interface
3838
export interface IVsCodeConfig {
39+
directoryMaxDepth: number | undefined;
3940
directoryIgnore: string[] | undefined;
4041
rootCharCode: number | undefined;
4142
childCharCode: number | undefined;

src/test/lib/directory.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ suite('lib/directory functions', function () {
4141
assert(findFileByName(files, '.env') === undefined);
4242
});
4343

44+
test('should correctly list directories when recursively into depth 3', async () => {
45+
const files = await listDirectory(rootDir, {
46+
maxDepth: 3,
47+
});
48+
assert(findFileByName(files, 'img'));
49+
assert(findFileByName(files, 'logo.png') === undefined);
50+
});
51+
4452
test('should correctly list directory when sort', async () => {
4553
const files = await listDirectory(rootDir, {
4654
sort: true,
@@ -55,14 +63,6 @@ suite('lib/directory functions', function () {
5563
]);
5664
});
5765

58-
test('should correctly list directories when recursively into depth 3', async () => {
59-
const files = await listDirectory(rootDir, {
60-
maxDepth: 3,
61-
});
62-
assert(findFileByName(files, 'img'));
63-
assert(findFileByName(files, 'logo.png') === undefined);
64-
});
65-
6666
test('should correctly format file tree items', async () => {
6767
const fileItems = await formatFileTreeItemsFromDirectory(rootDir, {
6868
maxDepth: Number.MAX_VALUE,

src/utils.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ export function getCharCodesFromConfig(): ICharset {
115115
if (isInTestMode) {
116116
return defaultCharset;
117117
}
118-
const config: IVsCodeConfig = getConfig();
119-
const charset: ICharset = {
118+
const config = getConfig();
119+
const charset = {
120120
root: validateCharCode(config.rootCharCode, defaultCharset.root),
121121
child: validateCharCode(config.childCharCode, defaultCharset.child),
122122
last: validateCharCode(config.lastCharCode, defaultCharset.last),
@@ -135,6 +135,14 @@ export function getDirectoryIgnoreFromConfig(): string[] {
135135
return defaultDirectoryIgnore;
136136
}
137137

138+
export function getDirectoryMaxDepthFromConfig(): number {
139+
const { directoryMaxDepth } = getConfig();
140+
if (directoryMaxDepth === 0 || !Number.isInteger(directoryMaxDepth)) {
141+
return Number.MAX_SAFE_INTEGER;
142+
}
143+
return directoryMaxDepth!;
144+
}
145+
138146
function validateCharCode(
139147
userValue: number | undefined,
140148
fallback: string

0 commit comments

Comments
 (0)