opencode/packages/util/src/path.ts

149 lines
4.8 KiB
TypeScript

export function getFilename(path: string | undefined) {
if (!path) return ""
const trimmed = path.replace(/[/\\]+$/, "")
const index = Math.max(trimmed.lastIndexOf("/"), trimmed.lastIndexOf("\\"))
return trimmed.slice(index + 1)
}
export function getDirectory(path: string | undefined) {
if (!path) return ""
const trimmed = path.replace(/[/\\]+$/, "")
const parts = trimmed.split(/[/\\]/)
return parts.slice(0, parts.length - 1).join("/") + "/"
}
export function getFilenameTruncated(path: string | undefined, maxLength = 20) {
const filename = getFilename(path)
if (filename.length <= maxLength) return filename
const lastDot = filename.lastIndexOf(".")
const extension = lastDot <= 0 ? "" : filename.slice(lastDot)
const available = maxLength - extension.length - 1
if (available <= 0) return filename.slice(0, maxLength - 1) + "…"
return filename.slice(0, available) + "…" + extension
}
export function truncateMiddle(text: string, maxLength = 20) {
if (maxLength <= 0) return ""
if (text.length <= maxLength) return text
const available = maxLength - 1
const start = Math.ceil(available / 2)
const end = Math.floor(available / 2)
if (end <= 0) return text.slice(0, start) + "…"
return text.slice(0, start) + "…" + text.slice(-end)
}
const isDrive = (value: string) => {
if (value.length !== 2) return false
const code = value.charCodeAt(0)
return value[1] === ":" && ((code >= 65 && code <= 90) || (code >= 97 && code <= 122))
}
const trimTrailingSlashes = (value: string) => {
for (let i = value.length - 1; i >= 0; i--) {
if (value[i] !== "/") return value.slice(0, i + 1)
}
return ""
}
const isWindowsPath = (value: string) => value[1] === ":" || value.startsWith("\\\\")
/** A comparable form of a directory path: forward slashes for Windows paths, no trailing slash except on roots. */
export function comparablePath(path: string) {
const value = isWindowsPath(path) ? path.replaceAll("\\", "/") : path
const trimmed = trimTrailingSlashes(value)
if (!trimmed && value.startsWith("/")) return "/"
if (isDrive(trimmed)) return `${trimmed}/`
return trimmed
}
/** `child` is `parent` or inside it. Windows drive and UNC paths compare case-insensitively. */
export function containsDirectory(parent: string, child: string) {
const normalize = (value: string) => {
const key = comparablePath(value)
return /^[a-z]:\//i.test(key) || key.startsWith("//") ? key.toLowerCase() : key
}
const root = normalize(parent)
const target = normalize(child)
return target === root || target.startsWith(root.endsWith("/") ? root : `${root}/`)
}
export function sameDirectory(a: string, b: string) {
return containsDirectory(a, b) && containsDirectory(b, a)
}
export function encodeFilePath(filepath: string): string {
// Normalize Windows paths: convert backslashes to forward slashes
const normalized = filepath.replace(/\\/g, "/")
// Handle Windows absolute paths (D:/path -> /D:/path for proper file:// URLs)
const rooted = /^[A-Za-z]:/.test(normalized) ? "/" + normalized : normalized
// Encode each path segment (preserving forward slashes as path separators)
// Keep the colon in Windows drive letters (`/C:/...`) so downstream file URL parsers
// can reliably detect drives.
return rooted
.split("/")
.map((segment, index) => {
if (index === 1 && /^[A-Za-z]:$/.test(segment)) return segment
return encodeURIComponent(segment)
})
.join("/")
}
const MAX_LINE_NUMBER = 1_000_000
const lineHashPattern = /^#L(\d+)(?:C\d+)?(?:-L?(\d+)(?:C\d+)?)?$/i
const lineHashSuffixPattern = /^(.*?)#L(\d+)(?:C\d+)?(?:-L?(\d+)(?:C\d+)?)?$/i
const lineColonSuffixPattern = /^(.*?):(\d+)(?::\d+)?(?:-(\d+)(?::\d+)?)?:?$/
function toLineRange(first: number, second: number) {
if (
!Number.isSafeInteger(first) ||
!Number.isSafeInteger(second) ||
first < 1 ||
second < 1 ||
first > MAX_LINE_NUMBER ||
second > MAX_LINE_NUMBER
) {
return undefined
}
return {
start: Math.min(first, second),
end: Math.max(first, second),
}
}
export function isLineRangeHash(hash: string) {
return lineHashPattern.test(hash)
}
export function parsePathLineSuffix(input: string) {
const hashMatch = input.match(lineHashSuffixPattern)
if (hashMatch) {
const first = Number(hashMatch[2])
const second = hashMatch[3] ? Number(hashMatch[3]) : first
return {
path: hashMatch[1] ?? "",
selection: toLineRange(first, second),
}
}
const colonMatch = input.match(lineColonSuffixPattern)
if (colonMatch && !/^[a-z]:?$/i.test(colonMatch[1] ?? "")) {
const first = Number(colonMatch[2])
const second = colonMatch[3] ? Number(colonMatch[3]) : first
return {
path: colonMatch[1] ?? "",
selection: toLineRange(first, second),
}
}
return { path: input, selection: undefined }
}