Skip to content

Commit c99b38b

Browse files
sheche@microsoft.comsheche@microsoft.com
sheche@microsoft.com
authored and
sheche@microsoft.com
committed
enable submit
1 parent 9debda7 commit c99b38b

File tree

4 files changed

+47
-3
lines changed

4 files changed

+47
-3
lines changed

package.json

+9-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"onCommand:leetcode.refreshExplorer",
1818
"onCommand:leetcode.showProblem",
1919
"onCommand:leetcode.searchProblem",
20+
"onCommand:leetcode.submitSolution",
2021
"onView:leetCodeExplorer"
2122
],
2223
"main": "./out/src/extension",
@@ -48,14 +49,19 @@
4849
},
4950
{
5051
"command": "leetcode.showProblem",
51-
"title": "Solve problem",
52+
"title": "Show problem",
5253
"category": "LeetCode"
5354
},
5455
{
5556
"command": "leetcode.searchProblem",
5657
"title": "Search",
5758
"category": "LeetCode",
5859
"icon": "resources/search.png"
60+
},
61+
{
62+
"command": "leetcode.submitSolution",
63+
"title": "Submit",
64+
"category": "LeetCode"
5965
}
6066
],
6167
"views": {
@@ -97,13 +103,15 @@
97103
"lint": "tslint --project tsconfig.json -e src/*.d.ts -t verbose"
98104
},
99105
"devDependencies": {
106+
"@types/fs-extra": "^5.0.0",
100107
"@types/mocha": "^2.2.42",
101108
"@types/node": "^7.0.43",
102109
"tslint": "^5.9.1",
103110
"typescript": "^2.6.1",
104111
"vscode": "^1.1.6"
105112
},
106113
"dependencies": {
114+
"fs-extra": "^5.0.0",
107115
"leetcode-cli": "2.5.1"
108116
}
109117
}

src/commands/show.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ export async function showProblem(node?: LeetCodeNode): Promise<void> {
1717
}
1818

1919
export async function searchProblem(): Promise<void> {
20-
const signInStatus = leetCodeManager.getUser();
21-
if (!signInStatus) {
20+
if (!leetCodeManager.getUser()) {
2221
return;
2322
}
2423
const choice: IQuickItemEx<string> | undefined = await vscode.window.showQuickPick(

src/commands/submit.ts

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"use strict";
2+
3+
import * as fse from "fs-extra";
4+
import * as os from "os";
5+
import * as path from "path";
6+
import * as vscode from "vscode";
7+
import { leetCodeManager } from "../leetCodeManager";
8+
import { leetCodeBinaryPath } from "../shared";
9+
import { executeCommand } from "../utils/cpUtils";
10+
import { DialogType, promptForOpenOutputChannel } from "../utils/uiUtils";
11+
12+
export async function submitSolution(): Promise<void> {
13+
if (!leetCodeManager.getUser()) {
14+
return;
15+
}
16+
const textEditor: vscode.TextEditor | undefined = vscode.window.activeTextEditor;
17+
if (!textEditor) {
18+
return;
19+
}
20+
if (!textEditor.document.save()) {
21+
vscode.window.showWarningMessage("Please save the solution file first.");
22+
return;
23+
}
24+
const filePath: string = textEditor.document.uri.fsPath;
25+
try {
26+
const result: string = await executeCommand("node", [leetCodeBinaryPath, "submit", filePath]);
27+
const resultPath: string = path.join(os.tmpdir(), "Result");
28+
await fse.ensureFile(resultPath);
29+
await fse.writeFile(resultPath, result);
30+
await vscode.window.showTextDocument(vscode.Uri.file(resultPath));
31+
} catch (error) {
32+
await promptForOpenOutputChannel("Failed to submit the solution. Please open the output channel for details", DialogType.error);
33+
}
34+
}

src/extension.ts

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import * as vscode from "vscode";
44
import * as session from "./commands/session";
55
import * as show from "./commands/show";
6+
import * as submit from "./commands/submit";
67
import { leetcodeChannel } from "./leetCodeChannel";
78
import { LeetCodeNode, LeetCodeTreeDataProvider } from "./leetCodeExplorer";
89
import { leetCodeManager } from "./leetCodeManager";
@@ -18,6 +19,8 @@ export function activate(context: vscode.ExtensionContext) {
1819
vscode.commands.registerCommand("leetcode.selectSessions", () => session.selectSession()),
1920
vscode.commands.registerCommand("leetcode.showProblem", (node: LeetCodeNode) => show.showProblem(node)),
2021
vscode.commands.registerCommand("leetcode.searchProblem", () => show.searchProblem()),
22+
vscode.commands.registerCommand("leetcode.refreshExplorer", () => leetCodeTreeDataProvider.refresh()),
23+
vscode.commands.registerCommand("leetcode.submitSolution", () => submit.submitSolution()),
2124
);
2225
leetCodeManager.on("statusChanged", () => {
2326
leetCodeStatusBarItem.updateStatusBar(leetCodeManager.getStatus(), leetCodeManager.getUser());

0 commit comments

Comments
 (0)