dsh-gufa-code 1.3.62 → 1.3.64
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/README.md +1 -1
- package/client.js +404 -18
- package/desktop/float/bin/gufa-float-macos-arm64 +0 -0
- package/desktop/float/bin/gufa-float-macos-universal +0 -0
- package/desktop/float/bin/gufa-float-macos-x64 +0 -0
- package/desktop/float//345/217/244/346/263/225/347/274/226/347/250/213/346/202/254/346/265/256/345/261/202.app/Contents/Info.plist +2 -2
- package/desktop/float//345/217/244/346/263/225/347/274/226/347/250/213/346/202/254/346/265/256/345/261/202.app/Contents/MacOS/gufa-float +0 -0
- package/docs/KNOWN-PITFALLS.md +8 -1
- package/host/fs.js +344 -1
- package/package.json +1 -1
- package/scripts/selftest-host.mjs +54 -0
- package/src/client/00-core.js +69 -2
- package/src/client/50-panels.js +289 -15
- package/src/client/90-app.js +45 -0
package/host/fs.js
CHANGED
|
@@ -87,6 +87,161 @@ async function projectOf(env, body) {
|
|
|
87
87
|
return project.root
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
+
/* ── 全项目搜索/替换共用的目录与文件枚举 ───────────────────────────────── */
|
|
91
|
+
const SEARCH_EXCLUDED = new Set([
|
|
92
|
+
'node_modules', '.git', 'dist', 'build', 'out', '.next', 'vendor', 'target', '.venv', 'venv',
|
|
93
|
+
'__pycache__', '.idea', '.cache', '.turbo', '.parcel-cache', '.svelte-kit', '.nuxt', '.output',
|
|
94
|
+
'coverage', '.gradle', 'DerivedData', 'Pods', '.tox', '.mypy_cache', '.pytest_cache', '.ruff_cache',
|
|
95
|
+
'.electron-cache', '.npm-cache', '.pip-cache', '.yarn', '.pnpm-store', '.pnpm',
|
|
96
|
+
])
|
|
97
|
+
const SEARCH_SKIP_EXT = new Set([
|
|
98
|
+
'png', 'jpg', 'jpeg', 'gif', 'webp', 'ico', 'icns', 'bmp', 'tiff', 'pdf', 'zip', 'gz', 'tgz', 'tar', '7z', 'rar', 'dmg', 'iso',
|
|
99
|
+
'mp4', 'mp3', 'wav', 'mov', 'avi', 'mkv', 'flac', 'jar', 'class', 'o', 'a', 'so', 'dylib', 'dll', 'exe', 'bin', 'dat',
|
|
100
|
+
'woff', 'woff2', 'ttf', 'otf', 'eot', 'wasm', 'pyc', 'node',
|
|
101
|
+
])
|
|
102
|
+
const SEARCH_EXCLUDE_PREFIXES = ['.venv', '.npm-cache', '.pip-cache', '.electron-cache', '.pnpm', '.yarn', '.cache', '.next-cache']
|
|
103
|
+
function searchDirExcluded(name) {
|
|
104
|
+
if (SEARCH_EXCLUDED.has(name)) return true
|
|
105
|
+
for (const prefix of SEARCH_EXCLUDE_PREFIXES) {
|
|
106
|
+
if (name.startsWith(prefix)) return true
|
|
107
|
+
}
|
|
108
|
+
return false
|
|
109
|
+
}
|
|
110
|
+
function searchExtensionOf(rel) {
|
|
111
|
+
const base = String(rel).split('/').pop() ?? ''
|
|
112
|
+
const dot = base.lastIndexOf('.')
|
|
113
|
+
return dot <= 0 ? '' : base.slice(dot + 1).toLowerCase()
|
|
114
|
+
}
|
|
115
|
+
function escapeRegExp(text) {
|
|
116
|
+
return String(text).replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
117
|
+
}
|
|
118
|
+
/** 解析「包含/排除的文件」:数组或逗号/换行分隔字符串。 */
|
|
119
|
+
function readPatternList(value) {
|
|
120
|
+
if (Array.isArray(value)) return value.filter((item) => typeof item === 'string').map((item) => item.trim()).filter(Boolean)
|
|
121
|
+
if (typeof value === 'string') return value.split(/[,\n]/).map((item) => item.trim()).filter(Boolean)
|
|
122
|
+
return []
|
|
123
|
+
}
|
|
124
|
+
/** 把 VS Code 风格 glob 编译成正则(支持 **、*、?;无 / 的模式匹配任意层级 basename)。 */
|
|
125
|
+
function globToRegExp(pattern) {
|
|
126
|
+
let text = String(pattern).replace(/\\/g, '/').trim()
|
|
127
|
+
if (text === '') return null
|
|
128
|
+
if (text.startsWith('./')) text = text.slice(2)
|
|
129
|
+
let body = ''
|
|
130
|
+
for (let index = 0; index < text.length; index += 1) {
|
|
131
|
+
const char = text[index]
|
|
132
|
+
if (char === '*') {
|
|
133
|
+
if (text[index + 1] === '*') {
|
|
134
|
+
index += 1
|
|
135
|
+
if (text[index + 1] === '/') {
|
|
136
|
+
index += 1
|
|
137
|
+
body += '(?:.*/)?'
|
|
138
|
+
} else {
|
|
139
|
+
body += '.*'
|
|
140
|
+
}
|
|
141
|
+
} else {
|
|
142
|
+
body += '[^/]*'
|
|
143
|
+
}
|
|
144
|
+
continue
|
|
145
|
+
}
|
|
146
|
+
if (char === '?') {
|
|
147
|
+
body += '[^/]'
|
|
148
|
+
continue
|
|
149
|
+
}
|
|
150
|
+
body += char.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
151
|
+
}
|
|
152
|
+
const hasSlash = text.includes('/')
|
|
153
|
+
const anchored = text.startsWith('/') ? `^/${body}$` : hasSlash ? `^${body}$` : `(?:^|/)${body}$`
|
|
154
|
+
try {
|
|
155
|
+
return new RegExp(anchored, 'i')
|
|
156
|
+
} catch {
|
|
157
|
+
return null
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
/** 构造路径过滤器:include 任一命中 + 不被 exclude 命中。 */
|
|
161
|
+
function buildPathFilter(options) {
|
|
162
|
+
const include = readPatternList(options.include).map(globToRegExp).filter(Boolean)
|
|
163
|
+
const exclude = readPatternList(options.exclude).map(globToRegExp).filter(Boolean)
|
|
164
|
+
return (rel) => {
|
|
165
|
+
if (include.length > 0 && !include.some((regex) => regex.test(rel))) return false
|
|
166
|
+
if (exclude.length > 0 && exclude.some((regex) => regex.test(rel))) return false
|
|
167
|
+
return true
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
/** VS Code 式「保留大小写」:全大写/全小写/首字母大写分别跟随匹配项。 */
|
|
171
|
+
function applyPreserveCase(matched, replacement) {
|
|
172
|
+
const text = String(matched ?? '')
|
|
173
|
+
if (text === '' || replacement === '') return replacement
|
|
174
|
+
const upper = text === text.toUpperCase() && text !== text.toLowerCase()
|
|
175
|
+
const lower = text === text.toLowerCase() && text !== text.toUpperCase()
|
|
176
|
+
if (upper) return replacement.toUpperCase()
|
|
177
|
+
if (lower) return replacement.toLowerCase()
|
|
178
|
+
if (/^[A-Z]/.test(text)) return replacement.charAt(0).toUpperCase() + replacement.slice(1)
|
|
179
|
+
return replacement
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
async function isGitProject(root, project) {
|
|
183
|
+
if (project !== null && project !== undefined && project.isGit === true) return true
|
|
184
|
+
try {
|
|
185
|
+
const dotGit = await stat(join(root, '.git'))
|
|
186
|
+
return dotGit.isDirectory() || dotGit.isFile()
|
|
187
|
+
} catch {
|
|
188
|
+
return false
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
/** 枚举项目内可搜索文件:Git 项目用 git ls-files,否则 BFS 遍历并跳过依赖/缓存目录。 */
|
|
192
|
+
async function listProjectCandidates(root, project, maxScanned) {
|
|
193
|
+
if (await isGitProject(root, project)) {
|
|
194
|
+
try {
|
|
195
|
+
const { stdout } = await runCommand('git', ['ls-files', '-z', '--cached', '--others', '--exclude-standard'], {
|
|
196
|
+
cwd: root,
|
|
197
|
+
maxBuffer: 64 * 1024 * 1024,
|
|
198
|
+
timeout: 15000,
|
|
199
|
+
})
|
|
200
|
+
const files = stdout.split('\0').filter((item) => item !== '')
|
|
201
|
+
files.sort((a, b) => a.localeCompare(b))
|
|
202
|
+
return { files, truncated: false, viaGit: true }
|
|
203
|
+
} catch {
|
|
204
|
+
/* 回退到目录遍历 */
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
const queue = ['']
|
|
208
|
+
const files = []
|
|
209
|
+
let head = 0
|
|
210
|
+
let scanned = 0
|
|
211
|
+
let truncated = false
|
|
212
|
+
while (head < queue.length) {
|
|
213
|
+
if (files.length >= maxScanned) {
|
|
214
|
+
truncated = true
|
|
215
|
+
break
|
|
216
|
+
}
|
|
217
|
+
const dirRel = queue[head]
|
|
218
|
+
head += 1
|
|
219
|
+
let entries
|
|
220
|
+
try {
|
|
221
|
+
entries = await readdir(join(root, dirRel), { withFileTypes: true })
|
|
222
|
+
} catch {
|
|
223
|
+
continue
|
|
224
|
+
}
|
|
225
|
+
entries.sort((a, b) => String(a.name).localeCompare(String(b.name)))
|
|
226
|
+
for (const entry of entries) {
|
|
227
|
+
if (entry.isDirectory()) {
|
|
228
|
+
if (!searchDirExcluded(entry.name)) queue.push(dirRel === '' ? entry.name : `${dirRel}/${entry.name}`)
|
|
229
|
+
continue
|
|
230
|
+
}
|
|
231
|
+
if (!entry.isFile() || searchDirExcluded(entry.name)) continue
|
|
232
|
+
const rel = dirRel === '' ? entry.name : `${dirRel}/${entry.name}`
|
|
233
|
+
if (SEARCH_SKIP_EXT.has(searchExtensionOf(rel))) continue
|
|
234
|
+
files.push(rel)
|
|
235
|
+
scanned += 1
|
|
236
|
+
if (files.length >= maxScanned) {
|
|
237
|
+
truncated = true
|
|
238
|
+
break
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
return { files, truncated, viaGit: false, scanned }
|
|
243
|
+
}
|
|
244
|
+
|
|
90
245
|
export function register(router, env) {
|
|
91
246
|
router.method('fs.list', async (body) => {
|
|
92
247
|
const root = await projectOf(env, body)
|
|
@@ -251,6 +406,7 @@ export function register(router, env) {
|
|
|
251
406
|
const options = body.options !== null && typeof body.options === 'object' ? body.options : {}
|
|
252
407
|
const caseSensitive = options.caseSensitive === true
|
|
253
408
|
const useRegex = options.regex === true
|
|
409
|
+
const pathFilter = buildPathFilter(options)
|
|
254
410
|
const clampInt = (value, fallback, min, max) => {
|
|
255
411
|
const number = Number(value)
|
|
256
412
|
if (!Number.isFinite(number)) return fallback
|
|
@@ -351,6 +507,7 @@ export function register(router, env) {
|
|
|
351
507
|
return matches
|
|
352
508
|
}
|
|
353
509
|
const acceptFile = async (rel) => {
|
|
510
|
+
if (!pathFilter(rel)) return
|
|
354
511
|
const matches = await searchFile(rel)
|
|
355
512
|
if (matches.length === 0) return
|
|
356
513
|
files.push({ path: rel, matches })
|
|
@@ -410,7 +567,7 @@ export function register(router, env) {
|
|
|
410
567
|
entries.sort((a, b) => String(a.name).localeCompare(String(b.name)))
|
|
411
568
|
for (const entry of entries) {
|
|
412
569
|
if (entry.isDirectory()) {
|
|
413
|
-
if (!EXCLUDED.has(entry.name)) queue.push(dirRel === '' ? entry.name : `${dirRel}/${entry.name}`)
|
|
570
|
+
if (!EXCLUDED.has(entry.name) && !SEARCH_EXCLUDE_PREFIXES.some((prefix) => entry.name.startsWith(prefix))) queue.push(dirRel === '' ? entry.name : `${dirRel}/${entry.name}`)
|
|
414
571
|
continue
|
|
415
572
|
}
|
|
416
573
|
if (!entry.isFile() || EXCLUDED.has(entry.name)) continue
|
|
@@ -433,6 +590,192 @@ export function register(router, env) {
|
|
|
433
590
|
return { query, files, total, truncated, scanned, viaGit: candidates !== null }
|
|
434
591
|
})
|
|
435
592
|
|
|
593
|
+
/** 全项目批量替换(左侧「搜索」面板的替换功能)。只写内容确实变化的文件。 */
|
|
594
|
+
router.method('fs.replace', async (body) => {
|
|
595
|
+
const project = env.projects.ensure(typeof body.root === 'string' ? body.root : '')
|
|
596
|
+
const root = project.root
|
|
597
|
+
const query = typeof body.query === 'string' ? body.query.slice(0, 200) : ''
|
|
598
|
+
const replacement = typeof body.replacement === 'string' ? body.replacement : ''
|
|
599
|
+
if (query === '') return { replacedFiles: [], replacedCount: 0, failed: [], scanned: 0, truncated: false, stale: false }
|
|
600
|
+
const options = body.options !== null && typeof body.options === 'object' ? body.options : {}
|
|
601
|
+
const caseSensitive = options.caseSensitive === true
|
|
602
|
+
const useRegex = options.regex === true
|
|
603
|
+
const preserveCase = options.preserveCase === true
|
|
604
|
+
const clampInt = (value, fallback, min, max) => {
|
|
605
|
+
const number = Number(value)
|
|
606
|
+
if (!Number.isFinite(number)) return fallback
|
|
607
|
+
return Math.min(Math.max(Math.trunc(number), min), max)
|
|
608
|
+
}
|
|
609
|
+
const maxReplacements = clampInt(body.maxReplacements, 5000, 1, 50000)
|
|
610
|
+
const flags = caseSensitive ? 'g' : 'gi'
|
|
611
|
+
let regex = null
|
|
612
|
+
try {
|
|
613
|
+
regex = new RegExp(useRegex ? query : escapeRegExp(query), flags)
|
|
614
|
+
} catch (error) {
|
|
615
|
+
throw new ApiError('bad-regex', `正则表达式无效:${error.message}`, 400)
|
|
616
|
+
}
|
|
617
|
+
if (useRegex && new RegExp(query, caseSensitive ? '' : 'i').test('')) {
|
|
618
|
+
throw new ApiError('bad-regex', '正则表达式不能匹配空字符串', 400)
|
|
619
|
+
}
|
|
620
|
+
const fileSizeLimit = 2 * 1024 * 1024
|
|
621
|
+
const pathFilter = buildPathFilter(options)
|
|
622
|
+
|
|
623
|
+
/* 模式 A:只替换某一个匹配(搜索结果行上的替换按钮),用行/列定位。 */
|
|
624
|
+
const target = body.target !== null && typeof body.target === 'object' ? body.target : null
|
|
625
|
+
if (target !== null) {
|
|
626
|
+
const rawPath = typeof target.path === 'string' ? target.path : ''
|
|
627
|
+
const targetLine = Number(target.line)
|
|
628
|
+
const targetColumn = Number(target.column)
|
|
629
|
+
if (rawPath === '' || !Number.isInteger(targetLine) || targetLine < 1 || !Number.isInteger(targetColumn) || targetColumn < 1) {
|
|
630
|
+
throw badRequest('target.line / target.column is required')
|
|
631
|
+
}
|
|
632
|
+
let absolute
|
|
633
|
+
try {
|
|
634
|
+
absolute = await resolveProjectPath(root, rawPath)
|
|
635
|
+
} catch {
|
|
636
|
+
return { replacedFiles: [], replacedCount: 0, failed: [rawPath], scanned: 1, truncated: false, stale: true }
|
|
637
|
+
}
|
|
638
|
+
const info = await stat(absolute).catch(() => undefined)
|
|
639
|
+
if (info === undefined || !info.isFile() || info.size > fileSizeLimit) {
|
|
640
|
+
return { replacedFiles: [], replacedCount: 0, failed: [rawPath], scanned: 1, truncated: false, stale: true }
|
|
641
|
+
}
|
|
642
|
+
let buffer
|
|
643
|
+
try {
|
|
644
|
+
buffer = await readFile(absolute)
|
|
645
|
+
} catch {
|
|
646
|
+
return { replacedFiles: [], replacedCount: 0, failed: [rawPath], scanned: 1, truncated: false, stale: false }
|
|
647
|
+
}
|
|
648
|
+
if (buffer.includes(0)) return { replacedFiles: [], replacedCount: 0, failed: [rawPath], scanned: 1, truncated: false, stale: false }
|
|
649
|
+
const text = buffer.toString('utf8')
|
|
650
|
+
regex.lastIndex = 0
|
|
651
|
+
let found = null
|
|
652
|
+
let cursor = 0
|
|
653
|
+
let line = 1
|
|
654
|
+
let lineStart = 0
|
|
655
|
+
let match
|
|
656
|
+
while ((match = regex.exec(text)) !== null) {
|
|
657
|
+
if (match[0].length === 0) {
|
|
658
|
+
regex.lastIndex += 1
|
|
659
|
+
continue
|
|
660
|
+
}
|
|
661
|
+
const index = match.index
|
|
662
|
+
if (index > cursor) {
|
|
663
|
+
const chunk = text.slice(cursor, index)
|
|
664
|
+
const newlines = chunk.split('\n').length - 1
|
|
665
|
+
line += newlines
|
|
666
|
+
const lastNewline = chunk.lastIndexOf('\n')
|
|
667
|
+
if (lastNewline !== -1) lineStart = cursor + lastNewline + 1
|
|
668
|
+
cursor = index
|
|
669
|
+
}
|
|
670
|
+
const column = index - lineStart + 1
|
|
671
|
+
if (line === targetLine && column === targetColumn) {
|
|
672
|
+
found = { index, value: match[0] }
|
|
673
|
+
break
|
|
674
|
+
}
|
|
675
|
+
if (line > targetLine || (line === targetLine && column > targetColumn)) break
|
|
676
|
+
}
|
|
677
|
+
if (found === null) {
|
|
678
|
+
return { replacedFiles: [], replacedCount: 0, failed: [], scanned: 1, truncated: false, stale: true }
|
|
679
|
+
}
|
|
680
|
+
let replacedValue = replacement
|
|
681
|
+
if (useRegex) {
|
|
682
|
+
try {
|
|
683
|
+
replacedValue = found.value.replace(new RegExp(`^(?:${query})$`, caseSensitive ? '' : 'i'), replacement)
|
|
684
|
+
} catch {
|
|
685
|
+
replacedValue = replacement
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
if (preserveCase) replacedValue = applyPreserveCase(found.value, replacedValue)
|
|
689
|
+
const updated = `${text.slice(0, found.index)}${replacedValue}${text.slice(found.index + found.value.length)}`
|
|
690
|
+
try {
|
|
691
|
+
await writeFile(absolute, updated, 'utf8')
|
|
692
|
+
} catch {
|
|
693
|
+
return { replacedFiles: [], replacedCount: 0, failed: [rawPath], scanned: 1, truncated: false, stale: false }
|
|
694
|
+
}
|
|
695
|
+
return { replacedFiles: [relativePath(root, absolute)], replacedCount: 1, failed: [], scanned: 1, truncated: false, stale: false }
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
/* 模式 B:批量替换(全部替换 / 单个文件替换)。 */
|
|
699
|
+
const pathsFilter = Array.isArray(body.paths)
|
|
700
|
+
? body.paths.filter((item) => typeof item === 'string' && item !== '').slice(0, 200)
|
|
701
|
+
: null
|
|
702
|
+
let candidates = []
|
|
703
|
+
if (pathsFilter !== null) {
|
|
704
|
+
for (const raw of pathsFilter) {
|
|
705
|
+
try {
|
|
706
|
+
const absolute = await resolveProjectPath(root, raw)
|
|
707
|
+
const rel = relativePath(root, absolute)
|
|
708
|
+
if (pathFilter(rel)) candidates.push(rel)
|
|
709
|
+
} catch {
|
|
710
|
+
/* 路径非法/已删除:跳过 */
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
} else {
|
|
714
|
+
const listed = await listProjectCandidates(root, project, 20000)
|
|
715
|
+
candidates = listed.files.filter((rel) => pathFilter(rel))
|
|
716
|
+
}
|
|
717
|
+
const replacedFiles = []
|
|
718
|
+
const failed = []
|
|
719
|
+
let replacedCount = 0
|
|
720
|
+
let truncated = false
|
|
721
|
+
const expand = (matched, groups) => {
|
|
722
|
+
let output = replacement
|
|
723
|
+
if (useRegex) {
|
|
724
|
+
output = output
|
|
725
|
+
.replace(/\$\$/g, '\u0000')
|
|
726
|
+
.replace(/\$&/g, matched)
|
|
727
|
+
.replace(/\$(\d{1,2})/g, (piece, index) => {
|
|
728
|
+
const group = groups[Number(index) - 1]
|
|
729
|
+
return group === undefined ? piece : group
|
|
730
|
+
})
|
|
731
|
+
.replace(/\u0000/g, '$')
|
|
732
|
+
}
|
|
733
|
+
if (preserveCase) output = applyPreserveCase(matched, output)
|
|
734
|
+
return output
|
|
735
|
+
}
|
|
736
|
+
for (const rel of candidates) {
|
|
737
|
+
if (replacedCount >= maxReplacements) {
|
|
738
|
+
truncated = true
|
|
739
|
+
break
|
|
740
|
+
}
|
|
741
|
+
const absolute = join(root, rel)
|
|
742
|
+
const info = await stat(absolute).catch(() => undefined)
|
|
743
|
+
if (info === undefined || !info.isFile() || info.size > fileSizeLimit) continue
|
|
744
|
+
let buffer
|
|
745
|
+
try {
|
|
746
|
+
buffer = await readFile(absolute)
|
|
747
|
+
} catch {
|
|
748
|
+
failed.push(rel)
|
|
749
|
+
continue
|
|
750
|
+
}
|
|
751
|
+
if (buffer.includes(0)) continue
|
|
752
|
+
const text = buffer.toString('utf8')
|
|
753
|
+
regex.lastIndex = 0
|
|
754
|
+
const matches = text.match(regex)
|
|
755
|
+
if (matches === null || matches.length === 0) continue
|
|
756
|
+
if (replacedCount + matches.length > maxReplacements) {
|
|
757
|
+
truncated = true
|
|
758
|
+
break
|
|
759
|
+
}
|
|
760
|
+
let updated
|
|
761
|
+
try {
|
|
762
|
+
updated = text.replace(regex, (matched, ...args) => expand(matched, args.slice(0, Math.max(args.length - 2, 0))))
|
|
763
|
+
} catch (error) {
|
|
764
|
+
failed.push(rel)
|
|
765
|
+
continue
|
|
766
|
+
}
|
|
767
|
+
if (updated === text) continue
|
|
768
|
+
try {
|
|
769
|
+
await writeFile(absolute, updated, 'utf8')
|
|
770
|
+
replacedFiles.push(rel)
|
|
771
|
+
replacedCount += matches.length
|
|
772
|
+
} catch {
|
|
773
|
+
failed.push(rel)
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
return { replacedFiles, replacedCount, failed, scanned: candidates.length, truncated, stale: false }
|
|
777
|
+
})
|
|
778
|
+
|
|
436
779
|
router.method('fs.matchFiles', async (body) => {
|
|
437
780
|
const root = await projectOf(env, body)
|
|
438
781
|
const wanted = Array.isArray(body.files)
|
package/package.json
CHANGED
|
@@ -155,6 +155,60 @@ try {
|
|
|
155
155
|
const gitSearch = await call(router, 'fs.search', { root, query: 'answer' })
|
|
156
156
|
check('search uses git file list', gitSearch.viaGit === true, String(gitSearch.viaGit))
|
|
157
157
|
check('search finds committed file', gitSearch.files.some((file) => file.path === 'src/main.ts'))
|
|
158
|
+
console.log('fs.replace')
|
|
159
|
+
await call(router, 'fs.create', { root, path: 'replace.txt', kind: 'file' })
|
|
160
|
+
await call(router, 'fs.write', { root, path: 'replace.txt', content: 'foo FOO foo\n' })
|
|
161
|
+
const replaced = await call(router, 'fs.replace', { root, query: 'foo', replacement: 'bar', paths: ['replace.txt'] })
|
|
162
|
+
check('replace count (case-insensitive)', replaced.replacedCount === 3, String(replaced.replacedCount))
|
|
163
|
+
const replacedDoc = await call(router, 'fs.read', { root, path: 'replace.txt' })
|
|
164
|
+
check('replace wrote file', replacedDoc.content === 'bar bar bar\n', JSON.stringify(replacedDoc.content))
|
|
165
|
+
await call(router, 'fs.write', { root, path: 'replace.txt', content: 'v1.2 v3.4\n', expectedRevision: replacedDoc.revision })
|
|
166
|
+
const regexReplaced = await call(router, 'fs.replace', {
|
|
167
|
+
root,
|
|
168
|
+
query: 'v(\\d)\\.(\\d)',
|
|
169
|
+
replacement: 'v$1-$2',
|
|
170
|
+
options: { regex: true },
|
|
171
|
+
paths: ['replace.txt'],
|
|
172
|
+
})
|
|
173
|
+
check('replace regex groups', regexReplaced.replacedCount === 2, String(regexReplaced.replacedCount))
|
|
174
|
+
const regexDoc = await call(router, 'fs.read', { root, path: 'replace.txt' })
|
|
175
|
+
check('replace regex result', regexDoc.content === 'v1-2 v3-4\n', JSON.stringify(regexDoc.content))
|
|
176
|
+
await call(router, 'fs.write', { root, path: 'replace.txt', content: 'abc\n', expectedRevision: regexDoc.revision })
|
|
177
|
+
const literalDollar = await call(router, 'fs.replace', { root, query: 'abc', replacement: '$1', paths: ['replace.txt'] })
|
|
178
|
+
const dollarDoc = await call(router, 'fs.read', { root, path: 'replace.txt' })
|
|
179
|
+
check('literal replacement keeps $', literalDollar.replacedCount === 1 && dollarDoc.content === '$1\n', JSON.stringify(dollarDoc.content))
|
|
180
|
+
let emptyRegexRejected = false
|
|
181
|
+
try {
|
|
182
|
+
await call(router, 'fs.replace', { root, query: 'a*', replacement: 'x', options: { regex: true }, paths: ['replace.txt'] })
|
|
183
|
+
} catch (error) {
|
|
184
|
+
emptyRegexRejected = error.code === 'bad-regex'
|
|
185
|
+
}
|
|
186
|
+
check('replace rejects empty-matching regex', emptyRegexRejected)
|
|
187
|
+
await call(router, 'fs.write', { root, path: 'replace.txt', content: 'foo FOO Foo\n', expectedRevision: dollarDoc.revision })
|
|
188
|
+
const cased = await call(router, 'fs.replace', { root, query: 'foo', replacement: 'bar', options: { preserveCase: true }, paths: ['replace.txt'] })
|
|
189
|
+
const casedDoc = await call(router, 'fs.read', { root, path: 'replace.txt' })
|
|
190
|
+
check('replace preserve case', cased.replacedCount === 3 && casedDoc.content === 'bar BAR Bar\n', JSON.stringify(casedDoc.content))
|
|
191
|
+
await call(router, 'fs.write', { root, path: 'replace.txt', content: 'one two two\n', expectedRevision: casedDoc.revision })
|
|
192
|
+
const single = await call(router, 'fs.replace', { root, query: 'two', replacement: 'X', target: { path: 'replace.txt', line: 1, column: 5 } })
|
|
193
|
+
const singleDoc = await call(router, 'fs.read', { root, path: 'replace.txt' })
|
|
194
|
+
check('replace single match by line/column', single.replacedCount === 1 && singleDoc.content === 'one X two\n', JSON.stringify(singleDoc.content))
|
|
195
|
+
const stale = await call(router, 'fs.replace', { root, query: 'two', replacement: 'Y', target: { path: 'replace.txt', line: 1, column: 50 } })
|
|
196
|
+
check('replace single stale target', stale.replacedCount === 0 && stale.stale === true)
|
|
197
|
+
await call(router, 'fs.create', { root, path: 'inc.md', kind: 'file' })
|
|
198
|
+
await call(router, 'fs.create', { root, path: 'inc.txt', kind: 'file' })
|
|
199
|
+
await call(router, 'fs.write', { root, path: 'inc.md', content: 'ZzIncToken here\n' })
|
|
200
|
+
await call(router, 'fs.write', { root, path: 'inc.txt', content: 'ZzIncToken here\n' })
|
|
201
|
+
const includeSearch = await call(router, 'fs.search', { root, query: 'ZzIncToken', options: { include: '*.md' } })
|
|
202
|
+
check('search include filter', includeSearch.files.length === 1 && includeSearch.files[0].path === 'inc.md', JSON.stringify(includeSearch.files.map((file) => file.path)))
|
|
203
|
+
const excludeSearch = await call(router, 'fs.search', { root, query: 'ZzIncToken', options: { exclude: '*.md' } })
|
|
204
|
+
check('search exclude filter', excludeSearch.files.length === 1 && excludeSearch.files[0].path === 'inc.txt', JSON.stringify(excludeSearch.files.map((file) => file.path)))
|
|
205
|
+
const includeReplace = await call(router, 'fs.replace', { root, query: 'ZzIncToken', replacement: 'Included', options: { include: '*.md' } })
|
|
206
|
+
const incMd = await call(router, 'fs.read', { root, path: 'inc.md' })
|
|
207
|
+
const incTxt = await call(router, 'fs.read', { root, path: 'inc.txt' })
|
|
208
|
+
check('replace include filter', includeReplace.replacedCount === 1 && incMd.content === 'Included here\n' && incTxt.content === 'ZzIncToken here\n', JSON.stringify([incMd.content, incTxt.content]))
|
|
209
|
+
await call(router, 'fs.delete', { root, path: 'inc.md' })
|
|
210
|
+
await call(router, 'fs.delete', { root, path: 'inc.txt' })
|
|
211
|
+
await call(router, 'fs.delete', { root, path: 'replace.txt' })
|
|
158
212
|
writeFileSync(join(projectDir, 'src/main.ts'), 'const answer = 43\n')
|
|
159
213
|
const diff = await call(router, 'git.diff', { root, path: 'src/main.ts' })
|
|
160
214
|
check('diff contains change', diff.patch.includes('43'))
|
package/src/client/00-core.js
CHANGED
|
@@ -191,9 +191,28 @@ const DICT_ZH = {
|
|
|
191
191
|
'search.searching': '搜索中…',
|
|
192
192
|
'search.idle': '输入关键字搜索整个项目',
|
|
193
193
|
'search.noResult': '没有找到匹配结果',
|
|
194
|
-
'search.summary': '{
|
|
194
|
+
'search.summary': '{files} 个文件中有 {count} 个结果',
|
|
195
195
|
'search.truncated': '结果过多已截断,请缩小范围',
|
|
196
196
|
'search.error': '搜索失败',
|
|
197
|
+
'search.replaceToggle': '显示/隐藏替换',
|
|
198
|
+
'search.include': '包含的文件',
|
|
199
|
+
'search.exclude': '排除的文件',
|
|
200
|
+
'search.includePlaceholder': '例如 *.js, src/**',
|
|
201
|
+
'search.excludePlaceholder': '例如 **/*.min.js, test/**',
|
|
202
|
+
'search.preserveCase': '保留大小写(AB)',
|
|
203
|
+
'search.replaceOne': '替换此匹配',
|
|
204
|
+
'search.dismiss': '忽略此匹配',
|
|
205
|
+
'search.openInEditor': '在编辑器中打开',
|
|
206
|
+
'search.stale': '匹配位置已变化,已重新搜索',
|
|
207
|
+
'search.replacePlaceholder': '替换为…',
|
|
208
|
+
'search.replaceAll': '全部替换',
|
|
209
|
+
'search.replaceThisFile': '替换此文件',
|
|
210
|
+
'search.replacing': '替换中…',
|
|
211
|
+
'search.replaced': '已替换 {count} 处({files} 个文件)',
|
|
212
|
+
'search.confirmReplace': '确定替换 {count} 处({files} 个文件)?直接写文件,编辑器撤销无效。',
|
|
213
|
+
'search.confirmYes': '确定替换',
|
|
214
|
+
'search.confirmNo': '取消',
|
|
215
|
+
'search.replaceError': '替换失败',
|
|
197
216
|
'explorer.empty': '目录为空',
|
|
198
217
|
'explorer.newFile': '新建文件',
|
|
199
218
|
'explorer.newFolder': '新建文件夹',
|
|
@@ -423,9 +442,28 @@ const DICT_EN = {
|
|
|
423
442
|
'search.searching': 'Searching…',
|
|
424
443
|
'search.idle': 'Type a keyword to search the whole project',
|
|
425
444
|
'search.noResult': 'No matches found',
|
|
426
|
-
'search.summary': '{count} results
|
|
445
|
+
'search.summary': '{count} results in {files} files',
|
|
427
446
|
'search.truncated': 'Too many results, truncated — narrow the query',
|
|
428
447
|
'search.error': 'Search failed',
|
|
448
|
+
'search.replaceToggle': 'Show / hide replace',
|
|
449
|
+
'search.include': 'files to include',
|
|
450
|
+
'search.exclude': 'files to exclude',
|
|
451
|
+
'search.includePlaceholder': 'e.g. *.js, src/**',
|
|
452
|
+
'search.excludePlaceholder': 'e.g. **/*.min.js, test/**',
|
|
453
|
+
'search.preserveCase': 'Preserve case (AB)',
|
|
454
|
+
'search.replaceOne': 'Replace this match',
|
|
455
|
+
'search.dismiss': 'Dismiss this match',
|
|
456
|
+
'search.openInEditor': 'Open in editor',
|
|
457
|
+
'search.stale': 'Match moved; search refreshed',
|
|
458
|
+
'search.replacePlaceholder': 'Replace with…',
|
|
459
|
+
'search.replaceAll': 'Replace all',
|
|
460
|
+
'search.replaceThisFile': 'Replace in this file',
|
|
461
|
+
'search.replacing': 'Replacing…',
|
|
462
|
+
'search.replaced': 'Replaced {count} occurrences ({files} files)',
|
|
463
|
+
'search.confirmReplace': 'Replace {count} occurrences in {files} files? This writes files directly; editor undo will not work.',
|
|
464
|
+
'search.confirmYes': 'Replace',
|
|
465
|
+
'search.confirmNo': 'Cancel',
|
|
466
|
+
'search.replaceError': 'Replace failed',
|
|
429
467
|
'explorer.empty': 'Folder is empty',
|
|
430
468
|
'explorer.newFile': 'New file',
|
|
431
469
|
'explorer.newFolder': 'New folder',
|
|
@@ -1697,6 +1735,35 @@ html[data-platform=darwin][data-fullscreen] .gufa-titlebar{height:44px;padding-l
|
|
|
1697
1735
|
.gufa-search-line{flex:0 0 auto;min-width:24px;text-align:right;font:11px ui-monospace,Menlo,Consolas,monospace;opacity:.65}
|
|
1698
1736
|
.gufa-search-text{flex:1 1 auto;min-width:0;font:11.5px ui-monospace,Menlo,Consolas,monospace;white-space:pre;overflow:hidden;text-overflow:ellipsis}
|
|
1699
1737
|
.gufa-search-hit{background:rgba(192,57,43,.38);color:var(--gufa-text);border-radius:2px;padding:0 1px}
|
|
1738
|
+
.gufa-search-replace-row{margin-top:0}
|
|
1739
|
+
.gufa-search-actions{display:flex;align-items:center;gap:6px;margin-top:6px}
|
|
1740
|
+
.gufa-search-action{height:24px;padding:0 9px;border-radius:6px;border:1px solid var(--gufa-border);background:var(--gufa-layer2);color:var(--gufa-text);font-size:11.5px;cursor:pointer}
|
|
1741
|
+
.gufa-search-action:hover:not(:disabled){border-color:var(--gufa-accent)}
|
|
1742
|
+
.gufa-search-action:disabled{opacity:.5;cursor:default}
|
|
1743
|
+
.gufa-search-action[data-primary]{background:var(--gufa-accent);border-color:var(--gufa-accent);color:#fff}
|
|
1744
|
+
.gufa-search-confirm{margin:6px 8px 0;padding:7px 9px;border-radius:7px;border:1px solid var(--gufa-accent);background:color-mix(in srgb, var(--gufa-accent) 12%, transparent);font-size:11.5px;line-height:1.55;color:var(--gufa-text)}
|
|
1745
|
+
.gufa-search-confirm-actions{display:flex;justify-content:flex-end;gap:6px;margin-top:6px}
|
|
1746
|
+
.gufa-search-notice{margin:6px 8px 0;padding:5px 9px;border-radius:7px;font-size:11.5px;color:var(--dsw-alias-state-success-primary);border:1px solid color-mix(in srgb, var(--dsw-alias-state-success-primary) 45%, transparent)}
|
|
1747
|
+
.gufa-search-error{margin:6px 8px 0;padding:5px 9px;border-radius:7px;font-size:11.5px;color:var(--dsw-alias-state-error-primary);border:1px solid color-mix(in srgb, var(--dsw-alias-state-error-primary) 45%, transparent)}
|
|
1748
|
+
.gufa-search-file-replace{flex:0 0 auto;display:inline-flex;align-items:center;justify-content:center;width:18px;height:18px;margin-left:2px;border-radius:4px;color:var(--gufa-text-dim);cursor:pointer}
|
|
1749
|
+
.gufa-search-file-replace:hover{background:var(--gufa-active);color:var(--gufa-text)}
|
|
1750
|
+
.gufa-search-file-replace{opacity:0;transition:opacity .12s ease}
|
|
1751
|
+
.gufa-search-file-head:hover .gufa-search-file-replace,.gufa-search-file-head:focus-within .gufa-search-file-replace{opacity:1}
|
|
1752
|
+
.gufa-search-match{min-height:23px;align-items:center}
|
|
1753
|
+
.gufa-search-match-actions{display:none;flex:0 0 auto;align-items:center;gap:4px;margin-left:auto}
|
|
1754
|
+
.gufa-search-match:hover .gufa-search-match-actions,.gufa-search-match:focus-within .gufa-search-match-actions{display:flex}
|
|
1755
|
+
.gufa-search-row-action{display:inline-flex;align-items:center;justify-content:center;width:18px;height:18px;border-radius:4px;color:var(--gufa-text-dim);cursor:pointer}
|
|
1756
|
+
.gufa-search-row-action:hover{background:var(--gufa-active);color:var(--gufa-text)}
|
|
1757
|
+
.gufa-search-filter{display:flex;flex-direction:column;gap:3px;margin-top:5px}
|
|
1758
|
+
.gufa-search-filter-label{font-size:10.5px;color:var(--gufa-text-dim)}
|
|
1759
|
+
.gufa-search-filter-input{width:100%;height:24px;padding:0 8px;border-radius:6px;border:1px solid var(--gufa-border);background:var(--gufa-layer2);color:var(--gufa-text);font-size:11.5px;outline:none}
|
|
1760
|
+
.gufa-search-filter-input:focus{border-color:var(--gufa-accent)}
|
|
1761
|
+
.gufa-search-filter-input::placeholder{color:var(--gufa-text-dim)}
|
|
1762
|
+
.gufa-search-summarybar{display:flex;align-items:center;gap:8px}
|
|
1763
|
+
.gufa-search-summarybar .gufa-search-summary{flex:1 1 auto;margin-left:0}
|
|
1764
|
+
.gufa-search-summarybar .gufa-search-link{flex:0 0 auto}
|
|
1765
|
+
.gufa-search-link{border:0;background:transparent;color:var(--gufa-accent);font-size:11px;cursor:pointer;padding:0}
|
|
1766
|
+
.gufa-search-link:hover{text-decoration:underline}
|
|
1700
1767
|
|
|
1701
1768
|
.gufa-tree-chevron{width:14px;flex:0 0 auto;opacity:.7;display:flex;align-items:center;justify-content:center}
|
|
1702
1769
|
.gufa-tree-icon{width:15px;flex:0 0 auto;opacity:.85;display:flex;align-items:center;justify-content:center}
|