forked from certinia/debug-log-analyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApexLogLanguageDetector.ts
More file actions
142 lines (120 loc) · 3.45 KB
/
ApexLogLanguageDetector.ts
File metadata and controls
142 lines (120 loc) · 3.45 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
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
142
/*
* Copyright (c) 2026 Certinia Inc. All rights reserved.
*/
import { closeSync, openSync, readSync } from 'node:fs';
import { extname } from 'node:path';
import {
TabInputText,
commands,
languages,
window,
workspace,
type TextDocument,
type Uri,
} from 'vscode';
import { Context } from '../Context.js';
const APEXLOG_HEADER = /^\d\d\.\d.+?APEX_CODE,\w.+$/;
const DETECT_EXTENSIONS = new Set(['.log', '.txt']);
const MAX_LINES_TO_CHECK = 100;
export function isApexLogContent(doc: TextDocument): boolean {
if (doc.lineCount === 0) {
return false;
}
const linesToCheck = Math.min(MAX_LINES_TO_CHECK, doc.lineCount);
for (let i = 0; i < linesToCheck; i++) {
if (APEXLOG_HEADER.test(doc.lineAt(i).text)) {
return true;
}
}
return false;
}
function isApexLogFile(fsPath: string): boolean {
let fd: number;
try {
fd = openSync(fsPath, 'r');
} catch {
return false;
}
try {
const buf = Buffer.alloc(4096);
const bytesRead = readSync(fd, buf, 0, 4096, 0);
const text = buf.toString('utf8', 0, bytesRead);
const lines = text.split('\n');
const linesToCheck = Math.min(MAX_LINES_TO_CHECK, lines.length);
for (let i = 0; i < linesToCheck; i++) {
if (APEXLOG_HEADER.test(lines[i] ?? '')) {
return true;
}
}
return false;
} finally {
closeSync(fd);
}
}
function hasDetectExtension(uri: Uri): boolean {
return DETECT_EXTENSIONS.has(extname(uri.fsPath).toLowerCase());
}
function getActiveTabUri(): Uri | undefined {
const activeTab = window.tabGroups.activeTabGroup.activeTab;
if (activeTab?.input instanceof TabInputText) {
return activeTab.input.uri;
}
return undefined;
}
function updateContextKey(): void {
const editor = window.activeTextEditor;
if (editor && editor.document.uri.scheme === 'file') {
const doc = editor.document;
if (hasDetectExtension(doc.uri)) {
const detected = isApexLogContent(doc);
commands.executeCommand('setContext', 'lana.isApexLog', detected);
return;
}
commands.executeCommand('setContext', 'lana.isApexLog', false);
return;
}
// Fallback to tab API for large files where activeTextEditor is undefined
const tabUri = getActiveTabUri();
if (tabUri && tabUri.scheme === 'file' && hasDetectExtension(tabUri)) {
const detected = isApexLogFile(tabUri.fsPath);
commands.executeCommand('setContext', 'lana.isApexLog', detected);
return;
}
commands.executeCommand('setContext', 'lana.isApexLog', false);
}
export class ApexLogLanguageDetector {
static apply(context: Context): void {
for (const doc of workspace.textDocuments) {
detectAndSetLanguage(doc);
}
context.context.subscriptions.push(
workspace.onDidOpenTextDocument((doc) => {
detectAndSetLanguage(doc);
}),
);
// Update context key when the active editor or tab changes
context.context.subscriptions.push(
window.onDidChangeActiveTextEditor(() => {
updateContextKey();
}),
);
context.context.subscriptions.push(
window.tabGroups.onDidChangeTabs(() => {
updateContextKey();
}),
);
// Set initial context
updateContextKey();
}
}
function detectAndSetLanguage(doc: TextDocument): void {
if (doc.languageId === 'apexlog' || doc.uri.scheme !== 'file') {
return;
}
if (!hasDetectExtension(doc.uri)) {
return;
}
if (isApexLogContent(doc)) {
languages.setTextDocumentLanguage(doc, 'apexlog');
}
}