From 122e36aad854f80edafee9fe3b5df9813e7caec1 Mon Sep 17 00:00:00 2001 From: Yueli <46314093+Yuelioi@users.noreply.github.com> Date: Sun, 18 Sep 2022 16:45:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20previewProblem=20=E5=91=BD?= =?UTF-8?q?=E4=BB=A4=E4=B8=8D=E6=AD=A3=E5=B8=B8=E5=B7=A5=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 跳过input验证 直接根据当前编辑器活动文件读取/判断题目id --- src/commands/show.ts | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/src/commands/show.ts b/src/commands/show.ts index 3aebce8..403bbcb 100644 --- a/src/commands/show.ts +++ b/src/commands/show.ts @@ -23,9 +23,32 @@ import * as list from "./list"; export async function previewProblem(input: IProblem | vscode.Uri, isSideMode: boolean = false): Promise { let node: IProblem; - if (input instanceof vscode.Uri) { - const activeFilePath: string = input.fsPath; - const id: string = await getNodeIdFromFile(activeFilePath); + // I dont know why I use this command, it not work,maybe theproblem of "input"? + // 不知道为什么,当我直接使用这个命令,他并不会正常工作,也许input的问题 + + //if (input instanceof vscode.Uri) { + // const activeFilePath: string = input.fsPath; + // const id: string = await getNodeIdFromFile(activeFilePath); + const editor = vscode.window.activeTextEditor; + let id:string; + + // * We can read file throught editor.document not fs + // * 我们可以直接用自带API读取文件内容,从而跳过input判断 + const editor = vscode.window.activeTextEditor; + if (editor) { + let fileContent = editor.document.getText(); + + const matchResults = fileContent.match(/@lc.+id=(.+?) /); + if (matchResults && matchResults.length === 2) { + id = matchResults[1]; + } + + + // Try to get id from file name if getting from comments failed + if (!id) { + id = path.basename(fsPath).split(".")[0]; + } + if (!id) { vscode.window.showErrorMessage(`Failed to resolve the problem id from file: ${activeFilePath}.`); return; @@ -38,9 +61,9 @@ export async function previewProblem(input: IProblem | vscode.Uri, isSideMode: b node = cachedNode; // Move the preview page aside if it's triggered from Code Lens isSideMode = true; - } else { - node = input; - } + // } else { + // node = input; + // } const needTranslation: boolean = settingUtils.shouldUseEndpointTranslation(); const descString: string = await leetCodeExecutor.getDescription(node.id, needTranslation); leetCodePreviewProvider.show(descString, node, isSideMode);