Skip to content

Commit

Permalink
fix: vscode#194692 has been fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
ggrossetie committed Oct 8, 2024
1 parent 65cfe9d commit f8abad0
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 43 deletions.
7 changes: 3 additions & 4 deletions src/test/workspace.test.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
import os from 'os'
import { Uri } from 'vscode'
import chai from 'chai'
import { normalizeUri } from '../util/workspace'

const expect = chai.expect

suite('Normalize URI', () => {
test('Should lowercase drive letter on Windows', async () => {
if (os.platform() === 'win32') {
const result = normalizeUri(Uri.parse('file:///C:/path/WITH/camelCase/A/b/C/index.adoc'))
const result = Uri.parse('file:///C:/path/WITH/camelCase/A/b/C/index.adoc')
expect(result.path).to.equal('/c:/path/WITH/camelCase/A/b/C/index.adoc')
}
})
test('Should do nothing since the drive letter is already lowercase', async () => {
if (os.platform() === 'win32') {
const result = normalizeUri(Uri.parse('file:///c:/path/WITH/camelCase/A/b/C/index.adoc'))
const result = Uri.parse('file:///c:/path/WITH/camelCase/A/b/C/index.adoc')
expect(result.path).to.equal('/c:/path/WITH/camelCase/A/b/C/index.adoc')
}
})
test('Should do nothing on Linux', async () => {
if (os.platform() !== 'win32') {
const result = normalizeUri(Uri.parse('/C/path/WITH/camelCase/A/b/C/index.adoc'))
const result = Uri.parse('/C/path/WITH/camelCase/A/b/C/index.adoc')
expect(result.path).to.equal('/C/path/WITH/camelCase/A/b/C/index.adoc')
}
})
Expand Down
8 changes: 4 additions & 4 deletions src/test/workspaceHelper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import vscode, { FileSystemError, FileType } from 'vscode'
import { getDefaultWorkspaceFolderUri, normalizeUri } from '../util/workspace'
import { getDefaultWorkspaceFolderUri } from '../util/workspace'
import { extensionContext } from './helper'

export async function removeFiles (files: vscode.Uri[]) {
Expand All @@ -26,7 +26,7 @@ async function exists (file: vscode.Uri): Promise<boolean> {
export async function createFile (content: string, ...pathSegments: string[]): Promise<vscode.Uri> {
const file = vscode.Uri.joinPath(getDefaultWorkspaceFolderUri(), ...pathSegments)
await vscode.workspace.fs.writeFile(file, Buffer.from(content))
return normalizeUri(file)
return file
}

export async function createDirectories (...pathSegments: string[]): Promise<void> {
Expand Down Expand Up @@ -54,7 +54,7 @@ export async function createDirectories (...pathSegments: string[]): Promise<voi
export async function createDirectory (...pathSegments: string[]): Promise<vscode.Uri> {
const dir = vscode.Uri.joinPath(getDefaultWorkspaceFolderUri(), ...pathSegments)
await vscode.workspace.fs.createDirectory(dir)
return normalizeUri(dir)
return dir
}

export async function createLink (existingPathSegments: string[], newPathSegments: string[]): Promise<vscode.Uri> {
Expand All @@ -63,7 +63,7 @@ export async function createLink (existingPathSegments: string[], newPathSegment
const existingPath = vscode.Uri.joinPath(workspaceUri, ...existingPathSegments)
const newPath = vscode.Uri.joinPath(workspaceUri, ...newPathSegments)
await fs.symlink(existingPath.fsPath, newPath.fsPath)
return normalizeUri(newPath)
return newPath
}

export async function enableAntoraSupport () {
Expand Down
40 changes: 5 additions & 35 deletions src/util/workspace.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,21 @@
import vscode, { Uri, WorkspaceFolder } from 'vscode'
import os from 'os'

const driveLetterRx = /(?<=^\/)([A-Z])(?=:\/)/

export function getWorkspaceFolder (uri: Uri): WorkspaceFolder | undefined {
const workspaceFolder = vscode.workspace.getWorkspaceFolder(uri)
if (workspaceFolder && os.platform() === 'win32') {
return {
uri: normalizeUri(workspaceFolder.uri),
name: workspaceFolder.name,
index: workspaceFolder.index,
}
}
return workspaceFolder
return vscode.workspace.getWorkspaceFolder(uri)
}

export function getWorkspaceFolders (): WorkspaceFolder[] | undefined {
return vscode.workspace.workspaceFolders?.map((workspaceFolder) => {
if (os.platform() === 'win32') {
return {
uri: normalizeUri(workspaceFolder.uri),
name: workspaceFolder.name,
index: workspaceFolder.index,
}
}
return workspaceFolder
})
export function getWorkspaceFolders (): readonly WorkspaceFolder[] {
return vscode.workspace.workspaceFolders
}

export function findDefaultWorkspaceFolderUri (): Uri | undefined {
const workspaceFolders = getWorkspaceFolders()
const workspaceFolders = vscode.workspace.workspaceFolders
if (workspaceFolders && workspaceFolders.length) {
return workspaceFolders[0].uri
}
return undefined
}

export function getDefaultWorkspaceFolderUri (): Uri | undefined {
const workspaceFolders = getWorkspaceFolders()
return normalizeUri(workspaceFolders[0].uri)
}

export function normalizeUri (uri: Uri): Uri {
// normalize Windows drive letter
// https://github.com/microsoft/vscode/issues/194692
if (os.platform() === 'win32') {
return uri.with({ path: uri.path.replace(driveLetterRx, (driverLetter) => driverLetter.toLowerCase()) })
}
return uri
return vscode.workspace.workspaceFolders[0].uri
}

0 comments on commit f8abad0

Please sign in to comment.