Skip to content

fix the icon bug in explorer #14

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 3 commits into from
Feb 26, 2018
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
Binary file added resources/x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 20 additions & 4 deletions src/commands/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

import * as vscode from "vscode";
import { leetCodeManager } from "../leetCodeManager";
import { leetCodeBinaryPath } from "../shared";
import { UserStatus } from "../shared";
import { leetCodeBinaryPath, ProblemState, UserStatus } from "../shared";
import { executeCommand } from "../utils/cpUtils";
import { DialogType, promptForOpenOutputChannel } from "../utils/uiUtils";

export interface IProblem {
solved: boolean;
state: ProblemState;
id: string;
name: string;
difficulty: string;
Expand All @@ -28,7 +27,7 @@ export async function listProblems(channel: vscode.OutputChannel): Promise<IProb
const match: RegExpMatchArray | null = line.match(reg);
if (match && match.length === 6) {
problems.push({
solved: !!(match[1].trim()),
state: parseProblemState(match[1]),
id: match[2].trim(),
name: match[3].trim(),
difficulty: match[4].trim(),
Expand All @@ -41,5 +40,22 @@ export async function listProblems(channel: vscode.OutputChannel): Promise<IProb
await promptForOpenOutputChannel("Failed to list problems. Please open the output channel for details", DialogType.error, channel);
return [];
}
}

function parseProblemState(stateOutput: string): ProblemState {
if (!stateOutput) {
return ProblemState.Unknown;
}
switch (stateOutput.trim()) {
case "v":
case "✔":
case "√":
return ProblemState.AC;
case "X":
case "✘":
case "×":
return ProblemState.NotAC;
default:
return ProblemState.Unknown;
}
}
15 changes: 13 additions & 2 deletions src/commands/show.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as fse from "fs-extra";
import * as vscode from "vscode";
import { LeetCodeNode } from "../leetCodeExplorer";
import { leetCodeManager } from "../leetCodeManager";
import { IQuickItemEx, languages, leetCodeBinaryPath } from "../shared";
import { IQuickItemEx, languages, leetCodeBinaryPath, ProblemState } from "../shared";
import { executeCommand } from "../utils/cpUtils";
import { DialogOptions, DialogType, promptForOpenOutputChannel, promptForSignIn } from "../utils/uiUtils";
import { selectWorkspaceFolder } from "../utils/workspaceUtils";
Expand Down Expand Up @@ -79,11 +79,22 @@ async function showProblemInternal(channel: vscode.OutputChannel, id: string): P
async function parseProblemsToPicks(p: Promise<list.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: list.IProblem) => Object.assign({}, {
label: `${problem.solved ? "$(check) " : ""}${problem.id}.${problem.name}`,
label: `${parseProblemDecorator(problem.state)}${problem.id}.${problem.name}`,
description: "",
detail: `AC rate: ${problem.passRate}, Difficulty: ${problem.difficulty}`,
value: problem.id,
}));
resolve(picks);
});
}

function parseProblemDecorator(state: ProblemState): string {
switch (state) {
case ProblemState.AC:
return "$(check) ";
case ProblemState.NotAC:
return "$(x) ";
default:
return "";
}
}
31 changes: 22 additions & 9 deletions src/leetCodeExplorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as path from "path";
import * as vscode from "vscode";
import * as list from "./commands/list";
import { leetCodeManager } from "./leetCodeManager";
import { ProblemState } from "./shared";

// tslint:disable:max-classes-per-file
export class LeetCodeNode {
Expand All @@ -13,8 +14,8 @@ export class LeetCodeNode {
return this.data.name;
}

public get solved(): boolean {
return this.data.solved;
public get state(): ProblemState {
return this.data.state;
}

public get id(): string {
Expand Down Expand Up @@ -64,11 +65,7 @@ export class LeetCodeTreeDataProvider implements vscode.TreeDataProvider<LeetCod
id: `${idPrefix}.${element.id}`,
collapsibleState: element.isProblem ? vscode.TreeItemCollapsibleState.None : vscode.TreeItemCollapsibleState.Collapsed,
contextValue: element.isProblem ? "problem" : "difficulty",
iconPath: element.isProblem ?
(element.solved ?
this.context.asAbsolutePath(path.join("resources", "check.png"))
: this.context.asAbsolutePath(path.join("resources", "blank.png")))
: "",
iconPath: this.parseIconPathFromProblemState(element),
};
}

Expand All @@ -77,7 +74,7 @@ export class LeetCodeTreeDataProvider implements vscode.TreeDataProvider<LeetCod
return [
new LeetCodeNode(
{
solved: false,
state: ProblemState.Unknown,
id: "notSignIn",
name: "Sign in to LeetCode",
difficulty: "",
Expand Down Expand Up @@ -128,7 +125,7 @@ export class LeetCodeTreeDataProvider implements vscode.TreeDataProvider<LeetCod
difficultynodes.push(
new LeetCodeNode(
{
solved: false,
state: ProblemState.Unknown,
id: difficulty,
name: difficulty,
difficulty: "",
Expand All @@ -155,4 +152,20 @@ export class LeetCodeTreeDataProvider implements vscode.TreeDataProvider<LeetCod
});
return difficultynodes;
}

private parseIconPathFromProblemState(element: LeetCodeNode): string {
if (!element.isProblem) {
return "";
}
switch (element.state) {
case ProblemState.AC:
return this.context.asAbsolutePath(path.join("resources", "check.png"));
case ProblemState.NotAC:
return this.context.asAbsolutePath(path.join("resources", "x.png"));
case ProblemState.Unknown:
return this.context.asAbsolutePath(path.join("resources", "blank.png"));
default:
return "";
}
}
}
6 changes: 6 additions & 0 deletions src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,9 @@ export const languages = [
"scala",
"swift",
];

export enum ProblemState {
AC = 1,
NotAC = 2,
Unknown = 3,
}