Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/util/vs/base/common/observableInternal/debugLocation.ts
13406 views
1
//!!! DO NOT modify, this file was COPIED from 'microsoft/vscode'
2
3
/*---------------------------------------------------------------------------------------------
4
* Copyright (c) Microsoft Corporation. All rights reserved.
5
* Licensed under the MIT License. See License.txt in the project root for license information.
6
*--------------------------------------------------------------------------------------------*/
7
8
export type DebugLocation = DebugLocationImpl | undefined;
9
10
export namespace DebugLocation {
11
let enabled = false;
12
13
export function enable(): void {
14
enabled = true;
15
}
16
17
export function ofCaller(): DebugLocation {
18
if (!enabled) {
19
return undefined;
20
}
21
const Err = Error as ErrorConstructor & { stackTraceLimit: number };
22
23
const l = Err.stackTraceLimit;
24
Err.stackTraceLimit = 3;
25
const stack = new Error().stack!;
26
Err.stackTraceLimit = l;
27
28
return DebugLocationImpl.fromStack(stack, 2);
29
}
30
}
31
32
class DebugLocationImpl implements ILocation {
33
public static fromStack(stack: string, parentIdx: number): DebugLocationImpl | undefined {
34
const lines = stack.split('\n');
35
const location = parseLine(lines[parentIdx + 1]);
36
if (location) {
37
return new DebugLocationImpl(
38
location.fileName,
39
location.line,
40
location.column,
41
location.id
42
);
43
} else {
44
return undefined;
45
}
46
}
47
48
constructor(
49
public readonly fileName: string,
50
public readonly line: number,
51
public readonly column: number,
52
public readonly id: string,
53
) {
54
}
55
}
56
57
58
export interface ILocation {
59
fileName: string;
60
line: number;
61
column: number;
62
id: string;
63
}
64
65
function parseLine(stackLine: string): ILocation | undefined {
66
const match = stackLine.match(/\((.*):(\d+):(\d+)\)/);
67
if (match) {
68
return {
69
fileName: match[1],
70
line: parseInt(match[2]),
71
column: parseInt(match[3]),
72
id: stackLine,
73
};
74
}
75
76
const match2 = stackLine.match(/at ([^\(\)]*):(\d+):(\d+)/);
77
78
if (match2) {
79
return {
80
fileName: match2[1],
81
line: parseInt(match2[2]),
82
column: parseInt(match2[3]),
83
id: stackLine,
84
};
85
}
86
87
return undefined;
88
}
89
90