@@ -2,36 +2,70 @@ import * as vscode from "vscode";
22import { normalizePaths , Uris2Paths } from "./fsUtils.js" ;
33import * as trashService from "./trashService.js" ;
44
5- /** 在explorer的右键上下文菜单点击,丢入回收站 */
6- export async function trashPutViaContextMenu (
7- _ : vscode . Uri ,
8- uris : vscode . Uri [ ] ,
9- ) {
10- // 处理参数:uris已经包含了所有的文件/文件夹,第一个参数是右键单击的那个文件/文件夹uri,不必理会。
11- let paths = Uris2Paths ( uris ) ;
12- paths = normalizePaths ( paths ) ;
13- await execTrashPut ( paths ) ;
14- }
15-
165const CLIPBOARD_DELAY_MS = 100 ;
176function delay ( ms : number ) : Promise < void > {
187 return new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ;
198}
209
21- /**在explorer选中,通过快捷键执行trash-put */
22- export async function trashPutViaShortcut ( ) {
23- const originalClipboard = await vscode . env . clipboard . readText ( ) ;
24- await vscode . commands . executeCommand ( "copyFilePath" ) ;
25- await delay ( CLIPBOARD_DELAY_MS ) ; // Required delay for VS Code to fill the clipboard asynchronously
26- const selectedPaths = await vscode . env . clipboard . readText ( ) ;
27- await vscode . env . clipboard . writeText ( originalClipboard ) ;
10+ /**
11+ * 统一的删除到回收站入口函数。
12+ * 无论是通过右键上下文菜单还是快捷键触发,VS Code 在侧边栏有焦点时都会传入参数:
13+ * @param activeUri 当前激活/聚焦的项的 URI
14+ * @param uris 当前选中的所有项 of URI 数组
15+ */
16+ export async function trashPut (
17+ activeUri ?: vscode . Uri ,
18+ uris ?: vscode . Uri [ ] ,
19+ ) {
20+ console . log ( "执行 trashPut 命令, activeUri:" , activeUri ?. fsPath , "uris count:" , uris ?. length ) ;
21+
22+ let targetUris : vscode . Uri [ ] = [ ] ;
23+
24+ // 1. 如果是通过右键菜单触发,VS Code 会在 arguments 中直接传入选中的 uris 数组
25+ if ( uris && uris . length > 0 ) {
26+ targetUris = uris ;
27+ } else {
28+ // 2. 如果是通过键盘快捷键(Delete)触发,VS Code 不会在参数中传入选中列表。
29+ // 此时我们必须使用剪贴板 Hack 来安全获取侧边栏的当前选中项目。
30+ try {
31+ const originalClipboard = await vscode . env . clipboard . readText ( ) ;
32+ await vscode . commands . executeCommand ( "copyFilePath" ) ;
33+ await delay ( CLIPBOARD_DELAY_MS ) ; // 等待异步剪贴板写入完成
34+ const selectedPaths = await vscode . env . clipboard . readText ( ) ;
35+ await vscode . env . clipboard . writeText ( originalClipboard ) ; // 立即恢复用户原剪贴板内容
2836
29- if ( ! selectedPaths . trim ( ) ) {
37+ if ( selectedPaths && selectedPaths . trim ( ) ) {
38+ const paths = selectedPaths . split ( / \r ? \n / ) ;
39+ const normalized = normalizePaths ( paths ) ;
40+ targetUris = normalized . map ( ( p ) => vscode . Uri . file ( p ) ) ;
41+ }
42+ } catch ( err ) {
43+ console . error ( "通过剪贴板获取侧边栏选中文件失败:" , err ) ;
44+ }
45+ }
46+
47+ // 3. 兜底策略:如果以上机制均未获取到,则尝试使用 activeUri 或当前活动编辑器对应的文件
48+ if ( targetUris . length === 0 ) {
49+ if ( activeUri ) {
50+ targetUris = [ activeUri ] ;
51+ } else if ( vscode . window . activeTextEditor ) {
52+ targetUris = [ vscode . window . activeTextEditor . document . uri ] ;
53+ }
54+ }
55+
56+ if ( targetUris . length === 0 ) {
57+ console . log ( "未检测到待删除的有效目标" ) ;
3058 return ;
3159 }
3260
33- let paths = selectedPaths . split ( "\n" ) ;
61+ // 转换成绝对路径并进行包含去重过滤
62+ let paths = Uris2Paths ( targetUris ) ;
3463 paths = normalizePaths ( paths ) ;
64+
65+ if ( paths . length === 0 ) {
66+ return ;
67+ }
68+
3569 await execTrashPut ( paths ) ;
3670}
3771
0 commit comments