Skip to content

Commit a39de3c

Browse files
authored
feat: add a new command to support switching the default language (#178)
1 parent 390a313 commit a39de3c

File tree

6 files changed

+54
-0
lines changed

6 files changed

+54
-0
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ All notable changes to the "leetcode" extension will be documented in this file.
33

44
Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.
55

6+
## [0.12.0]
7+
## Added
8+
- Add new command `LeetCode: Switch Default Language` to support switching the default language [#115](https://github.com/jdneo/vscode-leetcode/issues/115)
9+
610
## [0.11.0]
711
## Added
812
- Add new setting: `leetcode.outputFolder` to customize the sub-directory to save the files generated by 'Show Problem' [#119](https://github.com/jdneo/vscode-leetcode/issues/119)

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@
7171

7272
> Note: If no folder is opened in VS Code, the extension will save the problem files in **$HOME/.leetcode/**.
7373
74+
> You can switch the default language by triggering the command: `LeetCode: Switch Default Language`.
75+
7476
---
7577

7678
### Submit the Answer

docs/README_zh-CN.md

+2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@
7171

7272
> 注意:若当前 VS Code 没有已打开的文件夹,则生成的题目文件会存储于 **$HOME/.leetcode/** 目录下。
7373
74+
> 注意:你可以通过 `LeetCode: Switch Default Language` 命令变更答题时默认使用编程语言。
75+
7476
---
7577

7678
### 提交答案

package.json

+6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"onCommand:leetcode.searchProblem",
3737
"onCommand:leetcode.testSolution",
3838
"onCommand:leetcode.submitSolution",
39+
"onCommand:leetcode.switchDefaultLanguage",
3940
"onView:leetCodeExplorer"
4041
],
4142
"main": "./out/src/extension",
@@ -111,6 +112,11 @@
111112
"command": "leetcode.submitSolution",
112113
"title": "Submit to LeetCode",
113114
"category": "LeetCode"
115+
},
116+
{
117+
"command": "leetcode.switchDefaultLanguage",
118+
"title": "Switch Default Language",
119+
"category": "LeetCode"
114120
}
115121
],
116122
"viewsContainers": {

src/commands/language.ts

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) jdneo. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
import { QuickPickItem, window, workspace, WorkspaceConfiguration } from "vscode";
5+
import { languages } from "../shared";
6+
7+
export async function switchDefaultLanguage(): Promise<void> {
8+
const leetCodeConfig: WorkspaceConfiguration = workspace.getConfiguration("leetcode");
9+
const defaultLanguage: string | undefined = leetCodeConfig.get<string>("defaultLanguage");
10+
const languageItems: QuickPickItem[] = [];
11+
for (const language of languages) {
12+
languageItems.push({
13+
label: language,
14+
description: defaultLanguage === language ? "Currently used" : undefined,
15+
});
16+
}
17+
// Put the default language at the top of the list
18+
languageItems.sort((a: QuickPickItem, b: QuickPickItem) => {
19+
if (a.description) {
20+
return Number.MIN_SAFE_INTEGER;
21+
} else if (b.description) {
22+
return Number.MAX_SAFE_INTEGER;
23+
}
24+
return a.label.localeCompare(b.label);
25+
});
26+
27+
const selectedItem: QuickPickItem | undefined = await window.showQuickPick(languageItems, {
28+
placeHolder: "Please the default language",
29+
ignoreFocusOut: true,
30+
});
31+
32+
if (!selectedItem) {
33+
return;
34+
}
35+
36+
leetCodeConfig.update("defaultLanguage", selectedItem.label, true /* Global */);
37+
window.showInformationMessage(`Successfully set the default language to ${selectedItem.label}`);
38+
}

src/extension.ts

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import * as vscode from "vscode";
55
import { codeLensProvider } from "./codeLensProvider";
66
import * as cache from "./commands/cache";
7+
import { switchDefaultLanguage } from "./commands/language";
78
import * as plugin from "./commands/plugin";
89
import * as session from "./commands/session";
910
import * as show from "./commands/show";
@@ -47,6 +48,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
4748
vscode.commands.registerCommand("leetcode.refreshExplorer", () => leetCodeTreeDataProvider.refresh()),
4849
vscode.commands.registerCommand("leetcode.testSolution", (uri?: vscode.Uri) => test.testSolution(uri)),
4950
vscode.commands.registerCommand("leetcode.submitSolution", (uri?: vscode.Uri) => submit.submitSolution(uri)),
51+
vscode.commands.registerCommand("leetcode.switchDefaultLanguage", () => switchDefaultLanguage()),
5052
);
5153

5254
await leetCodeExecutor.switchEndpoint(plugin.getLeetCodeEndpoint());

0 commit comments

Comments
 (0)