Skip to content

feat: support load code from local file for issue 59 #149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,600 changes: 433 additions & 1,167 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@
},
"devDependencies": {
"@types/fs-extra": "5.0.0",
"@types/lodash.kebabcase": "^4.1.5",
"@types/mocha": "^2.2.42",
"@types/node": "^7.0.43",
"@types/require-from-string": "^1.2.0",
Expand All @@ -277,6 +278,7 @@
"dependencies": {
"fs-extra": "^6.0.1",
"leetcode-cli": "2.6.1",
"lodash.kebabcase": "^4.1.1",
"require-from-string": "^2.0.2"
}
}
12 changes: 3 additions & 9 deletions src/commands/show.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,10 @@ async function showProblemInternal(node: IProblem): Promise<void> {

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

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

if (!defaultLanguage && leetCodeConfig.get<boolean>("showSetDefaultLanguageHint")) {
const choice: vscode.MessageItem | undefined = await vscode.window.showInformationMessage(
Expand Down
15 changes: 12 additions & 3 deletions src/leetCodeExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import * as fse from "fs-extra";
import * as path from "path";
import * as requireFromString from "require-from-string";
import * as vscode from "vscode";
import { Endpoint } from "./shared";
import { Endpoint, IProblem } from "./shared";
import { executeCommand, executeCommandWithProgress } from "./utils/cpUtils";
import { genFileName } from "./utils/problemUtils";
import { DialogOptions, openUrl } from "./utils/uiUtils";
import * as wsl from "./utils/wslUtils";

Expand Down Expand Up @@ -74,8 +75,16 @@ class LeetCodeExecutor {
);
}

public async showProblem(id: string, language: string, outDir: string): Promise<string> {
return await this.executeCommandWithProgressEx("Fetching problem data...", "node", [await this.getLeetCodeBinaryPath(), "show", id, "-gx", "-l", language, "-o", `"${outDir}"`]);
public async showProblem(node: IProblem, language: string, outDir: string): Promise<string> {
const fileName: string = genFileName(node, language);
const filePath: string = path.join(outDir, fileName);

if (!await fse.pathExists(filePath)) {
const codeTemplate: string = await this.executeCommandWithProgressEx("Fetching problem data...", "node", [await this.getLeetCodeBinaryPath(), "show", node.id, "-cx", "-l", language]);
await fse.writeFile(filePath, codeTemplate);
}

return filePath;
}

public async listSessions(): Promise<string> {
Expand Down
17 changes: 17 additions & 0 deletions src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,23 @@ export const languages: string[] = [
"swift",
];

export const langExt: Map<string, string> = new Map([
["bash", "sh"],
["c", "c"],
["cpp", "cpp"],
["csharp", "cs"],
["golang", "go"],
["java", "java"],
["javascript", "js"],
["kotlin", "kt"],
["mysql", "sql"],
["python", "py"],
["python3", "py"],
["ruby", "rb"],
["scala", "scala"],
["swift", "swift"],
]);

export enum ProblemState {
AC = 1,
NotAC = 2,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/osUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export function isWindows(): boolean {
}

export function usingCmd(): boolean {
const comSpec: string = process.env.ComSpec;
const comSpec: string | undefined = process.env.ComSpec;
// 'cmd.exe' is used as a fallback if process.env.ComSpec is unavailable.
if (!comSpec) {
return true;
Expand Down
16 changes: 16 additions & 0 deletions src/utils/problemUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import kebabCase = require("lodash.kebabcase");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use import instead of require.

Copy link
Contributor Author

@poppinlp poppinlp Feb 22, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's because of the issue of lodash. T_T
Another way is to change the tsconfig.json. But I think its impact may be even bigger, so I wrote it like this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, it's fine.

import { IProblem, langExt } from "../shared";

export function genFileExt(language: string): string {
const ext: string | undefined = langExt.get(language);
if (!ext) {
throw new Error(`The language "${language}" is not supported.`);
}
return ext;
}

export function genFileName(node: IProblem, language: string): string {
const slug: string = kebabCase(node.name);
const ext: string = genFileExt(language);
return `${node.id}.${slug}.${ext}`;
}