Skip to content

Commit

Permalink
Add preference to ignore files in workspace functions
Browse files Browse the repository at this point in the history
fixed #14448

Signed-off-by: Jonas Helming <[email protected]>
  • Loading branch information
JonasHelming committed Nov 13, 2024
1 parent d769658 commit 769428d
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 13 deletions.
4 changes: 3 additions & 1 deletion packages/ai-workspace-agent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
"@theia/navigator": "1.55.0",
"@theia/terminal": "1.55.0",
"@theia/ai-core": "1.55.0",
"@theia/ai-chat": "1.55.0"
"@theia/ai-chat": "1.55.0",
"ignore": "^6.0.0",
"minimatch": "^10.0.0"
},
"publishConfig": {
"access": "public"
Expand Down
3 changes: 3 additions & 0 deletions packages/ai-workspace-agent/src/browser/frontend-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ import { ChatAgent } from '@theia/ai-chat/lib/common';
import { Agent, ToolProvider } from '@theia/ai-core/lib/common';
import { WorkspaceAgent } from './workspace-agent';
import { FileContentFunction, GetWorkspaceDirectoryStructure, GetWorkspaceFileList, WorkspaceFunctionScope } from './functions';
import { PreferenceContribution } from '@theia/core/lib/browser';
import { WorkspacePreferencesSchema } from './workspace-preferences';

export default new ContainerModule(bind => {
bind(PreferenceContribution).toConstantValue({ schema: WorkspacePreferencesSchema });
bind(WorkspaceAgent).toSelf().inSingletonScope();
bind(Agent).toService(WorkspaceAgent);
bind(ChatAgent).toService(WorkspaceAgent);
Expand Down
85 changes: 73 additions & 12 deletions packages/ai-workspace-agent/src/browser/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,26 @@ import { FileService } from '@theia/filesystem/lib/browser/file-service';
import { FileStat } from '@theia/filesystem/lib/common/files';
import { WorkspaceService } from '@theia/workspace/lib/browser';
import { FILE_CONTENT_FUNCTION_ID, GET_WORKSPACE_DIRECTORY_STRUCTURE_FUNCTION_ID, GET_WORKSPACE_FILE_LIST_FUNCTION_ID } from '../common/functions';

import ignore from 'ignore';
import { Minimatch } from 'minimatch';
import { PreferenceService } from '@theia/core/lib/browser';
import { CONSIDER_GITIGNORE_PREF, USER_EXCLUDES_PREF } from './workspace-preferences';
@injectable()
export class WorkspaceFunctionScope {
protected readonly GITIGNORE_FILE_NAME = '.gitignore';

@inject(WorkspaceService)
protected workspaceService: WorkspaceService;

@inject(FileService)
protected fileService: FileService;

@inject(PreferenceService)
protected preferences: PreferenceService;

private gitignoreMatcher: ReturnType<typeof ignore> | undefined;
private gitignoreWatcherInitialized = false;

async getWorkspaceRoot(): Promise<URI> {
const wsRoots = await this.workspaceService.roots;
if (wsRoots.length === 0) {
Expand All @@ -39,15 +53,59 @@ export class WorkspaceFunctionScope {
throw new Error('Access outside of the workspace is not allowed');
}
}
/**
* Determines whether a given file or directory should be excluded from workspace operations.
*
* @param stat - The `FileStat` object representing the file or directory to check.
* @returns `true` if the file or directory should be excluded, `false` otherwise.
*/
shouldExclude(stat: FileStat): boolean {
const excludedFolders = ['node_modules', 'lib'];
return stat.resource.path.base.startsWith('.') || excludedFolders.includes(stat.resource.path.base);

private async initializeGitignoreWatcher(workspaceRoot: URI): Promise<void> {
if (this.gitignoreWatcherInitialized) {
return;
}

const gitignoreUri = workspaceRoot.resolve(this.GITIGNORE_FILE_NAME);
this.fileService.watch(gitignoreUri);

this.fileService.onDidFilesChange(async event => {
if (event.contains(gitignoreUri)) {
this.gitignoreMatcher = undefined;
}
});

this.gitignoreWatcherInitialized = true;
}

async shouldExclude(stat: FileStat): Promise<boolean> {
const considerGitIgnore = this.preferences.get(CONSIDER_GITIGNORE_PREF, false);
const userExcludes = this.preferences.get<string[]>(USER_EXCLUDES_PREF, []);

const fileName = stat.resource.path.base;

for (const pattern of userExcludes) {
const matcher = new Minimatch(pattern, { dot: true });
if (matcher.match(fileName)) { return true; }
}

if (considerGitIgnore) {
const workspaceRoot = await this.getWorkspaceRoot();
await this.initializeGitignoreWatcher(workspaceRoot);

const gitignoreUri = workspaceRoot.resolve(this.GITIGNORE_FILE_NAME);

try {
const fileStat = await this.fileService.resolve(gitignoreUri);
if (fileStat && !fileStat.isDirectory) {
if (!this.gitignoreMatcher) {
const gitignoreContent = await this.fileService.read(gitignoreUri);
this.gitignoreMatcher = ignore().add(gitignoreContent.value);
}
const relativePath = workspaceRoot.relative(stat.resource);
if (relativePath && this.gitignoreMatcher.ignores(relativePath.toString())) {
return true;
}
}
} catch (error) {
// If .gitignore does not exist or cannot be read, continue without error
}
}

return false;
}
}

Expand Down Expand Up @@ -88,7 +146,7 @@ export class GetWorkspaceDirectoryStructure implements ToolProvider {

if (stat && stat.isDirectory && stat.children) {
for (const child of stat.children) {
if (!child.isDirectory || this.workspaceScope.shouldExclude(child)) { continue; };
if (!child.isDirectory || await this.workspaceScope.shouldExclude(child)) { continue; };
const path = `${prefix}${child.resource.path.base}/`;
result.push(path);
result.push(...await this.buildDirectoryStructure(child.resource, `${path}`));
Expand Down Expand Up @@ -225,12 +283,15 @@ export class GetWorkspaceFileList implements ToolProvider {
const result: string[] = [];

if (stat && stat.isDirectory) {
if (this.workspaceScope.shouldExclude(stat)) {
if (await this.workspaceScope.shouldExclude(stat)) {
return result;
}
const children = await this.fileService.resolve(uri);
if (children.children) {
for (const child of children.children) {
if (await this.workspaceScope.shouldExclude(child)) {
continue;
};
const relativePath = workspaceRootUri.relative(child.resource);
if (relativePath) {
result.push(relativePath.toString());
Expand Down
41 changes: 41 additions & 0 deletions packages/ai-workspace-agent/src/browser/workspace-preferences.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// *****************************************************************************
// Copyright (C) 2024 EclipseSource GmbH.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************

import { PreferenceSchema } from '@theia/core/lib/browser/preferences/preference-contribution';

export const CONSIDER_GITIGNORE_PREF = 'ai-features.workspace-functions.considerGitIgnore';
export const USER_EXCLUDES_PREF = 'ai-features.workspace-functions.userExcludes';

export const WorkspacePreferencesSchema: PreferenceSchema = {
type: 'object',
properties: {
[CONSIDER_GITIGNORE_PREF]: {
type: 'boolean',
title: 'Consider .gitignore',
description: 'If enabled, excludes files/folders specified in a global .gitignore file (expected location is the workspace root).',
default: false
},
[USER_EXCLUDES_PREF]: {
type: 'array',
title: 'User Excludes',
description: 'List of patterns (glob or regex) for files/folders to exclude by default.',
default: ['node_modules', 'lib', '.*'],
items: {
type: 'string'
}
}
}
};
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6953,6 +6953,11 @@ ignore@^5.0.4, ignore@^5.2.0, ignore@^5.2.4:
resolved "https://registry.yarnpkg.com/ignore/-/ignore-5.3.1.tgz#5073e554cd42c5b33b394375f538b8593e34d4ef"
integrity sha512-5Fytz/IraMjqpwfd34ke28PTVMjZjJG2MPn5t7OE4eUCUNf8BAa7b5WUS9/Qvr6mwOQS7Mk6vdsMno5he+T8Xw==

ignore@^6.0.0:
version "6.0.2"
resolved "https://registry.yarnpkg.com/ignore/-/ignore-6.0.2.tgz#77cccb72a55796af1b6d2f9eb14fa326d24f4283"
integrity sha512-InwqeHHN2XpumIkMvpl/DCJVrAHgCsG5+cn1XlnLWGwtZBm8QJfSusItfrwx81CTp5agNZqpKU2J/ccC5nGT4A==

image-size@~0.5.0:
version "0.5.5"
resolved "https://registry.yarnpkg.com/image-size/-/image-size-0.5.5.tgz#09dfd4ab9d20e29eb1c3e80b8990378df9e3cb9c"
Expand Down Expand Up @@ -8474,6 +8479,13 @@ [email protected], minimatch@^9.0.0, minimatch@^9.0.1:
dependencies:
brace-expansion "^2.0.1"

minimatch@^10.0.0:
version "10.0.1"
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-10.0.1.tgz#ce0521856b453c86e25f2c4c0d03e6ff7ddc440b"
integrity sha512-ethXTt3SGGR+95gudmqJ1eNhRO7eGEGIgYA9vnPatK4/etz2MEVDno5GMCibdMTuBMyElzIlgxMna3K94XDIDQ==
dependencies:
brace-expansion "^2.0.1"

minimatch@^3.0.0, minimatch@^3.0.3, minimatch@^3.0.4, minimatch@^3.1.1, minimatch@^3.1.2:
version "3.1.2"
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.1.2.tgz#19cd194bfd3e428f049a70817c038d89ab4be35b"
Expand Down

0 comments on commit 769428d

Please sign in to comment.