Skip to content

Commit

Permalink
fix: 处理bug 新增new icon
Browse files Browse the repository at this point in the history
  • Loading branch information
rottenpen committed Dec 30, 2020
1 parent 686475e commit 9343e75
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 41 deletions.
1 change: 1 addition & 0 deletions media/new.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 9 additions & 9 deletions src/codelens/CustomCodeLensProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ export class CustomCodeLensProvider implements vscode.CodeLensProvider {
document: vscode.TextDocument
): vscode.ProviderResult<vscode.CodeLens[]> {
const content: string = document.getText();
// const matchResult: RegExpMatchArray | null = content.match(
// "*[interview]: start"
// );
// console.log(matchResult)
// if (!matchResult) {
// return undefined;
// }

let codeLensLine: number = document.lineCount - 1;
for (let i: number = document.lineCount - 1; i >= 0; i--) {
Expand All @@ -34,6 +27,13 @@ export class CustomCodeLensProvider implements vscode.CodeLensProvider {
}
}

if (
content.indexOf("*[interview]: start") < 0 ||
content.indexOf("*[interview]: end") < 0
) {
return [];
}

const range: vscode.Range = new vscode.Range(
codeLensLine,
0,
Expand All @@ -46,15 +46,15 @@ export class CustomCodeLensProvider implements vscode.CodeLensProvider {
new vscode.CodeLens(range, {
title: "提交答案",
command: "interview.postAnswer",
arguments: [document.uri],
arguments: [document],
})
);

codeLens.push(
new vscode.CodeLens(range, {
title: "查看题解",
command: "interview.openAnswer",
arguments: [document.uri],
arguments: [document],
})
);
return codeLens;
Expand Down
5 changes: 2 additions & 3 deletions src/command/openQuestion.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import { resolve } from "path";
import * as fse from "fs-extra";
import client from "../mock";
import { selectWorkspaceFolder } from "../shared/selectWorkspaceFolder";
import { Uri, ViewColumn, window } from "vscode";
import { ListItem } from "../service";

export async function openQuestion(ele: ListItem): Promise<void> {
const { name, type, content } = ele;
const { name, type, content, day_id } = ele;
const workspaceFolder: string = await selectWorkspaceFolder();
if (!workspaceFolder) {
return;
}
const codeTemplate = getCodeTemplate(type, content);
const finalPath = await showProblem(
resolve(workspaceFolder, name + "." + type),
resolve(workspaceFolder, day_id + "." + name.replace(/[^a-zA-Z0-9\u4e00-\u9fa5]/g, '') + "." + type),
codeTemplate || ""
);
await window.showTextDocument(Uri.file(finalPath), {
Expand Down
7 changes: 4 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ export async function activate(
): Promise<void> {
try {
// -------- interview 相关 -------------
const interviewProvider = new Interview();
const interviewProvider = new Interview(context);
window.createTreeView("interview", {
treeDataProvider: interviewProvider,
});
commands.registerCommand("interview.openQuestion", (ele) => {
openQuestion(ele);
});
commands.registerCommand("interview.refresh", (name, index) => {
vscode.window.showInformationMessage("刷新数据");
commands.registerCommand("interview.refresh", () => {
interviewProvider.refresh();
vscode.window.showInformationMessage("刷新");
});
commands.registerCommand("interview.openAnswer", (name, index) => {
vscode.window.showInformationMessage("查看答案");
Expand Down
4 changes: 2 additions & 2 deletions src/service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ interface IListQuery {

export interface ListItem {
name: string,
id: number,
publish_date: Date | string,
day_id: number,
publish_date: string,
content: string,
type: 'md' | 'js'
}
Expand Down
48 changes: 24 additions & 24 deletions src/treeview/interviewTreeView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,44 @@ import {
TreeItem,
TreeDataProvider,
Event,
EventEmitter,
window,
} from "vscode";
import * as vscode from "vscode";
import * as path from "path";
import { getProblemList } from "../service";

export class Interview implements TreeDataProvider<Question> {
onDidChangeTreeData?: Event<void | Question | null | undefined> | undefined;
constructor(private context?: vscode.ExtensionContext) {}

private _onDidChangeTreeData: EventEmitter<any> = new EventEmitter<any>();

readonly onDidChangeTreeData: Event<any> = this._onDidChangeTreeData.event;
getTreeItem(element: Question): TreeItem {
return element;
}
getChildren(element?: Question): ProviderResult<Question[]> {
return this.getQuestions();
throw new Error("Method not implemented.");
}

refresh() {
this._onDidChangeTreeData.fire(undefined);
}

async getQuestions() {
try {
let result = await getProblemList();
let arr = result.data.map(
(ele, index) =>
new Question(ele.name, {
new Question(`${ele.day_id}.${ele.name}`, ele.publish_date, {
command: "interview.openQuestion",
title: "",
arguments: [ele],
})
}, this.context, index === 0 ? true : false)
);
return arr;
} catch (e) {
window.showErrorMessage("获取数据失败,请稍后刷新!");
console.log(e);
}
}
Expand All @@ -38,29 +50,17 @@ export class Interview implements TreeDataProvider<Question> {
export class Question extends TreeItem {
constructor(
public readonly label: string,
// public readonly url: string,
public readonly command?: Command
public readonly date: string,
public readonly command?: Command,
public readonly context?: vscode.ExtensionContext,
public readonly isNew: boolean = false
) {
super(label);
this.tooltip = `${this.label}`;
this.description = `${this.date}`;
}

iconPath = {
light: path.join(
__filename,
"..",
"..",
"resources",
"light",
"dependency.svg"
),
dark: path.join(
__filename,
"..",
"..",
"resources",
"dark",
"dependency.svg"
),
};
public iconPath? = this.isNew
? this.context?.asAbsolutePath(path.join("media", "new.svg"))
: "";
}

0 comments on commit 9343e75

Please sign in to comment.