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

Commit4ad07a8

Browse files
authored
feat: Add 'show solution' in editor context menu & code lens (LeetCode-OpenSource#313)
1 parenta521cf5 commit4ad07a8

File tree

6 files changed

+39
-24
lines changed

6 files changed

+39
-24
lines changed

‎package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,10 @@
221221
{
222222
"command":"leetcode.submitSolution",
223223
"group":"leetcode@2"
224+
},
225+
{
226+
"command":"leetcode.showSolution",
227+
"group":"leetcode@3"
224228
}
225229
]
226230
},

‎src/codelens/CustomCodeLensProvider.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,17 @@ export class CustomCodeLensProvider implements vscode.CodeLensProvider {
2020
newvscode.CodeLens(range,{
2121
title:"Submit",
2222
command:"leetcode.submitSolution",
23+
arguments:[document.uri],
2324
}),
2425
newvscode.CodeLens(range,{
2526
title:"Test",
2627
command:"leetcode.testSolution",
28+
arguments:[document.uri],
29+
}),
30+
newvscode.CodeLens(range,{
31+
title:"Solution",
32+
command:"leetcode.showSolution",
33+
arguments:[document.uri],
2734
}),
2835
];
2936
}

‎src/commands/show.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,24 @@ export async function searchProblem(): Promise<void> {
4747
awaitshowProblemInternal(choice.value);
4848
}
4949

50-
exportasyncfunctionshowSolution(node?:LeetCodeNode):Promise<void>{
51-
if(!node){
50+
exportasyncfunctionshowSolution(input:LeetCodeNode|vscode.Uri):Promise<void>{
51+
letproblemInput:string|undefined;
52+
if(inputinstanceofLeetCodeNode){
53+
problemInput=input.id;
54+
}elseif(inputinstanceofvscode.Uri){
55+
problemInput=`"${input.fsPath}"`;
56+
}else{
57+
vscode.window.showErrorMessage("Invalid input to fetch the solution data");
5258
return;
5359
}
60+
5461
constlanguage:string|undefined=awaitfetchProblemLanguage();
5562
if(!language){
5663
return;
5764
}
5865
try{
59-
constsolution:string=awaitleetCodeExecutor.showSolution(node,language);
60-
leetCodeSolutionProvider.show(unescapeJS(solution),node);
66+
constsolution:string=awaitleetCodeExecutor.showSolution(problemInput,language);
67+
leetCodeSolutionProvider.show(unescapeJS(solution));
6168
}catch(error){
6269
leetCodeChannel.appendLine(error.toString());
6370
awaitpromptForOpenOutputChannel("Failed to fetch the top voted solution. Please open the output channel for details.",DialogType.error);
@@ -133,8 +140,6 @@ async function showProblemInternal(node: IProblem): Promise<void> {
133140
asyncfunctionmovePreviewAsideIfNeeded(node:IProblem):Promise<void>{
134141
if(vscode.workspace.getConfiguration("leetcode").get<boolean>("enableSideMode",true)){
135142
returnpreviewProblem(node,true);
136-
}else{
137-
returnPromise.resolve();
138143
}
139144
}
140145

‎src/extension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
5656
vscode.commands.registerCommand("leetcode.previewProblem",(node:LeetCodeNode)=>show.previewProblem(node)),
5757
vscode.commands.registerCommand("leetcode.showProblem",(node:LeetCodeNode)=>show.showProblem(node)),
5858
vscode.commands.registerCommand("leetcode.searchProblem",()=>show.searchProblem()),
59-
vscode.commands.registerCommand("leetcode.showSolution",(node:LeetCodeNode)=>show.showSolution(node)),
59+
vscode.commands.registerCommand("leetcode.showSolution",(input:LeetCodeNode|vscode.Uri)=>show.showSolution(input)),
6060
vscode.commands.registerCommand("leetcode.refreshExplorer",()=>leetCodeTreeDataProvider.refresh()),
6161
vscode.commands.registerCommand("leetcode.testSolution",(uri?:vscode.Uri)=>test.testSolution(uri)),
6262
vscode.commands.registerCommand("leetcode.submitSolution",(uri?:vscode.Uri)=>submit.submitSolution(uri)),

‎src/leetCodeExecutor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ class LeetCodeExecutor implements Disposable {
107107
returnfilePath;
108108
}
109109

110-
publicasyncshowSolution(problemNode:IProblem,language:string):Promise<string>{
111-
constsolution:string=awaitthis.executeCommandWithProgressEx("Fetching top voted solution from discussions...",this.nodeExecutable,[awaitthis.getLeetCodeBinaryPath(),"show",problemNode.id,"--solution","-l",language]);
110+
publicasyncshowSolution(input:string,language:string):Promise<string>{
111+
constsolution:string=awaitthis.executeCommandWithProgressEx("Fetching top voted solution from discussions...",this.nodeExecutable,[awaitthis.getLeetCodeBinaryPath(),"show",input,"--solution","-l",language]);
112112
returnsolution;
113113
}
114114

‎src/webview/leetCodeSolutionProvider.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,33 @@
22
// Licensed under the MIT license.
33

44
import{ViewColumn}from"vscode";
5-
import{IProblem}from"../shared";
65
import{leetCodePreviewProvider}from"./leetCodePreviewProvider";
76
import{ILeetCodeWebviewOption,LeetCodeWebview}from"./LeetCodeWebview";
87
import{markdownEngine}from"./markdownEngine";
98

109
classLeetCodeSolutionProviderextendsLeetCodeWebview{
1110

1211
protectedreadonlyviewType:string="leetcode.solution";
12+
privateproblemName:string;
1313
privatesolution:Solution;
1414

15-
publicshow(solutionString:string,problem:IProblem):void{
16-
this.solution=this.parseSolution(solutionString,problem);
15+
publicshow(solutionString:string):void{
16+
this.solution=this.parseSolution(solutionString);
1717
this.showWebviewInternal();
1818
}
1919

2020
protectedgetWebviewOption():ILeetCodeWebviewOption{
21-
if(!leetCodePreviewProvider.isSideMode()){
22-
return{
23-
title:`${this.solution.problem}: Solution`,
24-
viewColumn:ViewColumn.One,
25-
};
26-
}else{
21+
if(leetCodePreviewProvider.isSideMode()){
2722
return{
2823
title:"Solution",
2924
viewColumn:ViewColumn.Two,
3025
preserveFocus:true,
3126
};
27+
}else{
28+
return{
29+
title:`Solution:${this.problemName}`,
30+
viewColumn:ViewColumn.One,
31+
};
3232
}
3333
}
3434

@@ -66,17 +66,17 @@ class LeetCodeSolutionProvider extends LeetCodeWebview {
6666
deletethis.solution;
6767
}
6868

69-
privateparseSolution(raw:string,problem:IProblem):Solution{
69+
privateparseSolution(raw:string):Solution{
70+
raw=raw.slice(1);// skip first empty line
71+
[this.problemName,raw]=raw.split(/\n\n([^]+)/);// parse problem name and skip one line
7072
constsolution:Solution=newSolution();
7173
// [^] matches everything including \n, yet can be replaced by . in ES2018's `m` flag
72-
raw=raw.slice(1);// skip first empty line
73-
[solution.title,raw]=raw.split(/\n\n([^]+)/);// parse title and skip one line
74-
[solution.url,raw]=raw.split(/\n\n([^]+)/);// parse url and skip one line
74+
[solution.title,raw]=raw.split(/\n\n([^]+)/);
75+
[solution.url,raw]=raw.split(/\n\n([^]+)/);
7576
[solution.lang,raw]=raw.match(/\*Lang:\s+(.+)\n([^]+)/)!.slice(1);
7677
[solution.author,raw]=raw.match(/\*Author:\s+(.+)\n([^]+)/)!.slice(1);
7778
[solution.votes,raw]=raw.match(/\*Votes:\s+(\d+)\n\n([^]+)/)!.slice(1);
7879
solution.body=raw;
79-
solution.problem=problem.name;
8080
returnsolution;
8181
}
8282
}
@@ -89,7 +89,6 @@ class Solution {
8989
publicauthor:string="";
9090
publicvotes:string="";
9191
publicbody:string="";// Markdown supported
92-
publicproblem:string="";
9392
}
9493

9594
exportconstleetCodeSolutionProvider:LeetCodeSolutionProvider=newLeetCodeSolutionProvider();

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp