Skip to content

Commit ef4cbf9

Browse files
committed
can show problem
1 parent 0e4120a commit ef4cbf9

File tree

7 files changed

+87
-37
lines changed

7 files changed

+87
-37
lines changed

README.md

-35
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,15 @@
11
# leetcode README
22

3-
This is the README for your extension "leetcode". After writing up a brief description, we recommend including the following sections.
4-
53
## Features
64

7-
Describe specific features of your extension including screenshots of your extension in action. Image paths are relative to this README file.
8-
9-
For example if there is an image subfolder under your extension project workspace:
10-
11-
\!\[feature X\]\(images/feature-x.png\)
12-
13-
> Tip: Many popular extensions utilize animations. This is an excellent way to show off your extension! We recommend short, focused animations that are easy to follow.
145

156
## Requirements
167

17-
If you have any requirements or dependencies, add a section describing those and how to install and configure them.
188

199
## Extension Settings
2010

21-
Include if your extension adds any VS Code settings through the `contributes.configuration` extension point.
22-
23-
For example:
24-
25-
This extension contributes the following settings:
26-
27-
* `myExtension.enable`: enable/disable this extension
28-
* `myExtension.thing`: set to `blah` to do something
2911

3012
## Known Issues
3113

32-
Calling out known issues can help limit users opening duplicate issues against your extension.
3314

3415
## Release Notes
35-
36-
Users appreciate release notes as you update your extension.
37-
38-
### 1.0.0
39-
40-
Initial release of ...
41-
42-
### 1.0.1
43-
44-
Fixed issue #.
45-
46-
### 1.1.0
47-
48-
Added features X, Y, and Z.
49-
50-
-----------------------------------------------------------------------------------------------------------

package.json

+15
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
"command": "leetcode.selectSessions",
3434
"title": "Select session",
3535
"category": "LeetCode"
36+
},
37+
{
38+
"command": "leetcode.showProblem",
39+
"title": "Solve problem",
40+
"category": "LeetCode"
3641
}
3742
],
3843
"views": {
@@ -42,6 +47,16 @@
4247
"name": "LeetCode"
4348
}
4449
]
50+
},
51+
"menus": {
52+
"view/title": [],
53+
"view/item/context": [
54+
{
55+
"command": "leetcode.showProblem",
56+
"when": "view == leetCodeExplorer && viewItem == problem",
57+
"group": "1@1"
58+
}
59+
]
4560
}
4661
},
4762
"scripts": {

src/commands/show.ts

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"use strict";
2+
3+
import * as vscode from "vscode";
4+
import { LeetCodeNode } from "../leetCodeExplorer"
5+
import { selectWorkspaceFolder } from "../utils/workspaceUtils";
6+
import { languages, leetCodeBinaryPath } from "../shared";
7+
import { executeCommand } from "../utils/cpUtils";
8+
import { DialogType, promptForOpenOutputChannel } from "../utils/uiUtils";
9+
10+
export async function showProblem(node?: LeetCodeNode): Promise<void> {
11+
let id: string;
12+
if (!node) {
13+
return;
14+
} else {
15+
id = node.id;
16+
}
17+
try {
18+
const language: string | undefined = await vscode.window.showQuickPick(languages, { placeHolder: "Select the language you want to use" });
19+
if (!language) {
20+
return;
21+
}
22+
const outdir: string = await selectWorkspaceFolder();
23+
const result: string = await executeCommand("node", [leetCodeBinaryPath, "show", id, "-gx", "-l", language, "-o", outdir]);
24+
const reg: RegExp = /\* Source Code:\s*(.*)/;
25+
const match: RegExpMatchArray | null = result.match(reg);
26+
if (match && match.length >= 2) {
27+
await vscode.window.showTextDocument(vscode.Uri.file(match[1].trim()));
28+
} else {
29+
throw new Error("Failed to fetch the problem information");
30+
}
31+
} catch (error) {
32+
await promptForOpenOutputChannel("Failed to fetch the problem information. Please open the output channel for details", DialogType.error);
33+
}
34+
}

src/extension.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
import * as vscode from "vscode";
44
import * as session from "./commands/session";
5+
import * as show from "./commands/show";
56
import { leetcodeChannel } from "./leetCodeChannel";
6-
import { LeetCodeTreeDataProvider } from "./leetCodeExplorer";
7+
import { LeetCodeTreeDataProvider, LeetCodeNode } from "./leetCodeExplorer";
78
import { leetCodeManager } from "./leetCodeManager";
89
import { leetCodeStatusBarItem } from "./leetCodeStatusBarItem";
910

@@ -15,6 +16,7 @@ export function activate(context: vscode.ExtensionContext) {
1516
vscode.commands.registerCommand("leetcode.signin", () => leetCodeManager.signIn()),
1617
vscode.commands.registerCommand("leetcode.signout", () => leetCodeManager.signOut()),
1718
vscode.commands.registerCommand("leetcode.selectSessions", () => session.selectSession()),
19+
vscode.commands.registerCommand("leetcode.showProblem", (node?: LeetCodeNode) => show.showProblem(node)),
1820
);
1921
leetCodeManager.on("statusChanged", () => {
2022
leetCodeStatusBarItem.updateStatusBar(leetCodeManager.getStatus(), leetCodeManager.getUser());

src/leetCodeStatusBarItem.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"use strict";
22

33
import * as vscode from "vscode";
4-
import { leetCodeManager } from "./leetCodeManager";
54
import { UserStatus } from "./shared";
65

76
export interface ILeetCodeStatusBarItem {

src/shared.ts

+17
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,20 @@ export enum UserStatus {
1313
SignedIn = 1,
1414
SignedOut = 2,
1515
}
16+
17+
export const languages = [
18+
"bash",
19+
"c",
20+
"cpp",
21+
"csharp",
22+
"golang",
23+
"java",
24+
"javascript",
25+
"kotlin",
26+
"mysql",
27+
"python",
28+
"python3",
29+
"ruby",
30+
"scala",
31+
"swift"
32+
];

src/utils/workspaceUtils.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"use strict";
2+
3+
import * as vscode from "vscode"
4+
import * as os from "os";
5+
6+
export async function selectWorkspaceFolder(): Promise<string> {
7+
let folder: vscode.WorkspaceFolder | undefined;
8+
if (vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0) {
9+
if (vscode.workspace.workspaceFolders.length > 1) {
10+
folder = await vscode.window.showWorkspaceFolderPick({
11+
placeHolder: "Select the working directory you wish to use",
12+
});
13+
} else {
14+
folder = vscode.workspace.workspaceFolders[0];
15+
}
16+
}
17+
return folder ? folder.uri.fsPath : os.tmpdir();
18+
}

0 commit comments

Comments
 (0)