@@ -23,9 +23,32 @@ 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+ const editor = vscode . window . activeTextEditor ;
33+ let id :string ;
34+
35+ // * We can read file throught editor.document not fs
36+ // * 我们可以直接用自带API读取文件内容,从而跳过input判断
37+ const editor = vscode . window . activeTextEditor ;
38+ if ( editor ) {
39+ let fileContent = editor . document . getText ( ) ;
40+
41+ const matchResults = fileContent . match ( / @ l c .+ i d = ( .+ ?) / ) ;
42+ if ( matchResults && matchResults . length === 2 ) {
43+ id = matchResults [ 1 ] ;
44+ }
45+
46+
47+ // Try to get id from file name if getting from comments failed
48+ if ( ! id ) {
49+ id = path . basename ( fsPath ) . split ( "." ) [ 0 ] ;
50+ }
51+
2952 if ( ! id ) {
3053 vscode . window . showErrorMessage ( `Failed to resolve the problem id from file: ${ activeFilePath } .` ) ;
3154 return ;
@@ -38,9 +61,9 @@ export async function previewProblem(input: IProblem | vscode.Uri, isSideMode: b
3861 node = cachedNode ;
3962 // Move the preview page aside if it's triggered from Code Lens
4063 isSideMode = true ;
41- } else {
42- node = input ;
43- }
64+ // } else {
65+ // node = input;
66+ // }
4467 const needTranslation : boolean = settingUtils . shouldUseEndpointTranslation ( ) ;
4568 const descString : string = await leetCodeExecutor . getDescription ( node . id , needTranslation ) ;
4669 leetCodePreviewProvider . show ( descString , node , isSideMode ) ;
0 commit comments