Skip to content

Commit

Permalink
Add setting to ignore file extensions; fix file watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas-McKanna-Test committed Oct 23, 2024
1 parent 6c39a1e commit 383d695
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 19 deletions.
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"displayName": "Files2Prompt",
"icon": "./files2prompt-icon.webp",
"description": "Copy file contents for LLM prompts",
"version": "1.5.0",
"version": "1.6.0",
"publisher": "thomas-mckanna",
"keywords": [
"files",
Expand Down Expand Up @@ -167,6 +167,11 @@
"type": "string",
"description": "Optional system message to include in the output.",
"default": ""
},
"files2prompt.ignoredExtensions": {
"type": "string",
"description": "Comma-separated list of file extensions to ignore during recursive selection. Example: 'png,jpg,jpeg,gif,svg'",
"default": "png,jpg,jpeg,gif,svg"
}
}
}
Expand Down
12 changes: 3 additions & 9 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export function activate(context: vscode.ExtensionContext) {
manageCheckboxStateManually: true,
});

// Add fileTreeProvider to ensure proper disposal
context.subscriptions.push(fileTreeProvider);

let history: string[][] = [];
let historyPosition: number = -1;

Expand Down Expand Up @@ -153,15 +156,6 @@ ${xmlContent}</files>`;
})
);

// Watch for file changes and refresh the tree view
const watcher = vscode.workspace.createFileSystemWatcher('**/*');

watcher.onDidCreate(uri => fileTreeProvider.refresh());
watcher.onDidDelete(uri => fileTreeProvider.refresh());
watcher.onDidChange(uri => fileTreeProvider.refresh());

context.subscriptions.push(watcher);

// Handle checkbox state changes asynchronously
treeView.onDidChangeCheckboxState(async (e) => {
for (const [item, state] of e.items) {
Expand Down
66 changes: 57 additions & 9 deletions src/fileTreeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as fs from "fs";
import * as path from "path";
import ignore from "ignore";

export class FileTreeProvider implements vscode.TreeDataProvider<FileItem> {
export class FileTreeProvider implements vscode.TreeDataProvider<FileItem>, vscode.Disposable {
private _onDidChangeTreeData: vscode.EventEmitter<
FileItem | undefined | null | void
> = new vscode.EventEmitter<FileItem | undefined | null | void>();
Expand All @@ -15,10 +15,36 @@ export class FileTreeProvider implements vscode.TreeDataProvider<FileItem> {
private workspaceRoot: string;
private checkedItems: Map<string, vscode.TreeItemCheckboxState> = new Map();
private gitignore = ignore();
private ignoredExtensions: Set<string> = new Set();
private watcher: vscode.FileSystemWatcher;

constructor(workspaceRoot: string) {
this.workspaceRoot = workspaceRoot;
this.loadGitignore();
this.loadIgnoredExtensions();

// Create a file system watcher
this.watcher = vscode.workspace.createFileSystemWatcher('**/*');

this.watcher.onDidCreate((uri) => this.onFileSystemChanged(uri));
this.watcher.onDidDelete((uri) => this.onFileSystemChanged(uri));
this.watcher.onDidChange((uri) => this.onFileSystemChanged(uri));

// Listen for configuration changes
vscode.workspace.onDidChangeConfiguration((event) => {
if (event.affectsConfiguration("files2prompt.ignoredExtensions")) {
this.loadIgnoredExtensions();
this.refresh();
}
});
}

public dispose(): void {
this.watcher.dispose();
}

private onFileSystemChanged(uri: vscode.Uri): void {
this.refresh();
}

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

const isIgnored = this.isGitIgnored(relativePath);
const extension = path.extname(entry.name).toLowerCase().replace(".", "");
const isIgnoredExtension = this.ignoredExtensions.has(extension);
const isGitIgnored = this.isGitIgnored(relativePath);

let checkboxState = this.checkedItems.get(fullPath);
const key = fullPath;
let checkboxState = this.checkedItems.get(key);

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

if (
parentCheckboxState === vscode.TreeItemCheckboxState.Checked &&
!isIgnored
!isGitIgnored &&
!isIgnoredExtension
) {
checkboxState = vscode.TreeItemCheckboxState.Checked;
this.checkedItems.set(fullPath, checkboxState);
Expand All @@ -94,7 +124,7 @@ export class FileTreeProvider implements vscode.TreeDataProvider<FileItem> {
: vscode.TreeItemCollapsibleState.None,
isDirectory,
checkboxState,
isIgnored
isGitIgnored || isIgnoredExtension
);

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

if (this.isGitIgnored(relativePath)) {
continue; // Skip gitignored items
const extension = path.extname(entry.name).toLowerCase().replace(".", "");
const isIgnoredExtension = this.ignoredExtensions.has(extension);

if (this.isGitIgnored(relativePath) || isIgnoredExtension) {
continue; // Skip gitignored items and ignored extensions
}

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

if (!parentIsGitIgnored && isGitIgnored) {
// Skip gitignored items when parent is not gitignored
const extension = path.extname(entry.name).toLowerCase().replace(".", "");
const isIgnoredExtension = this.ignoredExtensions.has(extension);

if (!parentIsGitIgnored && (isGitIgnored || isIgnoredExtension)) {
// Skip gitignored items and ignored extensions when parent is not gitignored
continue;
}

Expand Down Expand Up @@ -241,6 +277,18 @@ export class FileTreeProvider implements vscode.TreeDataProvider<FileItem> {
private isGitIgnored(relativePath: string): boolean {
return this.gitignore.ignores(relativePath);
}

private loadIgnoredExtensions() {
const config = vscode.workspace.getConfiguration("files2prompt");
const extensionsString = config.get<string>(
"ignoredExtensions",
"png,jpg,jpeg,gif,svg"
);
const extensionsArray = extensionsString
.split(",")
.map((ext) => ext.trim().toLowerCase());
this.ignoredExtensions = new Set(extensionsArray);
}
}

export class FileItem extends vscode.TreeItem {
Expand Down

0 comments on commit 383d695

Please sign in to comment.