src/vs/base/common/observableInternal/debugName.ts

148 LOC · 130 covered · 18 uncovered · 33 ranges · 6771 concepts · 5 introducers · 3467 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 > /*--------------------------------------------------------------------------------------------- devToolsLogger.ts ×24
2 > * Copyright (c) Microsoft Corporation. All rights reserved.
3 > * Licensed under the MIT License. See License.txt in the project root for license information.
4 > *--------------------------------------------------------------------------------------------*/
5 >
6 > export interface IDebugNameData {
7 > /**
8 > * The owner object of an observable.
9 > * Used for debugging only, such as computing a name for the observable by iterating over the fields of the owner.
10 > */
11 > readonly owner?: DebugOwner | undefined;
12 >
13 > /**
14 > * A string or function that returns a string that represents the name of the observable.
15 > * Used for debugging only.
16 > */
17 > readonly debugName?: DebugNameSource | undefined;
18 >
19 > /**
20 > * A function that points to the defining function of the object.
21 > * Used for debugging only.
22 > */
23 > readonly debugReferenceFn?: Function | undefined;
24 > }
25 >
26 > export class DebugNameData {
27 > constructor(
28 > public readonly owner: DebugOwner | undefined, debugLocation.ts ×2
29 > public readonly debugNameSource: DebugNameSource | undefined,
30 > public readonly referenceFn: Function | undefined,
31 > ) { }
33 > public getDebugName(target: object): string | undefined {
34 > return getDebugName(target, this); debugName.ts ×9
35 > }
37 >
38 > /**
39 > * The owning object of an observable.
40 > * Is only used for debugging purposes, such as computing a name for the observable by iterating over the fields of the owner.
41 > */
42 > export type DebugOwner = object | undefined;
43 > export type DebugNameSource = string | (() => string | undefined);
44 >
45 > const countPerName = new Map<string, number>();
46 > const cachedDebugName = new WeakMap<object, string>();
47 >
48 > export function getDebugName(target: object, data: DebugNameData): string | undefined {
49 > const cached = cachedDebugName.get(target); debugName.ts ×9
50 > if (cached) {
51 > return cached; consoleObservableLogger.ts ×8
52 > }
54 > const dbgName = computeDebugName(target, data);
55 > if (dbgName) {
56 > let count = countPerName.get(dbgName) ?? 0; consoleObservableLogger.ts ×8
57 > count++;
58 > countPerName.set(dbgName, count);
59 > const result = count === 1 ? dbgName : `${dbgName}#${count}`;
60 > cachedDebugName.set(target, result);
61 > return result;
62 > }
63 > return undefined; debugName.ts ×9
64 > }
66 > function computeDebugName(self: object, data: DebugNameData): string | undefined { debugName.ts ×9
67 > const cached = cachedDebugName.get(self);
68 > if (cached) {
69 return cached;
70 }
72 > const ownerStr = data.owner ? formatOwner(data.owner) + `.` : '';
73 >
74 > let result: string | undefined;
75 > const debugNameSource = data.debugNameSource;
76 > if (debugNameSource !== undefined) {
77 > if (typeof debugNameSource === 'function') { consoleObservableLogger.ts ×8
78 result = debugNameSource();
79 if (result !== undefined) {
80 return ownerStr + result;
81 }
83 > return ownerStr + debugNameSource;
84 > }
85 > }
87 > const referenceFn = data.referenceFn;
88 > if (referenceFn !== undefined) {
89 > result = getFunctionName(referenceFn); consoleObservableLogger.ts ×8
90 > if (result !== undefined) {
91 return ownerStr + result;
92 }
95 > if (data.owner !== undefined) {
96 > const key = findKey(data.owner, self); chatModel.ts ×13
97 > if (key !== undefined) {
98 return ownerStr + key;
99 }
101 > return undefined; debugName.ts ×9
102 > }
104 > function findKey(obj: object, value: object): string | undefined { chatModel.ts ×13
105 > for (const key in obj) {
106 > if ((obj as Record<string, unknown>)[key] === value) {
107 return key;
108 }
110 > return undefined;
111 > }
113 > const countPerClassName = new Map<string, number>();
114 > const ownerId = new WeakMap<object, string>();
115 >
116 > function formatOwner(owner: object): string { chatModel.ts ×13
117 > const id = ownerId.get(owner);
118 > if (id) {
119 return id;
120 }
121 > const className = getClassName(owner) ?? 'Object'; chatModel.ts ×13
122 > let count = countPerClassName.get(className) ?? 0;
123 > count++;
124 > countPerClassName.set(className, count);
125 > const result = count === 1 ? className : `${className}#${count}`;
126 > ownerId.set(owner, result);
127 > return result;
128 > }
130 > export function getClassName(obj: object): string | undefined {
131 > const ctor = obj.constructor; chatModel.ts ×13
132 > if (ctor) {
133 > if (ctor.name === 'Object') {
134 return undefined;
135 }
136 > return ctor.name; chatModel.ts ×13
137 > }
138 return undefined;
139 }
141 > export function getFunctionName(fn: Function): string | undefined {
142 > const fnSrc = fn.toString(); consoleObservableLogger.ts ×8
143 > // Pattern: /** @description ... */
144 > const regexp = /\/\*\*\s*@description\s*([^*]*)\*\//;
145 > const match = regexp.exec(fnSrc);
146 > const result = match ? match[1] : undefined;
147 > return result?.trim();
148 > }