/
debugSource.ts
141 lines (125 loc) · 4.29 KB
/
debugSource.ts
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as nls from 'vs/nls';
import { TPromise } from 'vs/base/common/winjs.base';
import { URI as uri } from 'vs/base/common/uri';
import * as paths from 'vs/base/common/paths';
import * as resources from 'vs/base/common/resources';
import { DEBUG_SCHEME } from 'vs/workbench/parts/debug/common/debug';
import { IRange } from 'vs/editor/common/core/range';
import { IEditorService, SIDE_GROUP, ACTIVE_GROUP } from 'vs/workbench/services/editor/common/editorService';
import { Schemas } from 'vs/base/common/network';
import { isUri } from 'vs/workbench/parts/debug/common/debugUtils';
const UNKNOWN_SOURCE_LABEL = nls.localize('unknownSource', "Unknown Source");
/**
* Debug URI format
*
* a debug URI represents a Source object and the debug session where the Source comes from.
*
* debug:arbitrary_path?session=123e4567-e89b-12d3-a456-426655440000&ref=1016
* \___/ \____________/ \__________________________________________/ \______/
* | | | |
* scheme source.path session id source.reference
*
* the arbitrary_path and the session id are encoded with 'encodeURIComponent'
*
*/
export class Source {
public readonly uri: uri;
public available: boolean;
constructor(public raw: DebugProtocol.Source, sessionId: string) {
let path: string;
if (raw) {
path = this.raw.path || this.raw.name;
this.available = true;
} else {
this.raw = { name: UNKNOWN_SOURCE_LABEL };
this.available = false;
path = `${DEBUG_SCHEME}:${UNKNOWN_SOURCE_LABEL}`;
}
if (this.raw.sourceReference > 0) {
this.uri = uri.parse(`${DEBUG_SCHEME}:${encodeURIComponent(path)}?session=${encodeURIComponent(sessionId)}&ref=${this.raw.sourceReference}`);
} else {
if (isUri(path)) {
this.uri = uri.parse(path);
} else {
// assume path
if (paths.isAbsolute_posix(path) || paths.isAbsolute_win32(path)) {
this.uri = uri.file(path);
} else {
// path is relative
// should not happen because relative paths always have a sourceReference > 0
console.error('cannot handle relative paths without sourceReference');
}
}
}
}
public get name() {
return this.raw.name || resources.basenameOrAuthority(this.uri);
}
public get origin() {
return this.raw.origin;
}
public get presentationHint() {
return this.raw.presentationHint;
}
public get reference() {
return this.raw.sourceReference;
}
public get inMemory() {
return this.uri.scheme === DEBUG_SCHEME;
}
public openInEditor(editorService: IEditorService, selection: IRange, preserveFocus?: boolean, sideBySide?: boolean, pinned?: boolean): TPromise<any> {
return !this.available ? Promise.resolve(null) : editorService.openEditor({
resource: this.uri,
description: this.origin,
options: {
preserveFocus,
selection,
revealIfVisible: true,
revealInCenterIfOutsideViewport: true,
pinned: pinned || (!preserveFocus && !this.inMemory)
}
}, sideBySide ? SIDE_GROUP : ACTIVE_GROUP);
}
public static getEncodedDebugData(modelUri: uri): { name: string, path: string, sessionId: string, sourceReference: number } {
let path: string;
let sourceReference: number;
let sessionId: string;
switch (modelUri.scheme) {
case Schemas.file:
path = paths.normalize(modelUri.fsPath, true);
break;
case DEBUG_SCHEME:
path = modelUri.path;
if (modelUri.query) {
const keyvalues = modelUri.query.split('&');
for (let keyvalue of keyvalues) {
const pair = keyvalue.split('=');
if (pair.length === 2) {
switch (pair[0]) {
case 'session':
sessionId = decodeURIComponent(pair[1]);
break;
case 'ref':
sourceReference = parseInt(pair[1]);
break;
}
}
}
}
break;
default:
path = modelUri.toString();
break;
}
return {
name: resources.basenameOrAuthority(modelUri),
path,
sourceReference,
sessionId
};
}
}