@@ -23,24 +23,49 @@ import * as list from "./list";
2323
2424export async function previewProblem ( input : IProblem | vscode . Uri , isSideMode : boolean = false ) : Promise < void > {
2525 let node : IProblem ;
26- if ( input instanceof vscode . Uri ) {
27- const activeFilePath : string = input . fsPath ;
28- const id : string = await getNodeIdFromFile ( activeFilePath ) ;
26+ // I dont know why I use this command, it not work,maybe theproblem of "input"?
27+ // 不知道为什么,当我直接使用这个命令,他并不会正常工作,也许input的问题
28+
29+ //if (input instanceof vscode.Uri) {
30+ // const activeFilePath: string = input.fsPath;
31+ // const id: string = await getNodeIdFromFile(activeFilePath);
32+
33+ let id = "" ;
34+
35+ // * We can read file throught editor.document not fs
36+ // * 我们可以直接用自带API读取文件内容,从而跳过input判断
37+ const editor = vscode . window . activeTextEditor ;
38+
39+ if ( editor ) {
40+ const fsPath = editor . document . fileName ;
41+ let fileContent = editor . document . getText ( ) ;
42+
43+ const matchResults = fileContent . match ( / @ l c .+ i d = ( .+ ?) / ) ;
44+ if ( matchResults && matchResults . length === 2 ) {
45+ id = matchResults [ 1 ] ;
46+ }
47+
48+ // Try to get id from file name if getting from comments failed
2949 if ( ! id ) {
30- vscode . window . showErrorMessage ( `Failed to resolve the problem id from file: ${ activeFilePath } .` ) ;
31- return ;
50+ id = path . basename ( fsPath ) . split ( "." ) [ 0 ] ;
3251 }
33- const cachedNode : IProblem | undefined = explorerNodeManager . getNodeById ( id ) ;
34- if ( ! cachedNode ) {
35- vscode . window . showErrorMessage ( `Failed to resolve the problem with id : ${ id } .` ) ;
52+
53+ if ( ! id ) {
54+ vscode . window . showErrorMessage ( `Failed to resolve the problem id from file : ${ fsPath } .` ) ;
3655 return ;
3756 }
38- node = cachedNode ;
39- // Move the preview page aside if it's triggered from Code Lens
40- isSideMode = true ;
41- } else {
42- node = input ;
4357 }
58+ const cachedNode : IProblem | undefined = explorerNodeManager . getNodeById ( id ) ;
59+ if ( ! cachedNode ) {
60+ vscode . window . showErrorMessage ( `Failed to resolve the problem with id: ${ id } .` ) ;
61+ return ;
62+ }
63+ node = cachedNode ;
64+ // Move the preview page aside if it's triggered from Code Lens
65+ isSideMode = true ;
66+ // } else {
67+ // node = input;
68+ // }
4469 const needTranslation : boolean = settingUtils . shouldUseEndpointTranslation ( ) ;
4570 const descString : string = await leetCodeExecutor . getDescription ( node . id , needTranslation ) ;
4671 leetCodePreviewProvider . show ( descString , node , isSideMode ) ;
0 commit comments