diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d5da203 --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright © 2022 Ruben van de Ven + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md index fa3e328..4b01ead 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,18 @@ I recommend using it in combination with an extension such as [Citation Picker f - CodeLens to open PDF's based on citekey - CodeLens to select item in Zotero based on citekey +It should find the page number following cases: + +- @test123 +- @test1.23 +- @test123 89 +- @test123, 89 +- [@test123, 89] +- @test123 [89] +- [@test123, p.89] +- [@test123, pp.89-92] +- [@test123, pp.89--92] + ## Requirements * Zotero @@ -21,9 +33,13 @@ none. ## Known Issues / Wishlist * It does not yet check if the citekey actually exists in Zotero, nor if it actually has any attachment. + * This should happen _before_ showing the ref. * No error if Zotero isn't running, or the Better-Bibtex plugin is not configured. * Does not yet consider the page number of the citation when opening the pdf. * Zotero port cannot yet be configured. +* explorer TreeView showing references. +* Possibly use CodeAction instead of Lens: https://code.visualstudio.com/api/language-extensions/programmatic-language-features#possible-actions-on-errors-or-warnings + ## Release Notes diff --git a/package.json b/package.json index b7770bf..9c0784c 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,19 @@ { "name": "zoterolens", + "publisher": "rubenvandeven", "displayName": "ZoteroLens", "description": "Lens providing quick access to Zotero information and attachments from citations.", - "version": "0.0.1", + "version": "0.1.0", "engines": { "vscode": "^1.63.0" }, + "author": { + "name": "Ruben van de Ven" + }, + "repository": { + "type": "git", + "url": "https://git.rubenvandeven.com/r/vscode-zoterolens.git" + }, "categories": [ "Other" ], @@ -16,11 +24,11 @@ "contributes": { "commands": [ { - "command": "extension.showInZotero", + "command": "zoterolens.showInZotero", "title": "Select item in Zotero by citeref" }, { - "command": "extension.openZoteroPDF", + "command": "zoterolens.openZoteroPDF", "title": "Open PDF attached in Zotero by citeref." } ] diff --git a/src/commands.ts b/src/commands.ts index a37a10a..34c494f 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -1,6 +1,6 @@ import { exec } from "child_process"; import { IncomingMessage, request } from "http"; -import Reference from "./zoteroCodeLensProvider"; +import { Reference } from "./zoteroCodeLensProvider"; async function showInZotero(reference: Reference) { @@ -8,7 +8,6 @@ async function showInZotero(reference: Reference) { } async function openZoteroPDF(reference: Reference) { - const req = request("http://127.0.0.1:23119/better-bibtex/json-rpc", { 'method': 'POST', 'headers': { @@ -17,7 +16,7 @@ async function openZoteroPDF(reference: Reference) { } }); req.on("response", (res: IncomingMessage) => { - if (res.statusCode != 200) { + if (res.statusCode !== 200) { console.error('Error with request. Is Zotero running and Better-BibTex installed?') } else { // console.log('test2', res.statusCode, res); @@ -30,7 +29,9 @@ async function openZoteroPDF(reference: Reference) { for (const result of data['result']) { if (result['path'].toLowerCase().endsWith('.pdf')) { // TODO add ?page=nr to open given page - openUrl(result['open']); + + const pagenr = reference.pagenr !== null ? "?page=" + reference.pagenr : ""; + openUrl(result['open'] + pagenr); break; // only open a single attachement } } @@ -42,7 +43,7 @@ async function openZoteroPDF(reference: Reference) { } function openUrl(url: string) { - + // console.log('open', url); switch (process.platform) { case 'darwin': exec('open "' + url + '"'); diff --git a/src/extension.ts b/src/extension.ts index ac3105e..c21930e 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,16 +1,16 @@ import { commands, ExtensionContext, languages } from "vscode"; -import ZoteroCodeLensProvider from "./zoteroCodeLensProvider"; +import { ZoteroCodeLensProvider } from "./zoteroCodeLensProvider"; import { openZoteroPDF, showInZotero } from "./commands"; export function activate(context: ExtensionContext) { // Register the command let commandDisposable2 = commands.registerCommand( - "extension.showInZotero", + "zoterolens.showInZotero", showInZotero ); let commandDisposable3 = commands.registerCommand( - "extension.openZoteroPDF", + "zoterolens.openZoteroPDF", openZoteroPDF ); @@ -31,6 +31,7 @@ export function activate(context: ExtensionContext) { context.subscriptions.push(commandDisposable2); context.subscriptions.push(commandDisposable3); context.subscriptions.push(codeLensProviderDisposable); + } export function deactivate() {} \ No newline at end of file diff --git a/src/zoteroCodeLensProvider.ts b/src/zoteroCodeLensProvider.ts index 7497d64..0b63773 100644 --- a/src/zoteroCodeLensProvider.ts +++ b/src/zoteroCodeLensProvider.ts @@ -7,32 +7,88 @@ import { Command } from "vscode"; -interface Reference { +export interface Reference { document: TextDocument; citekey: string; pagenr: number | null; range: Range; } -class ZoteroCodeLensProvider implements CodeLensProvider { +export class ZoteroCodeLensProvider implements CodeLensProvider { // Each provider requires a provideCodeLenses function which will give the various documents // the code lenses async provideCodeLenses(document: TextDocument): Promise { - // 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(); + return references + // sort by line, citekey, pagenr + .sort((a, b) => { + if (a.range.start.line !== b.range.start.line) { + return a.range.start.line - b.range.start.line; + } + if (a.citekey !== b.citekey) { + return a.citekey.localeCompare(b.citekey); + } + // nulls sort before anything else + if (a.pagenr === null) { + return -1; + } + if (b.pagenr === null) { + return 1; + } + return a.pagenr - b.pagenr; + }) + // remove duplicate items (which are in sequence) + .filter((reference, index) => { + if (index === 0) { return true; } + if ( + references[index - 1].range.start.line !== reference.range.start.line || + references[index - 1].citekey !== reference.citekey || + references[index - 1].pagenr !== reference.pagenr + ) { + return true; + } + return false; + }) + .map((reference, index) => { + let title = ""; + if ( + index === 0 || + references[index - 1].range.start.line !== reference.range.start.line || + references[index - 1].citekey !== reference.citekey + ) { + title = `@${reference.citekey}`; + if (reference.pagenr) { + title += ` p.${reference.pagenr}`; + } + } else { + // there has to be a pagenr because of filtering + title = `p.${reference.pagenr}`; + } + + // ignore char pos so that the order of the array + // is actually used (by default CodeLenses are sorted by position) + const range = new Range( + reference.range.start.line, 0, + reference.range.end.line, 0 + ); + + return [ + new CodeLens(range, { + title: title, + // command: 'zoterolens.showInZotero', + command: 'zoterolens.openZoteroPDF', + arguments: [reference] + }), + // new CodeLens(reference.range, { + // title: '(pdf)', + // command: 'zoterolens.openZoteroPDF', + // arguments: [reference] + // }) + ] + }) + .flat(); // let codeLens = new CodeLens(topOfDocument, c); @@ -49,16 +105,16 @@ class ZoteroCodeLensProvider implements CodeLensProvider { for (let lineNr = 0; lineNr < document.lineCount; lineNr++) { const line = document.lineAt(lineNr); let match: RegExpExecArray | null; - let regex = /(?<=@)\w+/g; + let regex = /(?<=@)([\w\.]+)[, ]*\[?(?:[p]{0,2}\.)?(\d+)?(?:-+\d+)?\]?/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, + citekey: match[1], + pagenr: match[2] ? parseInt(match[2]) : null, range: new Range(lineNr, match.index, lineNr, match.index + match[0].length) - }; + } as Reference; // if (result) { matches.push(result); // } @@ -68,4 +124,3 @@ class ZoteroCodeLensProvider implements CodeLensProvider { } } -export default ZoteroCodeLensProvider;