Use safe path comparison

This commit is contained in:
Saoud Rizwan
2024-09-22 20:05:38 -04:00
parent 69e681ebce
commit b5ca470ebf
5 changed files with 42 additions and 33 deletions

View File

@@ -1,6 +1,7 @@
import * as path from "path"
import * as os from "os"
import * as vscode from "vscode"
import { arePathsEqual } from "./path-helpers"
export async function openImage(dataUri: string) {
const matches = dataUri.match(/^data:image\/([a-zA-Z]+);base64,(.+)$/)
@@ -27,7 +28,7 @@ export async function openFile(absolutePath: string) {
try {
for (const group of vscode.window.tabGroups.all) {
const existingTab = group.tabs.find(
(tab) => tab.input instanceof vscode.TabInputText && tab.input.uri.fsPath === uri.fsPath
(tab) => tab.input instanceof vscode.TabInputText && arePathsEqual(tab.input.uri.fsPath, uri.fsPath)
)
if (existingTab) {
const activeColumn = vscode.window.activeTextEditor?.viewColumn

View File

@@ -46,30 +46,30 @@ String.prototype.toPosix = function (this: string): string {
}
// Safe path comparison that works across different platforms
// export function arePathsEqual(path1?: string, path2?: string): boolean {
// if (!path1 && !path2) {
// return true
// }
// if (!path1 || !path2) {
// return false
// }
export function arePathsEqual(path1?: string, path2?: string): boolean {
if (!path1 && !path2) {
return true
}
if (!path1 || !path2) {
return false
}
// path1 = normalizePath(path1)
// path2 = normalizePath(path2)
path1 = normalizePath(path1)
path2 = normalizePath(path2)
// if (process.platform === "win32") {
// return path1.toLowerCase() === path2.toLowerCase()
// }
// return path1 === path2
// }
if (process.platform === "win32") {
return path1.toLowerCase() === path2.toLowerCase()
}
return path1 === path2
}
// function normalizePath(p: string): string {
// // normalize resolve ./.. segments, removes duplicate slashes, and standardizes path separators
// let normalized = path.normalize(p)
// // however it doesn't remove trailing slashes
// // remove trailing slash, except for root paths
// if (normalized.length > 1 && (normalized.endsWith("/") || normalized.endsWith("\\"))) {
// normalized = normalized.slice(0, -1)
// }
// return normalized
// }
function normalizePath(p: string): string {
// normalize resolve ./.. segments, removes duplicate slashes, and standardizes path separators
let normalized = path.normalize(p)
// however it doesn't remove trailing slashes
// remove trailing slash, except for root paths
if (normalized.length > 1 && (normalized.endsWith("/") || normalized.endsWith("\\"))) {
normalized = normalized.slice(0, -1)
}
return normalized
}