Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复 previewProblem 命令不正常工作 #840

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions src/commands/show.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,32 @@ import * as list from "./list";

export async function previewProblem(input: IProblem | vscode.Uri, isSideMode: boolean = false): Promise<void> {
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;
Expand All @@ -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);
Expand Down