Skip to content

Commit 0d5a024

Browse files
committed
Tidy up path special case implementation
1 parent ea33fa1 commit 0d5a024

File tree

1 file changed

+13
-18
lines changed

1 file changed

+13
-18
lines changed

src/vs/workbench/contrib/terminalContrib/chatAgentTools/browser/commandLineAutoApprover.ts

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import { Disposable } from '../../../../../base/common/lifecycle.js';
77
import type { OperatingSystem } from '../../../../../base/common/platform.js';
8-
import { regExpLeadsToEndlessLoop } from '../../../../../base/common/strings.js';
8+
import { escapeRegExpCharacters, regExpLeadsToEndlessLoop } from '../../../../../base/common/strings.js';
99
import { isObject } from '../../../../../base/common/types.js';
1010
import { structuralEquals } from '../../../../../base/common/equals.js';
1111
import { IConfigurationService, type IConfigurationValue } from '../../../../../platform/configuration/common/configuration.js';
@@ -281,27 +281,22 @@ export class CommandLineAutoApprover extends Disposable {
281281
return neverMatchRegex;
282282
}
283283

284-
// Check if this looks like a path (contains path separators)
284+
let sanitizedValue: string;
285+
286+
// Match both path separators it if looks like a path
285287
if (value.includes('/') || value.includes('\\')) {
286-
// Handle path-like strings with flexible separator and optional ./ prefix matching
287-
288-
// Replace path separators with placeholders first, before escaping
289-
let pathPattern = value.replace(/[/\\]/g, '§PATH_SEP§');
290-
291-
// Now escape all regex special characters
292-
pathPattern = pathPattern.replace(/[\\^$.*+?()[\]{}|]/g, '\\$&');
293-
294-
// Replace placeholders with character class that matches both / and \
295-
pathPattern = pathPattern.replace(/§PATH_SEP§/g, '[/\\\\]');
296-
297-
// Create pattern that optionally matches ./ or .\ at the start
298-
const finalPattern = `^(?:\\.[/\\\\])?${pathPattern}\\b`;
299-
300-
return new RegExp(finalPattern);
288+
// Replace path separators with placeholders first, apply standard sanitization, then
289+
// apply special path handling
290+
let pattern = value.replace(/[/\\]/g, '%%PATH_SEP%%');
291+
pattern = escapeRegExpCharacters(pattern);
292+
pattern = pattern.replace(/%%PATH_SEP%%*/g, '[/\\\\]');
293+
sanitizedValue = `^(?:\\.[/\\\\])?${pattern}`;
301294
}
302295

303296
// Escape regex special characters for non-path strings
304-
const sanitizedValue = value.replace(/[\\^$.*+?()[\]{}|]/g, '\\$&');
297+
else {
298+
sanitizedValue = escapeRegExpCharacters(value);
299+
}
305300

306301
// Regular strings should match the start of the command line and be a word boundary
307302
return new RegExp(`^${sanitizedValue}\\b`);

0 commit comments

Comments
 (0)