forked from dirac-run/dirac
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvscode-shim.ts
More file actions
349 lines (293 loc) · 9.1 KB
/
vscode-shim.ts
File metadata and controls
349 lines (293 loc) · 9.1 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/**
* VSCode namespace shim for CLI mode
* Provides minimal stubs for VSCode types and enums used by the codebase
*/
import { existsSync, readFileSync } from "node:fs"
import path from "node:path"
import pino, { type Logger } from "pino"
import { printError, printInfo, printWarning } from "./utils/display"
import { DIRAC_CLI_DIR } from "./utils/path"
export { URI } from "vscode-uri"
export { DiracFileStorage } from "@/shared/storage"
export const CLI_LOG_FILE = path.join(DIRAC_CLI_DIR.log, "dirac.1.log")
/**
* Safely read and parse a JSON file, returning a default value on failure
*/
export function readJson<T = any>(filePath: string, defaultValue: T = {} as T): T {
try {
if (existsSync(filePath)) {
return JSON.parse(readFileSync(filePath, "utf8"))
}
} catch {
// Return default if file doesn't exist or is invalid
}
return defaultValue
}
/**
* Mock environment variable collection for non-VSCode environments
*/
export class EnvironmentVariableCollection {
private variables = new Map<string, { value: string; type: string }>()
persistent = true
description = "CLI Environment Variables"
entries() {
return this.variables.entries()
}
replace(variable: string, value: string) {
this.variables.set(variable, { value, type: "replace" })
}
append(variable: string, value: string) {
this.variables.set(variable, { value, type: "append" })
}
prepend(variable: string, value: string) {
this.variables.set(variable, { value, type: "prepend" })
}
get(variable: string) {
return this.variables.get(variable)
}
forEach(callback: (variable: string, mutator: { value: string; type: string }, collection: this) => void) {
this.variables.forEach((mutator, variable) => callback(variable, mutator, this))
}
delete(variable: string) {
return this.variables.delete(variable)
}
clear() {
this.variables.clear()
}
getScoped(_scope: unknown) {
return this
}
}
// ============================================================================
// VSCode enums
// ============================================================================
export enum ExtensionMode {
Production = 1,
Development = 2,
Test = 3,
}
export enum ExtensionKind {
UI = 1,
Workspace = 2,
}
export enum DiagnosticSeverity {
Error = 0,
Warning = 1,
Information = 2,
Hint = 3,
}
export enum EndOfLine {
LF = 1,
CRLF = 2,
}
const outputChannelLoggers = new Map<string, Logger>()
function getOutputChannelLogger(channelName: string): Logger {
let logger = outputChannelLoggers.get(channelName)
if (!logger) {
const transport = pino.transport({
target: "pino-roll",
options: {
name: channelName,
file: CLI_LOG_FILE.replace(".1", ""),
mkdir: true,
frequency: "daily",
limit: { count: 5 },
},
})
logger = pino({ timestamp: pino.stdTimeFunctions.isoTime }, transport)
outputChannelLoggers.set(channelName, logger)
}
return logger
}
export class Position {
constructor(
public readonly line: number,
public readonly character: number,
) {}
compareTo(other: Position): number {
return this.line - other.line || this.character - other.character
}
isAfter(other: Position): boolean {
return this.compareTo(other) > 0
}
isAfterOrEqual(other: Position): boolean {
return this.compareTo(other) >= 0
}
isBefore(other: Position): boolean {
return this.compareTo(other) < 0
}
isBeforeOrEqual(other: Position): boolean {
return this.compareTo(other) <= 0
}
isEqual(other: Position): boolean {
return this.compareTo(other) === 0
}
translate(lineDelta = 0, characterDelta = 0): Position {
return new Position(this.line + lineDelta, this.character + characterDelta)
}
with(line?: number, character?: number): Position {
return new Position(line ?? this.line, character ?? this.character)
}
}
export class Range {
public readonly start: Position
public readonly end: Position
constructor(start: Position, end: Position)
constructor(startLine: number, startCharacter: number, endLine: number, endCharacter: number)
constructor(
startOrStartLine: Position | number,
endOrStartCharacter: Position | number,
endLine?: number,
endCharacter?: number,
) {
if (typeof startOrStartLine === "number") {
this.start = new Position(startOrStartLine, endOrStartCharacter as number)
this.end = new Position(endLine!, endCharacter!)
} else {
this.start = startOrStartLine
this.end = endOrStartCharacter as Position
}
}
get isEmpty(): boolean {
return this.start.isEqual(this.end)
}
get isSingleLine(): boolean {
return this.start.line === this.end.line
}
contains(positionOrRange: Position | Range): boolean {
if (positionOrRange instanceof Range) {
return this.contains(positionOrRange.start) && this.contains(positionOrRange.end)
}
return positionOrRange.isAfterOrEqual(this.start) && positionOrRange.isBeforeOrEqual(this.end)
}
isEqual(other: Range): boolean {
return this.start.isEqual(other.start) && this.end.isEqual(other.end)
}
intersection(range: Range): Range | undefined {
const start = this.start.isAfter(range.start) ? this.start : range.start
const end = this.end.isBefore(range.end) ? this.end : range.end
return start.isAfter(end) ? undefined : new Range(start, end)
}
union(other: Range): Range {
const start = this.start.isBefore(other.start) ? this.start : other.start
const end = this.end.isAfter(other.end) ? this.end : other.end
return new Range(start, end)
}
with(start?: Position, end?: Position): Range {
return new Range(start ?? this.start, end ?? this.end)
}
}
export class Selection extends Range {
public readonly anchor: Position
public readonly active: Position
constructor(anchor: Position, active: Position)
constructor(anchorLine: number, anchorCharacter: number, activeLine: number, activeCharacter: number)
constructor(
anchorOrAnchorLine: Position | number,
activeOrAnchorCharacter: Position | number,
activeLine?: number,
activeCharacter?: number,
) {
const anchor =
typeof anchorOrAnchorLine === "number"
? new Position(anchorOrAnchorLine, activeOrAnchorCharacter as number)
: anchorOrAnchorLine
const active =
typeof anchorOrAnchorLine === "number"
? new Position(activeLine!, activeCharacter!)
: (activeOrAnchorCharacter as Position)
const isForward = anchor.isBefore(active)
super(isForward ? anchor : active, isForward ? active : anchor)
this.anchor = anchor
this.active = active
}
get isReversed(): boolean {
return this.anchor.isAfter(this.active)
}
}
export interface CancellationToken {
isCancellationRequested: boolean
onCancellationRequested: any
}
export class EventEmitter<T> {
private listeners: Array<(e: T) => void> = []
event = (listener: (e: T) => void) => {
this.listeners.push(listener)
return {
dispose: () => {
const idx = this.listeners.indexOf(listener)
if (idx >= 0) this.listeners.splice(idx, 1)
},
}
}
fire(data: T): void {
this.listeners.forEach((listener) => listener(data))
}
dispose(): void {
this.listeners.length = 0
}
}
export class Disposable {
constructor(private callOnDispose: () => void) {}
static from(...disposables: { dispose(): any }[]): Disposable {
return new Disposable(() => disposables.forEach((d) => d.dispose()))
}
dispose(): void {
this.callOnDispose()
}
}
const noop = () => {}
const noopAsync = async () => {}
const noopDisposable = { dispose: noop }
export const workspace = {
workspaceFolders: undefined as any[] | undefined,
getWorkspaceFolder: (_uri: any) => undefined,
onDidChangeWorkspaceFolders: () => noopDisposable,
fs: {
readFile: async (_uri: any): Promise<Uint8Array> => new Uint8Array(),
writeFile: noopAsync,
delete: noopAsync,
stat: async (_uri: any) => ({ type: 1, size: 0 }),
readDirectory: async (_uri: any): Promise<any[]> => [],
createDirectory: noopAsync,
},
}
export const window = {
showInformationMessage: async (message: string) => {
printInfo(`[INFO] ${message}`)
},
showWarningMessage: async (message: string) => {
printWarning(`[WARN] ${message}`)
},
showErrorMessage: async (message: string) => {
printError(`[ERROR] ${message}`)
},
createOutputChannel: (name: string) => {
const logger = getOutputChannelLogger(name)
const log = (text: string) => logger.info({ channel: name }, text)
return { appendLine: log, append: log, clear: noop, show: noop, hide: noop, dispose: noop }
},
terminals: [] as any[],
activeTerminal: undefined as any,
createTerminal: (_options?: any) => ({
name: "CLI Terminal",
processId: Promise.resolve(process.pid),
sendText: (text: string) => printInfo(`[${new Date().toISOString()}] [Terminal] ${text}`),
show: noop,
hide: noop,
dispose: noop,
}),
}
export type ExtensionContext = any
export type Memento = any
export type SecretStorage = any
// biome-ignore lint/correctness/noUnusedVariables: placeholder
export type Extension<T> = any
// ============================================================================
// Shutdown event for graceful cleanup
// ============================================================================
/**
* Event emitter for app shutdown notification.
* Components can listen to this to clean up UI before process exit.
*/
export const shutdownEvent = new EventEmitter<void>()