Skip to content

Commit

Permalink
Merge branch 'main' into patch
Browse files Browse the repository at this point in the history
  • Loading branch information
rottenpen authored Dec 30, 2020
2 parents c6c32cd + b3e46e6 commit 23567c5
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 59 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
32 changes: 15 additions & 17 deletions src/command/openQuestion.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
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";
import { filterInvalidPath } from "../shared";

export async function openQuestion(ele: ListItem): Promise<void> {
const { name, type, content } = ele;

const workspaceFolder: string = await selectWorkspaceFolder();
if (!workspaceFolder) {
return;
}
const codeTemplate = getCodeTemplate(type, content);
const finalPath = await showProblem(
resolve(workspaceFolder, filterInvalidPath(name) + "." + type),
codeTemplate || ""
);
await window.showTextDocument(Uri.file(finalPath), {
preview: false,
viewColumn: ViewColumn.One,
});
return;
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, day_id + "." + filterInvalidPath(name) + "." + type),
codeTemplate || ""
);
await window.showTextDocument(Uri.file(finalPath), {
preview: false,
viewColumn: ViewColumn.One,
});
return;
}

async function showProblem(filePath: string, codeTemplate: string) {
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
11 changes: 5 additions & 6 deletions src/service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ interface IListQuery {
}

export interface ListItem {
name: string;
id: number;
// eslint-disable-next-line @typescript-eslint/naming-convention
publish_date: Date | string;
content: string;
type: "md" | "js";
name: string,
day_id: number,
publish_date: string,
content: string,
type: 'md' | 'js'
}

type IListRes = IRes<ListItem[]>;
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 23567c5

Please sign in to comment.