Skip to content

Commit 383d695

Browse files
Add setting to ignore file extensions; fix file watcher
1 parent 6c39a1e commit 383d695

File tree

3 files changed

+66
-19
lines changed

3 files changed

+66
-19
lines changed

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"displayName": "Files2Prompt",
44
"icon": "./files2prompt-icon.webp",
55
"description": "Copy file contents for LLM prompts",
6-
"version": "1.5.0",
6+
"version": "1.6.0",
77
"publisher": "thomas-mckanna",
88
"keywords": [
99
"files",
@@ -167,6 +167,11 @@
167167
"type": "string",
168168
"description": "Optional system message to include in the output.",
169169
"default": ""
170+
},
171+
"files2prompt.ignoredExtensions": {
172+
"type": "string",
173+
"description": "Comma-separated list of file extensions to ignore during recursive selection. Example: 'png,jpg,jpeg,gif,svg'",
174+
"default": "png,jpg,jpeg,gif,svg"
170175
}
171176
}
172177
}

src/extension.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ export function activate(context: vscode.ExtensionContext) {
1717
manageCheckboxStateManually: true,
1818
});
1919

20+
// Add fileTreeProvider to ensure proper disposal
21+
context.subscriptions.push(fileTreeProvider);
22+
2023
let history: string[][] = [];
2124
let historyPosition: number = -1;
2225

@@ -153,15 +156,6 @@ ${xmlContent}</files>`;
153156
})
154157
);
155158

156-
// Watch for file changes and refresh the tree view
157-
const watcher = vscode.workspace.createFileSystemWatcher('**/*');
158-
159-
watcher.onDidCreate(uri => fileTreeProvider.refresh());
160-
watcher.onDidDelete(uri => fileTreeProvider.refresh());
161-
watcher.onDidChange(uri => fileTreeProvider.refresh());
162-
163-
context.subscriptions.push(watcher);
164-
165159
// Handle checkbox state changes asynchronously
166160
treeView.onDidChangeCheckboxState(async (e) => {
167161
for (const [item, state] of e.items) {

src/fileTreeProvider.ts

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as fs from "fs";
44
import * as path from "path";
55
import ignore from "ignore";
66

7-
export class FileTreeProvider implements vscode.TreeDataProvider<FileItem> {
7+
export class FileTreeProvider implements vscode.TreeDataProvider<FileItem>, vscode.Disposable {
88
private _onDidChangeTreeData: vscode.EventEmitter<
99
FileItem | undefined | null | void
1010
> = new vscode.EventEmitter<FileItem | undefined | null | void>();
@@ -15,10 +15,36 @@ export class FileTreeProvider implements vscode.TreeDataProvider<FileItem> {
1515
private workspaceRoot: string;
1616
private checkedItems: Map<string, vscode.TreeItemCheckboxState> = new Map();
1717
private gitignore = ignore();
18+
private ignoredExtensions: Set<string> = new Set();
19+
private watcher: vscode.FileSystemWatcher;
1820

1921
constructor(workspaceRoot: string) {
2022
this.workspaceRoot = workspaceRoot;
2123
this.loadGitignore();
24+
this.loadIgnoredExtensions();
25+
26+
// Create a file system watcher
27+
this.watcher = vscode.workspace.createFileSystemWatcher('**/*');
28+
29+
this.watcher.onDidCreate((uri) => this.onFileSystemChanged(uri));
30+
this.watcher.onDidDelete((uri) => this.onFileSystemChanged(uri));
31+
this.watcher.onDidChange((uri) => this.onFileSystemChanged(uri));
32+
33+
// Listen for configuration changes
34+
vscode.workspace.onDidChangeConfiguration((event) => {
35+
if (event.affectsConfiguration("files2prompt.ignoredExtensions")) {
36+
this.loadIgnoredExtensions();
37+
this.refresh();
38+
}
39+
});
40+
}
41+
42+
public dispose(): void {
43+
this.watcher.dispose();
44+
}
45+
46+
private onFileSystemChanged(uri: vscode.Uri): void {
47+
this.refresh();
2248
}
2349

2450
refresh(): void {
@@ -67,17 +93,21 @@ export class FileTreeProvider implements vscode.TreeDataProvider<FileItem> {
6793
const uri = vscode.Uri.file(fullPath);
6894
const isDirectory = entry.isDirectory();
6995

70-
const isIgnored = this.isGitIgnored(relativePath);
96+
const extension = path.extname(entry.name).toLowerCase().replace(".", "");
97+
const isIgnoredExtension = this.ignoredExtensions.has(extension);
98+
const isGitIgnored = this.isGitIgnored(relativePath);
7199

72-
let checkboxState = this.checkedItems.get(fullPath);
100+
const key = fullPath;
101+
let checkboxState = this.checkedItems.get(key);
73102

74103
if (checkboxState === undefined) {
75104
const parentPath = path.dirname(fullPath);
76105
const parentCheckboxState = this.checkedItems.get(parentPath);
77106

78107
if (
79108
parentCheckboxState === vscode.TreeItemCheckboxState.Checked &&
80-
!isIgnored
109+
!isGitIgnored &&
110+
!isIgnoredExtension
81111
) {
82112
checkboxState = vscode.TreeItemCheckboxState.Checked;
83113
this.checkedItems.set(fullPath, checkboxState);
@@ -94,7 +124,7 @@ export class FileTreeProvider implements vscode.TreeDataProvider<FileItem> {
94124
: vscode.TreeItemCollapsibleState.None,
95125
isDirectory,
96126
checkboxState,
97-
isIgnored
127+
isGitIgnored || isIgnoredExtension
98128
);
99129

100130
items.push(item);
@@ -138,8 +168,11 @@ export class FileTreeProvider implements vscode.TreeDataProvider<FileItem> {
138168
const siblingPath = path.join(dirPath, entry.name);
139169
const relativePath = path.relative(this.workspaceRoot, siblingPath);
140170

141-
if (this.isGitIgnored(relativePath)) {
142-
continue; // Skip gitignored items
171+
const extension = path.extname(entry.name).toLowerCase().replace(".", "");
172+
const isIgnoredExtension = this.ignoredExtensions.has(extension);
173+
174+
if (this.isGitIgnored(relativePath) || isIgnoredExtension) {
175+
continue; // Skip gitignored items and ignored extensions
143176
}
144177

145178
hasNonIgnoredChild = true;
@@ -180,8 +213,11 @@ export class FileTreeProvider implements vscode.TreeDataProvider<FileItem> {
180213
const relativePath = path.relative(this.workspaceRoot, fullPath);
181214
const isGitIgnored = this.isGitIgnored(relativePath);
182215

183-
if (!parentIsGitIgnored && isGitIgnored) {
184-
// Skip gitignored items when parent is not gitignored
216+
const extension = path.extname(entry.name).toLowerCase().replace(".", "");
217+
const isIgnoredExtension = this.ignoredExtensions.has(extension);
218+
219+
if (!parentIsGitIgnored && (isGitIgnored || isIgnoredExtension)) {
220+
// Skip gitignored items and ignored extensions when parent is not gitignored
185221
continue;
186222
}
187223

@@ -241,6 +277,18 @@ export class FileTreeProvider implements vscode.TreeDataProvider<FileItem> {
241277
private isGitIgnored(relativePath: string): boolean {
242278
return this.gitignore.ignores(relativePath);
243279
}
280+
281+
private loadIgnoredExtensions() {
282+
const config = vscode.workspace.getConfiguration("files2prompt");
283+
const extensionsString = config.get<string>(
284+
"ignoredExtensions",
285+
"png,jpg,jpeg,gif,svg"
286+
);
287+
const extensionsArray = extensionsString
288+
.split(",")
289+
.map((ext) => ext.trim().toLowerCase());
290+
this.ignoredExtensions = new Set(extensionsArray);
291+
}
244292
}
245293

246294
export class FileItem extends vscode.TreeItem {

0 commit comments

Comments
 (0)