forked from xdebug/vscode-php-debug
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaths.ts
More file actions
105 lines (101 loc) · 4.27 KB
/
Copy pathpaths.ts
File metadata and controls
105 lines (101 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import urlRelative = require('url-relative')
import fileUrl = require('file-url')
import * as url from 'url'
import * as path from 'path'
import { decode } from 'urlencode'
/** converts a server-side XDebug file URI to a local path for VS Code with respect to source root settings */
export function convertDebuggerPathToClient(
fileUri: string | url.Url,
pathMapping?: { [index: string]: string }
): string {
let localSourceRoot: string | undefined
let serverSourceRoot: string | undefined
if (typeof fileUri === 'string') {
fileUri = url.parse(fileUri)
}
// convert the file URI to a path
let serverPath = decode(fileUri.pathname!)
// strip the trailing slash from Windows paths (indicated by a drive letter with a colon)
const serverIsWindows = /^\/[a-zA-Z]:\//.test(serverPath)
if (serverIsWindows) {
serverPath = serverPath.substr(1)
}
if (pathMapping) {
for (const mappedServerPath of Object.keys(pathMapping)) {
const mappedLocalSource = pathMapping[mappedServerPath]
// normalize slashes for windows-to-unix
const serverRelative = (serverIsWindows ? path.win32 : path.posix).relative(mappedServerPath, serverPath)
if (serverRelative.indexOf('..') !== 0) {
serverSourceRoot = mappedServerPath
localSourceRoot = mappedLocalSource
break
}
}
}
let localPath: string
if (serverSourceRoot && localSourceRoot) {
// get the part of the path that is relative to the source root
const pathRelativeToSourceRoot = (serverIsWindows ? path.win32 : path.posix).relative(
serverSourceRoot,
serverPath
)
// resolve from the local source root
localPath = path.resolve(localSourceRoot, pathRelativeToSourceRoot)
} else {
localPath = path.normalize(serverPath)
}
return localPath
}
/** converts a local path from VS Code to a server-side XDebug file URI with respect to source root settings */
export function convertClientPathToDebugger(localPath: string, pathMapping?: { [index: string]: string }): string {
let localSourceRoot: string | undefined
let serverSourceRoot: string | undefined
// XDebug always lowercases Windows drive letters in file URIs
let localFileUri = fileUrl(localPath.replace(/^[A-Z]:\\/, match => match.toLowerCase()), { resolve: false })
let serverFileUri: string
if (pathMapping) {
for (const mappedServerPath of Object.keys(pathMapping)) {
const mappedLocalSource = pathMapping[mappedServerPath]
const localRelative = path.relative(mappedLocalSource, localPath)
if (localRelative.indexOf('..') !== 0) {
serverSourceRoot = mappedServerPath
localSourceRoot = mappedLocalSource
break
}
}
}
if (localSourceRoot) {
localSourceRoot = localSourceRoot.replace(/^[A-Z]:\\/, match => match.toLowerCase())
}
if (serverSourceRoot) {
serverSourceRoot = serverSourceRoot.replace(/^[A-Z]:\\/, match => match.toLowerCase())
}
if (serverSourceRoot && localSourceRoot) {
let localSourceRootUrl = fileUrl(localSourceRoot, { resolve: false })
if (!localSourceRootUrl.endsWith('/')) {
localSourceRootUrl += '/'
}
let serverSourceRootUrl = fileUrl(serverSourceRoot, { resolve: false })
if (!serverSourceRootUrl.endsWith('/')) {
serverSourceRootUrl += '/'
}
// get the part of the path that is relative to the source root
const urlRelativeToSourceRoot = urlRelative(localSourceRootUrl, localFileUri)
// resolve from the server source root
serverFileUri = url.resolve(serverSourceRootUrl, urlRelativeToSourceRoot)
} else {
serverFileUri = localFileUri
}
return serverFileUri
}
function isWindowsUri(path: string): boolean {
return /^file:\/\/\/[a-zA-Z]:\//.test(path)
}
export function isSameUri(clientUri: string, debuggerUri: string): boolean {
if (isWindowsUri(clientUri) || isWindowsUri(debuggerUri)) {
// compare case-insensitive on Windows
return debuggerUri.toLowerCase() === clientUri.toLowerCase()
} else {
return debuggerUri === clientUri
}
}