-
Notifications
You must be signed in to change notification settings - Fork 665
/
Copy pathreset.ts
52 lines (45 loc) · 2.21 KB
/
reset.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import * as vscode from "vscode";
import { getActiveFilePath } from "../utils/workspaceUtils";
import * as settingUtils from "../utils/settingUtils";
import { IDescriptionConfiguration } from "../utils/settingUtils";
import { langExt } from '../shared'
import { DialogType, promptForOpenOutputChannel } from "../utils/uiUtils";
import { leetCodeExecutor } from "../leetCodeExecutor";
const resetBtn = 'Reset';
export async function resetSolution(uri?: vscode.Uri): Promise<void> {
try {
const selection = await vscode.window.showInformationMessage("Are you sure to reset to default code definition", {
'detail': 'If reset, your current code will be lost',
modal: true
} as vscode.MessageOptions, resetBtn)
const filePath: string | undefined = await getActiveFilePath(uri);
if (selection === resetBtn && filePath) {
resetProblemFile(filePath)
}
} catch (error) {
await promptForOpenOutputChannel("Failed to test the solution. Please open the output channel for details.", DialogType.error);
}
}
async function resetProblemFile(finalPath): Promise<void> {
try {
const reg = /(\d+)\.\S+\.(\S+)/;
const problemId = finalPath.match(reg) && finalPath.match(reg)[1]
const fileExt = finalPath.match(reg) && finalPath.match(reg)[2]
let language;
for (let item of langExt) {
if (item[1] === fileExt) {
language = item[0]
break;
}
}
const descriptionConfig: IDescriptionConfiguration = settingUtils.getDescriptionConfiguration();
const needTranslation: boolean = settingUtils.shouldUseEndpointTranslation();
await leetCodeExecutor.resetProblem(problemId, language, finalPath, descriptionConfig.showInComment, needTranslation);
const column = vscode.window.activeTextEditor
? vscode.window.activeTextEditor.viewColumn
: vscode.ViewColumn.One;
await vscode.window.showTextDocument(vscode.Uri.file(finalPath), { preview: false, viewColumn: column });
} catch (error) {
await promptForOpenOutputChannel(`${error} Please open the output channel for details.`, DialogType.error);
}
}