2
2
// Licensed under the MIT license.
3
3
4
4
import * as fse from "fs-extra" ;
5
+ import * as path from "path" ;
5
6
import * as vscode from "vscode" ;
6
7
import { LeetCodeNode } from "../explorer/LeetCodeNode" ;
7
8
import { leetCodeExecutor } from "../leetCodeExecutor" ;
@@ -16,16 +17,17 @@ export async function showProblem(node?: LeetCodeNode): Promise<void> {
16
17
if ( ! node ) {
17
18
return ;
18
19
}
19
- await showProblemInternal ( node . id ) ;
20
+ await showProblemInternal ( node ) ;
20
21
}
21
22
22
23
export async function searchProblem ( ) : Promise < void > {
23
24
if ( ! leetCodeManager . getUser ( ) ) {
24
25
promptForSignIn ( ) ;
25
26
return ;
26
27
}
28
+ const problems : IProblem [ ] = await list . listProblems ( ) ;
27
29
const choice : IQuickItemEx < string > | undefined = await vscode . window . showQuickPick (
28
- parseProblemsToPicks ( list . listProblems ( ) ) ,
30
+ parseProblemsToPicks ( problems ) ,
29
31
{
30
32
matchOnDetail : true ,
31
33
placeHolder : "Select one problem" ,
@@ -34,12 +36,11 @@ export async function searchProblem(): Promise<void> {
34
36
if ( ! choice ) {
35
37
return ;
36
38
}
37
- await showProblemInternal ( choice . value ) ;
39
+ await showProblemInternal ( problems . find ( ( problem : IProblem ) => problem . id === choice . value ) as IProblem ) ;
38
40
}
39
41
40
- async function showProblemInternal ( id : string ) : Promise < void > {
42
+ async function showProblemInternal ( node : IProblem ) : Promise < void > {
41
43
try {
42
- const listProblems : IProblem [ ] = await list . listProblems ( ) ;
43
44
const leetCodeConfig : vscode . WorkspaceConfiguration = vscode . workspace . getConfiguration ( "leetcode" ) ;
44
45
let defaultLanguage : string | undefined = leetCodeConfig . get < string > ( "defaultLanguage" ) ;
45
46
if ( defaultLanguage && languages . indexOf ( defaultLanguage ) < 0 ) {
@@ -51,35 +52,35 @@ async function showProblemInternal(id: string): Promise<void> {
51
52
}
52
53
53
54
let outDir : string = await selectWorkspaceFolder ( ) ;
54
- const outputPath : string = leetCodeConfig . get < string > ( "outputPath" ) || "" ;
55
- const problem : IProblem | undefined = listProblems . find ( ( item : IProblem ) => item . id === id ) ;
56
- switch ( outputPath ) {
57
- case "" : {
58
- break ;
59
- }
60
- case "${tag}" : {
61
- if ( problem ) {
62
- outDir = `${ outDir } /${ problem . tags [ 0 ] } ` ;
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
+ const closestTag : string = node . tags . reduce ( ( prev : string , curr : string ) => {
61
+ return curr . length > prev . length ?
62
+ curr :
63
+ prev ;
64
+ } , "" ) ;
65
+ outDir = path . join ( outDir , closestTag ) ;
66
+ break ;
63
67
}
64
- break ;
65
- }
66
- case "${language}" : {
67
- outDir = `${ outDir } /${ language } ` ;
68
- break ;
69
- }
70
- case "${difficulty}" : {
71
- if ( problem ) {
72
- outDir = `${ outDir } /${ problem . difficulty } ` ;
68
+ case "language" : {
69
+ outDir = path . join ( outDir , language ) ;
70
+ break ;
73
71
}
74
- break ;
75
- }
76
- default : {
77
- outDir = `${ outDir } /${ outputPath } ` ;
78
- break ;
72
+ case "difficulty" : {
73
+ outDir = path . join ( outDir , node . difficulty ) ;
74
+ break ;
75
+ }
76
+ default : {
77
+ break ;
78
+ }
79
+
79
80
}
80
81
}
81
82
await fse . ensureDir ( outDir ) ;
82
- const result : string = await leetCodeExecutor . showProblem ( id , language , outDir ) ;
83
+ const result : string = await leetCodeExecutor . showProblem ( node . id , language , outDir ) ;
83
84
const reg : RegExp = / \* S o u r c e C o d e : \s * ( .* ) / ;
84
85
const match : RegExpMatchArray | null = result . match ( reg ) ;
85
86
if ( match && match . length >= 2 ) {
@@ -108,9 +109,9 @@ async function showProblemInternal(id: string): Promise<void> {
108
109
}
109
110
}
110
111
111
- async function parseProblemsToPicks ( p : Promise < IProblem [ ] > ) : Promise < Array < IQuickItemEx < string > > > {
112
+ async function parseProblemsToPicks ( p : IProblem [ ] ) : Promise < Array < IQuickItemEx < string > > > {
112
113
return new Promise ( async ( resolve : ( res : Array < IQuickItemEx < string > > ) => void ) : Promise < void > => {
113
- const picks : Array < IQuickItemEx < string > > = ( await p ) . map ( ( problem : IProblem ) => Object . assign ( { } , {
114
+ const picks : Array < IQuickItemEx < string > > = p . map ( ( problem : IProblem ) => Object . assign ( { } , {
114
115
label : `${ parseProblemDecorator ( problem . state , problem . locked ) } ${ problem . id } .${ problem . name } ` ,
115
116
description : "" ,
116
117
detail : `AC rate: ${ problem . passRate } , Difficulty: ${ problem . difficulty } ` ,
0 commit comments