Skip to content

Commit 4ddd1ea

Browse files
authored
support trigger test command in file explorer (LeetCode-OpenSource#30)
1 parent f569bb2 commit 4ddd1ea

File tree

5 files changed

+37
-28
lines changed

5 files changed

+37
-28
lines changed

package.json

+9-3
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
},
8282
{
8383
"command": "leetcode.testSolution",
84-
"title": "Test",
84+
"title": "Test in LeetCode",
8585
"category": "LeetCode"
8686
},
8787
{
@@ -115,7 +115,7 @@
115115
{
116116
"command": "leetcode.showProblem",
117117
"when": "view == leetCodeExplorer && viewItem == problem",
118-
"group": "1@1"
118+
"group": "leetcode-explorer@1"
119119
}
120120
],
121121
"commandPalette": [
@@ -125,9 +125,15 @@
125125
}
126126
],
127127
"explorer/context": [
128+
{
129+
"command": "leetcode.testSolution",
130+
"when": "explorerResourceIsFolder == false",
131+
"group": "file-explorer@1"
132+
},
128133
{
129134
"command": "leetcode.submitSolution",
130-
"when": "explorerResourceIsFolder == false"
135+
"when": "explorerResourceIsFolder == false",
136+
"group": "file-explorer@2"
131137
}
132138
]
133139
},

src/commands/submit.ts

+4-13
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,17 @@ import { leetCodeManager } from "../leetCodeManager";
55
import { leetCodeBinaryPath } from "../shared";
66
import { executeCommand } from "../utils/cpUtils";
77
import { DialogType, promptForOpenOutputChannel, promptForSignIn, showResultFile } from "../utils/uiUtils";
8+
import { getActivefilePath } from "../utils/workspaceUtils";
89

910
export async function submitSolution(channel: vscode.OutputChannel, uri?: vscode.Uri): Promise<void> {
1011
if (!leetCodeManager.getUser()) {
1112
promptForSignIn();
1213
return;
1314
}
1415

15-
let filePath: string;
16-
if (uri) {
17-
filePath = uri.fsPath;
18-
} else {
19-
const textEditor: vscode.TextEditor | undefined = vscode.window.activeTextEditor;
20-
if (!textEditor) {
21-
return;
22-
}
23-
if (!textEditor.document.save()) {
24-
vscode.window.showWarningMessage("Please save the solution file first.");
25-
return;
26-
}
27-
filePath = textEditor.document.uri.fsPath;
16+
const filePath: string | undefined = await getActivefilePath(uri);
17+
if (!filePath) {
18+
return;
2819
}
2920

3021
try {

src/commands/test.ts

+4-10
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,18 @@ import { leetCodeManager } from "../leetCodeManager";
66
import { IQuickItemEx, leetCodeBinaryPath, UserStatus } from "../shared";
77
import { executeCommand } from "../utils/cpUtils";
88
import { DialogType, promptForOpenOutputChannel, showFileSelectDialog, showResultFile } from "../utils/uiUtils";
9+
import { getActivefilePath } from "../utils/workspaceUtils";
910

10-
export async function testSolution(channel: vscode.OutputChannel): Promise<void> {
11+
export async function testSolution(channel: vscode.OutputChannel, uri?: vscode.Uri): Promise<void> {
1112
try {
1213
if (leetCodeManager.getStatus() === UserStatus.SignedOut) {
1314
return;
1415
}
1516

16-
const activeText: vscode.TextEditor | undefined = vscode.window.activeTextEditor;
17-
if (!activeText) {
18-
vscode.window.showErrorMessage("Please open a LeetCode solution file first.");
17+
const filePath: string | undefined = await getActivefilePath(uri);
18+
if (!filePath) {
1919
return;
2020
}
21-
if (!activeText.document.save()) {
22-
vscode.window.showWarningMessage("Please save the solution file first.");
23-
return;
24-
}
25-
26-
const filePath = activeText.document.uri.fsPath;
2721
const picks: Array<IQuickItemEx<string>> = [];
2822
picks.push(
2923
{

src/extension.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ export async function activate(context: vscode.ExtensionContext) {
2727
vscode.commands.registerCommand("leetcode.showProblem", (node: LeetCodeNode) => show.showProblem(channel, node)),
2828
vscode.commands.registerCommand("leetcode.searchProblem", () => show.searchProblem(channel)),
2929
vscode.commands.registerCommand("leetcode.refreshExplorer", () => leetCodeTreeDataProvider.refresh()),
30-
vscode.commands.registerCommand("leetcode.testSolution", () => test.testSolution(channel)),
31-
vscode.commands.registerCommand("leetcode.submitSolution", (uri: vscode.Uri) => submit.submitSolution(channel, uri)),
30+
vscode.commands.registerCommand("leetcode.testSolution", (uri?: vscode.Uri) => test.testSolution(channel, uri)),
31+
vscode.commands.registerCommand("leetcode.submitSolution", (uri?: vscode.Uri) => submit.submitSolution(channel, uri)),
3232
);
3333

3434
leetCodeManager.on("statusChanged", () => {

src/utils/workspaceUtils.ts

+18
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,21 @@ export async function selectWorkspaceFolder(): Promise<string> {
1717
}
1818
return folder ? folder.uri.fsPath : path.join(os.homedir(), ".leetcode");
1919
}
20+
21+
export async function getActivefilePath(uri?: vscode.Uri): Promise<string | undefined> {
22+
let textEditor: vscode.TextEditor | undefined;
23+
if (uri) {
24+
textEditor = await vscode.window.showTextDocument(uri, { preview: false });
25+
} else {
26+
textEditor = vscode.window.activeTextEditor;
27+
}
28+
29+
if (!textEditor) {
30+
return undefined;
31+
}
32+
if (textEditor.document.isDirty && !await textEditor.document.save()) {
33+
vscode.window.showWarningMessage("Please save the solution file first.");
34+
return undefined;
35+
}
36+
return textEditor.document.uri.fsPath;
37+
}

0 commit comments

Comments
 (0)