forked from baobao12356/front-end-daily-question
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -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(); |
This file contains 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 |
---|---|---|
@@ -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(); |
This file contains 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