Skip to content

feat: add a new command to support switching the default language #178

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

Merged
merged 1 commit into from
Mar 2, 2019
Merged
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: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ All notable changes to the "leetcode" extension will be documented in this file.

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

## [0.12.0]
## Added
- Add new command `LeetCode: Switch Default Language` to support switching the default language [#115](https://github.com/jdneo/vscode-leetcode/issues/115)

## [0.11.0]
## Added
- 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)
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@

> Note: If no folder is opened in VS Code, the extension will save the problem files in **$HOME/.leetcode/**.

> You can switch the default language by triggering the command: `LeetCode: Switch Default Language`.

---

### Submit the Answer
Expand Down
2 changes: 2 additions & 0 deletions docs/README_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@

> 注意:若当前 VS Code 没有已打开的文件夹,则生成的题目文件会存储于 **$HOME/.leetcode/** 目录下。

> 注意:你可以通过 `LeetCode: Switch Default Language` 命令变更答题时默认使用编程语言。

---

### 提交答案
Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"onCommand:leetcode.searchProblem",
"onCommand:leetcode.testSolution",
"onCommand:leetcode.submitSolution",
"onCommand:leetcode.switchDefaultLanguage",
"onView:leetCodeExplorer"
],
"main": "./out/src/extension",
Expand Down Expand Up @@ -111,6 +112,11 @@
"command": "leetcode.submitSolution",
"title": "Submit to LeetCode",
"category": "LeetCode"
},
{
"command": "leetcode.switchDefaultLanguage",
"title": "Switch Default Language",
"category": "LeetCode"
}
],
"viewsContainers": {
Expand Down
38 changes: 38 additions & 0 deletions src/commands/language.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) jdneo. All rights reserved.
// Licensed under the MIT license.

import { QuickPickItem, window, workspace, WorkspaceConfiguration } from "vscode";
import { languages } from "../shared";

export async function switchDefaultLanguage(): Promise<void> {
const leetCodeConfig: WorkspaceConfiguration = workspace.getConfiguration("leetcode");
const defaultLanguage: string | undefined = leetCodeConfig.get<string>("defaultLanguage");
const languageItems: QuickPickItem[] = [];
for (const language of languages) {
languageItems.push({
label: language,
description: defaultLanguage === language ? "Currently used" : undefined,
});
}
// Put the default language at the top of the list
languageItems.sort((a: QuickPickItem, b: QuickPickItem) => {
if (a.description) {
return Number.MIN_SAFE_INTEGER;
} else if (b.description) {
return Number.MAX_SAFE_INTEGER;
}
return a.label.localeCompare(b.label);
});

const selectedItem: QuickPickItem | undefined = await window.showQuickPick(languageItems, {
placeHolder: "Please the default language",
ignoreFocusOut: true,
});

if (!selectedItem) {
return;
}

leetCodeConfig.update("defaultLanguage", selectedItem.label, true /* Global */);
window.showInformationMessage(`Successfully set the default language to ${selectedItem.label}`);
}
2 changes: 2 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import * as vscode from "vscode";
import { codeLensProvider } from "./codeLensProvider";
import * as cache from "./commands/cache";
import { switchDefaultLanguage } from "./commands/language";
import * as plugin from "./commands/plugin";
import * as session from "./commands/session";
import * as show from "./commands/show";
Expand Down Expand Up @@ -47,6 +48,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
vscode.commands.registerCommand("leetcode.refreshExplorer", () => leetCodeTreeDataProvider.refresh()),
vscode.commands.registerCommand("leetcode.testSolution", (uri?: vscode.Uri) => test.testSolution(uri)),
vscode.commands.registerCommand("leetcode.submitSolution", (uri?: vscode.Uri) => submit.submitSolution(uri)),
vscode.commands.registerCommand("leetcode.switchDefaultLanguage", () => switchDefaultLanguage()),
);

await leetCodeExecutor.switchEndpoint(plugin.getLeetCodeEndpoint());
Expand Down