@@ -5,6 +5,7 @@ import * as fse from "fs-extra";
5
5
import * as path from "path" ;
6
6
import * as vscode from "vscode" ;
7
7
import { LeetCodeNode } from "../explorer/LeetCodeNode" ;
8
+ import { leetCodeChannel } from "../leetCodeChannel" ;
8
9
import { leetCodeExecutor } from "../leetCodeExecutor" ;
9
10
import { leetCodeManager } from "../leetCodeManager" ;
10
11
import { IProblem , IQuickItemEx , languages , ProblemState } from "../shared" ;
@@ -25,9 +26,8 @@ export async function searchProblem(): Promise<void> {
25
26
promptForSignIn ( ) ;
26
27
return ;
27
28
}
28
- const problems : IProblem [ ] = await list . listProblems ( ) ;
29
29
const choice : IQuickItemEx < IProblem > | undefined = await vscode . window . showQuickPick (
30
- parseProblemsToPicks ( problems ) ,
30
+ parseProblemsToPicks ( list . listProblems ( ) ) ,
31
31
{
32
32
matchOnDetail : true ,
33
33
placeHolder : "Select one problem" ,
@@ -52,42 +52,18 @@ async function showProblemInternal(node: IProblem): Promise<void> {
52
52
}
53
53
54
54
let outDir : string = await selectWorkspaceFolder ( ) ;
55
- const outputPathCfg : string = leetCodeConfig . get < string > ( "outputPath" ) || "" ;
56
- const outputPath : RegExpMatchArray | null = outputPathCfg . match ( / \$ \{ ( .* ?) \} / ) ;
57
- if ( outputPath ) {
58
- switch ( outputPath [ 1 ] . toLowerCase ( ) ) {
59
- case "tag" :
60
- let tag : string | undefined ;
61
- if ( node . tags . length === 1 ) {
62
- tag = node . tags [ 0 ] ;
63
- } else {
64
- tag = await vscode . window . showQuickPick (
65
- node . tags ,
66
- {
67
- matchOnDetail : true ,
68
- placeHolder : "Select one tag" ,
69
- } ,
70
- ) ;
71
- }
72
- if ( ! tag ) {
73
- return ;
74
- }
75
- outDir = path . join ( outDir , tag ) ;
76
- break ;
77
- case "language" :
78
- outDir = path . join ( outDir , language ) ;
79
- break ;
80
- case "difficulty" :
81
- outDir = path . join ( outDir , node . difficulty ) ;
82
- break ;
83
- default : {
84
- break ;
85
- }
86
-
55
+ let relativePath : string = ( leetCodeConfig . get < string > ( "outputPath" ) || "" ) . trim ( ) ;
56
+ const matchResult : RegExpMatchArray | null = relativePath . match ( / \$ \{ ( .* ?) \} / ) ;
57
+ if ( matchResult ) {
58
+ const resolvedPath : string | undefined = await resolveRelativePath ( matchResult [ 1 ] . toLocaleLowerCase ( ) , node , language ) ;
59
+ if ( ! resolvedPath ) {
60
+ leetCodeChannel . appendLine ( "No tag is picked, skip showing the problem." ) ;
61
+ return ;
87
62
}
88
- } else {
89
- outDir = path . join ( outDir , outputPathCfg ) ;
63
+ relativePath = resolvedPath ;
90
64
}
65
+
66
+ outDir = path . join ( outDir , relativePath ) ;
91
67
await fse . ensureDir ( outDir ) ;
92
68
const result : string = await leetCodeExecutor . showProblem ( node . id , language , outDir ) ;
93
69
const reg : RegExp = / \* S o u r c e C o d e : \s * ( .* ) / ;
@@ -114,13 +90,13 @@ async function showProblemInternal(node: IProblem): Promise<void> {
114
90
}
115
91
}
116
92
} catch ( error ) {
117
- await promptForOpenOutputChannel ( "Failed to fetch the problem information . Please open the output channel for details." , DialogType . error ) ;
93
+ await promptForOpenOutputChannel ( "Failed to show the problem. Please open the output channel for details." , DialogType . error ) ;
118
94
}
119
95
}
120
96
121
- async function parseProblemsToPicks ( p : IProblem [ ] ) : Promise < Array < IQuickItemEx < IProblem > > > {
97
+ async function parseProblemsToPicks ( p : Promise < IProblem [ ] > ) : Promise < Array < IQuickItemEx < IProblem > > > {
122
98
return new Promise ( async ( resolve : ( res : Array < IQuickItemEx < IProblem > > ) => void ) : Promise < void > => {
123
- const picks : Array < IQuickItemEx < IProblem > > = p . map ( ( problem : IProblem ) => Object . assign ( { } , {
99
+ const picks : Array < IQuickItemEx < IProblem > > = ( await p ) . map ( ( problem : IProblem ) => Object . assign ( { } , {
124
100
label : `${ parseProblemDecorator ( problem . state , problem . locked ) } ${ problem . id } .${ problem . name } ` ,
125
101
description : "" ,
126
102
detail : `AC rate: ${ problem . passRate } , Difficulty: ${ problem . difficulty } ` ,
@@ -140,3 +116,28 @@ function parseProblemDecorator(state: ProblemState, locked: boolean): string {
140
116
return locked ? "$(lock) " : "" ;
141
117
}
142
118
}
119
+
120
+ async function resolveRelativePath ( value : string , node : IProblem , selectedLanguage : string ) : Promise < string | undefined > {
121
+ switch ( value ) {
122
+ case "tag" :
123
+ if ( node . tags . length === 1 ) {
124
+ return node . tags [ 0 ] ;
125
+ }
126
+ return await vscode . window . showQuickPick (
127
+ node . tags ,
128
+ {
129
+ matchOnDetail : true ,
130
+ placeHolder : "Select one tag" ,
131
+ ignoreFocusOut : true ,
132
+ } ,
133
+ ) ;
134
+ case "language" :
135
+ return selectedLanguage ;
136
+ case "difficulty" :
137
+ return node . difficulty ;
138
+ default :
139
+ const errorMsg : string = `The config '${ value } ' is not supported.` ;
140
+ leetCodeChannel . appendLine ( errorMsg ) ;
141
+ throw new Error ( errorMsg ) ;
142
+ }
143
+ }
0 commit comments