Skip to content

Commit

Permalink
feat: 新增提交答案和查看题解的入口
Browse files Browse the repository at this point in the history
  • Loading branch information
rottenpen committed Dec 29, 2020
1 parent 6767a19 commit 686475e
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
20 changes: 20 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,26 @@
"description": "The path of the workspace folder to store the problem files.",
"default": ""
}
},
"interview.editor.shortcuts": {
"type": "array",
"default": [
"提交答案",
"查看题解"
],
"scope": "application",
"items": {
"type": "string",
"enum": [
"提交答案",
"查看题解"
],
"enumDescriptions": [
"Submit your answer.",
"Show other answers."
]
},
"description": "Customize the shorcuts in editors."
}
}
]
Expand Down
29 changes: 29 additions & 0 deletions src/codelens/CodelenController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { ConfigurationChangeEvent, Disposable, languages, workspace } from "vscode";
import { customCodeLensProvider, CustomCodeLensProvider } from "./CustomCodeLensProvider";

class CodeLensController implements Disposable {
private internalProvider: CustomCodeLensProvider;
private registeredProvider: Disposable | undefined;
private configurationChangeListener: Disposable;

constructor() {
this.internalProvider = customCodeLensProvider;

this.configurationChangeListener = workspace.onDidChangeConfiguration((event: ConfigurationChangeEvent) => {
if (event.affectsConfiguration("interview.editor.shortcuts")) {
this.internalProvider.refresh();
}
}, this);

this.registeredProvider = languages.registerCodeLensProvider({ scheme: "file" }, this.internalProvider);
}

public dispose(): void {
if (this.registeredProvider) {
this.registeredProvider.dispose();
}
this.configurationChangeListener.dispose();
}
}

export const codeLensController: CodeLensController = new CodeLensController();
64 changes: 64 additions & 0 deletions src/codelens/CustomCodeLensProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import * as vscode from "vscode";

export class CustomCodeLensProvider implements vscode.CodeLensProvider {
private onDidChangeCodeLensesEmitter: vscode.EventEmitter<
void
> = new vscode.EventEmitter<void>();

get onDidChangeCodeLenses(): vscode.Event<void> {
return this.onDidChangeCodeLensesEmitter.event;
}

public refresh(): void {
this.onDidChangeCodeLensesEmitter.fire();
}

public provideCodeLenses(
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--) {
const lineContent: string = document.lineAt(i).text;
if (lineContent.indexOf("*[interview]: end") >= 0) {
codeLensLine = i;
break;
}
}

const range: vscode.Range = new vscode.Range(
codeLensLine,
0,
codeLensLine,
0
);
const codeLens: vscode.CodeLens[] = [];

codeLens.push(
new vscode.CodeLens(range, {
title: "提交答案",
command: "interview.postAnswer",
arguments: [document.uri],
})
);

codeLens.push(
new vscode.CodeLens(range, {
title: "查看题解",
command: "interview.openAnswer",
arguments: [document.uri],
})
);
return codeLens;
}
}

export const customCodeLensProvider: CustomCodeLensProvider = new CustomCodeLensProvider();
4 changes: 4 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as vscode from "vscode";
import { commands, window } from "vscode";
import { codeLensController } from "./codelens/codelenController";
import { openQuestion } from "./command/openQuestion";
import { Interview } from "./treeview/interviewTreeView";
export async function activate(
Expand All @@ -23,6 +24,9 @@ export async function activate(
commands.registerCommand("interview.postAnswer", (name, index) => {
vscode.window.showInformationMessage("提交答案");
});
context.subscriptions.push(
codeLensController
);
// -------- interview 相关 -------------
} catch (error) {
window.showInformationMessage(error);
Expand Down

0 comments on commit 686475e

Please sign in to comment.