1
1
// Copyright (c) jdneo. All rights reserved.
2
2
// Licensed under the MIT license.
3
3
4
- import * as fse from "fs-extra" ;
5
- import * as os from "os" ;
6
- import * as path from "path" ;
7
4
import * as vscode from "vscode" ;
8
5
import { leetCodeExecutor } from "../leetCodeExecutor" ;
9
6
import { IQuickItemEx } from "../shared" ;
10
7
import { Endpoint } from "../shared" ;
11
8
import { DialogType , promptForOpenOutputChannel , promptForSignIn } from "../utils/uiUtils" ;
12
9
import { deleteCache } from "./cache" ;
13
10
14
- export async function toogleLeetCodeCn ( ) : Promise < void > {
15
- const isCnEnbaled : boolean = isLeetCodeCnEnabled ( ) ;
11
+ export async function switchEndpoint ( ) : Promise < void > {
12
+ const isCnEnbaled : boolean = getLeetCodeEndpoint ( ) === Endpoint . LeetCodeCN ;
16
13
const picks : Array < IQuickItemEx < string > > = [ ] ;
17
14
picks . push (
18
15
{
19
- label : `${ isCnEnbaled ? "$(check) " : "" } On ` ,
20
- description : "" ,
21
- detail : `Enable ${ Endpoint . LeetCodeCN } . ` ,
22
- value : "on" ,
16
+ label : `${ isCnEnbaled ? "" : " $(check) "} LeetCode ` ,
17
+ description : "leetcode.com " ,
18
+ detail : `Enable LeetCode US ` ,
19
+ value : Endpoint . LeetCode ,
23
20
} ,
24
21
{
25
- label : `${ isCnEnbaled ? "" : " $(check) "} Off ` ,
26
- description : "" ,
27
- detail : `Disable ${ Endpoint . LeetCodeCN } . ` ,
28
- value : "off" ,
22
+ label : `${ isCnEnbaled ? "$(check) " : "" } 力扣 ` ,
23
+ description : "leetcode-cn.com " ,
24
+ detail : `启用中国版 LeetCode ` ,
25
+ value : Endpoint . LeetCodeCN ,
29
26
} ,
30
27
) ;
31
28
const choice : IQuickItemEx < string > | undefined = await vscode . window . showQuickPick ( picks ) ;
@@ -34,9 +31,8 @@ export async function toogleLeetCodeCn(): Promise<void> {
34
31
}
35
32
const leetCodeConfig : vscode . WorkspaceConfiguration = vscode . workspace . getConfiguration ( "leetcode" ) ;
36
33
try {
37
- const enabled : boolean = choice . value === "on" ;
38
- const endpoint : string = enabled ? Endpoint . LeetCodeCN : Endpoint . LeetCode ;
39
- await leetCodeExecutor . toggleLeetCodeCn ( enabled ) ;
34
+ const endpoint : string = choice . value ;
35
+ await leetCodeExecutor . switchEndpoint ( endpoint ) ;
40
36
await leetCodeConfig . update ( "endpoint" , endpoint , true /* UserSetting */ ) ;
41
37
vscode . window . showInformationMessage ( `Switched the endpoint to ${ endpoint } ` ) ;
42
38
} catch ( error ) {
@@ -52,29 +48,7 @@ export async function toogleLeetCodeCn(): Promise<void> {
52
48
}
53
49
}
54
50
55
- export async function initializeEndpoint ( ) : Promise < void > {
56
- const isCnEnabledInExtension : boolean = isLeetCodeCnEnabled ( ) ;
57
- const isCnEnabledInCli : boolean = await isLeetCodeCnEnabledInCli ( ) ;
58
- await leetCodeExecutor . toggleLeetCodeCn ( isCnEnabledInExtension ) ;
59
- if ( isCnEnabledInCli !== isCnEnabledInExtension ) {
60
- await deleteCache ( ) ;
61
- }
62
- }
63
-
64
- export function isLeetCodeCnEnabled ( ) : boolean {
51
+ export function getLeetCodeEndpoint ( ) : string {
65
52
const leetCodeConfig : vscode . WorkspaceConfiguration = vscode . workspace . getConfiguration ( "leetcode" ) ;
66
- const endpoint : string | undefined = leetCodeConfig . get < string > ( "endpoint" ) ;
67
- if ( endpoint && endpoint === Endpoint . LeetCodeCN ) {
68
- return true ;
69
- }
70
- return false ;
71
- }
72
-
73
- async function isLeetCodeCnEnabledInCli ( ) : Promise < boolean > {
74
- const pluginsStatusFile : string = path . join ( os . homedir ( ) , ".lc" , "plugins.json" ) ;
75
- if ( ! await fse . pathExists ( pluginsStatusFile ) ) {
76
- return false ;
77
- }
78
- const pluginsObj : { } = await fse . readJson ( pluginsStatusFile ) ;
79
- return pluginsObj [ "leetcode.cn" ] ;
53
+ return leetCodeConfig . get < string > ( "endpoint" , Endpoint . LeetCode ) ;
80
54
}
0 commit comments