Skip to content

Commit 5fc09dd

Browse files
authored
Merge pull request #1 from jdneo/master
catch up
2 parents b781eef + 7092f8d commit 5fc09dd

File tree

12 files changed

+534
-1206
lines changed

12 files changed

+534
-1206
lines changed

ACKNOWLEDGEMENTS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ A big thanks to the following individuals for contributing:
88
- [@Vigilans](https://github.com/Vigilans) for contributing [#94](https://github.com/jdneo/vscode-leetcode/pull/94)
99
- [@ringcrl](https://github.com/ringcrl) for contributing [#123](https://github.com/jdneo/vscode-leetcode/pull/123)
1010
- [@pujiaxun](https://github.com/pujiaxun) for contributing [#143](https://github.com/jdneo/vscode-leetcode/pull/143)
11+
- [@edvardchen](https://github.com/edvardchen) for contributing [#147](https://github.com/jdneo/vscode-leetcode/pull/147)
12+
- [@poppinlp](https://github.com/poppinlp) for contributing [#149](https://github.com/jdneo/vscode-leetcode/pull/149)

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@ 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-
## [Unreleased]
6+
## [0.11.0]
77
## Added
88
- Add new setting: `leetcode.outputFolder` to customize the sub-directory to save the files generated by 'Show Problem' [#119](https://github.com/jdneo/vscode-leetcode/issues/119)
99
- Add tooltips for sub-category nodes in LeetCode Explorer [#143](https://github.com/jdneo/vscode-leetcode/pull/143)
1010

11+
## Changed
12+
- Now when triggering 'Show Problem', the extension will not generate a new file if it already exists [#59](https://github.com/jdneo/vscode-leetcode/issues/59)
13+
14+
## Fixed
15+
- Log in timeout when proxy is enabled [#117](https://github.com/jdneo/vscode-leetcode/issues/117)
16+
1117
## [0.10.2]
1218
## Fixed
1319
- Test cases cannot have double quotes [#60](https://github.com/jdneo/vscode-leetcode/issues/60)

package-lock.json

Lines changed: 434 additions & 1168 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 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.10.2",
5+
"version": "0.11.0",
66
"author": "Sheng Chen",
77
"publisher": "shengchen",
88
"license": "MIT",
@@ -267,6 +267,7 @@
267267
},
268268
"devDependencies": {
269269
"@types/fs-extra": "5.0.0",
270+
"@types/lodash.kebabcase": "^4.1.5",
270271
"@types/mocha": "^2.2.42",
271272
"@types/node": "^7.0.43",
272273
"@types/require-from-string": "^1.2.0",
@@ -277,6 +278,7 @@
277278
"dependencies": {
278279
"fs-extra": "^6.0.1",
279280
"leetcode-cli": "2.6.1",
281+
"lodash.kebabcase": "^4.1.1",
280282
"require-from-string": "^2.0.2"
281283
}
282284
}

src/commands/show.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,10 @@ async function showProblemInternal(node: IProblem): Promise<void> {
6565

6666
outDir = path.join(outDir, relativePath);
6767
await fse.ensureDir(outDir);
68-
const result: string = await leetCodeExecutor.showProblem(node.id, language, outDir);
69-
const reg: RegExp = /\* Source Code:\s*(.*)/;
70-
const match: RegExpMatchArray | null = result.match(reg);
71-
if (match && match.length >= 2) {
72-
const filePath: string = wsl.useWsl() ? await wsl.toWinPath(match[1].trim()) : match[1].trim();
7368

74-
await vscode.window.showTextDocument(vscode.Uri.file(filePath), { preview: false });
75-
} else {
76-
throw new Error("Failed to fetch the problem information.");
77-
}
69+
const originFilePath: string = await leetCodeExecutor.showProblem(node, language, outDir);
70+
const filePath: string = wsl.useWsl() ? await wsl.toWinPath(originFilePath) : originFilePath;
71+
await vscode.window.showTextDocument(vscode.Uri.file(filePath), { preview: false });
7872

7973
if (!defaultLanguage && leetCodeConfig.get<boolean>("showSetDefaultLanguageHint")) {
8074
const choice: vscode.MessageItem | undefined = await vscode.window.showInformationMessage(

src/explorer/LeetCodeTreeDataProvider.ts

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -65,27 +65,24 @@ export class LeetCodeTreeDataProvider implements vscode.TreeDataProvider<LeetCod
6565
];
6666
}
6767
if (!element) { // Root view
68-
return new Promise(async (resolve: (res: LeetCodeNode[]) => void): Promise<void> => {
69-
await this.getProblemData();
70-
resolve([
71-
new LeetCodeNode(Object.assign({}, defaultProblem, {
72-
id: Category.Difficulty,
73-
name: Category.Difficulty,
74-
}), "ROOT", false),
75-
new LeetCodeNode(Object.assign({}, defaultProblem, {
76-
id: Category.Tag,
77-
name: Category.Tag,
78-
}), "ROOT", false),
79-
new LeetCodeNode(Object.assign({}, defaultProblem, {
80-
id: Category.Company,
81-
name: Category.Company,
82-
}), "ROOT", false),
83-
new LeetCodeNode(Object.assign({}, defaultProblem, {
84-
id: Category.Favorite,
85-
name: Category.Favorite,
86-
}), "ROOT", false),
87-
]);
88-
});
68+
return [
69+
new LeetCodeNode(Object.assign({}, defaultProblem, {
70+
id: Category.Difficulty,
71+
name: Category.Difficulty,
72+
}), "ROOT", false),
73+
new LeetCodeNode(Object.assign({}, defaultProblem, {
74+
id: Category.Tag,
75+
name: Category.Tag,
76+
}), "ROOT", false),
77+
new LeetCodeNode(Object.assign({}, defaultProblem, {
78+
id: Category.Company,
79+
name: Category.Company,
80+
}), "ROOT", false),
81+
new LeetCodeNode(Object.assign({}, defaultProblem, {
82+
id: Category.Favorite,
83+
name: Category.Favorite,
84+
}), "ROOT", false),
85+
];
8986
} else {
9087
switch (element.name) { // First-level
9188
case Category.Favorite:

src/leetCodeExecutor.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import * as fse from "fs-extra";
66
import * as path from "path";
77
import * as requireFromString from "require-from-string";
88
import * as vscode from "vscode";
9-
import { Endpoint } from "./shared";
9+
import { Endpoint, IProblem } from "./shared";
1010
import { executeCommand, executeCommandWithProgress } from "./utils/cpUtils";
11+
import { genFileName } from "./utils/problemUtils";
1112
import { DialogOptions, openUrl } from "./utils/uiUtils";
1213
import * as wsl from "./utils/wslUtils";
1314

@@ -74,8 +75,16 @@ class LeetCodeExecutor {
7475
);
7576
}
7677

77-
public async showProblem(id: string, language: string, outDir: string): Promise<string> {
78-
return await this.executeCommandWithProgressEx("Fetching problem data...", "node", [await this.getLeetCodeBinaryPath(), "show", id, "-gx", "-l", language, "-o", `"${outDir}"`]);
78+
public async showProblem(node: IProblem, language: string, outDir: string): Promise<string> {
79+
const fileName: string = genFileName(node, language);
80+
const filePath: string = path.join(outDir, fileName);
81+
82+
if (!await fse.pathExists(filePath)) {
83+
const codeTemplate: string = await this.executeCommandWithProgressEx("Fetching problem data...", "node", [await this.getLeetCodeBinaryPath(), "show", node.id, "-cx", "-l", language]);
84+
await fse.writeFile(filePath, codeTemplate);
85+
}
86+
87+
return filePath;
7988
}
8089

8190
public async listSessions(): Promise<string> {

src/leetCodeManager.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as vscode from "vscode";
77
import { leetCodeChannel } from "./leetCodeChannel";
88
import { leetCodeExecutor } from "./leetCodeExecutor";
99
import { UserStatus } from "./shared";
10+
import { createEnvOption } from "./utils/cpUtils";
1011
import { DialogType, promptForOpenOutputChannel } from "./utils/uiUtils";
1112
import * as wsl from "./utils/wslUtils";
1213

@@ -42,7 +43,10 @@ class LeetCodeManager extends EventEmitter {
4243

4344
const childProc: cp.ChildProcess = wsl.useWsl()
4445
? cp.spawn("wsl", ["node", leetCodeBinaryPath, "user", "-l"], { shell: true })
45-
: cp.spawn("node", [leetCodeBinaryPath, "user", "-l"], { shell: true });
46+
: cp.spawn("node", [leetCodeBinaryPath, "user", "-l"], {
47+
shell: true,
48+
env: createEnvOption(),
49+
});
4650

4751
childProc.stdout.on("data", (data: string | Buffer) => {
4852
data = data.toString();

src/shared.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,23 @@ export const languages: string[] = [
2929
"swift",
3030
];
3131

32+
export const langExt: Map<string, string> = new Map([
33+
["bash", "sh"],
34+
["c", "c"],
35+
["cpp", "cpp"],
36+
["csharp", "cs"],
37+
["golang", "go"],
38+
["java", "java"],
39+
["javascript", "js"],
40+
["kotlin", "kt"],
41+
["mysql", "sql"],
42+
["python", "py"],
43+
["python3", "py"],
44+
["ruby", "rb"],
45+
["scala", "scala"],
46+
["swift", "swift"],
47+
]);
48+
3249
export enum ProblemState {
3350
AC = 1,
3451
NotAC = 2,

src/utils/cpUtils.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export async function executeCommand(command: string, args: string[], options: c
99
return new Promise((resolve: (res: string) => void, reject: (e: Error) => void): void => {
1010
let result: string = "";
1111

12-
const childProc: cp.ChildProcess = cp.spawn(command, args, options);
12+
const childProc: cp.ChildProcess = cp.spawn(command, args, { ...options, env: createEnvOption() });
1313

1414
childProc.stdout.on("data", (data: string | Buffer) => {
1515
data = data.toString();
@@ -45,3 +45,18 @@ export async function executeCommandWithProgress(message: string, command: strin
4545
});
4646
return result;
4747
}
48+
49+
// clone process.env and add http proxy
50+
export function createEnvOption(): {} {
51+
const proxy: string | undefined = getHttpAgent();
52+
if (proxy) {
53+
const env: any = Object.create(process.env);
54+
env.http_proxy = proxy;
55+
return env;
56+
}
57+
return process.env;
58+
}
59+
60+
function getHttpAgent(): string | undefined {
61+
return vscode.workspace.getConfiguration("http").get<string>("proxy");
62+
}

src/utils/osUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export function isWindows(): boolean {
66
}
77

88
export function usingCmd(): boolean {
9-
const comSpec: string = process.env.ComSpec;
9+
const comSpec: string | undefined = process.env.ComSpec;
1010
// 'cmd.exe' is used as a fallback if process.env.ComSpec is unavailable.
1111
if (!comSpec) {
1212
return true;

src/utils/problemUtils.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import kebabCase = require("lodash.kebabcase");
2+
import { IProblem, langExt } from "../shared";
3+
4+
export function genFileExt(language: string): string {
5+
const ext: string | undefined = langExt.get(language);
6+
if (!ext) {
7+
throw new Error(`The language "${language}" is not supported.`);
8+
}
9+
return ext;
10+
}
11+
12+
export function genFileName(node: IProblem, language: string): string {
13+
const slug: string = kebabCase(node.name);
14+
const ext: string = genFileExt(language);
15+
return `${node.id}.${slug}.${ext}`;
16+
}

0 commit comments

Comments
 (0)