src/vs/platform/agentHost/common/commandLineHelpers.ts
126 LOC · 120 covered · 6 uncovered · 27 ranges · 1895 concepts · 18 introducers · 899 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
/*---------------------------------------------------------------------------------------------
commandLineHelpers.ts ×3
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { extUriBiasedIgnorePathCase } from '../../../base/common/resources.js';
import { URI } from '../../../base/common/uri.js';
/**
* Result of {@link extractCdPrefix}: the directory the `cd` jumps to and the
* remaining command after the chain operator.
*/
export interface IExtractedCdPrefix {
readonly directory: string;
readonly command: string;
}
/**
* Extracts a `cd <dir> &&` (or PowerShell equivalent) prefix from a command
* line, returning the directory and remaining command. Does not check whether
* the directory matches anything — callers do that comparison themselves.
*
* The separator between the `cd` and the remaining command may be `&&` or a
* newline (bash treats a bare newline as a command separator like `;`).
* PowerShell additionally accepts `;`. The remaining command may span multiple
* lines (the model frequently emits `cd <dir>` on its own line followed by a
* multi-line script).
*
* Recognized forms:
* - bash: `cd <dir> && <suffix>`, `cd <dir>\n<suffix>`
* - powershell: `cd <dir> && <suffix>`, `cd <dir>; <suffix>`, `cd <dir>\n<suffix>`
* `cd /d <dir> && <suffix>`, `cd /d <dir>; <suffix>`
* `Set-Location <dir> && <suffix>`, `Set-Location <dir>; <suffix>`
* `Set-Location -Path <dir> && <suffix>`, `Set-Location -Path <dir>; <suffix>`
*
* Surrounding double quotes around `<dir>` are stripped.
*/
export function extractCdPrefix(commandLine: string, isPowerShell: boolean): IExtractedCdPrefix | undefined {
isPowerShell
? /^(?:cd(?: \/d)?|Set-Location(?: -Path)?) (?<dir>"[^"]*"|[^\s]+) *(?:&&|;|\r?\n)\s*(?<suffix>[\s\S]+)$/i
commandLineHelpers.ts ×1
const cdDir = cdPrefixMatch?.groups?.dir;
const cdSuffix = cdPrefixMatch?.groups?.suffix;
if (cdDir && cdSuffix) {
if (cdDirPath.startsWith('"') && cdDirPath.endsWith('"')) {
}
}
}
/**
* If `toolName` is a shell tool (`bash` or `powershell`) and
* `parameters.command` starts with a `cd <workingDirectory> && …` (or
* PowerShell equivalent) prefix, mutate `parameters.command` to drop the
* prefix and return `true`. Returns `false` otherwise.
*
* Path comparison normalizes trailing slashes and is case-insensitive on
* Windows.
*/
export function stripRedundantCdPrefix(
parameters: Record<string, unknown> | undefined,
workingDirectory: URI | undefined,
): boolean {
if (!workingDirectory || !parameters) {
}
const isPowerShell = toolName === 'powershell';
}
if (typeof command !== 'string') {
}
if (!extracted) {
}
}
return true;
}
/**
* Compares an extracted `cd <dir>` argument (a raw filesystem path string,
* possibly using either `/` or `\` separators) to a working-directory URI.
* Normalizes separators by routing the extracted string through `URI.file`,
* which converts to the platform-native `fsPath` shape, so that e.g.
* `cd C:/repo` matches a working directory of `C:\repo` on Windows.
*
* Path comparison uses {@link extUriBiasedIgnorePathCase}, which is
* case-insensitive on Windows / macOS.
*/
function sameDirectory(extractedDir: string, workingDirectory: URI): boolean {
commandLineHelpers.ts ×5
if (!extractedDir) {
return false;
}
// Strip trailing path separators (either flavor) so e.g. `/repo/project/`
commandLineHelpers.ts ×5
// matches `/repo/project`. Without this, URI.file would preserve the
// trailing slash and the URIs would not compare equal. We do this for
// both sides because the working directory may also end in a separator.
const trim = (p: string) => p.replace(/[\\/]+$/, '');
const trimmedExtracted = trim(extractedDir);
const trimmedWd = trim(workingDirectory.fsPath);
if (!trimmedExtracted || !trimmedWd) {
return false;
}
let wdUri: URI;
try {
extractedUri = URI.file(trimmedExtracted);
wdUri = URI.file(trimmedWd);
} catch {
return false;
}
}