@@ -5,14 +5,11 @@ import { t } from '../i18n'
55import { WorkspaceProvider } from '../context/providers/workspace/workspace-provider'
66import { OpenEditorsProvider } from '../context/providers/open-editors/open-editors-provider'
77import { AsciiTree } from '../utils/ascii-tree'
8+ import { LAST_COPY_PATHS_FORMAT_STATE_KEY } from '../constants/state-keys'
89
9- const format_paths = ( files : string [ ] ) => {
10- const config = vscode . workspace . getConfiguration ( 'codeWebChat' )
11- const format = config . get < 'bullet-list' | 'comma-separated' | 'ascii-tree' > (
12- 'copyPathsFormat' ,
13- 'bullet-list'
14- )
10+ type Format = 'bullet-list' | 'comma-separated' | 'ascii-tree'
1511
12+ const format_paths = ( files : string [ ] , format : Format ) => {
1613 const workspace_folders = vscode . workspace . workspaceFolders
1714 const is_multi_root = ! ! workspace_folders && workspace_folders . length > 1
1815
@@ -43,24 +40,116 @@ const format_paths = (files: string[]) => {
4340 return display_paths . map ( ( display_path ) => `- \`${ display_path } \`` ) . join ( '\n' )
4441}
4542
43+ const resolve_format = async (
44+ extension_context : vscode . ExtensionContext
45+ ) : Promise < Format | undefined > => {
46+ const config = vscode . workspace . getConfiguration ( 'codeWebChat' )
47+ const configured_format = config . get <
48+ 'ask' | 'bullet-list' | 'comma-separated' | 'ascii-tree'
49+ > ( 'copyPathsFormat' , 'ask' )
50+
51+ if ( configured_format != 'ask' ) {
52+ return configured_format as Format
53+ }
54+
55+ const last_format = extension_context . globalState . get < Format > (
56+ LAST_COPY_PATHS_FORMAT_STATE_KEY
57+ )
58+
59+ type Option = vscode . QuickPickItem & { value : Format }
60+
61+ const options : Option [ ] = [
62+ {
63+ label : t ( 'command.copy-paths.format.bullet-list' ) ,
64+ value : 'bullet-list'
65+ } ,
66+ {
67+ label : t ( 'command.copy-paths.format.comma-separated' ) ,
68+ value : 'comma-separated'
69+ } ,
70+ {
71+ label : t ( 'command.copy-paths.format.ascii-tree' ) ,
72+ value : 'ascii-tree'
73+ }
74+ ]
75+
76+ const quick_pick = vscode . window . createQuickPick < Option > ( )
77+ quick_pick . title = t ( 'command.copy-paths.title' )
78+ quick_pick . placeholder = t ( 'command.copy-paths.placeholder' )
79+ quick_pick . items = options
80+ quick_pick . ignoreFocusOut = true
81+
82+ if ( last_format ) {
83+ const last_option = options . find ( ( o ) => o . value === last_format )
84+ if ( last_option ) {
85+ quick_pick . activeItems = [ last_option ]
86+ }
87+ }
88+
89+ const close_button = {
90+ iconPath : new vscode . ThemeIcon ( 'close' ) ,
91+ tooltip : t ( 'common.close' )
92+ }
93+
94+ quick_pick . buttons = [ close_button ]
95+
96+ const selected = await new Promise < Option | undefined > ( ( resolve ) => {
97+ let is_accepted = false
98+
99+ quick_pick . onDidTriggerButton ( ( button ) => {
100+ if ( button === close_button ) {
101+ resolve ( undefined )
102+ quick_pick . hide ( )
103+ }
104+ } )
105+
106+ quick_pick . onDidAccept ( ( ) => {
107+ is_accepted = true
108+ resolve ( quick_pick . selectedItems [ 0 ] )
109+ quick_pick . hide ( )
110+ } )
111+
112+ quick_pick . onDidHide ( ( ) => {
113+ if ( ! is_accepted ) {
114+ resolve ( undefined )
115+ }
116+ quick_pick . dispose ( )
117+ } )
118+
119+ quick_pick . show ( )
120+ } )
121+
122+ if ( selected ) {
123+ await extension_context . globalState . update (
124+ LAST_COPY_PATHS_FORMAT_STATE_KEY ,
125+ selected . value
126+ )
127+ return selected . value
128+ }
129+
130+ return undefined
131+ }
132+
46133export const copy_paths_commands = (
47134 workspace_provider : WorkspaceProvider ,
48- open_editors_provider : OpenEditorsProvider
135+ open_editors_provider : OpenEditorsProvider ,
136+ extension_context : vscode . ExtensionContext
49137) => {
50138 return [
51139 vscode . commands . registerCommand ( 'codeWebChat.copyPaths' , async ( ) => {
52140 const checked_files = workspace_provider . get_checked_files ( )
53141
54142 if ( checked_files . length == 0 ) {
55- vscode . window . showWarningMessage (
56- t ( 'command.copy-context.warning.no-files-selected' )
57- )
143+ vscode . window . showWarningMessage ( t ( 'common.warning.no-files-selected' ) )
58144 return
59145 }
60146
61- const paths_text = format_paths ( checked_files )
147+ const format = await resolve_format ( extension_context )
148+ if ( ! format ) return
149+
150+ const paths_text = format_paths ( checked_files , format )
62151 await vscode . env . clipboard . writeText ( paths_text )
63- vscode . window . showInformationMessage ( 'Paths copied to clipboard.' )
152+ vscode . window . showInformationMessage ( t ( 'command.copy-paths.copied' ) )
64153 } ) ,
65154
66155 vscode . commands . registerCommand (
@@ -71,14 +160,17 @@ export const copy_paths_commands = (
71160
72161 if ( checked_files . length == 0 ) {
73162 vscode . window . showWarningMessage (
74- dictionary . warning_message . NO_OPEN_EDITORS_SELECTED
163+ t ( 'common.warning.no-files-selected' )
75164 )
76165 return
77166 }
78167
79- const paths_text = format_paths ( checked_files )
168+ const format = await resolve_format ( extension_context )
169+ if ( ! format ) return
170+
171+ const paths_text = format_paths ( checked_files , format )
80172 await vscode . env . clipboard . writeText ( paths_text )
81- vscode . window . showInformationMessage ( 'Paths copied to clipboard.' )
173+ vscode . window . showInformationMessage ( t ( 'command.copy-paths.copied' ) )
82174 }
83175 )
84176 ]
0 commit comments