forked from lessweb/deepcode-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.ts
More file actions
153 lines (128 loc) · 4.19 KB
/
state.ts
File metadata and controls
153 lines (128 loc) · 4.19 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
import * as path from "path";
import { posixPathToWindowsPath } from "./shell-utils";
export type FileLineEnding = "LF" | "CRLF";
export type FileState = {
filePath: string;
content: string;
timestamp: number;
offset?: number;
limit?: number;
isPartialView?: boolean;
encoding?: BufferEncoding;
lineEndings?: FileLineEnding;
};
export type FileSnippet = {
id: string;
filePath: string;
startLine: number;
endLine: number;
preview: string;
};
const fileStatesBySession = new Map<string, Map<string, FileState>>();
const snippetsBySession = new Map<string, Map<string, FileSnippet>>();
const snippetCountersBySession = new Map<string, number>();
export function normalizeFilePath(filePath: string, platform: NodeJS.Platform = process.platform): string {
const nativePath = normalizeNativeFilePath(filePath, platform);
return platform === "win32" ? path.win32.normalize(nativePath) : path.normalize(nativePath);
}
export function normalizeNativeFilePath(filePath: string, platform: NodeJS.Platform = process.platform): string {
if (platform !== "win32") {
return filePath;
}
if (isGitBashAbsolutePath(filePath)) {
return posixPathToWindowsPath(filePath);
}
return filePath;
}
export function isAbsoluteFilePath(filePath: string, platform: NodeJS.Platform = process.platform): boolean {
const nativePath = normalizeNativeFilePath(filePath, platform);
if (platform !== "win32") {
return path.isAbsolute(nativePath);
}
const normalized = path.win32.normalize(nativePath);
return path.win32.isAbsolute(normalized) && (/^[A-Za-z]:[\\/]/.test(normalized) || /^\\\\/.test(normalized));
}
function isGitBashAbsolutePath(filePath: string): boolean {
return /^\/[A-Za-z](?:\/|$)/.test(filePath) || /^\/cygdrive\/[A-Za-z](?:\/|$)/.test(filePath);
}
export function recordFileState(sessionId: string, state: FileState): void {
if (!sessionId || !state.filePath) {
return;
}
let sessionState = fileStatesBySession.get(sessionId);
if (!sessionState) {
sessionState = new Map<string, FileState>();
fileStatesBySession.set(sessionId, sessionState);
}
const normalizedPath = normalizeFilePath(state.filePath);
sessionState.set(normalizedPath, {
...state,
filePath: normalizedPath,
});
}
export function markFileRead(
sessionId: string,
filePath: string,
state: Omit<FileState, "filePath"> | null = null
): void {
if (!sessionId || !filePath) {
return;
}
recordFileState(sessionId, {
filePath,
content: state?.content ?? "",
timestamp: state?.timestamp ?? 0,
offset: state?.offset,
limit: state?.limit,
isPartialView: state?.isPartialView,
encoding: state?.encoding,
lineEndings: state?.lineEndings,
});
}
export function getFileState(sessionId: string, filePath: string): FileState | null {
if (!sessionId || !filePath) {
return null;
}
return fileStatesBySession.get(sessionId)?.get(normalizeFilePath(filePath)) ?? null;
}
export function wasFileRead(sessionId: string, filePath: string): boolean {
return getFileState(sessionId, filePath) !== null;
}
export function isFullFileView(state: FileState | null): boolean {
return Boolean(
state && !state.isPartialView && typeof state.offset === "undefined" && typeof state.limit === "undefined"
);
}
export function createSnippet(
sessionId: string,
filePath: string,
startLine: number,
endLine: number,
preview: string
): FileSnippet | null {
if (!sessionId || !filePath || startLine < 1 || endLine < startLine) {
return null;
}
const nextCounter = (snippetCountersBySession.get(sessionId) ?? 0) + 1;
snippetCountersBySession.set(sessionId, nextCounter);
const snippet: FileSnippet = {
id: `snippet_${nextCounter}`,
filePath: normalizeFilePath(filePath),
startLine,
endLine,
preview,
};
let snippets = snippetsBySession.get(sessionId);
if (!snippets) {
snippets = new Map<string, FileSnippet>();
snippetsBySession.set(sessionId, snippets);
}
snippets.set(snippet.id, snippet);
return snippet;
}
export function getSnippet(sessionId: string, snippetId: string): FileSnippet | null {
if (!sessionId || !snippetId) {
return null;
}
return snippetsBySession.get(sessionId)?.get(snippetId) ?? null;
}