Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7ca9b8a

Browse files
poppinlpjdneo
authored andcommitted
feat: support load code from local file for issue 59 (LeetCode-OpenSource#149)
1 parentb781eef commit7ca9b8a

File tree

7 files changed

+484
-1180
lines changed

7 files changed

+484
-1180
lines changed

‎package-lock.json

Lines changed: 433 additions & 1167 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
awaitfse.ensureDir(outDir);
68-
constresult:string=awaitleetCodeExecutor.showProblem(node.id,language,outDir);
69-
constreg:RegExp=/\*SourceCode:\s*(.*)/;
70-
constmatch:RegExpMatchArray|null=result.match(reg);
71-
if(match&&match.length>=2){
72-
constfilePath:string=wsl.useWsl() ?awaitwsl.toWinPath(match[1].trim()) :match[1].trim();
7368

74-
awaitvscode.window.showTextDocument(vscode.Uri.file(filePath),{preview:false});
75-
}else{
76-
thrownewError("Failed to fetch the problem information.");
77-
}
69+
constoriginFilePath:string=awaitleetCodeExecutor.showProblem(node,language,outDir);
70+
constfilePath:string=wsl.useWsl() ?awaitwsl.toWinPath(originFilePath) :originFilePath;
71+
awaitvscode.window.showTextDocument(vscode.Uri.file(filePath),{preview:false});
7872

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

‎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*aspathfrom"path";
77
import*asrequireFromStringfrom"require-from-string";
88
import*asvscodefrom"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*aswslfrom"./utils/wslUtils";
1314

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

77-
publicasyncshowProblem(id:string,language:string,outDir:string):Promise<string>{
78-
returnawaitthis.executeCommandWithProgressEx("Fetching problem data...","node",[awaitthis.getLeetCodeBinaryPath(),"show",id,"-gx","-l",language,"-o",`"${outDir}"`]);
78+
publicasyncshowProblem(node:IProblem,language:string,outDir:string):Promise<string>{
79+
constfileName:string=genFileName(node,language);
80+
constfilePath:string=path.join(outDir,fileName);
81+
82+
if(!awaitfse.pathExists(filePath)){
83+
constcodeTemplate:string=awaitthis.executeCommandWithProgressEx("Fetching problem data...","node",[awaitthis.getLeetCodeBinaryPath(),"show",node.id,"-cx","-l",language]);
84+
awaitfse.writeFile(filePath,codeTemplate);
85+
}
86+
87+
returnfilePath;
7988
}
8089

8190
publicasynclistSessions():Promise<string>{

‎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+
exportconstlangExt:Map<string,string>=newMap([
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
exportenumProblemState{
3350
AC=1,
3451
NotAC=2,

‎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
exportfunctionusingCmd():boolean{
9-
constcomSpec:string=process.env.ComSpec;
9+
constcomSpec:string|undefined=process.env.ComSpec;
1010
// 'cmd.exe' is used as a fallback if process.env.ComSpec is unavailable.
1111
if(!comSpec){
1212
returntrue;

‎src/utils/problemUtils.ts

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp