@@ -4,13 +4,7 @@ import { type CodyIgnoreFeature, showCodyIgnoreNotification } from './notificati
44
55type IgnoreRecord = Record < string , boolean >
66
7- interface CachedExcludeData {
8- gitignoreExclude : IgnoreRecord
9- ignoreExclude : IgnoreRecord
10- sgignoreExclude : IgnoreRecord
11- }
12-
13- const excludeCache = new Map < string , CachedExcludeData > ( )
7+ const excludeCache = new Map < string , IgnoreRecord > ( )
148const fileWatchers = new Map < string , vscode . FileSystemWatcher > ( )
159
1610function getCacheKey ( workspaceFolder : vscode . WorkspaceFolder | null ) : string {
@@ -27,26 +21,21 @@ export async function initializeCache(workspaceFolder: vscode.WorkspaceFolder |
2721 . getConfiguration ( '' , workspaceFolder )
2822 . get < boolean > ( 'search.useIgnoreFiles' )
2923
30- let gitignoreExclude : IgnoreRecord = { }
31- let ignoreExclude : IgnoreRecord = { }
3224 let sgignoreExclude : IgnoreRecord = { }
3325
3426 if ( useIgnoreFiles && workspaceFolder ) {
35- gitignoreExclude = await readIgnoreFile ( vscode . Uri . joinPath ( workspaceFolder . uri , '.gitignore' ) )
36- ignoreExclude = await readIgnoreFile ( vscode . Uri . joinPath ( workspaceFolder . uri , '.ignore' ) )
3727 sgignoreExclude = await readIgnoreFile (
38- vscode . Uri . joinPath ( workspaceFolder . uri , '.sourcegraph ' , '. ignore' )
28+ vscode . Uri . joinPath ( workspaceFolder . uri , '.cody ' , 'ignore' )
3929 )
4030
41- setupFileWatcher ( workspaceFolder , '.gitignore' )
42- setupFileWatcher ( workspaceFolder , '.ignore' )
43- setupFileWatcher ( workspaceFolder , '.sourcegraph/.ignore' )
31+ setupFileWatcher ( workspaceFolder )
4432 }
4533
46- excludeCache . set ( cacheKey , { gitignoreExclude , ignoreExclude , sgignoreExclude } )
34+ excludeCache . set ( cacheKey , sgignoreExclude )
4735}
4836
49- function setupFileWatcher ( workspaceFolder : vscode . WorkspaceFolder , filename : string ) : void {
37+ function setupFileWatcher ( workspaceFolder : vscode . WorkspaceFolder ) : void {
38+ const filename = '.cody/ignore'
5039 const watcherKey = `${ workspaceFolder . uri . toString ( ) } :${ filename } `
5140 if ( fileWatchers . has ( watcherKey ) ) {
5241 return
@@ -57,35 +46,17 @@ function setupFileWatcher(workspaceFolder: vscode.WorkspaceFolder, filename: str
5746
5847 const updateCache = async ( ) => {
5948 const cacheKey = getCacheKey ( workspaceFolder )
60- const cached = excludeCache . get ( cacheKey )
61- if ( ! cached ) return
6249
6350 const fileUri = vscode . Uri . joinPath ( workspaceFolder . uri , filename )
6451 const ignoreData = await readIgnoreFile ( fileUri )
65-
66- if ( filename === '.gitignore' ) {
67- cached . gitignoreExclude = ignoreData
68- } else if ( filename === '.ignore' ) {
69- cached . ignoreExclude = ignoreData
70- } else if ( filename === '.sourcegraph/.ignore' ) {
71- cached . sgignoreExclude = ignoreData
72- }
52+ excludeCache . set ( cacheKey , ignoreData )
7353 }
7454
7555 watcher . onDidChange ( updateCache )
7656 watcher . onDidCreate ( updateCache )
7757 watcher . onDidDelete ( ( ) => {
7858 const cacheKey = getCacheKey ( workspaceFolder )
79- const cached = excludeCache . get ( cacheKey )
80- if ( ! cached ) return
81-
82- if ( filename === '.gitignore' ) {
83- cached . gitignoreExclude = { }
84- } else if ( filename === '.ignore' ) {
85- cached . ignoreExclude = { }
86- } else if ( filename === '.sourcegraph/.ignore' ) {
87- cached . sgignoreExclude = { }
88- }
59+ excludeCache . delete ( cacheKey )
8960 } )
9061
9162 fileWatchers . set ( watcherKey , watcher )
@@ -102,22 +73,17 @@ export async function getExcludePattern(
10273
10374 const cacheKey = getCacheKey ( workspaceFolder )
10475 const cached = excludeCache . get ( cacheKey )
105- const gitignoreExclude = cached ?. gitignoreExclude ?? { }
106- const ignoreExclude = cached ?. ignoreExclude ?? { }
107- const sgignoreExclude = cached ?. sgignoreExclude ?? { }
108-
76+ const sgignoreExclude = cached ?? { }
10977 const mergedExclude : IgnoreRecord = {
11078 ...filesExclude ,
11179 ...searchExclude ,
112- ...gitignoreExclude ,
113- ...ignoreExclude ,
11480 ...sgignoreExclude ,
11581 }
11682 const excludePatterns = Object . keys ( mergedExclude ) . filter ( key => mergedExclude [ key ] === true )
11783 return `{${ excludePatterns . join ( ',' ) } }`
11884}
11985
120- async function readIgnoreFile ( uri : vscode . Uri ) : Promise < IgnoreRecord > {
86+ export async function readIgnoreFile ( uri : vscode . Uri ) : Promise < IgnoreRecord > {
12187 const ignore : IgnoreRecord = { }
12288 try {
12389 const data = await vscode . workspace . fs . readFile ( uri )
@@ -126,13 +92,19 @@ async function readIgnoreFile(uri: vscode.Uri): Promise<IgnoreRecord> {
12692 continue
12793 }
12894
129- // Strip comment and trailing whitespace.
130- line = line . replace ( / \s * ( # .* ) ? $ / , '' )
95+ // Strip comment and whitespace.
96+ line = line . replace ( / \s * ( # .* ) ? $ / , '' ) . trim ( )
13197
13298 if ( line === '' ) {
13399 continue
134100 }
135101
102+ // Replace , with . that contain commas to avoid typos for entries such as
103+ // *,something
104+ if ( line . includes ( ',' ) ) {
105+ line = line . replace ( ',' , '.' )
106+ }
107+
136108 if ( line . endsWith ( '/' ) ) {
137109 line = line . slice ( 0 , - 1 )
138110 }
0 commit comments