Filtering and sorting of lenses
This commit is contained in:
parent
7edbd604e7
commit
17374b0bf9
6 changed files with 118 additions and 30 deletions
7
LICENSE
Normal file
7
LICENSE
Normal file
|
@ -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.
|
16
README.md
16
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 open PDF's based on citekey
|
||||||
- CodeLens to select item in Zotero 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
|
## Requirements
|
||||||
|
|
||||||
* Zotero
|
* Zotero
|
||||||
|
@ -21,9 +33,13 @@ none.
|
||||||
## Known Issues / Wishlist
|
## Known Issues / Wishlist
|
||||||
|
|
||||||
* It does not yet check if the citekey actually exists in Zotero, nor if it actually has any attachment.
|
* 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.
|
* 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.
|
* Does not yet consider the page number of the citation when opening the pdf.
|
||||||
* Zotero port cannot yet be configured.
|
* 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
|
## Release Notes
|
||||||
|
|
||||||
|
|
14
package.json
14
package.json
|
@ -1,11 +1,19 @@
|
||||||
{
|
{
|
||||||
"name": "zoterolens",
|
"name": "zoterolens",
|
||||||
|
"publisher": "rubenvandeven",
|
||||||
"displayName": "ZoteroLens",
|
"displayName": "ZoteroLens",
|
||||||
"description": "Lens providing quick access to Zotero information and attachments from citations.",
|
"description": "Lens providing quick access to Zotero information and attachments from citations.",
|
||||||
"version": "0.0.1",
|
"version": "0.1.0",
|
||||||
"engines": {
|
"engines": {
|
||||||
"vscode": "^1.63.0"
|
"vscode": "^1.63.0"
|
||||||
},
|
},
|
||||||
|
"author": {
|
||||||
|
"name": "Ruben van de Ven"
|
||||||
|
},
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://git.rubenvandeven.com/r/vscode-zoterolens.git"
|
||||||
|
},
|
||||||
"categories": [
|
"categories": [
|
||||||
"Other"
|
"Other"
|
||||||
],
|
],
|
||||||
|
@ -16,11 +24,11 @@
|
||||||
"contributes": {
|
"contributes": {
|
||||||
"commands": [
|
"commands": [
|
||||||
{
|
{
|
||||||
"command": "extension.showInZotero",
|
"command": "zoterolens.showInZotero",
|
||||||
"title": "Select item in Zotero by citeref"
|
"title": "Select item in Zotero by citeref"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"command": "extension.openZoteroPDF",
|
"command": "zoterolens.openZoteroPDF",
|
||||||
"title": "Open PDF attached in Zotero by citeref."
|
"title": "Open PDF attached in Zotero by citeref."
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { exec } from "child_process";
|
import { exec } from "child_process";
|
||||||
import { IncomingMessage, request } from "http";
|
import { IncomingMessage, request } from "http";
|
||||||
import Reference from "./zoteroCodeLensProvider";
|
import { Reference } from "./zoteroCodeLensProvider";
|
||||||
|
|
||||||
|
|
||||||
async function showInZotero(reference: Reference) {
|
async function showInZotero(reference: Reference) {
|
||||||
|
@ -8,7 +8,6 @@ async function showInZotero(reference: Reference) {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function openZoteroPDF(reference: Reference) {
|
async function openZoteroPDF(reference: Reference) {
|
||||||
|
|
||||||
const req = request("http://127.0.0.1:23119/better-bibtex/json-rpc", {
|
const req = request("http://127.0.0.1:23119/better-bibtex/json-rpc", {
|
||||||
'method': 'POST',
|
'method': 'POST',
|
||||||
'headers': {
|
'headers': {
|
||||||
|
@ -17,7 +16,7 @@ async function openZoteroPDF(reference: Reference) {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
req.on("response", (res: IncomingMessage) => {
|
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?')
|
console.error('Error with request. Is Zotero running and Better-BibTex installed?')
|
||||||
} else {
|
} else {
|
||||||
// console.log('test2', res.statusCode, res);
|
// console.log('test2', res.statusCode, res);
|
||||||
|
@ -30,7 +29,9 @@ async function openZoteroPDF(reference: Reference) {
|
||||||
for (const result of data['result']) {
|
for (const result of data['result']) {
|
||||||
if (result['path'].toLowerCase().endsWith('.pdf')) {
|
if (result['path'].toLowerCase().endsWith('.pdf')) {
|
||||||
// TODO add ?page=nr to open given page
|
// 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
|
break; // only open a single attachement
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -42,7 +43,7 @@ async function openZoteroPDF(reference: Reference) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function openUrl(url: string) {
|
function openUrl(url: string) {
|
||||||
|
// console.log('open', url);
|
||||||
switch (process.platform) {
|
switch (process.platform) {
|
||||||
case 'darwin':
|
case 'darwin':
|
||||||
exec('open "' + url + '"');
|
exec('open "' + url + '"');
|
||||||
|
|
|
@ -1,16 +1,16 @@
|
||||||
import { commands, ExtensionContext, languages } from "vscode";
|
import { commands, ExtensionContext, languages } from "vscode";
|
||||||
import ZoteroCodeLensProvider from "./zoteroCodeLensProvider";
|
import { ZoteroCodeLensProvider } from "./zoteroCodeLensProvider";
|
||||||
import { openZoteroPDF, showInZotero } from "./commands";
|
import { openZoteroPDF, showInZotero } from "./commands";
|
||||||
|
|
||||||
export function activate(context: ExtensionContext) {
|
export function activate(context: ExtensionContext) {
|
||||||
// Register the command
|
// Register the command
|
||||||
let commandDisposable2 = commands.registerCommand(
|
let commandDisposable2 = commands.registerCommand(
|
||||||
"extension.showInZotero",
|
"zoterolens.showInZotero",
|
||||||
showInZotero
|
showInZotero
|
||||||
);
|
);
|
||||||
|
|
||||||
let commandDisposable3 = commands.registerCommand(
|
let commandDisposable3 = commands.registerCommand(
|
||||||
"extension.openZoteroPDF",
|
"zoterolens.openZoteroPDF",
|
||||||
openZoteroPDF
|
openZoteroPDF
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -31,6 +31,7 @@ export function activate(context: ExtensionContext) {
|
||||||
context.subscriptions.push(commandDisposable2);
|
context.subscriptions.push(commandDisposable2);
|
||||||
context.subscriptions.push(commandDisposable3);
|
context.subscriptions.push(commandDisposable3);
|
||||||
context.subscriptions.push(codeLensProviderDisposable);
|
context.subscriptions.push(codeLensProviderDisposable);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function deactivate() {}
|
export function deactivate() {}
|
|
@ -7,32 +7,88 @@ import {
|
||||||
Command
|
Command
|
||||||
} from "vscode";
|
} from "vscode";
|
||||||
|
|
||||||
interface Reference {
|
export interface Reference {
|
||||||
document: TextDocument;
|
document: TextDocument;
|
||||||
citekey: string;
|
citekey: string;
|
||||||
pagenr: number | null;
|
pagenr: number | null;
|
||||||
range: Range;
|
range: Range;
|
||||||
}
|
}
|
||||||
|
|
||||||
class ZoteroCodeLensProvider implements CodeLensProvider {
|
export class ZoteroCodeLensProvider implements CodeLensProvider {
|
||||||
// Each provider requires a provideCodeLenses function which will give the various documents
|
// Each provider requires a provideCodeLenses function which will give the various documents
|
||||||
// the code lenses
|
// the code lenses
|
||||||
async provideCodeLenses(document: TextDocument): Promise<CodeLens[]> {
|
async provideCodeLenses(document: TextDocument): Promise<CodeLens[]> {
|
||||||
// const txt = document.getText();
|
|
||||||
|
|
||||||
const references = this.findReferences(document);
|
const references = this.findReferences(document);
|
||||||
|
|
||||||
return references.map(reference => [
|
return references
|
||||||
new CodeLens(reference.range, {
|
// sort by line, citekey, pagenr
|
||||||
title: '@' + reference.citekey,
|
.sort((a, b) => {
|
||||||
command: 'extension.showInZotero',
|
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]
|
arguments: [reference]
|
||||||
}),
|
}),
|
||||||
new CodeLens(reference.range, {
|
// new CodeLens(reference.range, {
|
||||||
title: '(pdf)',
|
// title: '(pdf)',
|
||||||
command: 'extension.openZoteroPDF',
|
// command: 'zoterolens.openZoteroPDF',
|
||||||
arguments: [reference]
|
// arguments: [reference]
|
||||||
})]).flat();
|
// })
|
||||||
|
]
|
||||||
|
})
|
||||||
|
.flat();
|
||||||
|
|
||||||
|
|
||||||
// let codeLens = new CodeLens(topOfDocument, c);
|
// let codeLens = new CodeLens(topOfDocument, c);
|
||||||
|
@ -49,16 +105,16 @@ class ZoteroCodeLensProvider implements CodeLensProvider {
|
||||||
for (let lineNr = 0; lineNr < document.lineCount; lineNr++) {
|
for (let lineNr = 0; lineNr < document.lineCount; lineNr++) {
|
||||||
const line = document.lineAt(lineNr);
|
const line = document.lineAt(lineNr);
|
||||||
let match: RegExpExecArray | null;
|
let match: RegExpExecArray | null;
|
||||||
let regex = /(?<=@)\w+/g;
|
let regex = /(?<=@)([\w\.]+)[, ]*\[?(?:[p]{0,2}\.)?(\d+)?(?:-+\d+)?\]?/g;
|
||||||
regex.lastIndex = 0;
|
regex.lastIndex = 0;
|
||||||
const text = line.text;//.substring(0, 1000);
|
const text = line.text;//.substring(0, 1000);
|
||||||
while ((match = regex.exec(text))) {
|
while ((match = regex.exec(text))) {
|
||||||
const result = {
|
const result = {
|
||||||
document: document,
|
document: document,
|
||||||
citekey: match[0],
|
citekey: match[1],
|
||||||
pagenr: null,
|
pagenr: match[2] ? parseInt(match[2]) : null,
|
||||||
range: new Range(lineNr, match.index, lineNr, match.index + match[0].length)
|
range: new Range(lineNr, match.index, lineNr, match.index + match[0].length)
|
||||||
};
|
} as Reference;
|
||||||
// if (result) {
|
// if (result) {
|
||||||
matches.push(result);
|
matches.push(result);
|
||||||
// }
|
// }
|
||||||
|
@ -68,4 +124,3 @@ class ZoteroCodeLensProvider implements CodeLensProvider {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default ZoteroCodeLensProvider;
|
|
||||||
|
|
Loading…
Reference in a new issue