Skip to content

Add second choice for unsupported default language #885

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": ["--extensionDevelopmentPath=${workspaceRoot}" ],
"args": ["--extensionDevelopmentPath=${workspaceRoot}", "--disable-extensions" ],
"stopOnEntry": false,
"sourceMaps": true,
"outFiles": [ "${workspaceRoot}/out/src/**/*.js" ],
Expand All @@ -25,4 +25,4 @@
"preLaunchTask": "npm"
}
]
}
}
76 changes: 46 additions & 30 deletions src/commands/show.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,45 +132,61 @@ async function fetchProblemLanguage(): Promise<string | undefined> {
return language;
}

async function showProblemInternal(node: IProblem): Promise<void> {
try {
const language: string | undefined = await fetchProblemLanguage();
if (!language) {
return;
}

const leetCodeConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("leetcode");
const workspaceFolder: string = await selectWorkspaceFolder();
if (!workspaceFolder) {
return;
}
async function createPath(language:string,node:IProblem):Promise<string>{
const leetCodeConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("leetcode");
const workspaceFolder: string = await selectWorkspaceFolder();
if (!workspaceFolder) {
Promise.reject("No workspace is opened.");
}

const fileFolder: string = leetCodeConfig
.get<string>(`filePath.${language}.folder`, leetCodeConfig.get<string>(`filePath.default.folder`, ""))
.trim();
const fileName: string = leetCodeConfig
.get<string>(
`filePath.${language}.filename`,
leetCodeConfig.get<string>(`filePath.default.filename`) || genFileName(node, language),
)
.trim();
const fileFolder: string = leetCodeConfig
.get<string>(`filePath.${language}.folder`, leetCodeConfig.get<string>(`filePath.default.folder`, ""))
.trim();
const fileName: string = leetCodeConfig
.get<string>(
`filePath.${language}.filename`,
leetCodeConfig.get<string>(`filePath.default.filename`) || genFileName(node, language),
)
.trim();

let finalPath: string = path.join(workspaceFolder, fileFolder, fileName);
let finalPath: string = path.join(workspaceFolder, fileFolder, fileName);

if (finalPath) {
finalPath = await resolveRelativePath(finalPath, node, language);
if (!finalPath) {
leetCodeChannel.appendLine("Showing problem canceled by user.");
return;
}
if (finalPath) {
finalPath = await resolveRelativePath(finalPath, node, language);
if (!finalPath) {
leetCodeChannel.appendLine("Showing problem canceled by user.");
Promise.reject("Showing problem canceled by user.");
}
}

finalPath = wsl.useWsl() ? await wsl.toWinPath(finalPath) : finalPath;
return finalPath;
}

finalPath = wsl.useWsl() ? await wsl.toWinPath(finalPath) : finalPath;
async function showProblemInternal(node: IProblem): Promise<void> {
try {
let language: string | undefined = await fetchProblemLanguage();
if (!language) {
return;
}
let finalPath: string = await createPath(language,node);

const descriptionConfig: IDescriptionConfiguration = settingUtils.getDescriptionConfiguration();
const needTranslation: boolean = settingUtils.shouldUseEndpointTranslation();
try {
await leetCodeExecutor.showProblem(node, language, finalPath, descriptionConfig.showInComment, needTranslation);
} catch (e) {
if (e instanceof Error) {
//this shows all languages, not just the ones, which are supported for this specific problem
language = await vscode.window.showQuickPick(languages, { placeHolder: "This problem is not supporting your default language, please choose another", ignoreFocusOut: true });
if (language === undefined) { throw e; }

await leetCodeExecutor.showProblem(node, language, finalPath, descriptionConfig.showInComment, needTranslation);
finalPath = await createPath(language,node);
await leetCodeExecutor.showProblem(node, language, finalPath, descriptionConfig.showInComment, needTranslation);
} else {
throw e;
}
}
const promises: any[] = [
vscode.window.showTextDocument(vscode.Uri.file(finalPath), { preview: false, viewColumn: vscode.ViewColumn.One }),
promptHintMessage(
Expand Down
6 changes: 4 additions & 2 deletions src/leetCodeExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,12 @@ class LeetCodeExecutor implements Disposable {
if (!needTranslation) {
cmd.push("-T"); // use -T to force English version
}

const codeTemplate: string = await this.executeCommandWithProgressEx("Fetching problem data...", this.nodeExecutable, cmd)
if(codeTemplate === undefined) {
throw new Error("language not supported");
}
if (!await fse.pathExists(filePath)) {
await fse.createFile(filePath);
const codeTemplate: string = await this.executeCommandWithProgressEx("Fetching problem data...", this.nodeExecutable, cmd);
await fse.writeFile(filePath, codeTemplate);
}
}
Expand Down