@innovatorved/code-lab 0.1.6 → 0.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ARCHITECTURE.md +34 -16
- package/README.md +17 -7
- package/dist/code-lab.css +26 -0
- package/dist/code-lab.d.ts +271 -1
- package/dist/code-lab.esm.js +2909 -166
- package/dist/code-lab.js +2869 -164
- package/dist/csp.d.ts +86 -0
- package/dist/diagnostics.d.ts +43 -0
- package/dist/extensions/guided-lab.esm.js +66 -0
- package/dist/extensions/guided-lab.js +66 -0
- package/dist/html.d.ts +24 -0
- package/dist/index.d.ts +9 -1
- package/dist/logger.d.ts +60 -0
- package/dist/preview-sw.js +425 -11
- package/dist/runtime.d.ts +6 -27
- package/dist/runtime.esm.js +171 -31
- package/dist/runtime.js +171 -31
- package/dist/schemas.d.ts +366 -25
- package/dist/schemas.esm.js +73 -0
- package/dist/schemas.js +73 -0
- package/dist/scm.d.ts +77 -0
- package/dist/search.d.ts +75 -0
- package/dist/shell.d.ts +18 -47
- package/dist/shell.esm.js +236 -37
- package/dist/shell.js +236 -37
- package/dist/storage.d.ts +57 -0
- package/dist/terminal.d.ts +11 -87
- package/dist/terminal.esm.js +124 -7
- package/dist/terminal.js +124 -7
- package/dist/zip.d.ts +42 -0
- package/package.json +11 -3
- package/src/code-lab.css +26 -0
- package/src/csp.ts +396 -0
- package/src/diagnostics.ts +116 -0
- package/src/html.ts +88 -0
- package/src/index.ts +39 -1
- package/src/logger.ts +139 -0
- package/src/preview-sw.js +97 -11
- package/src/runtime.ts +132 -105
- package/src/schemas.ts +94 -1
- package/src/scm.ts +268 -0
- package/src/search.ts +389 -0
- package/src/shell.ts +410 -369
- package/src/storage.ts +377 -0
- package/src/terminal.ts +898 -918
- package/src/workbench.ts +926 -89
- package/src/zip.ts +326 -0
- package/src/runtime.js +0 -8229
- package/src/shell.js +0 -7257
- package/src/terminal.js +0 -1046
- package/src/workbench.js +0 -7353
package/src/scm.ts
ADDED
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @innovatorved/code-lab — Interactive Git Source Control (SCM) Engine
|
|
3
|
+
* ============================================================================
|
|
4
|
+
* Pure TypeScript in-browser Git status, staging, unstaging, and commit engine
|
|
5
|
+
* backed by workspace VFS state. Powers both the interactive Source Control UI
|
|
6
|
+
* and shell/CLI Git operations.
|
|
7
|
+
*
|
|
8
|
+
* Strict leaf module: fully typed, zero explicit any, zero empty catches.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
export type GitFileChangeType = 'modified' | 'added' | 'deleted' | 'untracked';
|
|
12
|
+
|
|
13
|
+
export interface GitFileStatus {
|
|
14
|
+
path: string;
|
|
15
|
+
status: GitFileChangeType;
|
|
16
|
+
staged: boolean;
|
|
17
|
+
oldContent?: string;
|
|
18
|
+
newContent?: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface GitCommitRecord {
|
|
22
|
+
hash: string;
|
|
23
|
+
message: string;
|
|
24
|
+
timestamp: number;
|
|
25
|
+
author?: string;
|
|
26
|
+
files: Record<string, string>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface GitState {
|
|
30
|
+
branch: string;
|
|
31
|
+
branches?: string[];
|
|
32
|
+
headCommit: string;
|
|
33
|
+
headFiles: Record<string, string>;
|
|
34
|
+
staged: Map<string, string | null>;
|
|
35
|
+
commits: GitCommitRecord[];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface GitStatusResult {
|
|
39
|
+
branch: string;
|
|
40
|
+
headCommit: string;
|
|
41
|
+
untracked: GitFileStatus[];
|
|
42
|
+
modified: GitFileStatus[];
|
|
43
|
+
deleted: GitFileStatus[];
|
|
44
|
+
staged: GitFileStatus[];
|
|
45
|
+
unstaged: GitFileStatus[];
|
|
46
|
+
hasChanges: boolean;
|
|
47
|
+
isClean: boolean;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Computes a deterministic 7-character short hash for a commit payload.
|
|
52
|
+
*/
|
|
53
|
+
export function computeShortCommitHash(input: string): string {
|
|
54
|
+
let h1 = 0xdeadbeef ^ input.length;
|
|
55
|
+
let h2 = 0x41c6ce57 ^ input.length;
|
|
56
|
+
for (let i = 0; i < input.length; i++) {
|
|
57
|
+
const ch = input.charCodeAt(i);
|
|
58
|
+
h1 = Math.imul(h1 ^ ch, 2654435761);
|
|
59
|
+
h2 = Math.imul(h2 ^ ch, 1597334677);
|
|
60
|
+
}
|
|
61
|
+
h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ Math.imul(h2 ^ (h2 >>> 13), 3266489909);
|
|
62
|
+
h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ Math.imul(h1 ^ (h1 >>> 13), 3266489909);
|
|
63
|
+
const combined = 4294967296 * (2097151 & h2) + (h1 >>> 0);
|
|
64
|
+
return combined.toString(16).padStart(7, '0').slice(0, 7);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Initializes a fresh in-memory Git repository state from initial VFS files.
|
|
69
|
+
*/
|
|
70
|
+
export function createGitState(
|
|
71
|
+
initialFiles: Record<string, string> = {},
|
|
72
|
+
branch = 'main'
|
|
73
|
+
): GitState {
|
|
74
|
+
const headFiles: Record<string, string> = { ...initialFiles };
|
|
75
|
+
const initialHash = computeShortCommitHash(JSON.stringify(headFiles) + ':init');
|
|
76
|
+
return {
|
|
77
|
+
branch,
|
|
78
|
+
branches: [branch],
|
|
79
|
+
headCommit: initialHash,
|
|
80
|
+
headFiles,
|
|
81
|
+
staged: new Map<string, string | null>(),
|
|
82
|
+
commits: [
|
|
83
|
+
{
|
|
84
|
+
hash: initialHash,
|
|
85
|
+
message: 'Initial commit',
|
|
86
|
+
timestamp: Date.now(),
|
|
87
|
+
files: { ...headFiles }
|
|
88
|
+
}
|
|
89
|
+
]
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Computes working-tree and staging-area Git status against `gitState`.
|
|
95
|
+
*/
|
|
96
|
+
export function computeGitStatus(
|
|
97
|
+
vfsFiles: Record<string, string>,
|
|
98
|
+
_repoRoot = '/workspace',
|
|
99
|
+
gitState?: GitState
|
|
100
|
+
): GitStatusResult {
|
|
101
|
+
const state = gitState || createGitState({});
|
|
102
|
+
const headFiles = state.headFiles || {};
|
|
103
|
+
const currentStaged = state.staged instanceof Map ? state.staged : new Map<string, string | null>();
|
|
104
|
+
|
|
105
|
+
const untracked: GitFileStatus[] = [];
|
|
106
|
+
const modified: GitFileStatus[] = [];
|
|
107
|
+
const deleted: GitFileStatus[] = [];
|
|
108
|
+
const staged: GitFileStatus[] = [];
|
|
109
|
+
|
|
110
|
+
for (const [filePath, stagedContent] of currentStaged.entries()) {
|
|
111
|
+
const inHead = Object.prototype.hasOwnProperty.call(headFiles, filePath);
|
|
112
|
+
if (stagedContent === null) {
|
|
113
|
+
staged.push({
|
|
114
|
+
path: filePath,
|
|
115
|
+
status: 'deleted',
|
|
116
|
+
staged: true,
|
|
117
|
+
oldContent: headFiles[filePath]
|
|
118
|
+
});
|
|
119
|
+
} else {
|
|
120
|
+
staged.push({
|
|
121
|
+
path: filePath,
|
|
122
|
+
status: inHead ? 'modified' : 'added',
|
|
123
|
+
staged: true,
|
|
124
|
+
oldContent: inHead ? headFiles[filePath] : undefined,
|
|
125
|
+
newContent: stagedContent
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
for (const [filePath, content] of Object.entries(vfsFiles || {})) {
|
|
131
|
+
if (currentStaged.has(filePath)) {
|
|
132
|
+
const stagedVal = currentStaged.get(filePath);
|
|
133
|
+
if (stagedVal !== content) {
|
|
134
|
+
modified.push({
|
|
135
|
+
path: filePath,
|
|
136
|
+
status: 'modified',
|
|
137
|
+
staged: false,
|
|
138
|
+
oldContent: stagedVal ?? undefined,
|
|
139
|
+
newContent: content
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
} else if (Object.prototype.hasOwnProperty.call(headFiles, filePath)) {
|
|
143
|
+
if (headFiles[filePath] !== content) {
|
|
144
|
+
modified.push({
|
|
145
|
+
path: filePath,
|
|
146
|
+
status: 'modified',
|
|
147
|
+
staged: false,
|
|
148
|
+
oldContent: headFiles[filePath],
|
|
149
|
+
newContent: content
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
} else {
|
|
153
|
+
untracked.push({
|
|
154
|
+
path: filePath,
|
|
155
|
+
status: 'untracked',
|
|
156
|
+
staged: false,
|
|
157
|
+
newContent: content
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
for (const filePath of Object.keys(headFiles)) {
|
|
163
|
+
if (
|
|
164
|
+
!Object.prototype.hasOwnProperty.call(vfsFiles, filePath) &&
|
|
165
|
+
!currentStaged.has(filePath)
|
|
166
|
+
) {
|
|
167
|
+
deleted.push({
|
|
168
|
+
path: filePath,
|
|
169
|
+
status: 'deleted',
|
|
170
|
+
staged: false,
|
|
171
|
+
oldContent: headFiles[filePath]
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const unstaged: GitFileStatus[] = [...modified, ...deleted];
|
|
177
|
+
const isClean =
|
|
178
|
+
untracked.length === 0 &&
|
|
179
|
+
modified.length === 0 &&
|
|
180
|
+
deleted.length === 0 &&
|
|
181
|
+
staged.length === 0;
|
|
182
|
+
|
|
183
|
+
return {
|
|
184
|
+
branch: state.branch || 'main',
|
|
185
|
+
headCommit: state.headCommit || '',
|
|
186
|
+
untracked,
|
|
187
|
+
modified,
|
|
188
|
+
deleted,
|
|
189
|
+
staged,
|
|
190
|
+
unstaged,
|
|
191
|
+
hasChanges: !isClean,
|
|
192
|
+
isClean
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Stages a file addition, modification, or deletion (`currentContent === null`).
|
|
198
|
+
*/
|
|
199
|
+
export function stageFile(
|
|
200
|
+
gitState: GitState,
|
|
201
|
+
relativePath: string,
|
|
202
|
+
currentContent: string | null
|
|
203
|
+
): void {
|
|
204
|
+
if (!gitState) return;
|
|
205
|
+
if (!(gitState.staged instanceof Map)) {
|
|
206
|
+
gitState.staged = new Map<string, string | null>();
|
|
207
|
+
}
|
|
208
|
+
gitState.staged.set(relativePath, currentContent === undefined ? null : currentContent);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Removes a file from the staging index.
|
|
213
|
+
*/
|
|
214
|
+
export function unstageFile(gitState: GitState, relativePath: string): void {
|
|
215
|
+
if (!gitState || !(gitState.staged instanceof Map)) return;
|
|
216
|
+
gitState.staged.delete(relativePath);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Commits all staged changes to `gitState`, advancing `headCommit` and `headFiles`,
|
|
221
|
+
* and clearing the staging area. Throws if message is empty or staging area is empty.
|
|
222
|
+
*/
|
|
223
|
+
export function commitChanges(gitState: GitState, message: string): string {
|
|
224
|
+
const trimmed = typeof message === 'string' ? message.trim() : '';
|
|
225
|
+
if (!trimmed) {
|
|
226
|
+
throw new Error('Commit message cannot be empty');
|
|
227
|
+
}
|
|
228
|
+
if (!gitState || !(gitState.staged instanceof Map) || gitState.staged.size === 0) {
|
|
229
|
+
throw new Error('Nothing to commit (staging area is empty)');
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const newHeadFiles: Record<string, string> = { ...(gitState.headFiles || {}) };
|
|
233
|
+
for (const [filePath, content] of gitState.staged.entries()) {
|
|
234
|
+
if (content === null) {
|
|
235
|
+
delete newHeadFiles[filePath];
|
|
236
|
+
} else {
|
|
237
|
+
newHeadFiles[filePath] = content;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
const timestamp = Date.now();
|
|
242
|
+
const hash = computeShortCommitHash(`${gitState.headCommit}:${trimmed}:${timestamp}`);
|
|
243
|
+
const commit: GitCommitRecord = {
|
|
244
|
+
hash,
|
|
245
|
+
message: trimmed,
|
|
246
|
+
timestamp,
|
|
247
|
+
files: newHeadFiles
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
if (!Array.isArray(gitState.commits)) {
|
|
251
|
+
gitState.commits = [];
|
|
252
|
+
}
|
|
253
|
+
gitState.commits.push(commit);
|
|
254
|
+
gitState.headCommit = hash;
|
|
255
|
+
gitState.headFiles = newHeadFiles;
|
|
256
|
+
gitState.staged.clear();
|
|
257
|
+
|
|
258
|
+
return hash;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
export default {
|
|
262
|
+
createGitState,
|
|
263
|
+
computeGitStatus,
|
|
264
|
+
stageFile,
|
|
265
|
+
unstageFile,
|
|
266
|
+
commitChanges,
|
|
267
|
+
computeShortCommitHash
|
|
268
|
+
};
|
package/src/search.ts
ADDED
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @innovatorved/code-lab — High-Performance Cross-File Search & Replace Core
|
|
3
|
+
* ============================================================================
|
|
4
|
+
* Pure TypeScript implementation of workspace-wide text search and atomic multi-file
|
|
5
|
+
* replacements. Supports regex, whole-word, case-sensitive matching, glob filtering,
|
|
6
|
+
* line/column coordinate extraction, and match highlight snippets.
|
|
7
|
+
*
|
|
8
|
+
* Strict leaf module: fully typed, zero explicit any, zero empty catches.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
export interface SearchOptions {
|
|
12
|
+
query?: string;
|
|
13
|
+
caseSensitive?: boolean;
|
|
14
|
+
wholeWord?: boolean;
|
|
15
|
+
regex?: boolean;
|
|
16
|
+
isRegex?: boolean;
|
|
17
|
+
include?: string;
|
|
18
|
+
includeGlob?: string;
|
|
19
|
+
includePattern?: string | string[];
|
|
20
|
+
exclude?: string;
|
|
21
|
+
excludeGlob?: string;
|
|
22
|
+
excludePattern?: string | string[];
|
|
23
|
+
maxMatches?: number;
|
|
24
|
+
maxMatchesPerFile?: number;
|
|
25
|
+
targetFiles?: string[];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface SearchMatchItem {
|
|
29
|
+
line: number;
|
|
30
|
+
col: number;
|
|
31
|
+
column?: number;
|
|
32
|
+
length: number;
|
|
33
|
+
matchText?: string;
|
|
34
|
+
lineText: string;
|
|
35
|
+
beforeSnippet?: string;
|
|
36
|
+
afterSnippet?: string;
|
|
37
|
+
matchIndex?: number;
|
|
38
|
+
matchEndIndex?: number;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface SearchMatchGroup {
|
|
42
|
+
file: string;
|
|
43
|
+
matches: SearchMatchItem[];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface SearchResult extends Array<SearchMatchGroup> {
|
|
47
|
+
matches: SearchMatchGroup[];
|
|
48
|
+
totalMatches: number;
|
|
49
|
+
fileCount: number;
|
|
50
|
+
filesCount: number;
|
|
51
|
+
query: string;
|
|
52
|
+
durationMs?: number;
|
|
53
|
+
error?: string;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface ReplaceResult {
|
|
57
|
+
updatedFiles: Record<string, string>;
|
|
58
|
+
replacedCount: number;
|
|
59
|
+
filesAffected: string[];
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Escapes regex special characters in a literal string.
|
|
64
|
+
*/
|
|
65
|
+
export function escapeRegex(text: string): string {
|
|
66
|
+
return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Normalizes a virtual workspace path (POSIX forward-slashes, no leading ./ or /).
|
|
71
|
+
*/
|
|
72
|
+
export function normalizeSearchPath(filePath: string): string {
|
|
73
|
+
let p = filePath.replace(/\\/g, '/');
|
|
74
|
+
while (p.startsWith('./')) {
|
|
75
|
+
p = p.slice(2);
|
|
76
|
+
}
|
|
77
|
+
while (p.startsWith('/')) {
|
|
78
|
+
p = p.slice(1);
|
|
79
|
+
}
|
|
80
|
+
return p;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Matches a file path against a glob or literal substring filter.
|
|
85
|
+
*/
|
|
86
|
+
export function matchesGlob(filePath: string, pattern: string): boolean {
|
|
87
|
+
if (!pattern || pattern === '*' || pattern === '**') return true;
|
|
88
|
+
const normPath = normalizeSearchPath(filePath);
|
|
89
|
+
const normPattern = normalizeSearchPath(pattern);
|
|
90
|
+
|
|
91
|
+
if (normPattern === normPath) return true;
|
|
92
|
+
|
|
93
|
+
// Plain substring match if no wildcards present
|
|
94
|
+
if (!normPattern.includes('*') && !normPattern.includes('?')) {
|
|
95
|
+
return normPath.includes(normPattern) || normPath.endsWith(normPattern);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Convert glob pattern to regular expression
|
|
99
|
+
let regexStr = '^';
|
|
100
|
+
let i = 0;
|
|
101
|
+
while (i < normPattern.length) {
|
|
102
|
+
const c = normPattern[i];
|
|
103
|
+
if (c === '*') {
|
|
104
|
+
if (normPattern[i + 1] === '*') {
|
|
105
|
+
regexStr += '.*';
|
|
106
|
+
i += 2;
|
|
107
|
+
if (normPattern[i] === '/') {
|
|
108
|
+
i++;
|
|
109
|
+
}
|
|
110
|
+
continue;
|
|
111
|
+
} else {
|
|
112
|
+
regexStr += '[^/]*';
|
|
113
|
+
i++;
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
} else if (c === '?') {
|
|
117
|
+
regexStr += '[^/]';
|
|
118
|
+
i++;
|
|
119
|
+
continue;
|
|
120
|
+
} else if ('./+^$()|[]{}\\'.includes(c)) {
|
|
121
|
+
regexStr += '\\' + c;
|
|
122
|
+
i++;
|
|
123
|
+
continue;
|
|
124
|
+
} else {
|
|
125
|
+
regexStr += c;
|
|
126
|
+
i++;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
regexStr += '$';
|
|
130
|
+
|
|
131
|
+
try {
|
|
132
|
+
const re = new RegExp(regexStr);
|
|
133
|
+
return re.test(normPath);
|
|
134
|
+
} catch (err) {
|
|
135
|
+
void err;
|
|
136
|
+
return normPath.includes(normPattern.replace(/[*?]/g, ''));
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function checkFilter(path: string, filter?: string | string[]): boolean {
|
|
141
|
+
if (!filter) return true;
|
|
142
|
+
if (Array.isArray(filter)) {
|
|
143
|
+
return filter.some((f) => matchesGlob(path, f));
|
|
144
|
+
}
|
|
145
|
+
return matchesGlob(path, filter);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function createSearchResult(
|
|
149
|
+
groups: SearchMatchGroup[],
|
|
150
|
+
totalMatches: number,
|
|
151
|
+
fileCount: number,
|
|
152
|
+
query: string,
|
|
153
|
+
error?: string
|
|
154
|
+
): SearchResult {
|
|
155
|
+
const arr = [...groups];
|
|
156
|
+
const result = Object.assign(arr, {
|
|
157
|
+
matches: groups,
|
|
158
|
+
totalMatches,
|
|
159
|
+
fileCount,
|
|
160
|
+
filesCount: fileCount,
|
|
161
|
+
query,
|
|
162
|
+
durationMs: 0,
|
|
163
|
+
error
|
|
164
|
+
});
|
|
165
|
+
return result as SearchResult;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Searches across a map of virtual files for matching text or regex patterns.
|
|
170
|
+
*/
|
|
171
|
+
export function searchInFiles(
|
|
172
|
+
files: Record<string, string>,
|
|
173
|
+
queryOrOptions: string | SearchOptions,
|
|
174
|
+
optionsOrUndefined?: SearchOptions
|
|
175
|
+
): SearchResult {
|
|
176
|
+
let query = '';
|
|
177
|
+
let opts: SearchOptions = {};
|
|
178
|
+
|
|
179
|
+
if (typeof queryOrOptions === 'string') {
|
|
180
|
+
query = queryOrOptions;
|
|
181
|
+
opts = optionsOrUndefined || {};
|
|
182
|
+
} else if (queryOrOptions && typeof queryOrOptions === 'object') {
|
|
183
|
+
opts = queryOrOptions;
|
|
184
|
+
query = opts.query || '';
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (!query) {
|
|
188
|
+
return createSearchResult([], 0, 0, query);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const isCaseSensitive = Boolean(opts.caseSensitive);
|
|
192
|
+
const isWholeWord = Boolean(opts.wholeWord);
|
|
193
|
+
const isRegex = Boolean(opts.regex || opts.isRegex);
|
|
194
|
+
const maxMatches = opts.maxMatches ?? 10000;
|
|
195
|
+
const maxMatchesPerFile = opts.maxMatchesPerFile ?? 1000;
|
|
196
|
+
|
|
197
|
+
const includeFilter = opts.include || opts.includeGlob || opts.includePattern;
|
|
198
|
+
const excludeFilter = opts.exclude || opts.excludeGlob || opts.excludePattern;
|
|
199
|
+
|
|
200
|
+
let patternStr = isRegex ? query : escapeRegex(query);
|
|
201
|
+
if (isWholeWord) {
|
|
202
|
+
patternStr = `\\b${patternStr}\\b`;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
let regex: RegExp = /(?:)/;
|
|
206
|
+
try {
|
|
207
|
+
regex = new RegExp(patternStr, isCaseSensitive ? 'g' : 'gi');
|
|
208
|
+
} catch (err) {
|
|
209
|
+
const _err = err;
|
|
210
|
+
const msg =
|
|
211
|
+
typeof _err === 'object' && _err && 'message' in _err
|
|
212
|
+
? String((_err as { message: unknown }).message)
|
|
213
|
+
: 'Invalid regular expression';
|
|
214
|
+
return createSearchResult([], 0, 0, query, msg);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const matchedGroups: SearchMatchGroup[] = [];
|
|
218
|
+
let totalMatches = 0;
|
|
219
|
+
|
|
220
|
+
for (const [filePath, content] of Object.entries(files)) {
|
|
221
|
+
if (includeFilter && !checkFilter(filePath, includeFilter)) continue;
|
|
222
|
+
if (excludeFilter && checkFilter(filePath, excludeFilter)) continue;
|
|
223
|
+
if (opts.targetFiles && !opts.targetFiles.includes(filePath)) continue;
|
|
224
|
+
|
|
225
|
+
const fileMatches: SearchMatchItem[] = [];
|
|
226
|
+
const lines = content.split('\n');
|
|
227
|
+
|
|
228
|
+
for (let lineIndex = 0; lineIndex < lines.length; lineIndex++) {
|
|
229
|
+
const lineText = lines[lineIndex];
|
|
230
|
+
regex.lastIndex = 0;
|
|
231
|
+
let m: RegExpExecArray | null = null;
|
|
232
|
+
|
|
233
|
+
while ((m = regex.exec(lineText)) !== null) {
|
|
234
|
+
const matchLen = m[0].length;
|
|
235
|
+
const col = m.index + 1;
|
|
236
|
+
fileMatches.push({
|
|
237
|
+
line: lineIndex + 1,
|
|
238
|
+
col,
|
|
239
|
+
column: col,
|
|
240
|
+
length: matchLen,
|
|
241
|
+
matchText: m[0],
|
|
242
|
+
lineText,
|
|
243
|
+
beforeSnippet: lineText.slice(0, m.index),
|
|
244
|
+
afterSnippet: lineText.slice(m.index + matchLen),
|
|
245
|
+
matchIndex: m.index,
|
|
246
|
+
matchEndIndex: m.index + matchLen
|
|
247
|
+
});
|
|
248
|
+
totalMatches++;
|
|
249
|
+
|
|
250
|
+
if (fileMatches.length >= maxMatchesPerFile || totalMatches >= maxMatches) {
|
|
251
|
+
break;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// Prevent infinite loop on zero-width match
|
|
255
|
+
if (matchLen === 0) {
|
|
256
|
+
regex.lastIndex++;
|
|
257
|
+
if (regex.lastIndex > lineText.length) {
|
|
258
|
+
break;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
if (fileMatches.length >= maxMatchesPerFile || totalMatches >= maxMatches) {
|
|
264
|
+
break;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
if (fileMatches.length > 0) {
|
|
269
|
+
matchedGroups.push({
|
|
270
|
+
file: filePath,
|
|
271
|
+
matches: fileMatches
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
if (totalMatches >= maxMatches) {
|
|
276
|
+
break;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
return createSearchResult(matchedGroups, totalMatches, matchedGroups.length, query);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Replaces occurrences of a query pattern across virtual workspace files.
|
|
285
|
+
*/
|
|
286
|
+
export function replaceInFiles(
|
|
287
|
+
files: Record<string, string>,
|
|
288
|
+
arg2: string | SearchOptions,
|
|
289
|
+
arg3: string,
|
|
290
|
+
arg4?: SearchOptions | string[]
|
|
291
|
+
): ReplaceResult {
|
|
292
|
+
let query = '';
|
|
293
|
+
const replacement = typeof arg3 === 'string' ? arg3 : '';
|
|
294
|
+
let opts: SearchOptions = {};
|
|
295
|
+
let targetFiles: string[] | undefined = undefined;
|
|
296
|
+
|
|
297
|
+
if (typeof arg2 === 'string') {
|
|
298
|
+
query = arg2;
|
|
299
|
+
if (Array.isArray(arg4)) {
|
|
300
|
+
targetFiles = arg4;
|
|
301
|
+
} else if (arg4 && typeof arg4 === 'object') {
|
|
302
|
+
opts = arg4;
|
|
303
|
+
targetFiles = opts.targetFiles;
|
|
304
|
+
}
|
|
305
|
+
} else if (arg2 && typeof arg2 === 'object') {
|
|
306
|
+
opts = arg2;
|
|
307
|
+
query = opts.query || '';
|
|
308
|
+
if (Array.isArray(arg4)) {
|
|
309
|
+
targetFiles = arg4;
|
|
310
|
+
} else {
|
|
311
|
+
targetFiles = opts.targetFiles;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
if (!query) {
|
|
316
|
+
return {
|
|
317
|
+
updatedFiles: { ...files },
|
|
318
|
+
replacedCount: 0,
|
|
319
|
+
filesAffected: []
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
const isCaseSensitive = Boolean(opts.caseSensitive);
|
|
324
|
+
const isWholeWord = Boolean(opts.wholeWord);
|
|
325
|
+
const isRegex = Boolean(opts.regex || opts.isRegex);
|
|
326
|
+
|
|
327
|
+
const includeFilter = opts.include || opts.includeGlob || opts.includePattern;
|
|
328
|
+
const excludeFilter = opts.exclude || opts.excludeGlob || opts.excludePattern;
|
|
329
|
+
|
|
330
|
+
let patternStr = isRegex ? query : escapeRegex(query);
|
|
331
|
+
if (isWholeWord) {
|
|
332
|
+
patternStr = `\\b${patternStr}\\b`;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
let regex: RegExp = /(?:)/;
|
|
336
|
+
try {
|
|
337
|
+
regex = new RegExp(patternStr, isCaseSensitive ? 'g' : 'gi');
|
|
338
|
+
} catch (err) {
|
|
339
|
+
void err;
|
|
340
|
+
return {
|
|
341
|
+
updatedFiles: { ...files },
|
|
342
|
+
replacedCount: 0,
|
|
343
|
+
filesAffected: []
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
const updatedFiles: Record<string, string> = { ...files };
|
|
348
|
+
const filesAffected: string[] = [];
|
|
349
|
+
let replacedCount = 0;
|
|
350
|
+
|
|
351
|
+
for (const [filePath, content] of Object.entries(files)) {
|
|
352
|
+
if (targetFiles && !targetFiles.includes(filePath)) continue;
|
|
353
|
+
if (includeFilter && !checkFilter(filePath, includeFilter)) continue;
|
|
354
|
+
if (excludeFilter && checkFilter(filePath, excludeFilter)) continue;
|
|
355
|
+
|
|
356
|
+
// Count matches safely
|
|
357
|
+
regex.lastIndex = 0;
|
|
358
|
+
let countInFile = 0;
|
|
359
|
+
let m: RegExpExecArray | null;
|
|
360
|
+
while ((m = regex.exec(content)) !== null) {
|
|
361
|
+
countInFile++;
|
|
362
|
+
if (m[0].length === 0) {
|
|
363
|
+
regex.lastIndex++;
|
|
364
|
+
if (regex.lastIndex > content.length) {
|
|
365
|
+
break;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
if (countInFile > 0) {
|
|
371
|
+
regex.lastIndex = 0;
|
|
372
|
+
let newContent: string;
|
|
373
|
+
if (isRegex) {
|
|
374
|
+
newContent = content.replace(regex, replacement);
|
|
375
|
+
} else {
|
|
376
|
+
newContent = content.replace(regex, () => replacement);
|
|
377
|
+
}
|
|
378
|
+
updatedFiles[filePath] = newContent;
|
|
379
|
+
filesAffected.push(filePath);
|
|
380
|
+
replacedCount += countInFile;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
return {
|
|
385
|
+
updatedFiles,
|
|
386
|
+
replacedCount,
|
|
387
|
+
filesAffected
|
|
388
|
+
};
|
|
389
|
+
}
|