72 lines
2.0 KiB
TypeScript
72 lines
2.0 KiB
TypeScript
|
|
import {
|
|
CodeLensProvider,
|
|
TextDocument,
|
|
CodeLens,
|
|
Range,
|
|
Command
|
|
} from "vscode";
|
|
|
|
interface Reference {
|
|
document: TextDocument;
|
|
citekey: string;
|
|
pagenr: number | null;
|
|
range: Range;
|
|
}
|
|
|
|
class ZoteroCodeLensProvider implements CodeLensProvider {
|
|
// Each provider requires a provideCodeLenses function which will give the various documents
|
|
// the code lenses
|
|
async provideCodeLenses(document: TextDocument): Promise<CodeLens[]> {
|
|
// const txt = document.getText();
|
|
|
|
const references = this.findReferences(document);
|
|
|
|
return references.map(reference => [
|
|
new CodeLens(reference.range, {
|
|
title: '@' + reference.citekey,
|
|
command: 'extension.showInZotero',
|
|
arguments: [reference]
|
|
}),
|
|
new CodeLens(reference.range, {
|
|
title: '(pdf)',
|
|
command: 'extension.openZoteroPDF',
|
|
arguments: [reference]
|
|
})]).flat();
|
|
|
|
|
|
// let codeLens = new CodeLens(topOfDocument, c);
|
|
|
|
// return [codeLens];
|
|
}
|
|
|
|
// TODO match citations in brackets [@citation] inline @citatinon,
|
|
// but also page number in [see @citation p.23] and
|
|
// inlince @citation [p.23] (so brackets after inline)
|
|
// TODO possibly use https://github.com/martinring/markdown-it-citations/blob/ba82a511de047a2438b4ac060c4c71b5a5c82da9/src/index.ts#L43
|
|
findReferences(document: TextDocument): Reference[] {
|
|
const matches: Reference[] = [];
|
|
for (let lineNr = 0; lineNr < document.lineCount; lineNr++) {
|
|
const line = document.lineAt(lineNr);
|
|
let match: RegExpExecArray | null;
|
|
let regex = /(?<=@)\w+/g;
|
|
regex.lastIndex = 0;
|
|
const text = line.text;//.substring(0, 1000);
|
|
while ((match = regex.exec(text))) {
|
|
const result = {
|
|
document: document,
|
|
citekey: match[0],
|
|
pagenr: null,
|
|
range: new Range(lineNr, match.index, lineNr, match.index + match[0].length)
|
|
};
|
|
// if (result) {
|
|
matches.push(result);
|
|
// }
|
|
}
|
|
}
|
|
return matches;
|
|
}
|
|
}
|
|
|
|
export default ZoteroCodeLensProvider;
|