@@ -8,6 +8,7 @@ import * as v from "valibot";
88import { ProblemSchema , type Testcase } from "../shared/schemas" ;
99import type JudgeViewProvider from "./providers/JudgeViewProvider" ;
1010import { getLogger } from "./utils/logging" ;
11+ import { getFileWorkspace } from "./utils/vscode" ;
1112
1213type Problem = v . InferOutput < typeof ProblemSchema > ;
1314
@@ -49,13 +50,13 @@ let prevBatchId: string | undefined;
4950 */
5051async function promptForTargetFile (
5152 problem : Problem ,
52- workspaceRoot : string ,
53- files : vscode . Uri [ ] ,
53+ files : string [ ] ,
54+ workspace ?: string ,
5455 currentFileRelativePath ?: string
5556) : Promise < string > {
5657 const options : vscode . QuickPickItem [ ] = files . map ( ( file ) => ( {
57- label : path . parse ( file . fsPath ) . base ,
58- description : path . relative ( workspaceRoot , file . fsPath ) ,
58+ label : path . parse ( file ) . base ,
59+ description : path . relative ( workspace ?? "" , file ) ,
5960 } ) ) ;
6061
6162 const pick = vscode . window . createQuickPick ( ) ;
@@ -126,7 +127,7 @@ async function promptForTargetFile(
126127 */
127128async function processProblem ( problem : Problem , judge : JudgeViewProvider ) : Promise < void > {
128129 const activeFile = vscode . window . activeTextEditor ?. document . fileName ;
129- const workspaceRoot = vscode . workspace . workspaceFolders ?. at ( 0 ) ?. uri . fsPath ?? "" ;
130+ const workspace = await getFileWorkspace ( activeFile ) ;
130131 const config = vscode . workspace . getConfiguration ( "fastolympiccoding" ) ;
131132 const openSelectedFiles = config . get < boolean > ( "openSelectedFiles" ) ! ;
132133 const askForWhichFile = config . get < boolean > ( "askForWhichFile" ) ! ;
@@ -138,29 +139,51 @@ async function processProblem(problem: Problem, judge: JudgeViewProvider): Promi
138139 if ( prevBatchId === problem . batch . id ) {
139140 currentFileRelativePath = prevSelection ;
140141 } else {
141- currentFileRelativePath = activeFile ? path . relative ( workspaceRoot , activeFile ) : undefined ;
142+ currentFileRelativePath =
143+ workspace && activeFile ? path . relative ( workspace , activeFile ) : undefined ;
142144 }
143145 let relativePath = isSingleProblem && currentFileRelativePath ? currentFileRelativePath : "" ;
144146
145147 if ( needsPrompt ) {
146148 // Gather files before prompting to include newly created files from previous problems
147149 const include = config . get < string > ( "includePattern" ) ! ;
148150 const exclude = config . get < string > ( "excludePattern" ) ! ;
149- const updatedFiles = await vscode . workspace . findFiles ( include , exclude ) ;
150- relativePath = await promptForTargetFile (
151- problem ,
152- workspaceRoot ,
153- updatedFiles ,
154- currentFileRelativePath
155- ) ;
151+
152+ let files : string [ ] ;
153+ if ( vscode . workspace . workspaceFolders ) {
154+ files = await vscode . workspace
155+ . findFiles ( include , exclude )
156+ . then ( ( uris ) => uris . map ( ( uri ) => uri . fsPath ) ) ;
157+ } else if ( activeFile ) {
158+ files = [ activeFile ] ;
159+ } else {
160+ const choice = await vscode . window . showWarningMessage (
161+ `No active workspace nor file opened to infer files to put testcases for "${ problem . name } ".` ,
162+ "Select A File" ,
163+ "Dismiss"
164+ ) ;
165+ if ( choice === "Dismiss" ) {
166+ return ;
167+ }
168+
169+ const result = await vscode . window . showOpenDialog ( {
170+ canSelectFiles : true ,
171+ canSelectFolders : false ,
172+ canSelectMany : false ,
173+ title : `Select a file to put testcases for "${ problem . name } "` ,
174+ } ) ;
175+ files = result ? [ result [ 0 ] . fsPath ] : [ ] ;
176+ }
177+
178+ relativePath = await promptForTargetFile ( problem , files , workspace , currentFileRelativePath ) ;
156179 }
157180
158181 if ( relativePath === "" ) {
159182 vscode . window . showWarningMessage ( `No file to write testcases for "${ problem . name } "` ) ;
160183 return ;
161184 }
162185
163- const absolutePath = path . join ( workspaceRoot , relativePath ) ;
186+ const absolutePath = path . resolve ( path . join ( workspace ?? "" , relativePath ) ) ;
164187 try {
165188 await fs . writeFile ( absolutePath , "" , { flag : "a" } ) ; // Create file if it doesn't exist
166189 } catch ( error ) {
0 commit comments