Skip to content

Commit

Permalink
feat: support load code from local file for issue 59 (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
poppinlp authored and jdneo committed Feb 24, 2019
1 parent b781eef commit 7ca9b8a
Show file tree
Hide file tree
Showing 7 changed files with 484 additions and 1,180 deletions.
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");
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}`;
}

0 comments on commit 7ca9b8a

Please sign in to comment.