Skip to content

Commit ccef9ae

Browse files
committed
Add second choice for unsupported default language
and include --disable-extensions for extension debugging session
1 parent d713ad5 commit ccef9ae

File tree

3 files changed

+52
-34
lines changed

3 files changed

+52
-34
lines changed

.vscode/launch.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"type": "extensionHost",
88
"request": "launch",
99
"runtimeExecutable": "${execPath}",
10-
"args": ["--extensionDevelopmentPath=${workspaceRoot}" ],
10+
"args": ["--extensionDevelopmentPath=${workspaceRoot}", "--disable-extensions" ],
1111
"stopOnEntry": false,
1212
"sourceMaps": true,
1313
"outFiles": [ "${workspaceRoot}/out/src/**/*.js" ],
@@ -25,4 +25,4 @@
2525
"preLaunchTask": "npm"
2626
}
2727
]
28-
}
28+
}

src/commands/show.ts

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -132,45 +132,61 @@ async function fetchProblemLanguage(): Promise<string | undefined> {
132132
return language;
133133
}
134134

135-
async function showProblemInternal(node: IProblem): Promise<void> {
136-
try {
137-
const language: string | undefined = await fetchProblemLanguage();
138-
if (!language) {
139-
return;
140-
}
141-
142-
const leetCodeConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("leetcode");
143-
const workspaceFolder: string = await selectWorkspaceFolder();
144-
if (!workspaceFolder) {
145-
return;
146-
}
135+
async function createPath(language:string,node:IProblem):Promise<string>{
136+
const leetCodeConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("leetcode");
137+
const workspaceFolder: string = await selectWorkspaceFolder();
138+
if (!workspaceFolder) {
139+
Promise.reject("No workspace is opened.");
140+
}
147141

148-
const fileFolder: string = leetCodeConfig
149-
.get<string>(`filePath.${language}.folder`, leetCodeConfig.get<string>(`filePath.default.folder`, ""))
150-
.trim();
151-
const fileName: string = leetCodeConfig
152-
.get<string>(
153-
`filePath.${language}.filename`,
154-
leetCodeConfig.get<string>(`filePath.default.filename`) || genFileName(node, language),
155-
)
156-
.trim();
142+
const fileFolder: string = leetCodeConfig
143+
.get<string>(`filePath.${language}.folder`, leetCodeConfig.get<string>(`filePath.default.folder`, ""))
144+
.trim();
145+
const fileName: string = leetCodeConfig
146+
.get<string>(
147+
`filePath.${language}.filename`,
148+
leetCodeConfig.get<string>(`filePath.default.filename`) || genFileName(node, language),
149+
)
150+
.trim();
157151

158-
let finalPath: string = path.join(workspaceFolder, fileFolder, fileName);
152+
let finalPath: string = path.join(workspaceFolder, fileFolder, fileName);
159153

160-
if (finalPath) {
161-
finalPath = await resolveRelativePath(finalPath, node, language);
162-
if (!finalPath) {
163-
leetCodeChannel.appendLine("Showing problem canceled by user.");
164-
return;
165-
}
154+
if (finalPath) {
155+
finalPath = await resolveRelativePath(finalPath, node, language);
156+
if (!finalPath) {
157+
leetCodeChannel.appendLine("Showing problem canceled by user.");
158+
Promise.reject("Showing problem canceled by user.");
166159
}
160+
}
161+
162+
finalPath = wsl.useWsl() ? await wsl.toWinPath(finalPath) : finalPath;
163+
return finalPath;
164+
}
167165

168-
finalPath = wsl.useWsl() ? await wsl.toWinPath(finalPath) : finalPath;
166+
async function showProblemInternal(node: IProblem): Promise<void> {
167+
try {
168+
let language: string | undefined = await fetchProblemLanguage();
169+
if (!language) {
170+
return;
171+
}
172+
let finalPath: string = await createPath(language,node);
169173

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

173-
await leetCodeExecutor.showProblem(node, language, finalPath, descriptionConfig.showInComment, needTranslation);
184+
finalPath = await createPath(language,node);
185+
await leetCodeExecutor.showProblem(node, language, finalPath, descriptionConfig.showInComment, needTranslation);
186+
} else {
187+
throw e;
188+
}
189+
}
174190
const promises: any[] = [
175191
vscode.window.showTextDocument(vscode.Uri.file(finalPath), { preview: false, viewColumn: vscode.ViewColumn.One }),
176192
promptHintMessage(

src/leetCodeExecutor.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,12 @@ class LeetCodeExecutor implements Disposable {
107107
if (!needTranslation) {
108108
cmd.push("-T"); // use -T to force English version
109109
}
110-
110+
const codeTemplate: string = await this.executeCommandWithProgressEx("Fetching problem data...", this.nodeExecutable, cmd)
111+
if(codeTemplate === undefined) {
112+
throw new Error("language not supported");
113+
}
111114
if (!await fse.pathExists(filePath)) {
112115
await fse.createFile(filePath);
113-
const codeTemplate: string = await this.executeCommandWithProgressEx("Fetching problem data...", this.nodeExecutable, cmd);
114116
await fse.writeFile(filePath, codeTemplate);
115117
}
116118
}

0 commit comments

Comments
 (0)