-
Notifications
You must be signed in to change notification settings - Fork 677
add outputPath Config #123
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
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
b8b91a6
feat: 下载题目的时候根据 tag 进行分类
99dcf76
feat: add outputPath config
99cb09b
feat: update outputPath api
8f51449
Merge branch 'master' into master
ringcrl cf72574
chore: revert chinese README
3275cde
Merge branch 'master' of github.com:ringcrl/vscode-leetcode
96487df
refactor: treat with the outputPath
559d25d
Merge branch 'master' into master
ringcrl 3b7a86a
refactor: Update judgment condition
d96bca9
merge master
f6352cd
feat: add tag selector
3a688cd
Refactoring
jdneo a7afe9c
Refine wording
jdneo 8c22805
Refine the wording
jdneo 3e3cb77
Change the setting name
jdneo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,10 @@ | |
// Licensed under the MIT license. | ||
|
||
import * as fse from "fs-extra"; | ||
import * as path from "path"; | ||
import * as vscode from "vscode"; | ||
import { LeetCodeNode } from "../explorer/LeetCodeNode"; | ||
import { leetCodeChannel } from "../leetCodeChannel"; | ||
import { leetCodeExecutor } from "../leetCodeExecutor"; | ||
import { leetCodeManager } from "../leetCodeManager"; | ||
import { IProblem, IQuickItemEx, languages, ProblemState } from "../shared"; | ||
|
@@ -16,15 +18,15 @@ export async function showProblem(node?: LeetCodeNode): Promise<void> { | |
if (!node) { | ||
return; | ||
} | ||
await showProblemInternal(node.id); | ||
await showProblemInternal(node); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Though type casting from
|
||
} | ||
|
||
export async function searchProblem(): Promise<void> { | ||
if (!leetCodeManager.getUser()) { | ||
promptForSignIn(); | ||
return; | ||
} | ||
const choice: IQuickItemEx<string> | undefined = await vscode.window.showQuickPick( | ||
const choice: IQuickItemEx<IProblem> | undefined = await vscode.window.showQuickPick( | ||
parseProblemsToPicks(list.listProblems()), | ||
{ | ||
matchOnDetail: true, | ||
|
@@ -37,7 +39,7 @@ export async function searchProblem(): Promise<void> { | |
await showProblemInternal(choice.value); | ||
} | ||
|
||
async function showProblemInternal(id: string): Promise<void> { | ||
async function showProblemInternal(node: IProblem): Promise<void> { | ||
try { | ||
const leetCodeConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("leetcode"); | ||
let defaultLanguage: string | undefined = leetCodeConfig.get<string>("defaultLanguage"); | ||
|
@@ -49,9 +51,21 @@ async function showProblemInternal(id: string): Promise<void> { | |
return; | ||
} | ||
|
||
const outDir: string = await selectWorkspaceFolder(); | ||
let outDir: string = await selectWorkspaceFolder(); | ||
let relativePath: string = (leetCodeConfig.get<string>("outputFolder") || "").trim(); | ||
const matchResult: RegExpMatchArray | null = relativePath.match(/\$\{(.*?)\}/); | ||
if (matchResult) { | ||
const resolvedPath: string | undefined = await resolveRelativePath(matchResult[1].toLocaleLowerCase(), node, language); | ||
if (!resolvedPath) { | ||
leetCodeChannel.appendLine("Showing problem canceled by user."); | ||
return; | ||
} | ||
relativePath = resolvedPath; | ||
} | ||
|
||
outDir = path.join(outDir, relativePath); | ||
await fse.ensureDir(outDir); | ||
const result: string = await leetCodeExecutor.showProblem(id, language, outDir); | ||
const result: string = await leetCodeExecutor.showProblem(node.id, language, outDir); | ||
const reg: RegExp = /\* Source Code:\s*(.*)/; | ||
const match: RegExpMatchArray | null = result.match(reg); | ||
if (match && match.length >= 2) { | ||
|
@@ -76,17 +90,17 @@ async function showProblemInternal(id: string): Promise<void> { | |
} | ||
} | ||
} catch (error) { | ||
await promptForOpenOutputChannel("Failed to fetch the problem information. Please open the output channel for details.", DialogType.error); | ||
await promptForOpenOutputChannel("Failed to show the problem. Please open the output channel for details.", DialogType.error); | ||
} | ||
} | ||
|
||
async function parseProblemsToPicks(p: Promise<IProblem[]>): Promise<Array<IQuickItemEx<string>>> { | ||
return new Promise(async (resolve: (res: Array<IQuickItemEx<string>>) => void): Promise<void> => { | ||
const picks: Array<IQuickItemEx<string>> = (await p).map((problem: IProblem) => Object.assign({}, { | ||
async function parseProblemsToPicks(p: Promise<IProblem[]>): Promise<Array<IQuickItemEx<IProblem>>> { | ||
return new Promise(async (resolve: (res: Array<IQuickItemEx<IProblem>>) => void): Promise<void> => { | ||
const picks: Array<IQuickItemEx<IProblem>> = (await p).map((problem: IProblem) => Object.assign({}, { | ||
label: `${parseProblemDecorator(problem.state, problem.locked)}${problem.id}.${problem.name}`, | ||
description: "", | ||
detail: `AC rate: ${problem.passRate}, Difficulty: ${problem.difficulty}`, | ||
value: problem.id, | ||
value: problem, | ||
})); | ||
resolve(picks); | ||
}); | ||
|
@@ -102,3 +116,28 @@ function parseProblemDecorator(state: ProblemState, locked: boolean): string { | |
return locked ? "$(lock) " : ""; | ||
} | ||
} | ||
|
||
async function resolveRelativePath(value: string, node: IProblem, selectedLanguage: string): Promise<string | undefined> { | ||
switch (value) { | ||
case "tag": | ||
if (node.tags.length === 1) { | ||
return node.tags[0]; | ||
} | ||
return await vscode.window.showQuickPick( | ||
node.tags, | ||
{ | ||
matchOnDetail: true, | ||
placeHolder: "Multiple tags available, please select one", | ||
ignoreFocusOut: true, | ||
}, | ||
); | ||
case "language": | ||
return selectedLanguage; | ||
case "difficulty": | ||
return node.difficulty; | ||
default: | ||
const errorMsg: string = `The config '${value}' is not supported.`; | ||
leetCodeChannel.appendLine(errorMsg); | ||
throw new Error(errorMsg); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.