Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf58422f

Browse files
authored
support test solution (LeetCode-OpenSource#16)
* support test solution* escape the line seperator* update readme
1 parent5b4bff1 commitf58422f

File tree

11 files changed

+144
-20
lines changed

11 files changed

+144
-20
lines changed

‎CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ All notable changes to the "leetcode" extension will be documented in this file.
33

44
Check[Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.
55

6+
##[0.3.0]
7+
###Added
8+
- Test current solution file[(#15)](https://github.com/jdneo/vscode-leetcode/issues/15)
9+
610
##[0.2.1]
711
###Fixed
812
- Fix the wrong icon bug in LeetCode Explorer[(#9)](https://github.com/jdneo/vscode-leetcode/issues/9)

‎README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Solve LeetCode problems in VS Code.
1818
- Switch and create session
1919
- Show problems in explorer
2020
- Search problems by keywords
21+
- Test solutions by customized test case
2122
- Submit solutions to LeetCode
2223

2324
###Sign In and Sign Out
@@ -32,6 +33,9 @@ Solve LeetCode problems in VS Code.
3233
###Search Problems by Keywords
3334
![SearchProblem](resources/gif/searchproblem.gif)
3435

36+
###Test solutions by customized test case
37+
![TestSolution](resources/gif/testsolution.gif)
38+
3539
###Submit Solutions to LeetCode
3640
![SubmitSolution](resources/gif/solveproblem.gif)
3741

@@ -43,6 +47,7 @@ This extension provides several commands in the Command Palette (F1 or Ctrl + Sh
4347
-**LeetCode: Create new session** - Create a new session
4448
-**LeetCode: Refresh** - Refresh the LeetCode Explorer
4549
-**LeetCode: Search Problem** - Search for problems by keywords
50+
-**LeetCode: Test Current File** - Test the solution by customized test case
4651
-**LeetCode: Submit** - Submit the solution to LeetCode
4752

4853
##Known Issues:
@@ -68,6 +73,7 @@ This extension is based on [@skygragon](https://github.com/skygragon)'s [leetcod
6873
- 切换及创建 session
6974
- 在 Explorer 中展示题目
7075
- 根据关键字搜索题目
76+
- 用自定义测试用例测试答案
7177
- 向 LeetCode 提交答案
7278

7379
###登入及登出
@@ -82,6 +88,9 @@ This extension is based on [@skygragon](https://github.com/skygragon)'s [leetcod
8288
###根据关键字搜索题目
8389
![SearchProblem](resources/gif/searchproblem.gif)
8490

91+
###用自定义测试用例测试答案
92+
![TestSolution](resources/gif/testsolution.gif)
93+
8594
###向 LeetCode 提交答案
8695
![SubmitSolution](resources/gif/solveproblem.gif)
8796

@@ -93,6 +102,7 @@ This extension is based on [@skygragon](https://github.com/skygragon)'s [leetcod
93102
-**LeetCode: Create new session** - 创建一个新的答题进度存档
94103
-**LeetCode: Refresh** - 刷新左侧题目列表视图
95104
-**LeetCode: Search Problem** - 根据关键字搜索题目
105+
-**LeetCode: Test Current File** - 用自定义测试用例测试答案
96106
-**LeetCode: Submit** - 提交答案到 LeetCode
97107

98108
##已知问题

‎package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name":"vscode-leetcode",
33
"displayName":"LeetCode",
44
"description":"Solve LeetCode problems in VS Code",
5-
"version":"0.2.1",
5+
"version":"0.3.0",
66
"author":"Sheng Chen",
77
"publisher":"shengchen",
88
"icon":"resources/LeetCode.png",
@@ -32,6 +32,7 @@
3232
"onCommand:leetcode.refreshExplorer",
3333
"onCommand:leetcode.showProblem",
3434
"onCommand:leetcode.searchProblem",
35+
"onCommand:leetcode.testSolution",
3536
"onCommand:leetcode.submitSolution",
3637
"onView:leetCodeExplorer"
3738
],
@@ -78,6 +79,11 @@
7879
"category":"LeetCode",
7980
"icon":"resources/search.png"
8081
},
82+
{
83+
"command":"leetcode.testSolution",
84+
"title":"Test Current File",
85+
"category":"LeetCode"
86+
},
8187
{
8288
"command":"leetcode.submitSolution",
8389
"title":"Submit",

‎resources/gif/testsolution.gif

351 KB
Loading

‎src/commands/list.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export async function listProblems(channel: vscode.OutputChannel): Promise<IProb
3737
}
3838
returnproblems.reverse();
3939
}catch(error){
40-
awaitpromptForOpenOutputChannel("Failed to list problems. Please open the output channel for details",DialogType.error,channel);
40+
awaitpromptForOpenOutputChannel("Failed to list problems. Please open the output channel for details.",DialogType.error,channel);
4141
return[];
4242
}
4343
}

‎src/commands/session.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import*asvscodefrom"vscode";
44
import{leetCodeManager}from"../leetCodeManager";
55
import{IQuickItemEx,leetCodeBinaryPath}from"../shared";
6-
import*ascpfrom"../utils/cpUtils";
6+
import{executeCommand}from"../utils/cpUtils";
77
import{DialogType,promptForOpenOutputChannel,promptForSignIn}from"../utils/uiUtils";
88

99
exportasyncfunctiongetSessionList(channel:vscode.OutputChannel):Promise<ISession[]>{
@@ -12,7 +12,7 @@ export async function getSessionList(channel: vscode.OutputChannel): Promise<ISe
1212
promptForSignIn();
1313
return[];
1414
}
15-
constresult:string=awaitcp.executeCommand(channel,"node",[leetCodeBinaryPath,"session"]);
15+
constresult:string=awaitexecuteCommand(channel,"node",[leetCodeBinaryPath,"session"]);
1616
constlines:string[]=result.split("\n");
1717
constsessions:ISession[]=[];
1818
constreg:RegExp=/(.?)\s*(\d+)\s+(.*)\s+(\d+\(\s*\d+\.\d+%\))\s+(\d+\(\s*\d+\.\d+%\))/;
@@ -41,11 +41,11 @@ export async function selectSession(channel: vscode.OutputChannel): Promise<void
4141
return;
4242
}
4343
try{
44-
awaitcp.executeCommand(channel,"node",[leetCodeBinaryPath,"session","-e",choice.value]);
44+
awaitexecuteCommand(channel,"node",[leetCodeBinaryPath,"session","-e",choice.value]);
4545
vscode.window.showInformationMessage(`Successfully switched to session '${choice.label}'.`);
4646
awaitvscode.commands.executeCommand("leetcode.refreshExplorer");
4747
}catch(error){
48-
awaitpromptForOpenOutputChannel("Failed to switch session. Please open the output channel for details",DialogType.error,channel);
48+
awaitpromptForOpenOutputChannel("Failed to switch session. Please open the output channel for details.",DialogType.error,channel);
4949
}
5050
}
5151

@@ -67,7 +67,7 @@ async function parseSessionsToPicks(channel: vscode.OutputChannel): Promise<Arra
6767
});
6868
resolve(picks);
6969
}catch(error){
70-
returnawaitpromptForOpenOutputChannel("Failed to list sessions. Please open the output channel for details",DialogType.error,channel);
70+
returnawaitpromptForOpenOutputChannel("Failed to list sessions. Please open the output channel for details.",DialogType.error,channel);
7171
}
7272
});
7373
}
@@ -81,10 +81,10 @@ export async function createSession(channel: vscode.OutputChannel): Promise<void
8181
return;
8282
}
8383
try{
84-
awaitcp.executeCommand(channel,"node",[leetCodeBinaryPath,"session","-c",session]);
84+
awaitexecuteCommand(channel,"node",[leetCodeBinaryPath,"session","-c",session]);
8585
vscode.window.showInformationMessage("New session created, you can switch to it by clicking the status bar.");
8686
}catch(error){
87-
awaitpromptForOpenOutputChannel("Failed to create session. Please open the output channel for details",DialogType.error,channel);
87+
awaitpromptForOpenOutputChannel("Failed to create session. Please open the output channel for details.",DialogType.error,channel);
8888
}
8989
}
9090

‎src/commands/show.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ async function showProblemInternal(channel: vscode.OutputChannel, id: string): P
5555
if(match&&match.length>=2){
5656
awaitvscode.window.showTextDocument(vscode.Uri.file(match[1].trim()),{preview:false});
5757
}else{
58-
thrownewError("Failed to fetch the problem information");
58+
thrownewError("Failed to fetch the problem information.");
5959
}
6060

6161
if(!defaultLanguage&&leetCodeConfig.get<boolean>("showSetDefaultLanguageHint")){
@@ -72,7 +72,7 @@ async function showProblemInternal(channel: vscode.OutputChannel, id: string): P
7272
}
7373
}
7474
}catch(error){
75-
awaitpromptForOpenOutputChannel("Failed to fetch the problem information. Please open the output channel for details",DialogType.error,channel);
75+
awaitpromptForOpenOutputChannel("Failed to fetch the problem information. Please open the output channel for details.",DialogType.error,channel);
7676
}
7777
}
7878

‎src/commands/submit.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
"use strict";
22

3-
import*asfsefrom"fs-extra";
4-
import*asosfrom"os";
5-
import*aspathfrom"path";
63
import*asvscodefrom"vscode";
74
import{leetCodeManager}from"../leetCodeManager";
85
import{leetCodeBinaryPath}from"../shared";
96
import{executeCommand}from"../utils/cpUtils";
10-
import{DialogType,promptForOpenOutputChannel,promptForSignIn}from"../utils/uiUtils";
7+
import{DialogType,promptForOpenOutputChannel,promptForSignIn,showResultFile}from"../utils/uiUtils";
118

129
exportasyncfunctionsubmitSolution(channel:vscode.OutputChannel):Promise<void>{
1310
if(!leetCodeManager.getUser()){
@@ -25,11 +22,8 @@ export async function submitSolution(channel: vscode.OutputChannel): Promise<voi
2522
constfilePath:string=textEditor.document.uri.fsPath;
2623
try{
2724
constresult:string=awaitexecuteCommand(channel,"node",[leetCodeBinaryPath,"submit",filePath]);
28-
constresultPath:string=path.join(os.homedir(),".leetcode","Result");
29-
awaitfse.ensureFile(resultPath);
30-
awaitfse.writeFile(resultPath,result);
31-
awaitvscode.window.showTextDocument(vscode.Uri.file(resultPath));
25+
awaitshowResultFile(result);
3226
}catch(error){
33-
awaitpromptForOpenOutputChannel("Failed to submit the solution. Please open the output channel for details",DialogType.error,channel);
27+
awaitpromptForOpenOutputChannel("Failed to submit the solution. Please open the output channel for details.",DialogType.error,channel);
3428
}
3529
}

‎src/commands/test.ts

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
"use strict";
2+
3+
import*asfsefrom"fs-extra";
4+
import*asvscodefrom"vscode";
5+
import{leetCodeManager}from"../leetCodeManager";
6+
import{IQuickItemEx,leetCodeBinaryPath,UserStatus}from"../shared";
7+
import{executeCommand}from"../utils/cpUtils";
8+
import{DialogType,promptForOpenOutputChannel,showFileSelectDialog,showResultFile}from"../utils/uiUtils";
9+
10+
exportasyncfunctiontestSolution(channel:vscode.OutputChannel):Promise<void>{
11+
try{
12+
if(leetCodeManager.getStatus()===UserStatus.SignedOut){
13+
return;
14+
}
15+
16+
constactiveText:vscode.TextEditor|undefined=vscode.window.activeTextEditor;
17+
if(!activeText){
18+
vscode.window.showErrorMessage("Please open a LeetCode solution file first.");
19+
return;
20+
}
21+
22+
constfilePath=activeText.document.uri.fsPath;
23+
constpicks:Array<IQuickItemEx<string>>=[];
24+
picks.push(
25+
{
26+
label:"$(three-bars) Default test cases",
27+
description:"",
28+
detail:"Test with the default cases",
29+
value:":default",
30+
},
31+
{
32+
label:"$(pencil) Write directly...",
33+
description:"",
34+
detail:"Write test cases in input box",
35+
value:":direct",
36+
},
37+
{
38+
label:"$(file-text) Browse...",
39+
description:"",
40+
detail:"Test with the writen cases in file",
41+
value:":file",
42+
},
43+
);
44+
constchoice:IQuickItemEx<string>|undefined=awaitvscode.window.showQuickPick(picks);
45+
if(!choice){
46+
return;
47+
}
48+
49+
letresult:string|undefined;
50+
switch(choice.value){
51+
case":default":
52+
result=awaitexecuteCommand(channel,"node",[leetCodeBinaryPath,"test",filePath]);
53+
break;
54+
case":direct":
55+
consttestString:string|undefined=awaitvscode.window.showInputBox({
56+
prompt:"Enter the test cases.",
57+
validateInput:(s:string)=>s&&s.trim() ?undefined :"Test case must not be empty.",
58+
placeHolder:"Example: [1,2,3]\\n4",
59+
ignoreFocusOut:true,
60+
});
61+
if(testString){
62+
result=awaitexecuteCommand(channel,"node",[leetCodeBinaryPath,"test",filePath,"-t",`"${testString.replace(/"/g,"")}"`]);
63+
}
64+
break;
65+
case":file":
66+
consttestFile:vscode.Uri[]|undefined=awaitshowFileSelectDialog();
67+
if(testFile&&testFile.length){
68+
constinput:string=awaitfse.readFile(testFile[0].fsPath,"utf-8");
69+
if(input.trim()){
70+
result=awaitexecuteCommand(channel,"node",[leetCodeBinaryPath,"test",filePath,"-t",`"${input.replace(/"/g,"").replace(/\r?\n/g,"\\n")}"`]);
71+
}else{
72+
vscode.window.showErrorMessage("The selected test file must not be empty.");
73+
}
74+
}
75+
break;
76+
default:
77+
break;
78+
}
79+
if(!result){
80+
return;
81+
}
82+
awaitshowResultFile(result);
83+
}catch(error){
84+
awaitpromptForOpenOutputChannel("Failed to test the solution. Please open the output channel for details.",DialogType.error,channel);
85+
}
86+
}

‎src/extension.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as vscode from "vscode";
44
import*assessionfrom"./commands/session";
55
import*asshowfrom"./commands/show";
66
import*assubmitfrom"./commands/submit";
7+
import*astestfrom"./commands/test";
78
import{LeetCodeNode,LeetCodeTreeDataProvider}from"./leetCodeExplorer";
89
import{leetCodeManager}from"./leetCodeManager";
910
import{leetCodeStatusBarItem}from"./leetCodeStatusBarItem";
@@ -26,6 +27,7 @@ export async function activate(context: vscode.ExtensionContext) {
2627
vscode.commands.registerCommand("leetcode.showProblem",(node:LeetCodeNode)=>show.showProblem(channel,node)),
2728
vscode.commands.registerCommand("leetcode.searchProblem",()=>show.searchProblem(channel)),
2829
vscode.commands.registerCommand("leetcode.refreshExplorer",()=>leetCodeTreeDataProvider.refresh()),
30+
vscode.commands.registerCommand("leetcode.testSolution",()=>test.testSolution(channel)),
2931
vscode.commands.registerCommand("leetcode.submitSolution",()=>submit.submitSolution(channel)),
3032
);
3133

‎src/utils/uiUtils.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
"use strict";
22

3+
import*asfsefrom"fs-extra";
34
import*asopnfrom"opn";
5+
import*asosfrom"os";
6+
import*aspathfrom"path";
47
import*asvscodefrom"vscode";
58

69
exportnamespaceDialogOptions{
@@ -51,6 +54,25 @@ export async function promptForSignIn(): Promise<void> {
5154
}
5255
}
5356

57+
exportasyncfunctionshowFileSelectDialog():Promise<vscode.Uri[]|undefined>{
58+
constdefaultUri:vscode.Uri|undefined=vscode.workspace.rootPath ?vscode.Uri.file(vscode.workspace.rootPath) :undefined;
59+
constoptions:vscode.OpenDialogOptions={
60+
defaultUri,
61+
canSelectFiles:true,
62+
canSelectFolders:false,
63+
canSelectMany:false,
64+
openLabel:"Select",
65+
};
66+
returnawaitvscode.window.showOpenDialog(options);
67+
}
68+
69+
exportasyncfunctionshowResultFile(result:string):Promise<void>{
70+
constresultPath:string=path.join(os.homedir(),".leetcode","Result");
71+
awaitfse.ensureFile(resultPath);
72+
awaitfse.writeFile(resultPath,result);
73+
awaitvscode.window.showTextDocument(vscode.Uri.file(resultPath));
74+
}
75+
5476
exportenumDialogType{
5577
info="info",
5678
warning="warning",

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp