MCPcopy Create free account
hub / github.com/ZToolsCenter/ZTools / searchFileCommands

Function searchFileCommands

src/renderer/src/stores/commandDataStore.ts:1284–1360  ·  view source on GitHub ↗
(
    pastedFiles?: Array<{ path: string; name: string; isDirectory: boolean }>
  )

Source from the content-addressed store, hash-verified

1282
1283 // 搜索支持文件的指令(根据配置属性过滤)
1284 function searchFileCommands(
1285 pastedFiles?: Array<{ path: string; name: string; isDirectory: boolean }>
1286 ): SearchResult[] {
1287 if (!pastedFiles || pastedFiles.length === 0) {
1288 return []
1289 }
1290
1291 const filesCommandsList = regexCommands.value.filter((c) => c.matchCmd?.type === 'files')
1292
1293 const result = filesCommandsList.filter((cmd) => {
1294 const filesCmd = cmd.matchCmd as FilesCmd
1295
1296 // 1. 检查文件数量是否满足要求
1297 const fileCount = pastedFiles.length
1298 const minLength = filesCmd.minLength ?? 1
1299 const maxLength = filesCmd.maxLength ?? 10000
1300
1301 if (fileCount < minLength || fileCount > maxLength) {
1302 return false
1303 }
1304
1305 // 2. 检查每个文件是否满足条件
1306 const allFilesMatch = pastedFiles.every((file) => {
1307 // 2.1 检查文件类型(file 或 directory)
1308 if (filesCmd.fileType) {
1309 if (filesCmd.fileType === 'file' && file.isDirectory) {
1310 return false
1311 }
1312 if (filesCmd.fileType === 'directory' && !file.isDirectory) {
1313 return false
1314 }
1315 }
1316
1317 // 2.2 检查文件扩展名(只对文件有效,不检查文件夹)
1318 if (filesCmd.extensions && !file.isDirectory) {
1319 const ext = file.name.split('.').pop()?.toLowerCase()
1320 const allowedExts = filesCmd.extensions.map((e) => e.toLowerCase())
1321 if (!ext || !allowedExts.includes(ext)) {
1322 return false
1323 }
1324 }
1325
1326 // 2.3 检查正则表达式匹配
1327 if (filesCmd.match) {
1328 try {
1329 // 解析正则表达式字符串(格式:/pattern/flags)
1330 const match = filesCmd.match.match(/^\/(.+)\/([gimuy]*)$/)
1331 if (match) {
1332 const pattern = match[1]
1333 const flags = match[2]
1334 const regex = new RegExp(pattern, flags)
1335 const testResult = regex.test(file.name)
1336 if (!testResult) {
1337 return false
1338 }
1339 } else {
1340 // 如果不是标准格式,直接作为字符串匹配
1341 const testResult = file.name.includes(filesCmd.match)

Callers 1

useSearchResultsFunction · 0.85

Calls 2

applySpecialConfigFunction · 0.90
isCommandDisabledFunction · 0.85

Tested by

no test coverage detected