-
Notifications
You must be signed in to change notification settings - Fork 298
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
speed up & respect ignores in finding workspace files for file @-ment…
…ions (#3433)
- Loading branch information
Showing
8 changed files
with
157 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import type { ContextItem } from '@sourcegraph/cody-shared' | ||
import type * as vscode from 'vscode' | ||
import { | ||
getFileContextFiles, | ||
getOpenTabsContextFile, | ||
getSymbolContextFiles, | ||
} from '../../editor/utils/editor-context' | ||
|
||
export async function getChatContextItemsForMention( | ||
query: string, | ||
cancellationToken: vscode.CancellationToken, | ||
telemetryRecorder?: { | ||
empty: () => void | ||
withType: (type: 'symbol' | 'file') => void | ||
} | ||
): Promise<ContextItem[]> { | ||
// Logging: log when the at-mention starts, and then log when we know the type (after the 1st | ||
// character is typed). Don't log otherwise because we would be logging prefixes of the same | ||
// query repeatedly, which is not needed. | ||
const queryType = query.startsWith('#') ? 'symbol' : 'file' | ||
if (query === '') { | ||
telemetryRecorder?.empty() | ||
} else if (query.length === 1) { | ||
telemetryRecorder?.withType(queryType) | ||
} | ||
|
||
if (query.length === 0) { | ||
return getOpenTabsContextFile() | ||
} | ||
|
||
const MAX_RESULTS = 20 | ||
if (query.startsWith('#')) { | ||
// It would be nice if the VS Code symbols API supports cancellation, but it doesn't | ||
return getSymbolContextFiles(query.slice(1), MAX_RESULTS) | ||
} | ||
return getFileContextFiles(query, MAX_RESULTS, cancellationToken) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import * as vscode from 'vscode' | ||
|
||
/** | ||
* Find all files in all workspace folders, respecting the user's `files.exclude`, `search.exclude`, | ||
* and other exclude settings. The intent is to match the files shown by VS Code's built-in `Go to | ||
* File...` command. | ||
*/ | ||
export async function findWorkspaceFiles( | ||
cancellationToken: vscode.CancellationToken | ||
): Promise<vscode.Uri[]> { | ||
return ( | ||
await Promise.all( | ||
(vscode.workspace.workspaceFolders ?? [null]).map(async workspaceFolder => | ||
vscode.workspace.findFiles( | ||
workspaceFolder ? new vscode.RelativePattern(workspaceFolder, '**') : '', | ||
await getExcludePattern(workspaceFolder), | ||
undefined, | ||
cancellationToken | ||
) | ||
) | ||
) | ||
).flat() | ||
} | ||
|
||
type IgnoreRecord = Record<string, boolean> | ||
|
||
async function getExcludePattern(workspaceFolder: vscode.WorkspaceFolder | null): Promise<string> { | ||
const config = vscode.workspace.getConfiguration('', workspaceFolder) | ||
const filesExclude = config.get<IgnoreRecord>('files.exclude', {}) | ||
const searchExclude = config.get<IgnoreRecord>('search.exclude', {}) | ||
|
||
const useIgnoreFiles = config.get<boolean>('search.useIgnoreFiles') | ||
const gitignoreExclude = | ||
useIgnoreFiles && workspaceFolder | ||
? await readIgnoreFile(vscode.Uri.joinPath(workspaceFolder.uri, '.gitignore')) | ||
: {} | ||
const ignoreExclude = | ||
useIgnoreFiles && workspaceFolder | ||
? await readIgnoreFile(vscode.Uri.joinPath(workspaceFolder.uri, '.ignore')) | ||
: {} | ||
|
||
const mergedExclude: IgnoreRecord = { | ||
...filesExclude, | ||
...searchExclude, | ||
...gitignoreExclude, | ||
...ignoreExclude, | ||
} | ||
const excludePatterns = Object.keys(mergedExclude).filter(key => mergedExclude[key] === true) | ||
return `{${excludePatterns.join(',')}}` | ||
} | ||
|
||
async function readIgnoreFile(uri: vscode.Uri): Promise<IgnoreRecord> { | ||
const ignore: IgnoreRecord = {} | ||
try { | ||
const data = await vscode.workspace.fs.readFile(uri) | ||
for (let line of Buffer.from(data).toString('utf-8').split('\n')) { | ||
if (line.startsWith('!')) { | ||
continue | ||
} | ||
|
||
// Strip comment and trailing whitespace. | ||
line = line.replace(/\s*(#.*)?$/, '') | ||
|
||
if (line === '') { | ||
continue | ||
} | ||
|
||
if (line.endsWith('/')) { | ||
line = line.slice(0, -1) | ||
} | ||
if (!line.startsWith('/') && !line.startsWith('**/')) { | ||
line = `**/${line}` | ||
} | ||
ignore[line] = true | ||
} | ||
} catch {} | ||
return ignore | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters