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

Commitcdb46d0

Browse files
authored
feat: Support open preview page through Code Lens and context menu (LeetCode-OpenSource#338)
1 parente6cd998 commitcdb46d0

File tree

5 files changed

+56
-5
lines changed

5 files changed

+56
-5
lines changed

‎package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,10 @@
225225
{
226226
"command":"leetcode.showSolution",
227227
"group":"leetcode@3"
228+
},
229+
{
230+
"command":"leetcode.previewProblem",
231+
"group":"leetcode@4"
228232
}
229233
]
230234
},

‎src/codelens/CustomCodeLensProvider.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ export class CustomCodeLensProvider implements vscode.CodeLensProvider {
3232
command:"leetcode.showSolution",
3333
arguments:[document.uri],
3434
}),
35+
newvscode.CodeLens(range,{
36+
title:"Preview",
37+
command:"leetcode.previewProblem",
38+
arguments:[document.uri],
39+
}),
3540
];
3641
}
3742
}

‎src/commands/show.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,45 @@ import * as fse from "fs-extra";
55
import*aspathfrom"path";
66
import*asunescapeJSfrom"unescape-js";
77
import*asvscodefrom"vscode";
8+
import{explorerNodeManager}from"../explorer/explorerNodeManager";
89
import{LeetCodeNode}from"../explorer/LeetCodeNode";
910
import{leetCodeChannel}from"../leetCodeChannel";
1011
import{leetCodeExecutor}from"../leetCodeExecutor";
1112
import{leetCodeManager}from"../leetCodeManager";
1213
import{IProblem,IQuickItemEx,languages,ProblemState}from"../shared";
14+
import{getNodeIdFromFile}from"../utils/problemUtils";
1315
import{DialogOptions,DialogType,openSettingsEditor,promptForOpenOutputChannel,promptForSignIn,promptHintMessage}from"../utils/uiUtils";
1416
import{selectWorkspaceFolder}from"../utils/workspaceUtils";
1517
import*aswslfrom"../utils/wslUtils";
1618
import{leetCodePreviewProvider}from"../webview/leetCodePreviewProvider";
1719
import{leetCodeSolutionProvider}from"../webview/leetCodeSolutionProvider";
1820
import*aslistfrom"./list";
1921

20-
exportasyncfunctionpreviewProblem(node:IProblem,isSideMode:boolean=false):Promise<void>{
21-
constdescString:string=awaitleetCodeExecutor.getDescription(node);
22+
exportasyncfunctionpreviewProblem(input:IProblem|vscode.Uri,isSideMode:boolean=false):Promise<void>{
23+
letnode:LeetCodeNode;
24+
if(inputinstanceofLeetCodeNode){
25+
node=input;
26+
}elseif(inputinstanceofvscode.Uri){
27+
constactiveFilePath:string=input.fsPath;
28+
constid:string=awaitgetNodeIdFromFile(activeFilePath);
29+
if(!id){
30+
vscode.window.showErrorMessage(`Failed to resolve the problem id from file:${activeFilePath}.`);
31+
return;
32+
}
33+
constcachedNode:LeetCodeNode|undefined=explorerNodeManager.getNodeById(id);
34+
if(!cachedNode){
35+
vscode.window.showErrorMessage(`Failed to resolve the problem with id:${id}.`);
36+
return;
37+
}
38+
node=cachedNode;
39+
// Move the preview page aside if it's triggered from Code Lens
40+
isSideMode=true;
41+
}else{
42+
vscode.window.showErrorMessage("Invalid input to fetch the preview data.");
43+
return;
44+
}
45+
46+
constdescString:string=awaitleetCodeExecutor.getDescription(node.id);
2247
leetCodePreviewProvider.show(descString,node,isSideMode);
2348
}
2449

@@ -54,7 +79,7 @@ export async function showSolution(input: LeetCodeNode | vscode.Uri): Promise<vo
5479
}elseif(inputinstanceofvscode.Uri){
5580
problemInput=`"${input.fsPath}"`;
5681
}else{
57-
vscode.window.showErrorMessage("Invalid input to fetch the solution data");
82+
vscode.window.showErrorMessage("Invalid input to fetch the solution data.");
5883
return;
5984
}
6085

‎src/leetCodeExecutor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ class LeetCodeExecutor implements Disposable {
112112
returnsolution;
113113
}
114114

115-
publicasyncgetDescription(problemNode:IProblem):Promise<string>{
116-
returnawaitthis.executeCommandWithProgressEx("Fetching problem description...",this.nodeExecutable,[awaitthis.getLeetCodeBinaryPath(),"show",problemNode.id,"-x"]);
115+
publicasyncgetDescription(problemNodeId:string):Promise<string>{
116+
returnawaitthis.executeCommandWithProgressEx("Fetching problem description...",this.nodeExecutable,[awaitthis.getLeetCodeBinaryPath(),"show",problemNodeId,"-x"]);
117117
}
118118

119119
publicasynclistSessions():Promise<string>{

‎src/utils/problemUtils.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// Copyright (c) jdneo. All rights reserved.
22
// Licensed under the MIT license.
33

4+
import*asfsefrom"fs-extra";
45
import*as_from"lodash";
6+
import*aspathfrom"path";
57
import{IProblem,langExt}from"../shared";
68

79
exportfunctiongenFileExt(language:string):string{
@@ -17,3 +19,18 @@ export function genFileName(node: IProblem, language: string): string {
1719
constext:string=genFileExt(language);
1820
return`${node.id}.${slug}.${ext}`;
1921
}
22+
23+
exportasyncfunctiongetNodeIdFromFile(fsPath:string):Promise<string>{
24+
constfileContent:string=awaitfse.readFile(fsPath,"utf8");
25+
letid:string="";
26+
constmatchResults:RegExpMatchArray|null=fileContent.match(/@lc.+id=(.+?)/);
27+
if(matchResults&&matchResults.length===2){
28+
id=matchResults[1];
29+
}
30+
// Try to get id from file name if getting from comments failed
31+
if(!id){
32+
id=path.basename(fsPath).split(".")[0];
33+
}
34+
35+
returnid;
36+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp