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
65 lines (60 loc) · 2.74 KB
/
Copy pathpaths.ts
File metadata and controls
65 lines (60 loc) · 2.74 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
import urlRelative = require('url-relative');
import fileUrl = require('file-url');
import * as url from 'url';
import * as path from 'path';
/** 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, localSourceRoot?: string, serverSourceRoot?: string): string {
if (typeof fileUri === 'string') {
fileUri = url.parse(fileUri);
}
// convert the file URI to a path
let serverPath = decodeURI(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);
}
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, localSourceRoot?: string, serverSourceRoot?: string): string {
let localFileUri = fileUrl(localPath, {resolve: false});
let serverFileUri: string;
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;
}
}