-
-
Notifications
You must be signed in to change notification settings - Fork 183
Expand file tree
/
Copy pathlogpoint.ts
More file actions
53 lines (48 loc) · 1.75 KB
/
logpoint.ts
File metadata and controls
53 lines (48 loc) · 1.75 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
import stringReplaceAsync = require('string-replace-async')
import { isWindowsUri } from './paths'
export class LogPointManager {
private _logpoints = new Map<string, Map<number, string>>()
public addLogPoint(fileUri: string, lineNumber: number, logMessage: string) {
if (isWindowsUri(fileUri)) {
fileUri = fileUri.toLowerCase()
}
if (!this._logpoints.has(fileUri)) {
this._logpoints.set(fileUri, new Map<number, string>())
}
this._logpoints.get(fileUri)!.set(lineNumber, logMessage)
}
public clearFromFile(fileUri: string) {
if (isWindowsUri(fileUri)) {
fileUri = fileUri.toLowerCase()
}
if (this._logpoints.has(fileUri)) {
this._logpoints.get(fileUri)!.clear()
}
}
public hasLogPoint(fileUri: string, lineNumber: number): boolean {
if (isWindowsUri(fileUri)) {
fileUri = fileUri.toLowerCase()
}
return this._logpoints.has(fileUri) && this._logpoints.get(fileUri)!.has(lineNumber)
}
public async resolveExpressions(
fileUri: string,
lineNumber: number,
callback: (expr: string) => Promise<string>
): Promise<string> {
if (isWindowsUri(fileUri)) {
fileUri = fileUri.toLowerCase()
}
if (!this.hasLogPoint(fileUri, lineNumber)) {
return Promise.reject('Logpoint not found')
}
const expressionRegex = /\{(.*?)\}/gm
return await stringReplaceAsync(
this._logpoints.get(fileUri)!.get(lineNumber)!,
expressionRegex,
function (_: string, group: string) {
return group.length === 0 ? Promise.resolve('') : callback(group)
}
)
}
}